Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 1 | /** |
| 2 | * Open and close a tutorial page. |
| 3 | * The current page is stored and retrieved in a session cookie. |
| 4 | */ |
Akron | 7bbdb25 | 2018-07-19 16:44:34 +0200 | [diff] [blame] | 5 | // TODO: Make this a panel |
Nils Diewald | ab4d3ca | 2015-04-17 01:48:43 +0000 | [diff] [blame] | 6 | // TODO: Add query mechanism! |
| 7 | // TODO: Highlight current section: |
| 8 | // http://stackoverflow.com/questions/24887258/highlight-navigation-link-as-i-scroll-down-the-page |
Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 9 | "use strict"; |
| 10 | |
Akron | 7bbdb25 | 2018-07-19 16:44:34 +0200 | [diff] [blame] | 11 | define(['session','buttongroup','util'], function (sessionClass, buttonGroupClass) { |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 12 | |
| 13 | // Localization values |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 14 | const loc = KorAP.Locale; |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 15 | loc.CLOSE = loc.CLOSE || 'Close'; |
| 16 | |
Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 17 | const d = document; |
| 18 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 19 | return { |
Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 20 | |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 21 | /** |
| 22 | * Create new tutorial object. |
| 23 | * Accepts an element to bind the tutorial window to. |
| 24 | */ |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 25 | create : function (obj,session) { |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 26 | if (!obj) |
Akron | 086fe5d | 2017-11-13 14:01:45 +0100 | [diff] [blame] | 27 | return null; |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 28 | return Object.create(this)._init(obj,session); |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 29 | }, |
| 30 | |
Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 31 | |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 32 | // Initialize Tutorial object |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 33 | _init : function (obj, session) { |
Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 34 | const t = this; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 35 | |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 36 | if (session === undefined) { |
Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 37 | t._session = sessionClass.create(); |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 38 | } |
| 39 | else { |
Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 40 | t._session = session; |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 41 | }; |
| 42 | |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 43 | if (obj) { |
Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 44 | t._show = obj; |
| 45 | t.start = obj.getAttribute('href'); |
| 46 | |
| 47 | // Unknown which tutorial to show |
| 48 | if (!t.start) |
| 49 | return null; |
| 50 | |
Akron | 086fe5d | 2017-11-13 14:01:45 +0100 | [diff] [blame] | 51 | obj.removeAttribute('href'); |
Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 52 | |
Akron | 086fe5d | 2017-11-13 14:01:45 +0100 | [diff] [blame] | 53 | obj.onclick = function () { |
Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 54 | this.show(); |
| 55 | }.bind(t); |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 56 | |
Akron | 086fe5d | 2017-11-13 14:01:45 +0100 | [diff] [blame] | 57 | // Injects a tutorial div to the body |
Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 58 | const div = d.createElement('div'); |
Akron | 086fe5d | 2017-11-13 14:01:45 +0100 | [diff] [blame] | 59 | div.setAttribute('id', 'tutorial'); |
| 60 | div.style.display = 'none'; |
Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 61 | d.getElementsByTagName('body')[0].appendChild(div); |
| 62 | |
| 63 | t._iframe = null; |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 64 | t._el = div; |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 65 | |
Akron | 086fe5d | 2017-11-13 14:01:45 +0100 | [diff] [blame] | 66 | // Some fields |
Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 67 | t._ql = d.getElementById("ql-field"); |
| 68 | t._q = d.getElementById("q-field") |
| 69 | t._cutoff = d.getElementById("q-cutoff-field"); |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 70 | }; |
| 71 | |
Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 72 | return t; |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 73 | }, |
| 74 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 75 | |
| 76 | /** |
| 77 | * Initialize a search with a defined query. |
| 78 | */ |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 79 | useQuery : function (e) { |
Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 80 | 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 Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 85 | if (qc !== 0 && qc !== "0" && qc !== "off" && qc !== null) { |
Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 86 | if (t._cuttoff) |
| 87 | t._cutoff.checked = true; |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 88 | }; |
| 89 | |
Akron | 086fe5d | 2017-11-13 14:01:45 +0100 | [diff] [blame] | 90 | if (KorAP.QLmenu) { |
| 91 | KorAP.QLmenu.selectValue(ql); |
Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 92 | } |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 93 | |
Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 94 | 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(); |
Akron | a141507 | 2024-10-01 09:57:01 +0200 | [diff] [blame] | 105 | |
| 106 | d.body.scrollTop = 0; |
| 107 | d.documentElement.scrollTop = 0; |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 108 | }, |
| 109 | |
Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 110 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 111 | /** |
| 112 | * Decorate a page with query event handler. |
| 113 | */ |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 114 | initQueries : function (d) { |
Akron | f6302ff | 2021-03-01 17:11:11 +0100 | [diff] [blame] | 115 | 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 Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 122 | }, |
| 123 | |
Nils Diewald | 61e6ff5 | 2015-05-07 17:26:50 +0000 | [diff] [blame] | 124 | /** |
| 125 | * Decorate a page with documentation links |
| 126 | */ |
| 127 | initDocLinks : function (d) { |
Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 128 | const that = this; |
Akron | b50964a | 2020-10-12 11:44:37 +0200 | [diff] [blame] | 129 | Array.from(d.getElementsByClassName('doc-link')).forEach( |
| 130 | i => |
Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 131 | i.onclick = function () { |
Akron | b50964a | 2020-10-12 11:44:37 +0200 | [diff] [blame] | 132 | that.setPage(this.getAttribute('href')); |
| 133 | return true; |
| 134 | } |
Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 135 | ); |
Nils Diewald | 61e6ff5 | 2015-05-07 17:26:50 +0000 | [diff] [blame] | 136 | }, |
| 137 | |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 138 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 139 | /** |
| 140 | * Show the tutorial page embedded. |
| 141 | */ |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 142 | show : function () { |
Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 143 | const t = this; |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 144 | const element = t._el; |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 145 | if (element.style.display === 'block') |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 146 | return; |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 147 | |
Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 148 | if (t._iframe === null) { |
| 149 | t._iframe = d.createElement('iframe'); |
| 150 | t._iframe.setAttribute( |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 151 | 'src', |
Helge | 989947b | 2023-11-07 15:35:06 +0100 | [diff] [blame] | 152 | (t.getPage() || t.start + '?embedded=true') |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 153 | ); |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 154 | |
Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 155 | const btn = buttonGroupClass.create( |
Akron | 7bbdb25 | 2018-07-19 16:44:34 +0200 | [diff] [blame] | 156 | ['action','button-view'] |
| 157 | ); |
| 158 | |
Akron | 792b1a4 | 2020-09-14 18:56:38 +0200 | [diff] [blame] | 159 | btn.add(loc.CLOSE, {'cls':['button-icon','close']}, function () { |
Akron | 7bbdb25 | 2018-07-19 16:44:34 +0200 | [diff] [blame] | 160 | element.style.display = 'none'; |
| 161 | }); |
Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 162 | |
Akron | 7bbdb25 | 2018-07-19 16:44:34 +0200 | [diff] [blame] | 163 | element.appendChild(btn.element()); |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 164 | |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 165 | // 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); |
Akron | 7bbdb25 | 2018-07-19 16:44:34 +0200 | [diff] [blame] | 173 | |
Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 174 | ul.appendChild(close); |
| 175 | element.appendChild(ul); |
Akron | 7bbdb25 | 2018-07-19 16:44:34 +0200 | [diff] [blame] | 176 | */ |
Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 177 | element.appendChild(t._iframe); |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 178 | }; |
| 179 | |
| 180 | element.style.display = 'block'; |
| 181 | }, |
| 182 | |
Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 183 | |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 184 | /** |
| 185 | * Close tutorial window. |
| 186 | */ |
| 187 | hide : function () { |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 188 | this._el.style.display = 'none'; |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 189 | }, |
| 190 | |
Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 191 | |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 192 | /** |
| 193 | * Set a page to be the current tutorial page. |
| 194 | * Expects either a string or an element. |
| 195 | */ |
| 196 | setPage : function (obj) { |
Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 197 | let page = obj; |
| 198 | |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 199 | if (typeof page != 'string') { |
Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 200 | const l = this._iframe !== null ? window.frames[0].location : window.location; |
| 201 | |
Akron | 086fe5d | 2017-11-13 14:01:45 +0100 | [diff] [blame] | 202 | page = l.pathname + l.search; |
Akron | 792f58b | 2015-07-08 18:59:36 +0200 | [diff] [blame] | 203 | |
Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 204 | for (let i = 1; i < 5; i++) { |
| 205 | if ((obj.nodeName === 'SECTION' || obj.nodeName === 'PRE') && obj.hasAttribute('id')) { |
Akron | 086fe5d | 2017-11-13 14:01:45 +0100 | [diff] [blame] | 206 | page += '#' + obj.getAttribute('id'); |
| 207 | break; |
| 208 | } |
| 209 | else { |
| 210 | obj = obj.parentNode; |
Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 211 | if (obj === null) |
| 212 | break; |
Akron | 086fe5d | 2017-11-13 14:01:45 +0100 | [diff] [blame] | 213 | }; |
| 214 | }; |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 215 | }; |
Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 216 | |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 217 | this._session.set('tutpage', page); |
| 218 | }, |
| 219 | |
Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 220 | |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 221 | /** |
| 222 | * Get the current tutorial URL |
| 223 | */ |
| 224 | getPage : function () { |
Akron | 792f58b | 2015-07-08 18:59:36 +0200 | [diff] [blame] | 225 | return this._session.get('tutpage'); |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 226 | } |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 227 | }; |
| 228 | }); |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 229 | |