blob: 55c24157e691717ec617346f5e1e72c88cebaeb6 [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 * Decorate a page with query event handler.
83 */
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000084 initQueries : function (d) {
85 var qs = d.querySelectorAll('pre.query.tutorial');
86 var that = this;
87 for (var i = 0; i < qs.length; i++) {
88 qs[i].onclick = function (e) {
89 that.useQuery(this,e);
90 };
91 };
92 },
93
Nils Diewald61e6ff52015-05-07 17:26:50 +000094 /**
95 * Decorate a page with documentation links
96 */
97 initDocLinks : function (d) {
98 var dl = d.getElementsByClassName('doc-link');
99 var that = this;
100 for (var i = 0; i < dl.length; i++) {
101 dl[i].onclick = function (e) {
102 that.setPage(this.getAttribute('href'));
103 return true;
104 };
105 };
106 },
107
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000108
Nils Diewald7148c6f2015-05-04 15:07:53 +0000109 /**
110 * Show the tutorial page embedded.
111 */
Nils Diewald58141332015-04-07 16:18:45 +0000112 show : function () {
113 var element = this._element;
114 if (element.style.display === 'block')
115 return;
116
117 if (this._iframe === null) {
118 this._iframe = document.createElement('iframe');
Nils Diewaldab4d3ca2015-04-17 01:48:43 +0000119 this._iframe.setAttribute(
120 'src',
121 (this.getPage() || this.start) + '?embedded=true'
122 );
Nils Diewald58141332015-04-07 16:18:45 +0000123
124 var ul = document.createElement('ul');
125 ul.classList.add('action', 'right');
126
Nils Diewald58141332015-04-07 16:18:45 +0000127 // Add close button
128 var close = document.createElement('li');
129 close.appendChild(document.createElement('span'))
130 .appendChild(document.createTextNode(loc.CLOSE));
131 close.classList.add('close');
132 close.setAttribute('title', loc.CLOSE);
133 close.onclick = function () {
134 element.style.display = 'none';
135 };
136
137 // Add open in new window button
138 // Add scroll to top button
139 /*
140 var info = document.createElement('li');
141 info.appendChild(document.createElement('span'))
142 .appendChild(document.createTextNode(loc.SHOWINFO));
143 info.classList.add('info');
144 info.setAttribute('title', loc.SHOWINFO);
145 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000146
Nils Diewald58141332015-04-07 16:18:45 +0000147 ul.appendChild(close);
148
149 element.appendChild(ul);
150 element.appendChild(this._iframe);
151 };
152
153 element.style.display = 'block';
154 },
155
156 /**
157 * Close tutorial window.
158 */
159 hide : function () {
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000160 this._element.style.display = 'none';
Nils Diewald58141332015-04-07 16:18:45 +0000161 },
162
163 /**
164 * Set a page to be the current tutorial page.
165 * Expects either a string or an element.
166 */
167 setPage : function (obj) {
168 var page = obj;
169 if (typeof page != 'string') {
170 page = window.location.pathname + window.location.search;
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000171 for (var i = 1; i < 5; i++) {
Nils Diewald58141332015-04-07 16:18:45 +0000172 if (obj.nodeName === 'SECTION') {
173 if (obj.hasAttribute('id'))
174 page += '#' + obj.getAttribute('id');
175 break;
176 }
177 else if (obj.nodeName === 'PRE' && obj.hasAttribute('id')) {
178 page += '#' + obj.getAttribute('id');
179 break;
180 }
181 else {
182 obj = obj.parentNode;
183 };
184 };
185 };
186 this._session.set('tutpage', page);
187 },
188
189 /**
190 * Get the current tutorial URL
191 */
192 getPage : function () {
193 this._session.get('tutpage');
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000194 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000195 };
196});
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000197