blob: 2420707196eae97aa90d41ad5e5356c28af04d05 [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
Akron7bbdb252018-07-19 16:44:34 +02009define(['session','buttongroup','util'], function (sessionClass, buttonGroupClass) {
Nils Diewald58141332015-04-07 16:18:45 +000010 "use strict";
11
12 // Localization values
Akron0b489ad2018-02-02 16:49:32 +010013 const loc = KorAP.Locale;
Nils Diewald58141332015-04-07 16:18:45 +000014 loc.CLOSE = loc.CLOSE || 'Close';
15
Nils Diewald0e6992a2015-04-14 20:13:52 +000016 return {
Nils Diewald58141332015-04-07 16:18:45 +000017 /**
18 * Create new tutorial object.
19 * Accepts an element to bind the tutorial window to.
20 */
Akron7716f012015-07-01 20:38:32 +020021 create : function (obj,session) {
Nils Diewald0e6992a2015-04-14 20:13:52 +000022 if (!obj)
Akron086fe5d2017-11-13 14:01:45 +010023 return null;
Akron7716f012015-07-01 20:38:32 +020024 return Object.create(this)._init(obj,session);
Nils Diewald58141332015-04-07 16:18:45 +000025 },
26
27 // Initialize Tutorial object
Akron7716f012015-07-01 20:38:32 +020028 _init : function (obj, session) {
Nils Diewald0e6992a2015-04-14 20:13:52 +000029
Akron7716f012015-07-01 20:38:32 +020030 if (session === undefined) {
Akron086fe5d2017-11-13 14:01:45 +010031 this._session = sessionClass.create();
Akron7716f012015-07-01 20:38:32 +020032 }
33 else {
Akron086fe5d2017-11-13 14:01:45 +010034 this._session = session;
Akron7716f012015-07-01 20:38:32 +020035 };
36
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000037
38 if (obj) {
Akron086fe5d2017-11-13 14:01:45 +010039 this._show = obj;
40 this.start = obj.getAttribute('href');
41 obj.removeAttribute('href');
42 var that = this;
43 obj.onclick = function () {
44 that.show();
45 };
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000046
Akron086fe5d2017-11-13 14:01:45 +010047 // Injects a tutorial div to the body
48 var div = document.createElement('div');
49 div.setAttribute('id', 'tutorial');
50 div.style.display = 'none';
51 document.getElementsByTagName('body')[0].appendChild(div);
52 this._iframe = null;
53 this._element = div;
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000054
Akron086fe5d2017-11-13 14:01:45 +010055 // Some fields
56 this._ql = document.getElementById("ql-field");
57 this._q = document.getElementById("q-field")
58 this._cutoff = document.getElementById("q-cutoff-field");
Nils Diewald58141332015-04-07 16:18:45 +000059 };
60
Nils Diewald58141332015-04-07 16:18:45 +000061 return this;
62 },
63
Nils Diewald7148c6f2015-05-04 15:07:53 +000064
65 /**
66 * Initialize a search with a defined query.
67 */
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000068 useQuery : function (e) {
69 var q = e.getAttribute("data-query");
70 var ql = e.getAttribute("data-query-language");
71 var qc = e.getAttribute("data-query-cutoff");
72 if (qc !== 0 && qc !== "0" && qc !== "off" && qc !== null) {
Akron086fe5d2017-11-13 14:01:45 +010073 this._cutoff.checked = true;
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000074 };
75
Akron086fe5d2017-11-13 14:01:45 +010076 if (KorAP.QLmenu) {
77 KorAP.QLmenu.selectValue(ql);
78 };
79 /*
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000080 var qlf = this._ql.options;
81 for (var i in qlf) {
Akron99713ef2017-06-28 18:19:28 +020082 if (qlf[i].value == ql) {
83 qlf[i].selected = true;
84 break;
85 };
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000086 };
Akron086fe5d2017-11-13 14:01:45 +010087 */
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000088
89 this._q.value = q;
90 this.setPage(e);
91 this.hide();
92 },
93
Nils Diewald7148c6f2015-05-04 15:07:53 +000094 /**
95 * Decorate a page with query event handler.
96 */
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000097 initQueries : function (d) {
Akronf4a7cf42018-01-09 15:58:45 +010098 var qs = d.querySelectorAll('pre.query.tutorial:not(.unsupported)');
Nils Diewaldfccfbcb2015-04-29 20:48:19 +000099 var that = this;
100 for (var i = 0; i < qs.length; i++) {
Akron086fe5d2017-11-13 14:01:45 +0100101 qs[i].onclick = function (e) {
102 that.useQuery(this,e);
103 };
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000104 };
105 },
106
Nils Diewald61e6ff52015-05-07 17:26:50 +0000107 /**
108 * Decorate a page with documentation links
109 */
110 initDocLinks : function (d) {
111 var dl = d.getElementsByClassName('doc-link');
112 var that = this;
113 for (var i = 0; i < dl.length; i++) {
Akron086fe5d2017-11-13 14:01:45 +0100114 dl[i].onclick = function (e) {
115 that.setPage(this.getAttribute('href'));
116 return true;
117 };
Nils Diewald61e6ff52015-05-07 17:26:50 +0000118 };
119 },
120
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000121
Nils Diewald7148c6f2015-05-04 15:07:53 +0000122 /**
123 * Show the tutorial page embedded.
124 */
Nils Diewald58141332015-04-07 16:18:45 +0000125 show : function () {
126 var element = this._element;
127 if (element.style.display === 'block')
Akron99713ef2017-06-28 18:19:28 +0200128 return;
Nils Diewald58141332015-04-07 16:18:45 +0000129
130 if (this._iframe === null) {
Akron99713ef2017-06-28 18:19:28 +0200131 this._iframe = document.createElement('iframe');
132 this._iframe.setAttribute(
133 'src',
134 (this.getPage() || this.start) + '?embedded=true'
135 );
Nils Diewald58141332015-04-07 16:18:45 +0000136
Akron7bbdb252018-07-19 16:44:34 +0200137 var btn = buttonGroupClass.create(
138 ['action','button-view']
139 );
140
141 var that = this;
Akron792b1a42020-09-14 18:56:38 +0200142 btn.add(loc.CLOSE, {'cls':['button-icon','close']}, function () {
Akron7bbdb252018-07-19 16:44:34 +0200143 element.style.display = 'none';
144 });
145 element.appendChild(btn.element());
Nils Diewald58141332015-04-07 16:18:45 +0000146
Akron99713ef2017-06-28 18:19:28 +0200147 // Add open in new window button
148 // Add scroll to top button
149 /*
150 var info = document.createElement('li');
151 info.appendChild(document.createElement('span'))
152 .appendChild(document.createTextNode(loc.SHOWINFO));
153 info.classList.add('info');
154 info.setAttribute('title', loc.SHOWINFO);
155 */
Akron7bbdb252018-07-19 16:44:34 +0200156
157 /*
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);
Akron7bbdb252018-07-19 16:44:34 +0200161 */
Akron99713ef2017-06-28 18:19:28 +0200162 element.appendChild(this._iframe);
Nils Diewald58141332015-04-07 16:18:45 +0000163 };
164
165 element.style.display = 'block';
166 },
167
168 /**
169 * Close tutorial window.
170 */
171 hide : function () {
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000172 this._element.style.display = 'none';
Nils Diewald58141332015-04-07 16:18:45 +0000173 },
174
175 /**
176 * Set a page to be the current tutorial page.
177 * Expects either a string or an element.
178 */
179 setPage : function (obj) {
180 var page = obj;
181 if (typeof page != 'string') {
Akron086fe5d2017-11-13 14:01:45 +0100182 var l = this._iframe !== null ? window.frames[0].location : window.location;
183 page = l.pathname + l.search;
Akron792f58b2015-07-08 18:59:36 +0200184
Akron086fe5d2017-11-13 14:01:45 +0100185 for (var i = 1; i < 5; i++) {
186 if (obj.nodeName === 'SECTION') {
187 if (obj.hasAttribute('id'))
188 page += '#' + obj.getAttribute('id');
189 break;
190 }
191 else if (obj.nodeName === 'PRE' && obj.hasAttribute('id')) {
192 page += '#' + obj.getAttribute('id');
193 break;
194 }
195 else {
196 obj = obj.parentNode;
197 };
198 };
Nils Diewald58141332015-04-07 16:18:45 +0000199 };
200 this._session.set('tutpage', page);
201 },
202
203 /**
204 * Get the current tutorial URL
205 */
206 getPage : function () {
Akron792f58b2015-07-08 18:59:36 +0200207 return this._session.get('tutpage');
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000208 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000209 };
210});
Nils Diewaldfccfbcb2015-04-29 20:48:19 +0000211