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 | 7bbdb25 | 2018-07-19 16:44:34 +0200 | [diff] [blame] | 9 | define(['session','buttongroup','util'], function (sessionClass, buttonGroupClass) { |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 10 | "use strict"; |
| 11 | |
| 12 | // Localization values |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 13 | const loc = KorAP.Locale; |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 14 | loc.CLOSE = loc.CLOSE || 'Close'; |
| 15 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 16 | return { |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 17 | /** |
| 18 | * Create new tutorial object. |
| 19 | * Accepts an element to bind the tutorial window to. |
| 20 | */ |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 21 | create : function (obj,session) { |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 22 | if (!obj) |
Akron | 086fe5d | 2017-11-13 14:01:45 +0100 | [diff] [blame] | 23 | return null; |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 24 | return Object.create(this)._init(obj,session); |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 25 | }, |
| 26 | |
| 27 | // Initialize Tutorial object |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 28 | _init : function (obj, session) { |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 29 | |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 30 | if (session === undefined) { |
Akron | 086fe5d | 2017-11-13 14:01:45 +0100 | [diff] [blame] | 31 | this._session = sessionClass.create(); |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 32 | } |
| 33 | else { |
Akron | 086fe5d | 2017-11-13 14:01:45 +0100 | [diff] [blame] | 34 | this._session = session; |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 35 | }; |
| 36 | |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 37 | |
| 38 | if (obj) { |
Akron | 086fe5d | 2017-11-13 14:01:45 +0100 | [diff] [blame] | 39 | 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 Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 46 | |
Akron | 086fe5d | 2017-11-13 14:01:45 +0100 | [diff] [blame] | 47 | // 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 Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 54 | |
Akron | 086fe5d | 2017-11-13 14:01:45 +0100 | [diff] [blame] | 55 | // 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 Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 61 | return this; |
| 62 | }, |
| 63 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * Initialize a search with a defined query. |
| 67 | */ |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 68 | 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) { |
Akron | 086fe5d | 2017-11-13 14:01:45 +0100 | [diff] [blame] | 73 | this._cutoff.checked = true; |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 74 | }; |
| 75 | |
Akron | 086fe5d | 2017-11-13 14:01:45 +0100 | [diff] [blame] | 76 | if (KorAP.QLmenu) { |
| 77 | KorAP.QLmenu.selectValue(ql); |
| 78 | }; |
| 79 | /* |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 80 | var qlf = this._ql.options; |
| 81 | for (var i in qlf) { |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 82 | if (qlf[i].value == ql) { |
| 83 | qlf[i].selected = true; |
| 84 | break; |
| 85 | }; |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 86 | }; |
Akron | 086fe5d | 2017-11-13 14:01:45 +0100 | [diff] [blame] | 87 | */ |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 88 | |
| 89 | this._q.value = q; |
| 90 | this.setPage(e); |
| 91 | this.hide(); |
| 92 | }, |
| 93 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 94 | /** |
| 95 | * Decorate a page with query event handler. |
| 96 | */ |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 97 | initQueries : function (d) { |
Akron | f4a7cf4 | 2018-01-09 15:58:45 +0100 | [diff] [blame] | 98 | var qs = d.querySelectorAll('pre.query.tutorial:not(.unsupported)'); |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 99 | var that = this; |
| 100 | for (var i = 0; i < qs.length; i++) { |
Akron | 086fe5d | 2017-11-13 14:01:45 +0100 | [diff] [blame] | 101 | qs[i].onclick = function (e) { |
| 102 | that.useQuery(this,e); |
| 103 | }; |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 104 | }; |
| 105 | }, |
| 106 | |
Nils Diewald | 61e6ff5 | 2015-05-07 17:26:50 +0000 | [diff] [blame] | 107 | /** |
| 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++) { |
Akron | 086fe5d | 2017-11-13 14:01:45 +0100 | [diff] [blame] | 114 | dl[i].onclick = function (e) { |
| 115 | that.setPage(this.getAttribute('href')); |
| 116 | return true; |
| 117 | }; |
Nils Diewald | 61e6ff5 | 2015-05-07 17:26:50 +0000 | [diff] [blame] | 118 | }; |
| 119 | }, |
| 120 | |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 121 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 122 | /** |
| 123 | * Show the tutorial page embedded. |
| 124 | */ |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 125 | show : function () { |
| 126 | var element = this._element; |
| 127 | if (element.style.display === 'block') |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 128 | return; |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 129 | |
| 130 | if (this._iframe === null) { |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 131 | this._iframe = document.createElement('iframe'); |
| 132 | this._iframe.setAttribute( |
| 133 | 'src', |
| 134 | (this.getPage() || this.start) + '?embedded=true' |
| 135 | ); |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 136 | |
Akron | 7bbdb25 | 2018-07-19 16:44:34 +0200 | [diff] [blame] | 137 | var btn = buttonGroupClass.create( |
| 138 | ['action','button-view'] |
| 139 | ); |
| 140 | |
| 141 | var that = this; |
Akron | 792b1a4 | 2020-09-14 18:56:38 +0200 | [diff] [blame] | 142 | btn.add(loc.CLOSE, {'cls':['button-icon','close']}, function () { |
Akron | 7bbdb25 | 2018-07-19 16:44:34 +0200 | [diff] [blame] | 143 | element.style.display = 'none'; |
| 144 | }); |
| 145 | element.appendChild(btn.element()); |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 146 | |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 147 | // 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 | */ |
Akron | 7bbdb25 | 2018-07-19 16:44:34 +0200 | [diff] [blame] | 156 | |
| 157 | /* |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 158 | ul.appendChild(close); |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 159 | |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 160 | element.appendChild(ul); |
Akron | 7bbdb25 | 2018-07-19 16:44:34 +0200 | [diff] [blame] | 161 | */ |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 162 | element.appendChild(this._iframe); |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | element.style.display = 'block'; |
| 166 | }, |
| 167 | |
| 168 | /** |
| 169 | * Close tutorial window. |
| 170 | */ |
| 171 | hide : function () { |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 172 | this._element.style.display = 'none'; |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 173 | }, |
| 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') { |
Akron | 086fe5d | 2017-11-13 14:01:45 +0100 | [diff] [blame] | 182 | var l = this._iframe !== null ? window.frames[0].location : window.location; |
| 183 | page = l.pathname + l.search; |
Akron | 792f58b | 2015-07-08 18:59:36 +0200 | [diff] [blame] | 184 | |
Akron | 086fe5d | 2017-11-13 14:01:45 +0100 | [diff] [blame] | 185 | 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 Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 199 | }; |
| 200 | this._session.set('tutpage', page); |
| 201 | }, |
| 202 | |
| 203 | /** |
| 204 | * Get the current tutorial URL |
| 205 | */ |
| 206 | getPage : function () { |
Akron | 792f58b | 2015-07-08 18:59:36 +0200 | [diff] [blame] | 207 | return this._session.get('tutpage'); |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 208 | } |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 209 | }; |
| 210 | }); |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 211 | |