blob: ae42badf2d20f3c4d11befdc1855d47eed414f70 [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;
Akron24aa0052020-11-10 11:00:34 +010064 t._el = 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();
Akrona1415072024-10-01 09:57:01 +0200105
106 d.body.scrollTop = 0;
107 d.documentElement.scrollTop = 0;
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000108 },
109
Akronb7829c42020-10-20 16:29:46 +0200110
Nils Diewald7148c6f2015-05-04 15:07:53 +0000111 /**
112 * Decorate a page with query event handler.
113 */
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000114 initQueries : function (d) {
Akronf6302ff2021-03-01 17:11:11 +0100115 let qs = d.querySelectorAll('pre.query.tutorial:not(.unsupported)');
116 const that = this;
117 for (var i = 0; i < qs.length; i++) {
118 qs[i].onclick = function (e) {
119 that.useQuery(this,e);
120 };
121 };
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000122 },
123
Nils Diewald61e6ff52015-05-07 17:26:50 +0000124 /**
125 * Decorate a page with documentation links
126 */
127 initDocLinks : function (d) {
Akronb7829c42020-10-20 16:29:46 +0200128 const that = this;
Akronb50964a2020-10-12 11:44:37 +0200129 Array.from(d.getElementsByClassName('doc-link')).forEach(
130 i =>
Akronb7829c42020-10-20 16:29:46 +0200131 i.onclick = function () {
Akronb50964a2020-10-12 11:44:37 +0200132 that.setPage(this.getAttribute('href'));
133 return true;
134 }
Akronb7829c42020-10-20 16:29:46 +0200135 );
Nils Diewald61e6ff52015-05-07 17:26:50 +0000136 },
137
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000138
Nils Diewald7148c6f2015-05-04 15:07:53 +0000139 /**
140 * Show the tutorial page embedded.
141 */
Nils Diewald58141332015-04-07 16:18:45 +0000142 show : function () {
Akronb7829c42020-10-20 16:29:46 +0200143 const t = this;
Akron24aa0052020-11-10 11:00:34 +0100144 const element = t._el;
Nils Diewald58141332015-04-07 16:18:45 +0000145 if (element.style.display === 'block')
Akron99713ef2017-06-28 18:19:28 +0200146 return;
Nils Diewald58141332015-04-07 16:18:45 +0000147
Akronb7829c42020-10-20 16:29:46 +0200148 if (t._iframe === null) {
149 t._iframe = d.createElement('iframe');
150 t._iframe.setAttribute(
Akron99713ef2017-06-28 18:19:28 +0200151 'src',
Helge989947b2023-11-07 15:35:06 +0100152 (t.getPage() || t.start + '?embedded=true')
Akron99713ef2017-06-28 18:19:28 +0200153 );
Nils Diewald58141332015-04-07 16:18:45 +0000154
Akronb7829c42020-10-20 16:29:46 +0200155 const btn = buttonGroupClass.create(
Akron7bbdb252018-07-19 16:44:34 +0200156 ['action','button-view']
157 );
158
Akron792b1a42020-09-14 18:56:38 +0200159 btn.add(loc.CLOSE, {'cls':['button-icon','close']}, function () {
Akron7bbdb252018-07-19 16:44:34 +0200160 element.style.display = 'none';
161 });
Akronb7829c42020-10-20 16:29:46 +0200162
Akron7bbdb252018-07-19 16:44:34 +0200163 element.appendChild(btn.element());
Nils Diewald58141332015-04-07 16:18:45 +0000164
Akron99713ef2017-06-28 18:19:28 +0200165 // Add open in new window button
166 // Add scroll to top button
167 /*
168 var info = document.createElement('li');
169 info.appendChild(document.createElement('span'))
170 .appendChild(document.createTextNode(loc.SHOWINFO));
171 info.classList.add('info');
172 info.setAttribute('title', loc.SHOWINFO);
Akron7bbdb252018-07-19 16:44:34 +0200173
Akronb7829c42020-10-20 16:29:46 +0200174 ul.appendChild(close);
175 element.appendChild(ul);
Akron7bbdb252018-07-19 16:44:34 +0200176 */
Akronb7829c42020-10-20 16:29:46 +0200177 element.appendChild(t._iframe);
Nils Diewald58141332015-04-07 16:18:45 +0000178 };
179
180 element.style.display = 'block';
181 },
182
Akronb7829c42020-10-20 16:29:46 +0200183
Nils Diewald58141332015-04-07 16:18:45 +0000184 /**
185 * Close tutorial window.
186 */
187 hide : function () {
Akron24aa0052020-11-10 11:00:34 +0100188 this._el.style.display = 'none';
Nils Diewald58141332015-04-07 16:18:45 +0000189 },
190
Akronb7829c42020-10-20 16:29:46 +0200191
Nils Diewald58141332015-04-07 16:18:45 +0000192 /**
193 * Set a page to be the current tutorial page.
194 * Expects either a string or an element.
195 */
196 setPage : function (obj) {
Akronb7829c42020-10-20 16:29:46 +0200197 let page = obj;
198
Nils Diewald58141332015-04-07 16:18:45 +0000199 if (typeof page != 'string') {
Akronb7829c42020-10-20 16:29:46 +0200200 const l = this._iframe !== null ? window.frames[0].location : window.location;
201
Akron086fe5d2017-11-13 14:01:45 +0100202 page = l.pathname + l.search;
Akron792f58b2015-07-08 18:59:36 +0200203
Akronb7829c42020-10-20 16:29:46 +0200204 for (let i = 1; i < 5; i++) {
205 if ((obj.nodeName === 'SECTION' || obj.nodeName === 'PRE') && obj.hasAttribute('id')) {
Akron086fe5d2017-11-13 14:01:45 +0100206 page += '#' + obj.getAttribute('id');
207 break;
208 }
209 else {
210 obj = obj.parentNode;
Akronb7829c42020-10-20 16:29:46 +0200211 if (obj === null)
212 break;
Akron086fe5d2017-11-13 14:01:45 +0100213 };
214 };
Nils Diewald58141332015-04-07 16:18:45 +0000215 };
Akronb7829c42020-10-20 16:29:46 +0200216
Nils Diewald58141332015-04-07 16:18:45 +0000217 this._session.set('tutpage', page);
218 },
219
Akronb7829c42020-10-20 16:29:46 +0200220
Nils Diewald58141332015-04-07 16:18:45 +0000221 /**
222 * Get the current tutorial URL
223 */
224 getPage : function () {
Akron792f58b2015-07-08 18:59:36 +0200225 return this._session.get('tutpage');
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000226 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000227 };
228});
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000229