blob: 5573682ab9e4912d764d83a5418d65b6784d4aaf [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();
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) {
Akronf6302ff2021-03-01 17:11:11 +0100112 let qs = d.querySelectorAll('pre.query.tutorial:not(.unsupported)');
113 const that = this;
114 for (var i = 0; i < qs.length; i++) {
115 qs[i].onclick = function (e) {
116 that.useQuery(this,e);
117 };
118 };
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000119 },
120
Nils Diewald61e6ff52015-05-07 17:26:50 +0000121 /**
122 * Decorate a page with documentation links
123 */
124 initDocLinks : function (d) {
Akronb7829c42020-10-20 16:29:46 +0200125 const that = this;
Akronb50964a2020-10-12 11:44:37 +0200126 Array.from(d.getElementsByClassName('doc-link')).forEach(
127 i =>
Akronb7829c42020-10-20 16:29:46 +0200128 i.onclick = function () {
Akronb50964a2020-10-12 11:44:37 +0200129 that.setPage(this.getAttribute('href'));
130 return true;
131 }
Akronb7829c42020-10-20 16:29:46 +0200132 );
Nils Diewald61e6ff52015-05-07 17:26:50 +0000133 },
134
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000135
Nils Diewald7148c6f2015-05-04 15:07:53 +0000136 /**
137 * Show the tutorial page embedded.
138 */
Nils Diewald58141332015-04-07 16:18:45 +0000139 show : function () {
Akronb7829c42020-10-20 16:29:46 +0200140 const t = this;
Akron24aa0052020-11-10 11:00:34 +0100141 const element = t._el;
Nils Diewald58141332015-04-07 16:18:45 +0000142 if (element.style.display === 'block')
Akron99713ef2017-06-28 18:19:28 +0200143 return;
Nils Diewald58141332015-04-07 16:18:45 +0000144
Akronb7829c42020-10-20 16:29:46 +0200145 if (t._iframe === null) {
146 t._iframe = d.createElement('iframe');
147 t._iframe.setAttribute(
Akron99713ef2017-06-28 18:19:28 +0200148 'src',
Helge989947b2023-11-07 15:35:06 +0100149 (t.getPage() || t.start + '?embedded=true')
Akron99713ef2017-06-28 18:19:28 +0200150 );
Nils Diewald58141332015-04-07 16:18:45 +0000151
Akronb7829c42020-10-20 16:29:46 +0200152 const btn = buttonGroupClass.create(
Akron7bbdb252018-07-19 16:44:34 +0200153 ['action','button-view']
154 );
155
Akron792b1a42020-09-14 18:56:38 +0200156 btn.add(loc.CLOSE, {'cls':['button-icon','close']}, function () {
Akron7bbdb252018-07-19 16:44:34 +0200157 element.style.display = 'none';
158 });
Akronb7829c42020-10-20 16:29:46 +0200159
Akron7bbdb252018-07-19 16:44:34 +0200160 element.appendChild(btn.element());
Nils Diewald58141332015-04-07 16:18:45 +0000161
Akron99713ef2017-06-28 18:19:28 +0200162 // Add open in new window button
163 // Add scroll to top button
164 /*
165 var info = document.createElement('li');
166 info.appendChild(document.createElement('span'))
167 .appendChild(document.createTextNode(loc.SHOWINFO));
168 info.classList.add('info');
169 info.setAttribute('title', loc.SHOWINFO);
Akron7bbdb252018-07-19 16:44:34 +0200170
Akronb7829c42020-10-20 16:29:46 +0200171 ul.appendChild(close);
172 element.appendChild(ul);
Akron7bbdb252018-07-19 16:44:34 +0200173 */
Akronb7829c42020-10-20 16:29:46 +0200174 element.appendChild(t._iframe);
Nils Diewald58141332015-04-07 16:18:45 +0000175 };
176
177 element.style.display = 'block';
178 },
179
Akronb7829c42020-10-20 16:29:46 +0200180
Nils Diewald58141332015-04-07 16:18:45 +0000181 /**
182 * Close tutorial window.
183 */
184 hide : function () {
Akron24aa0052020-11-10 11:00:34 +0100185 this._el.style.display = 'none';
Nils Diewald58141332015-04-07 16:18:45 +0000186 },
187
Akronb7829c42020-10-20 16:29:46 +0200188
Nils Diewald58141332015-04-07 16:18:45 +0000189 /**
190 * Set a page to be the current tutorial page.
191 * Expects either a string or an element.
192 */
193 setPage : function (obj) {
Akronb7829c42020-10-20 16:29:46 +0200194 let page = obj;
195
Nils Diewald58141332015-04-07 16:18:45 +0000196 if (typeof page != 'string') {
Akronb7829c42020-10-20 16:29:46 +0200197 const l = this._iframe !== null ? window.frames[0].location : window.location;
198
Akron086fe5d2017-11-13 14:01:45 +0100199 page = l.pathname + l.search;
Akron792f58b2015-07-08 18:59:36 +0200200
Akronb7829c42020-10-20 16:29:46 +0200201 for (let i = 1; i < 5; i++) {
202 if ((obj.nodeName === 'SECTION' || obj.nodeName === 'PRE') && obj.hasAttribute('id')) {
Akron086fe5d2017-11-13 14:01:45 +0100203 page += '#' + obj.getAttribute('id');
204 break;
205 }
206 else {
207 obj = obj.parentNode;
Akronb7829c42020-10-20 16:29:46 +0200208 if (obj === null)
209 break;
Akron086fe5d2017-11-13 14:01:45 +0100210 };
211 };
Nils Diewald58141332015-04-07 16:18:45 +0000212 };
Akronb7829c42020-10-20 16:29:46 +0200213
Nils Diewald58141332015-04-07 16:18:45 +0000214 this._session.set('tutpage', page);
215 },
216
Akronb7829c42020-10-20 16:29:46 +0200217
Nils Diewald58141332015-04-07 16:18:45 +0000218 /**
219 * Get the current tutorial URL
220 */
221 getPage : function () {
Akron792f58b2015-07-08 18:59:36 +0200222 return this._session.get('tutpage');
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000223 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000224 };
225});
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000226