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 | /** |
| 83 | * Decorate a page with query event handler. |
| 84 | */ |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 85 | initQueries : function (d) { |
| 86 | var qs = d.querySelectorAll('pre.query.tutorial'); |
| 87 | var that = this; |
| 88 | for (var i = 0; i < qs.length; i++) { |
| 89 | qs[i].onclick = function (e) { |
| 90 | that.useQuery(this,e); |
| 91 | }; |
| 92 | }; |
| 93 | }, |
| 94 | |
| 95 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame^] | 96 | /** |
| 97 | * Show the tutorial page embedded. |
| 98 | */ |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 99 | show : function () { |
| 100 | var element = this._element; |
| 101 | if (element.style.display === 'block') |
| 102 | return; |
| 103 | |
| 104 | if (this._iframe === null) { |
| 105 | this._iframe = document.createElement('iframe'); |
Nils Diewald | ab4d3ca | 2015-04-17 01:48:43 +0000 | [diff] [blame] | 106 | this._iframe.setAttribute( |
| 107 | 'src', |
| 108 | (this.getPage() || this.start) + '?embedded=true' |
| 109 | ); |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 110 | |
| 111 | var ul = document.createElement('ul'); |
| 112 | ul.classList.add('action', 'right'); |
| 113 | |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 114 | // Add close button |
| 115 | var close = document.createElement('li'); |
| 116 | close.appendChild(document.createElement('span')) |
| 117 | .appendChild(document.createTextNode(loc.CLOSE)); |
| 118 | close.classList.add('close'); |
| 119 | close.setAttribute('title', loc.CLOSE); |
| 120 | close.onclick = function () { |
| 121 | element.style.display = 'none'; |
| 122 | }; |
| 123 | |
| 124 | // Add open in new window button |
| 125 | // Add scroll to top button |
| 126 | /* |
| 127 | var info = document.createElement('li'); |
| 128 | info.appendChild(document.createElement('span')) |
| 129 | .appendChild(document.createTextNode(loc.SHOWINFO)); |
| 130 | info.classList.add('info'); |
| 131 | info.setAttribute('title', loc.SHOWINFO); |
| 132 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 133 | |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 134 | ul.appendChild(close); |
| 135 | |
| 136 | element.appendChild(ul); |
| 137 | element.appendChild(this._iframe); |
| 138 | }; |
| 139 | |
| 140 | element.style.display = 'block'; |
| 141 | }, |
| 142 | |
| 143 | /** |
| 144 | * Close tutorial window. |
| 145 | */ |
| 146 | hide : function () { |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 147 | this._element.style.display = 'none'; |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 148 | }, |
| 149 | |
| 150 | /** |
| 151 | * Set a page to be the current tutorial page. |
| 152 | * Expects either a string or an element. |
| 153 | */ |
| 154 | setPage : function (obj) { |
| 155 | var page = obj; |
| 156 | if (typeof page != 'string') { |
| 157 | page = window.location.pathname + window.location.search; |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 158 | for (var i = 1; i < 5; i++) { |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 159 | if (obj.nodeName === 'SECTION') { |
| 160 | if (obj.hasAttribute('id')) |
| 161 | page += '#' + obj.getAttribute('id'); |
| 162 | break; |
| 163 | } |
| 164 | else if (obj.nodeName === 'PRE' && obj.hasAttribute('id')) { |
| 165 | page += '#' + obj.getAttribute('id'); |
| 166 | break; |
| 167 | } |
| 168 | else { |
| 169 | obj = obj.parentNode; |
| 170 | }; |
| 171 | }; |
| 172 | }; |
| 173 | this._session.set('tutpage', page); |
| 174 | }, |
| 175 | |
| 176 | /** |
| 177 | * Get the current tutorial URL |
| 178 | */ |
| 179 | getPage : function () { |
| 180 | this._session.get('tutpage'); |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 181 | } |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 182 | }; |
| 183 | }); |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 184 | |