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 | */ |
| 20 | create : function (obj) { |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 21 | if (!obj) |
| 22 | return null; |
| 23 | return Object.create(this)._init(obj); |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 24 | }, |
| 25 | |
| 26 | // Initialize Tutorial object |
| 27 | _init : function (obj) { |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 28 | |
| 29 | this._session = sessionClass.create(); |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 30 | |
| 31 | if (obj) { |
| 32 | this._show = obj; |
| 33 | this.start = obj.getAttribute('href'); |
| 34 | obj.removeAttribute('href'); |
| 35 | var that = this; |
| 36 | obj.onclick = function () { |
| 37 | that.show(); |
| 38 | }; |
| 39 | |
| 40 | // Injects a tutorial div to the body |
| 41 | var div = document.createElement('div'); |
| 42 | div.setAttribute('id', 'tutorial'); |
| 43 | div.style.display = 'none'; |
| 44 | document.getElementsByTagName('body')[0].appendChild(div); |
| 45 | this._iframe = null; |
| 46 | this._element = div; |
| 47 | |
| 48 | // Some fields |
| 49 | this._ql = document.getElementById("ql-field"); |
| 50 | this._q = document.getElementById("q-field") |
| 51 | this._cutoff = document.getElementById("q-cutoff-field"); |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 52 | }; |
| 53 | |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 54 | return this; |
| 55 | }, |
| 56 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 57 | |
| 58 | /** |
| 59 | * Initialize a search with a defined query. |
| 60 | */ |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 61 | useQuery : function (e) { |
| 62 | var q = e.getAttribute("data-query"); |
| 63 | var ql = e.getAttribute("data-query-language"); |
| 64 | var qc = e.getAttribute("data-query-cutoff"); |
| 65 | if (qc !== 0 && qc !== "0" && qc !== "off" && qc !== null) { |
| 66 | this._cutoff.checked = true; |
| 67 | }; |
| 68 | |
| 69 | var qlf = this._ql.options; |
| 70 | for (var i in qlf) { |
| 71 | if (qlf[i].value == ql) { |
| 72 | qlf[i].selected = true; |
| 73 | }; |
| 74 | }; |
| 75 | |
| 76 | this._q.value = q; |
| 77 | this.setPage(e); |
| 78 | this.hide(); |
| 79 | }, |
| 80 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 81 | /** |
| 82 | * Decorate a page with query event handler. |
| 83 | */ |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 84 | initQueries : function (d) { |
| 85 | var qs = d.querySelectorAll('pre.query.tutorial'); |
| 86 | var that = this; |
| 87 | for (var i = 0; i < qs.length; i++) { |
| 88 | qs[i].onclick = function (e) { |
| 89 | that.useQuery(this,e); |
| 90 | }; |
| 91 | }; |
| 92 | }, |
| 93 | |
Nils Diewald | 61e6ff5 | 2015-05-07 17:26:50 +0000 | [diff] [blame^] | 94 | /** |
| 95 | * Decorate a page with documentation links |
| 96 | */ |
| 97 | initDocLinks : function (d) { |
| 98 | var dl = d.getElementsByClassName('doc-link'); |
| 99 | var that = this; |
| 100 | for (var i = 0; i < dl.length; i++) { |
| 101 | dl[i].onclick = function (e) { |
| 102 | that.setPage(this.getAttribute('href')); |
| 103 | return true; |
| 104 | }; |
| 105 | }; |
| 106 | }, |
| 107 | |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 108 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 109 | /** |
| 110 | * Show the tutorial page embedded. |
| 111 | */ |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 112 | show : function () { |
| 113 | var element = this._element; |
| 114 | if (element.style.display === 'block') |
| 115 | return; |
| 116 | |
| 117 | if (this._iframe === null) { |
| 118 | this._iframe = document.createElement('iframe'); |
Nils Diewald | ab4d3ca | 2015-04-17 01:48:43 +0000 | [diff] [blame] | 119 | this._iframe.setAttribute( |
| 120 | 'src', |
| 121 | (this.getPage() || this.start) + '?embedded=true' |
| 122 | ); |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 123 | |
| 124 | var ul = document.createElement('ul'); |
| 125 | ul.classList.add('action', 'right'); |
| 126 | |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 127 | // Add close button |
| 128 | var close = document.createElement('li'); |
| 129 | close.appendChild(document.createElement('span')) |
| 130 | .appendChild(document.createTextNode(loc.CLOSE)); |
| 131 | close.classList.add('close'); |
| 132 | close.setAttribute('title', loc.CLOSE); |
| 133 | close.onclick = function () { |
| 134 | element.style.display = 'none'; |
| 135 | }; |
| 136 | |
| 137 | // Add open in new window button |
| 138 | // Add scroll to top button |
| 139 | /* |
| 140 | var info = document.createElement('li'); |
| 141 | info.appendChild(document.createElement('span')) |
| 142 | .appendChild(document.createTextNode(loc.SHOWINFO)); |
| 143 | info.classList.add('info'); |
| 144 | info.setAttribute('title', loc.SHOWINFO); |
| 145 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 146 | |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 147 | ul.appendChild(close); |
| 148 | |
| 149 | element.appendChild(ul); |
| 150 | element.appendChild(this._iframe); |
| 151 | }; |
| 152 | |
| 153 | element.style.display = 'block'; |
| 154 | }, |
| 155 | |
| 156 | /** |
| 157 | * Close tutorial window. |
| 158 | */ |
| 159 | hide : function () { |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 160 | this._element.style.display = 'none'; |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 161 | }, |
| 162 | |
| 163 | /** |
| 164 | * Set a page to be the current tutorial page. |
| 165 | * Expects either a string or an element. |
| 166 | */ |
| 167 | setPage : function (obj) { |
| 168 | var page = obj; |
| 169 | if (typeof page != 'string') { |
| 170 | page = window.location.pathname + window.location.search; |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 171 | for (var i = 1; i < 5; i++) { |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 172 | if (obj.nodeName === 'SECTION') { |
| 173 | if (obj.hasAttribute('id')) |
| 174 | page += '#' + obj.getAttribute('id'); |
| 175 | break; |
| 176 | } |
| 177 | else if (obj.nodeName === 'PRE' && obj.hasAttribute('id')) { |
| 178 | page += '#' + obj.getAttribute('id'); |
| 179 | break; |
| 180 | } |
| 181 | else { |
| 182 | obj = obj.parentNode; |
| 183 | }; |
| 184 | }; |
| 185 | }; |
| 186 | this._session.set('tutpage', page); |
| 187 | }, |
| 188 | |
| 189 | /** |
| 190 | * Get the current tutorial URL |
| 191 | */ |
| 192 | getPage : function () { |
| 193 | this._session.get('tutpage'); |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 194 | } |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 195 | }; |
| 196 | }); |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 197 | |