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(); |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 105 | }, |
| 106 | |
Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 107 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 108 | /** |
| 109 | * Decorate a page with query event handler. |
| 110 | */ |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 111 | initQueries : function (d) { |
Akron | f6302ff | 2021-03-01 17:11:11 +0100 | [diff] [blame] | 112 | 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 Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 119 | }, |
| 120 | |
Nils Diewald | 61e6ff5 | 2015-05-07 17:26:50 +0000 | [diff] [blame] | 121 | /** |
| 122 | * Decorate a page with documentation links |
| 123 | */ |
| 124 | initDocLinks : function (d) { |
Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 125 | const that = this; |
Akron | b50964a | 2020-10-12 11:44:37 +0200 | [diff] [blame] | 126 | Array.from(d.getElementsByClassName('doc-link')).forEach( |
| 127 | i => |
Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 128 | i.onclick = function () { |
Akron | b50964a | 2020-10-12 11:44:37 +0200 | [diff] [blame] | 129 | that.setPage(this.getAttribute('href')); |
| 130 | return true; |
| 131 | } |
Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 132 | ); |
Nils Diewald | 61e6ff5 | 2015-05-07 17:26:50 +0000 | [diff] [blame] | 133 | }, |
| 134 | |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 135 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 136 | /** |
| 137 | * Show the tutorial page embedded. |
| 138 | */ |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 139 | show : function () { |
Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 140 | const t = this; |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 141 | const element = t._el; |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 142 | if (element.style.display === 'block') |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 143 | return; |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 144 | |
Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 145 | if (t._iframe === null) { |
| 146 | t._iframe = d.createElement('iframe'); |
| 147 | t._iframe.setAttribute( |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 148 | 'src', |
Helge | 989947b | 2023-11-07 15:35:06 +0100 | [diff] [blame^] | 149 | (t.getPage() || t.start + '?embedded=true') |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 150 | ); |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 151 | |
Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 152 | const btn = buttonGroupClass.create( |
Akron | 7bbdb25 | 2018-07-19 16:44:34 +0200 | [diff] [blame] | 153 | ['action','button-view'] |
| 154 | ); |
| 155 | |
Akron | 792b1a4 | 2020-09-14 18:56:38 +0200 | [diff] [blame] | 156 | btn.add(loc.CLOSE, {'cls':['button-icon','close']}, function () { |
Akron | 7bbdb25 | 2018-07-19 16:44:34 +0200 | [diff] [blame] | 157 | element.style.display = 'none'; |
| 158 | }); |
Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 159 | |
Akron | 7bbdb25 | 2018-07-19 16:44:34 +0200 | [diff] [blame] | 160 | element.appendChild(btn.element()); |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 161 | |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 162 | // 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); |
Akron | 7bbdb25 | 2018-07-19 16:44:34 +0200 | [diff] [blame] | 170 | |
Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 171 | ul.appendChild(close); |
| 172 | element.appendChild(ul); |
Akron | 7bbdb25 | 2018-07-19 16:44:34 +0200 | [diff] [blame] | 173 | */ |
Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 174 | element.appendChild(t._iframe); |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 175 | }; |
| 176 | |
| 177 | element.style.display = 'block'; |
| 178 | }, |
| 179 | |
Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 180 | |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 181 | /** |
| 182 | * Close tutorial window. |
| 183 | */ |
| 184 | hide : function () { |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 185 | this._el.style.display = 'none'; |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 186 | }, |
| 187 | |
Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 188 | |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 189 | /** |
| 190 | * Set a page to be the current tutorial page. |
| 191 | * Expects either a string or an element. |
| 192 | */ |
| 193 | setPage : function (obj) { |
Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 194 | let page = obj; |
| 195 | |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 196 | if (typeof page != 'string') { |
Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 197 | const l = this._iframe !== null ? window.frames[0].location : window.location; |
| 198 | |
Akron | 086fe5d | 2017-11-13 14:01:45 +0100 | [diff] [blame] | 199 | page = l.pathname + l.search; |
Akron | 792f58b | 2015-07-08 18:59:36 +0200 | [diff] [blame] | 200 | |
Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 201 | for (let i = 1; i < 5; i++) { |
| 202 | if ((obj.nodeName === 'SECTION' || obj.nodeName === 'PRE') && obj.hasAttribute('id')) { |
Akron | 086fe5d | 2017-11-13 14:01:45 +0100 | [diff] [blame] | 203 | page += '#' + obj.getAttribute('id'); |
| 204 | break; |
| 205 | } |
| 206 | else { |
| 207 | obj = obj.parentNode; |
Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 208 | if (obj === null) |
| 209 | break; |
Akron | 086fe5d | 2017-11-13 14:01:45 +0100 | [diff] [blame] | 210 | }; |
| 211 | }; |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 212 | }; |
Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 213 | |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 214 | this._session.set('tutpage', page); |
| 215 | }, |
| 216 | |
Akron | b7829c4 | 2020-10-20 16:29:46 +0200 | [diff] [blame] | 217 | |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 218 | /** |
| 219 | * Get the current tutorial URL |
| 220 | */ |
| 221 | getPage : function () { |
Akron | 792f58b | 2015-07-08 18:59:36 +0200 | [diff] [blame] | 222 | return this._session.get('tutpage'); |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 223 | } |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 224 | }; |
| 225 | }); |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 226 | |