blob: 78d4a8693fc1a4b2bf0994d740df454786ee9c0f [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
Akron0b489ad2018-02-02 16:49:32 +010012 const 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 */
Akron7716f012015-07-01 20:38:32 +020020 create : function (obj,session) {
Nils Diewald0e6992a2015-04-14 20:13:52 +000021 if (!obj)
Akron086fe5d2017-11-13 14:01:45 +010022 return null;
Akron7716f012015-07-01 20:38:32 +020023 return Object.create(this)._init(obj,session);
Nils Diewald58141332015-04-07 16:18:45 +000024 },
25
26 // Initialize Tutorial object
Akron7716f012015-07-01 20:38:32 +020027 _init : function (obj, session) {
Nils Diewald0e6992a2015-04-14 20:13:52 +000028
Akron7716f012015-07-01 20:38:32 +020029 if (session === undefined) {
Akron086fe5d2017-11-13 14:01:45 +010030 this._session = sessionClass.create();
Akron7716f012015-07-01 20:38:32 +020031 }
32 else {
Akron086fe5d2017-11-13 14:01:45 +010033 this._session = session;
Akron7716f012015-07-01 20:38:32 +020034 };
35
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000036
37 if (obj) {
Akron086fe5d2017-11-13 14:01:45 +010038 this._show = obj;
39 this.start = obj.getAttribute('href');
40 obj.removeAttribute('href');
41 var that = this;
42 obj.onclick = function () {
43 that.show();
44 };
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000045
Akron086fe5d2017-11-13 14:01:45 +010046 // Injects a tutorial div to the body
47 var div = document.createElement('div');
48 div.setAttribute('id', 'tutorial');
49 div.style.display = 'none';
50 document.getElementsByTagName('body')[0].appendChild(div);
51 this._iframe = null;
52 this._element = div;
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000053
Akron086fe5d2017-11-13 14:01:45 +010054 // Some fields
55 this._ql = document.getElementById("ql-field");
56 this._q = document.getElementById("q-field")
57 this._cutoff = document.getElementById("q-cutoff-field");
Nils Diewald58141332015-04-07 16:18:45 +000058 };
59
Nils Diewald58141332015-04-07 16:18:45 +000060 return this;
61 },
62
Nils Diewald7148c6f2015-05-04 15:07:53 +000063
64 /**
65 * Initialize a search with a defined query.
66 */
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000067 useQuery : function (e) {
68 var q = e.getAttribute("data-query");
69 var ql = e.getAttribute("data-query-language");
70 var qc = e.getAttribute("data-query-cutoff");
71 if (qc !== 0 && qc !== "0" && qc !== "off" && qc !== null) {
Akron086fe5d2017-11-13 14:01:45 +010072 this._cutoff.checked = true;
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000073 };
74
Akron086fe5d2017-11-13 14:01:45 +010075 if (KorAP.QLmenu) {
76 KorAP.QLmenu.selectValue(ql);
77 };
78 /*
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000079 var qlf = this._ql.options;
80 for (var i in qlf) {
Akron99713ef2017-06-28 18:19:28 +020081 if (qlf[i].value == ql) {
82 qlf[i].selected = true;
83 break;
84 };
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000085 };
Akron086fe5d2017-11-13 14:01:45 +010086 */
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000087
88 this._q.value = q;
89 this.setPage(e);
90 this.hide();
91 },
92
Nils Diewald7148c6f2015-05-04 15:07:53 +000093 /**
94 * Decorate a page with query event handler.
95 */
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000096 initQueries : function (d) {
Akronf4a7cf42018-01-09 15:58:45 +010097 var qs = d.querySelectorAll('pre.query.tutorial:not(.unsupported)');
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000098 var that = this;
99 for (var i = 0; i < qs.length; i++) {
Akron086fe5d2017-11-13 14:01:45 +0100100 qs[i].onclick = function (e) {
101 that.useQuery(this,e);
102 };
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000103 };
104 },
105
Nils Diewald61e6ff52015-05-07 17:26:50 +0000106 /**
107 * Decorate a page with documentation links
108 */
109 initDocLinks : function (d) {
110 var dl = d.getElementsByClassName('doc-link');
111 var that = this;
112 for (var i = 0; i < dl.length; i++) {
Akron086fe5d2017-11-13 14:01:45 +0100113 dl[i].onclick = function (e) {
114 that.setPage(this.getAttribute('href'));
115 return true;
116 };
Nils Diewald61e6ff52015-05-07 17:26:50 +0000117 };
118 },
119
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000120
Nils Diewald7148c6f2015-05-04 15:07:53 +0000121 /**
122 * Show the tutorial page embedded.
123 */
Nils Diewald58141332015-04-07 16:18:45 +0000124 show : function () {
125 var element = this._element;
126 if (element.style.display === 'block')
Akron99713ef2017-06-28 18:19:28 +0200127 return;
Nils Diewald58141332015-04-07 16:18:45 +0000128
129 if (this._iframe === null) {
Akron99713ef2017-06-28 18:19:28 +0200130 this._iframe = document.createElement('iframe');
131 this._iframe.setAttribute(
132 'src',
133 (this.getPage() || this.start) + '?embedded=true'
134 );
Akron086fe5d2017-11-13 14:01:45 +0100135
Akron99713ef2017-06-28 18:19:28 +0200136 var ul = document.createElement('ul');
137 ul.classList.add('action', 'right');
Nils Diewald58141332015-04-07 16:18:45 +0000138
Akron99713ef2017-06-28 18:19:28 +0200139 // Add close button
140 var close = document.createElement('li');
Akron0b489ad2018-02-02 16:49:32 +0100141 close.addE('span').addT(loc.CLOSE);
Akron99713ef2017-06-28 18:19:28 +0200142 close.classList.add('close');
143 close.setAttribute('title', loc.CLOSE);
144 close.onclick = function () {
145 element.style.display = 'none';
146 };
Nils Diewald58141332015-04-07 16:18:45 +0000147
Akron99713ef2017-06-28 18:19:28 +0200148 // Add open in new window button
149 // Add scroll to top button
150 /*
151 var info = document.createElement('li');
152 info.appendChild(document.createElement('span'))
153 .appendChild(document.createTextNode(loc.SHOWINFO));
154 info.classList.add('info');
155 info.setAttribute('title', loc.SHOWINFO);
156 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000157
Akron99713ef2017-06-28 18:19:28 +0200158 ul.appendChild(close);
Nils Diewald58141332015-04-07 16:18:45 +0000159
Akron99713ef2017-06-28 18:19:28 +0200160 element.appendChild(ul);
161 element.appendChild(this._iframe);
Nils Diewald58141332015-04-07 16:18:45 +0000162 };
163
164 element.style.display = 'block';
165 },
166
167 /**
168 * Close tutorial window.
169 */
170 hide : function () {
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000171 this._element.style.display = 'none';
Nils Diewald58141332015-04-07 16:18:45 +0000172 },
173
174 /**
175 * Set a page to be the current tutorial page.
176 * Expects either a string or an element.
177 */
178 setPage : function (obj) {
179 var page = obj;
180 if (typeof page != 'string') {
Akron086fe5d2017-11-13 14:01:45 +0100181 var l = this._iframe !== null ? window.frames[0].location : window.location;
182 page = l.pathname + l.search;
Akron792f58b2015-07-08 18:59:36 +0200183
Akron086fe5d2017-11-13 14:01:45 +0100184 for (var i = 1; i < 5; i++) {
185 if (obj.nodeName === 'SECTION') {
186 if (obj.hasAttribute('id'))
187 page += '#' + obj.getAttribute('id');
188 break;
189 }
190 else if (obj.nodeName === 'PRE' && obj.hasAttribute('id')) {
191 page += '#' + obj.getAttribute('id');
192 break;
193 }
194 else {
195 obj = obj.parentNode;
196 };
197 };
Nils Diewald58141332015-04-07 16:18:45 +0000198 };
199 this._session.set('tutpage', page);
200 },
201
202 /**
203 * Get the current tutorial URL
204 */
205 getPage : function () {
Akron792f58b2015-07-08 18:59:36 +0200206 return this._session.get('tutpage');
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000207 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000208 };
209});
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000210