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 | */ |
Nils Diewald | ab4d3ca | 2015-04-17 01:48:43 +0000 | [diff] [blame] | 5 | // TODO: Add query mechanism! |
| 6 | // TODO: Highlight current section: |
| 7 | // http://stackoverflow.com/questions/24887258/highlight-navigation-link-as-i-scroll-down-the-page |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 8 | define(['session', 'util'], function (sessionClass) { |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 9 | "use strict"; |
| 10 | |
| 11 | // Localization values |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 12 | var loc = KorAP.Locale; |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 13 | loc.CLOSE = loc.CLOSE || 'Close'; |
| 14 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 15 | return { |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 16 | /** |
| 17 | * Create new tutorial object. |
| 18 | * Accepts an element to bind the tutorial window to. |
| 19 | */ |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 20 | create : function (obj,session) { |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 21 | if (!obj) |
Akron | 086fe5d | 2017-11-13 14:01:45 +0100 | [diff] [blame] | 22 | return null; |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 23 | return Object.create(this)._init(obj,session); |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 24 | }, |
| 25 | |
| 26 | // Initialize Tutorial object |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 27 | _init : function (obj, session) { |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 28 | |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 29 | if (session === undefined) { |
Akron | 086fe5d | 2017-11-13 14:01:45 +0100 | [diff] [blame] | 30 | this._session = sessionClass.create(); |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 31 | } |
| 32 | else { |
Akron | 086fe5d | 2017-11-13 14:01:45 +0100 | [diff] [blame] | 33 | this._session = session; |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 34 | }; |
| 35 | |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 36 | |
| 37 | if (obj) { |
Akron | 086fe5d | 2017-11-13 14:01:45 +0100 | [diff] [blame] | 38 | this._show = obj; |
| 39 | this.start = obj.getAttribute('href'); |
| 40 | obj.removeAttribute('href'); |
| 41 | var that = this; |
| 42 | obj.onclick = function () { |
| 43 | that.show(); |
| 44 | }; |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 45 | |
Akron | 086fe5d | 2017-11-13 14:01:45 +0100 | [diff] [blame] | 46 | // Injects a tutorial div to the body |
| 47 | var div = document.createElement('div'); |
| 48 | div.setAttribute('id', 'tutorial'); |
| 49 | div.style.display = 'none'; |
| 50 | document.getElementsByTagName('body')[0].appendChild(div); |
| 51 | this._iframe = null; |
| 52 | this._element = div; |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 53 | |
Akron | 086fe5d | 2017-11-13 14:01:45 +0100 | [diff] [blame] | 54 | // Some fields |
| 55 | this._ql = document.getElementById("ql-field"); |
| 56 | this._q = document.getElementById("q-field") |
| 57 | this._cutoff = document.getElementById("q-cutoff-field"); |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 58 | }; |
| 59 | |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 60 | return this; |
| 61 | }, |
| 62 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 63 | |
| 64 | /** |
| 65 | * Initialize a search with a defined query. |
| 66 | */ |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 67 | useQuery : function (e) { |
| 68 | var q = e.getAttribute("data-query"); |
| 69 | var ql = e.getAttribute("data-query-language"); |
| 70 | var qc = e.getAttribute("data-query-cutoff"); |
| 71 | if (qc !== 0 && qc !== "0" && qc !== "off" && qc !== null) { |
Akron | 086fe5d | 2017-11-13 14:01:45 +0100 | [diff] [blame] | 72 | this._cutoff.checked = true; |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 73 | }; |
| 74 | |
Akron | 086fe5d | 2017-11-13 14:01:45 +0100 | [diff] [blame] | 75 | if (KorAP.QLmenu) { |
| 76 | KorAP.QLmenu.selectValue(ql); |
| 77 | }; |
| 78 | /* |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 79 | var qlf = this._ql.options; |
| 80 | for (var i in qlf) { |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 81 | if (qlf[i].value == ql) { |
| 82 | qlf[i].selected = true; |
| 83 | break; |
| 84 | }; |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 85 | }; |
Akron | 086fe5d | 2017-11-13 14:01:45 +0100 | [diff] [blame] | 86 | */ |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 87 | |
| 88 | this._q.value = q; |
| 89 | this.setPage(e); |
| 90 | this.hide(); |
| 91 | }, |
| 92 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 93 | /** |
| 94 | * Decorate a page with query event handler. |
| 95 | */ |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 96 | initQueries : function (d) { |
Akron | f4a7cf4 | 2018-01-09 15:58:45 +0100 | [diff] [blame] | 97 | var qs = d.querySelectorAll('pre.query.tutorial:not(.unsupported)'); |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 98 | var that = this; |
| 99 | for (var i = 0; i < qs.length; i++) { |
Akron | 086fe5d | 2017-11-13 14:01:45 +0100 | [diff] [blame] | 100 | qs[i].onclick = function (e) { |
| 101 | that.useQuery(this,e); |
| 102 | }; |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 103 | }; |
| 104 | }, |
| 105 | |
Nils Diewald | 61e6ff5 | 2015-05-07 17:26:50 +0000 | [diff] [blame] | 106 | /** |
| 107 | * Decorate a page with documentation links |
| 108 | */ |
| 109 | initDocLinks : function (d) { |
| 110 | var dl = d.getElementsByClassName('doc-link'); |
| 111 | var that = this; |
| 112 | for (var i = 0; i < dl.length; i++) { |
Akron | 086fe5d | 2017-11-13 14:01:45 +0100 | [diff] [blame] | 113 | dl[i].onclick = function (e) { |
| 114 | that.setPage(this.getAttribute('href')); |
| 115 | return true; |
| 116 | }; |
Nils Diewald | 61e6ff5 | 2015-05-07 17:26:50 +0000 | [diff] [blame] | 117 | }; |
| 118 | }, |
| 119 | |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 120 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 121 | /** |
| 122 | * Show the tutorial page embedded. |
| 123 | */ |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 124 | show : function () { |
| 125 | var element = this._element; |
| 126 | if (element.style.display === 'block') |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 127 | return; |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 128 | |
| 129 | if (this._iframe === null) { |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 130 | this._iframe = document.createElement('iframe'); |
| 131 | this._iframe.setAttribute( |
| 132 | 'src', |
| 133 | (this.getPage() || this.start) + '?embedded=true' |
| 134 | ); |
Akron | 086fe5d | 2017-11-13 14:01:45 +0100 | [diff] [blame] | 135 | |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 136 | var ul = document.createElement('ul'); |
| 137 | ul.classList.add('action', 'right'); |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 138 | |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 139 | // Add close button |
| 140 | var close = document.createElement('li'); |
| 141 | close.appendChild(document.createElement('span')) |
| 142 | .appendChild(document.createTextNode(loc.CLOSE)); |
| 143 | close.classList.add('close'); |
| 144 | close.setAttribute('title', loc.CLOSE); |
| 145 | close.onclick = function () { |
| 146 | element.style.display = 'none'; |
| 147 | }; |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 148 | |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 149 | // Add open in new window button |
| 150 | // Add scroll to top button |
| 151 | /* |
| 152 | var info = document.createElement('li'); |
| 153 | info.appendChild(document.createElement('span')) |
| 154 | .appendChild(document.createTextNode(loc.SHOWINFO)); |
| 155 | info.classList.add('info'); |
| 156 | info.setAttribute('title', loc.SHOWINFO); |
| 157 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 158 | |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 159 | ul.appendChild(close); |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 160 | |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 161 | element.appendChild(ul); |
| 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 | |