blob: ee311ddb11de20fabbb9cfc8e6bd589f4054357f [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 */
Akron7bbdb252018-07-19 16:44:34 +02005// TODO: Make this a panel
Nils Diewaldab4d3ca2015-04-17 01:48:43 +00006// TODO: Add query mechanism!
7// TODO: Highlight current section:
8// http://stackoverflow.com/questions/24887258/highlight-navigation-link-as-i-scroll-down-the-page
Akronb7829c42020-10-20 16:29:46 +02009"use strict";
10
Akron7bbdb252018-07-19 16:44:34 +020011define(['session','buttongroup','util'], function (sessionClass, buttonGroupClass) {
Nils Diewald58141332015-04-07 16:18:45 +000012
13 // Localization values
Akron0b489ad2018-02-02 16:49:32 +010014 const loc = KorAP.Locale;
Nils Diewald58141332015-04-07 16:18:45 +000015 loc.CLOSE = loc.CLOSE || 'Close';
16
Akronb7829c42020-10-20 16:29:46 +020017 const d = document;
18
Nils Diewald0e6992a2015-04-14 20:13:52 +000019 return {
Akronb7829c42020-10-20 16:29:46 +020020
Nils Diewald58141332015-04-07 16:18:45 +000021 /**
22 * Create new tutorial object.
23 * Accepts an element to bind the tutorial window to.
24 */
Akron7716f012015-07-01 20:38:32 +020025 create : function (obj,session) {
Nils Diewald0e6992a2015-04-14 20:13:52 +000026 if (!obj)
Akron086fe5d2017-11-13 14:01:45 +010027 return null;
Akron7716f012015-07-01 20:38:32 +020028 return Object.create(this)._init(obj,session);
Nils Diewald58141332015-04-07 16:18:45 +000029 },
30
Akronb7829c42020-10-20 16:29:46 +020031
Nils Diewald58141332015-04-07 16:18:45 +000032 // Initialize Tutorial object
Akron7716f012015-07-01 20:38:32 +020033 _init : function (obj, session) {
Akronb7829c42020-10-20 16:29:46 +020034 const t = this;
Nils Diewald0e6992a2015-04-14 20:13:52 +000035
Akron7716f012015-07-01 20:38:32 +020036 if (session === undefined) {
Akronb7829c42020-10-20 16:29:46 +020037 t._session = sessionClass.create();
Akron7716f012015-07-01 20:38:32 +020038 }
39 else {
Akronb7829c42020-10-20 16:29:46 +020040 t._session = session;
Akron7716f012015-07-01 20:38:32 +020041 };
42
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000043 if (obj) {
Akronb7829c42020-10-20 16:29:46 +020044 t._show = obj;
45 t.start = obj.getAttribute('href');
46
47 // Unknown which tutorial to show
48 if (!t.start)
49 return null;
50
Akron086fe5d2017-11-13 14:01:45 +010051 obj.removeAttribute('href');
Akronb7829c42020-10-20 16:29:46 +020052
Akron086fe5d2017-11-13 14:01:45 +010053 obj.onclick = function () {
Akronb7829c42020-10-20 16:29:46 +020054 this.show();
55 }.bind(t);
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000056
Akron086fe5d2017-11-13 14:01:45 +010057 // Injects a tutorial div to the body
Akronb7829c42020-10-20 16:29:46 +020058 const div = d.createElement('div');
Akron086fe5d2017-11-13 14:01:45 +010059 div.setAttribute('id', 'tutorial');
60 div.style.display = 'none';
Akronb7829c42020-10-20 16:29:46 +020061 d.getElementsByTagName('body')[0].appendChild(div);
62
63 t._iframe = null;
64 t._element = div;
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000065
Akron086fe5d2017-11-13 14:01:45 +010066 // Some fields
Akronb7829c42020-10-20 16:29:46 +020067 t._ql = d.getElementById("ql-field");
68 t._q = d.getElementById("q-field")
69 t._cutoff = d.getElementById("q-cutoff-field");
Nils Diewald58141332015-04-07 16:18:45 +000070 };
71
Akronb7829c42020-10-20 16:29:46 +020072 return t;
Nils Diewald58141332015-04-07 16:18:45 +000073 },
74
Nils Diewald7148c6f2015-05-04 15:07:53 +000075
76 /**
77 * Initialize a search with a defined query.
78 */
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000079 useQuery : function (e) {
Akronb7829c42020-10-20 16:29:46 +020080 const t = this;
81 const q = e.getAttribute("data-query"),
82 ql = e.getAttribute("data-query-language"),
83 qc = e.getAttribute("data-query-cutoff");
84
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000085 if (qc !== 0 && qc !== "0" && qc !== "off" && qc !== null) {
Akronb7829c42020-10-20 16:29:46 +020086 if (t._cuttoff)
87 t._cutoff.checked = true;
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000088 };
89
Akron086fe5d2017-11-13 14:01:45 +010090 if (KorAP.QLmenu) {
91 KorAP.QLmenu.selectValue(ql);
Akronb7829c42020-10-20 16:29:46 +020092 }
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000093
Akronb7829c42020-10-20 16:29:46 +020094 else if (t._ql) {
95 let found = Array.from(t._ql.options).find(o => o.value === ql);
96 if (found)
97 found.selected = true;
98 };
99
100 if (t._q)
101 t._q.value = q;
102
103 t.setPage(e);
104 t.hide();
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000105 },
106
Akronb7829c42020-10-20 16:29:46 +0200107
Nils Diewald7148c6f2015-05-04 15:07:53 +0000108 /**
109 * Decorate a page with query event handler.
110 */
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000111 initQueries : function (d) {
Akronb50964a2020-10-12 11:44:37 +0200112 Array.from(d.querySelectorAll('pre.query.tutorial:not(.unsupported)')).forEach(
113 i =>
114 i.onclick = function (e) {
Akronb7829c42020-10-20 16:29:46 +0200115 this.useQuery(this,e);
116 }.bind(this)
Akronb50964a2020-10-12 11:44:37 +0200117 );
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000118 },
119
Nils Diewald61e6ff52015-05-07 17:26:50 +0000120 /**
121 * Decorate a page with documentation links
122 */
123 initDocLinks : function (d) {
Akronb7829c42020-10-20 16:29:46 +0200124 const that = this;
Akronb50964a2020-10-12 11:44:37 +0200125 Array.from(d.getElementsByClassName('doc-link')).forEach(
126 i =>
Akronb7829c42020-10-20 16:29:46 +0200127 i.onclick = function () {
Akronb50964a2020-10-12 11:44:37 +0200128 that.setPage(this.getAttribute('href'));
129 return true;
130 }
Akronb7829c42020-10-20 16:29:46 +0200131 );
Nils Diewald61e6ff52015-05-07 17:26:50 +0000132 },
133
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000134
Nils Diewald7148c6f2015-05-04 15:07:53 +0000135 /**
136 * Show the tutorial page embedded.
137 */
Nils Diewald58141332015-04-07 16:18:45 +0000138 show : function () {
Akronb7829c42020-10-20 16:29:46 +0200139 const t = this;
140 const element = t._element;
Nils Diewald58141332015-04-07 16:18:45 +0000141 if (element.style.display === 'block')
Akron99713ef2017-06-28 18:19:28 +0200142 return;
Nils Diewald58141332015-04-07 16:18:45 +0000143
Akronb7829c42020-10-20 16:29:46 +0200144 if (t._iframe === null) {
145 t._iframe = d.createElement('iframe');
146 t._iframe.setAttribute(
Akron99713ef2017-06-28 18:19:28 +0200147 'src',
Akronb7829c42020-10-20 16:29:46 +0200148 (t.getPage() || t.start) + '?embedded=true'
Akron99713ef2017-06-28 18:19:28 +0200149 );
Nils Diewald58141332015-04-07 16:18:45 +0000150
Akronb7829c42020-10-20 16:29:46 +0200151 const btn = buttonGroupClass.create(
Akron7bbdb252018-07-19 16:44:34 +0200152 ['action','button-view']
153 );
154
Akron792b1a42020-09-14 18:56:38 +0200155 btn.add(loc.CLOSE, {'cls':['button-icon','close']}, function () {
Akron7bbdb252018-07-19 16:44:34 +0200156 element.style.display = 'none';
157 });
Akronb7829c42020-10-20 16:29:46 +0200158
Akron7bbdb252018-07-19 16:44:34 +0200159 element.appendChild(btn.element());
Nils Diewald58141332015-04-07 16:18:45 +0000160
Akron99713ef2017-06-28 18:19:28 +0200161 // Add open in new window button
162 // Add scroll to top button
163 /*
164 var info = document.createElement('li');
165 info.appendChild(document.createElement('span'))
166 .appendChild(document.createTextNode(loc.SHOWINFO));
167 info.classList.add('info');
168 info.setAttribute('title', loc.SHOWINFO);
Akron7bbdb252018-07-19 16:44:34 +0200169
Akronb7829c42020-10-20 16:29:46 +0200170 ul.appendChild(close);
171 element.appendChild(ul);
Akron7bbdb252018-07-19 16:44:34 +0200172 */
Akronb7829c42020-10-20 16:29:46 +0200173 element.appendChild(t._iframe);
Nils Diewald58141332015-04-07 16:18:45 +0000174 };
175
176 element.style.display = 'block';
177 },
178
Akronb7829c42020-10-20 16:29:46 +0200179
Nils Diewald58141332015-04-07 16:18:45 +0000180 /**
181 * Close tutorial window.
182 */
183 hide : function () {
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000184 this._element.style.display = 'none';
Nils Diewald58141332015-04-07 16:18:45 +0000185 },
186
Akronb7829c42020-10-20 16:29:46 +0200187
Nils Diewald58141332015-04-07 16:18:45 +0000188 /**
189 * Set a page to be the current tutorial page.
190 * Expects either a string or an element.
191 */
192 setPage : function (obj) {
Akronb7829c42020-10-20 16:29:46 +0200193 let page = obj;
194
Nils Diewald58141332015-04-07 16:18:45 +0000195 if (typeof page != 'string') {
Akronb7829c42020-10-20 16:29:46 +0200196 const l = this._iframe !== null ? window.frames[0].location : window.location;
197
Akron086fe5d2017-11-13 14:01:45 +0100198 page = l.pathname + l.search;
Akron792f58b2015-07-08 18:59:36 +0200199
Akronb7829c42020-10-20 16:29:46 +0200200 for (let i = 1; i < 5; i++) {
201 if ((obj.nodeName === 'SECTION' || obj.nodeName === 'PRE') && obj.hasAttribute('id')) {
Akron086fe5d2017-11-13 14:01:45 +0100202 page += '#' + obj.getAttribute('id');
203 break;
204 }
205 else {
206 obj = obj.parentNode;
Akronb7829c42020-10-20 16:29:46 +0200207 if (obj === null)
208 break;
Akron086fe5d2017-11-13 14:01:45 +0100209 };
210 };
Nils Diewald58141332015-04-07 16:18:45 +0000211 };
Akronb7829c42020-10-20 16:29:46 +0200212
Nils Diewald58141332015-04-07 16:18:45 +0000213 this._session.set('tutpage', page);
214 },
215
Akronb7829c42020-10-20 16:29:46 +0200216
Nils Diewald58141332015-04-07 16:18:45 +0000217 /**
218 * Get the current tutorial URL
219 */
220 getPage : function () {
Akron792f58b2015-07-08 18:59:36 +0200221 return this._session.get('tutpage');
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000222 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000223 };
224});
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000225