blob: a028b8eaabb5dc8cb29687527f693f27256cb9ab [file] [log] [blame]
Nils Diewald58141332015-04-07 16:18:45 +00001/**
2 * Open and close a tutorial page.
3 * The current page is stored and retrieved in a session cookie.
4 */
Nils Diewaldab4d3ca2015-04-17 01:48:43 +00005// TODO: Add query mechanism!
6// TODO: Highlight current section:
7// http://stackoverflow.com/questions/24887258/highlight-navigation-link-as-i-scroll-down-the-page
Nils Diewald0e6992a2015-04-14 20:13:52 +00008define(['session', 'util'], function (sessionClass) {
Nils Diewald58141332015-04-07 16:18:45 +00009 "use strict";
10
11 // Localization values
Nils Diewald0e6992a2015-04-14 20:13:52 +000012 var loc = KorAP.Locale;
Nils Diewald58141332015-04-07 16:18:45 +000013 loc.CLOSE = loc.CLOSE || 'Close';
14
Nils Diewald0e6992a2015-04-14 20:13:52 +000015 return {
Nils Diewald58141332015-04-07 16:18:45 +000016 /**
17 * Create new tutorial object.
18 * Accepts an element to bind the tutorial window to.
19 */
20 create : function (obj) {
Nils Diewald0e6992a2015-04-14 20:13:52 +000021 if (!obj)
22 return null;
23 return Object.create(this)._init(obj);
Nils Diewald58141332015-04-07 16:18:45 +000024 },
25
26 // Initialize Tutorial object
27 _init : function (obj) {
Nils Diewald0e6992a2015-04-14 20:13:52 +000028
29 this._session = sessionClass.create();
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000030
31 if (obj) {
32 this._show = obj;
33 this.start = obj.getAttribute('href');
34 obj.removeAttribute('href');
35 var that = this;
36 obj.onclick = function () {
37 that.show();
38 };
39
40 // Injects a tutorial div to the body
41 var div = document.createElement('div');
42 div.setAttribute('id', 'tutorial');
43 div.style.display = 'none';
44 document.getElementsByTagName('body')[0].appendChild(div);
45 this._iframe = null;
46 this._element = div;
47
48 // Some fields
49 this._ql = document.getElementById("ql-field");
50 this._q = document.getElementById("q-field")
51 this._cutoff = document.getElementById("q-cutoff-field");
Nils Diewald58141332015-04-07 16:18:45 +000052 };
53
Nils Diewald58141332015-04-07 16:18:45 +000054 return this;
55 },
56
Nils Diewald7148c6f2015-05-04 15:07:53 +000057
58 /**
59 * Initialize a search with a defined query.
60 */
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000061 useQuery : function (e) {
62 var q = e.getAttribute("data-query");
63 var ql = e.getAttribute("data-query-language");
64 var qc = e.getAttribute("data-query-cutoff");
65 if (qc !== 0 && qc !== "0" && qc !== "off" && qc !== null) {
66 this._cutoff.checked = true;
67 };
68
69 var qlf = this._ql.options;
70 for (var i in qlf) {
71 if (qlf[i].value == ql) {
72 qlf[i].selected = true;
73 };
74 };
75
76 this._q.value = q;
77 this.setPage(e);
78 this.hide();
79 },
80
Nils Diewald7148c6f2015-05-04 15:07:53 +000081
82 /**
83 * Decorate a page with query event handler.
84 */
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000085 initQueries : function (d) {
86 var qs = d.querySelectorAll('pre.query.tutorial');
87 var that = this;
88 for (var i = 0; i < qs.length; i++) {
89 qs[i].onclick = function (e) {
90 that.useQuery(this,e);
91 };
92 };
93 },
94
95
Nils Diewald7148c6f2015-05-04 15:07:53 +000096 /**
97 * Show the tutorial page embedded.
98 */
Nils Diewald58141332015-04-07 16:18:45 +000099 show : function () {
100 var element = this._element;
101 if (element.style.display === 'block')
102 return;
103
104 if (this._iframe === null) {
105 this._iframe = document.createElement('iframe');
Nils Diewaldab4d3ca2015-04-17 01:48:43 +0000106 this._iframe.setAttribute(
107 'src',
108 (this.getPage() || this.start) + '?embedded=true'
109 );
Nils Diewald58141332015-04-07 16:18:45 +0000110
111 var ul = document.createElement('ul');
112 ul.classList.add('action', 'right');
113
Nils Diewald58141332015-04-07 16:18:45 +0000114 // Add close button
115 var close = document.createElement('li');
116 close.appendChild(document.createElement('span'))
117 .appendChild(document.createTextNode(loc.CLOSE));
118 close.classList.add('close');
119 close.setAttribute('title', loc.CLOSE);
120 close.onclick = function () {
121 element.style.display = 'none';
122 };
123
124 // Add open in new window button
125 // Add scroll to top button
126 /*
127 var info = document.createElement('li');
128 info.appendChild(document.createElement('span'))
129 .appendChild(document.createTextNode(loc.SHOWINFO));
130 info.classList.add('info');
131 info.setAttribute('title', loc.SHOWINFO);
132 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000133
Nils Diewald58141332015-04-07 16:18:45 +0000134 ul.appendChild(close);
135
136 element.appendChild(ul);
137 element.appendChild(this._iframe);
138 };
139
140 element.style.display = 'block';
141 },
142
143 /**
144 * Close tutorial window.
145 */
146 hide : function () {
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000147 this._element.style.display = 'none';
Nils Diewald58141332015-04-07 16:18:45 +0000148 },
149
150 /**
151 * Set a page to be the current tutorial page.
152 * Expects either a string or an element.
153 */
154 setPage : function (obj) {
155 var page = obj;
156 if (typeof page != 'string') {
157 page = window.location.pathname + window.location.search;
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000158 for (var i = 1; i < 5; i++) {
Nils Diewald58141332015-04-07 16:18:45 +0000159 if (obj.nodeName === 'SECTION') {
160 if (obj.hasAttribute('id'))
161 page += '#' + obj.getAttribute('id');
162 break;
163 }
164 else if (obj.nodeName === 'PRE' && obj.hasAttribute('id')) {
165 page += '#' + obj.getAttribute('id');
166 break;
167 }
168 else {
169 obj = obj.parentNode;
170 };
171 };
172 };
173 this._session.set('tutpage', page);
174 },
175
176 /**
177 * Get the current tutorial URL
178 */
179 getPage : function () {
180 this._session.get('tutpage');
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000181 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000182 };
183});
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000184