Akron | 7524be1 | 2016-06-01 17:31:33 +0200 | [diff] [blame] | 1 | /* |
| 2 | * TODO: Create lazy loading of objects including |
| 3 | * - obj.hint() |
| 4 | * - obj.alertify() |
| 5 | * - obj.session() |
| 6 | * - obj.tutorial() |
| 7 | * - obj.vc() // toggle |
| 8 | * - obj.matchCreate() (using webpack) |
| 9 | * - obj.koral() (show result, parse for errors ...) |
| 10 | * - obj.alignment() // toggle |
| 11 | */ |
| 12 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 13 | define([ |
| 14 | 'match', |
| 15 | 'hint', |
| 16 | 'vc', |
| 17 | 'tutorial', |
| 18 | 'lib/domReady', |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 19 | 'hint/array', |
Akron | 27ae9ec | 2015-06-23 00:43:21 +0200 | [diff] [blame] | 20 | 'vc/array', |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 21 | 'lib/alertify', |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 22 | 'session', |
Akron | be105b9 | 2016-01-04 14:13:20 +0100 | [diff] [blame] | 23 | 'tagger', |
Akron | 6bb7158 | 2016-06-10 20:41:08 +0200 | [diff] [blame] | 24 | 'selectMenu', |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 25 | 'api', |
Nils Diewald | c46003b | 2015-05-07 15:55:35 +0000 | [diff] [blame] | 26 | 'mailToChiffre', |
Nils Diewald | 845282c | 2015-05-14 07:53:03 +0000 | [diff] [blame] | 27 | 'lib/highlight/highlight.pack', |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 28 | 'util' |
| 29 | ], function (matchClass, |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 30 | hintClass, |
| 31 | vcClass, |
| 32 | tutClass, |
| 33 | domReady, |
| 34 | hintArray, |
| 35 | vcArray, |
| 36 | alertifyClass, |
| 37 | sessionClass, |
| 38 | tagger, |
| 39 | selectMenuClass) { |
Nils Diewald | a0defc4 | 2015-05-07 23:54:17 +0000 | [diff] [blame] | 40 | |
| 41 | // Set hint array for hint helper |
| 42 | KorAP.hintArray = hintArray; |
| 43 | |
Akron | 9cc3eaf | 2015-06-10 22:15:52 +0200 | [diff] [blame] | 44 | // Localization values |
| 45 | var loc = KorAP.Locale; |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 46 | loc.VC_allCorpora = loc.VC_allCorpora || 'all Corpora'; |
| 47 | loc.VC_oneCollection = loc.VC_oneCollection || 'one Collection'; |
| 48 | loc.TOGGLE_ALIGN = loc.TOGGLE_ALIGN || 'toggle Alignment'; |
| 49 | loc.SHOW_KQ = loc.SHOW_KQ || 'show KoralQuery'; |
Akron | 9a5b1e1 | 2016-12-06 18:18:23 +0100 | [diff] [blame] | 50 | loc.SHOW_META = loc.SHOW_META || 'show Metadata'; |
| 51 | |
Akron | 9cc3eaf | 2015-06-10 22:15:52 +0200 | [diff] [blame] | 52 | |
Nils Diewald | a0defc4 | 2015-05-07 23:54:17 +0000 | [diff] [blame] | 53 | // Override KorAP.log |
| 54 | window.alertify = alertifyClass; |
Akron | f55504a | 2015-06-18 16:42:55 +0200 | [diff] [blame] | 55 | KorAP.log = function (code, msg) { |
Nils Diewald | a0defc4 | 2015-05-07 23:54:17 +0000 | [diff] [blame] | 56 | |
| 57 | // Use alertify to log errors |
| 58 | alertifyClass.log( |
Akron | f55504a | 2015-06-18 16:42:55 +0200 | [diff] [blame] | 59 | (code === 0 ? '' : code + ': ') + |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 60 | msg, |
Nils Diewald | a0defc4 | 2015-05-07 23:54:17 +0000 | [diff] [blame] | 61 | 'error', |
Akron | f55504a | 2015-06-18 16:42:55 +0200 | [diff] [blame] | 62 | 10000 |
Nils Diewald | a0defc4 | 2015-05-07 23:54:17 +0000 | [diff] [blame] | 63 | ); |
| 64 | }; |
| 65 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 66 | domReady(function (event) { |
| 67 | var obj = {}; |
Akron | 71b91e4 | 2016-06-01 22:12:43 +0200 | [diff] [blame] | 68 | |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 69 | var session = sessionClass.create('KalamarJS'); |
| 70 | |
| 71 | // What should be visible? |
| 72 | var show = session.get('show') || {}; |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 73 | |
| 74 | /** |
Akron | f55504a | 2015-06-18 16:42:55 +0200 | [diff] [blame] | 75 | * Release notifications |
| 76 | */ |
| 77 | if (KorAP.Notifications !== undefined) { |
| 78 | var n = KorAP.Notifications; |
| 79 | for (var i = 0; i < n.length; i++) { |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 80 | alertifyClass.log(n[i][1], n[i][0], 10000); |
Akron | f55504a | 2015-06-18 16:42:55 +0200 | [diff] [blame] | 81 | }; |
| 82 | }; |
| 83 | |
| 84 | /** |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 85 | * Replace Virtual Collection field |
| 86 | */ |
| 87 | var vcname; |
Akron | c1457bf | 2015-06-11 19:24:00 +0200 | [diff] [blame] | 88 | var input = document.getElementById('collection'); |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 89 | if (input) { |
| 90 | input.style.display = 'none'; |
| 91 | vcname = document.createElement('span'); |
| 92 | vcname.setAttribute('id', 'vc-choose'); |
Akron | 6bb7158 | 2016-06-10 20:41:08 +0200 | [diff] [blame] | 93 | vcname.classList.add('select'); |
Akron | 941551e | 2015-06-11 16:06:22 +0200 | [diff] [blame] | 94 | |
Akron | 27ae9ec | 2015-06-23 00:43:21 +0200 | [diff] [blame] | 95 | var currentVC = loc.VC_allCorpora; |
| 96 | if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) { |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 97 | currentVC = loc.VC_oneCollection; |
Akron | 27ae9ec | 2015-06-23 00:43:21 +0200 | [diff] [blame] | 98 | }; |
| 99 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 100 | vcname.appendChild( |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 101 | document.createTextNode( |
| 102 | document.getElementById('collection-name').value || currentVC |
| 103 | ) |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 104 | ); |
Akron | 27ae9ec | 2015-06-23 00:43:21 +0200 | [diff] [blame] | 105 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 106 | input.parentNode.insertBefore(vcname, input); |
| 107 | }; |
| 108 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 109 | /** |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 110 | * Add actions to match entries |
| 111 | */ |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 112 | var inactiveLi = document.querySelectorAll( |
| 113 | '#search > ol > li:not(.active)' |
| 114 | ); |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 115 | var i = 0; |
Akron | 6a535d4 | 2015-08-26 20:16:58 +0200 | [diff] [blame] | 116 | |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 117 | for (i = 0; i < inactiveLi.length; i++) { |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 118 | inactiveLi[i].addEventListener('click', function (e) { |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 119 | if (this._match !== undefined) |
| 120 | this._match.open(); |
| 121 | else { |
| 122 | // lazyLoad |
| 123 | matchClass.create(this).open(); |
| 124 | }; |
Akron | e0c32c7 | 2017-04-25 22:38:23 +0200 | [diff] [blame] | 125 | // This would prevent the sidebar to go back |
| 126 | // e.halt(); |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 127 | }); |
Akron | 6a535d4 | 2015-08-26 20:16:58 +0200 | [diff] [blame] | 128 | inactiveLi[i].addEventListener('keydown', function (e) { |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 129 | var code = _codeFromEvent(e); |
| 130 | |
| 131 | switch (code) { |
| 132 | case 32: |
| 133 | if (this._match !== undefined) |
| 134 | this._match.toggle(); |
| 135 | else { |
| 136 | // lazyLoad |
| 137 | matchClass.create(this).open(); |
| 138 | }; |
| 139 | e.halt(); |
| 140 | break; |
| 141 | }; |
Akron | 6a535d4 | 2015-08-26 20:16:58 +0200 | [diff] [blame] | 142 | }); |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 143 | }; |
| 144 | |
Akron | e0c32c7 | 2017-04-25 22:38:23 +0200 | [diff] [blame] | 145 | // Add focus listener to aside |
| 146 | var aside = document.getElementsByTagName('aside')[0]; |
| 147 | |
| 148 | if (aside && aside.classList.contains('active') == false) { |
Akron | 1885ce9 | 2017-04-26 23:10:01 +0200 | [diff] [blame] | 149 | |
Akron | 5258d46 | 2017-04-26 23:32:57 +0200 | [diff] [blame] | 150 | // Horrible lock to deal with sidebar clicks |
| 151 | var asideClicked = false; |
| 152 | |
Akron | 1885ce9 | 2017-04-26 23:10:01 +0200 | [diff] [blame] | 153 | // Make aside active on focus |
| 154 | aside.addEventListener('focus', function(e) { |
Akron | e0c32c7 | 2017-04-25 22:38:23 +0200 | [diff] [blame] | 155 | this.classList.add('active'); |
| 156 | }); |
| 157 | |
Akron | 1885ce9 | 2017-04-26 23:10:01 +0200 | [diff] [blame] | 158 | // Deactivate focus when clicking anywhere else |
| 159 | var body = document.getElementsByTagName('body')[0]; |
| 160 | if (body !== null) { |
| 161 | body.addEventListener('click', function() { |
Akron | 5258d46 | 2017-04-26 23:32:57 +0200 | [diff] [blame] | 162 | if (!asideClicked) { |
| 163 | aside.classList.remove('active'); |
| 164 | } |
| 165 | else { |
| 166 | asideClicked = false; |
| 167 | }; |
Akron | 1885ce9 | 2017-04-26 23:10:01 +0200 | [diff] [blame] | 168 | }); |
| 169 | }; |
| 170 | |
| 171 | /* Stop click event on aside |
| 172 | * (to not trickle down to body) |
| 173 | */ |
| 174 | aside.addEventListener('click', function(e) { |
Akron | 5258d46 | 2017-04-26 23:32:57 +0200 | [diff] [blame] | 175 | asideClicked = true; |
Akron | e0c32c7 | 2017-04-25 22:38:23 +0200 | [diff] [blame] | 176 | }); |
| 177 | }; |
Akron | b9cdb10 | 2017-04-25 00:52:31 +0200 | [diff] [blame] | 178 | |
| 179 | |
Akron | 6bb7158 | 2016-06-10 20:41:08 +0200 | [diff] [blame] | 180 | // Replace QL select menus with KorAP menus |
Akron | aba7a5a | 2016-08-15 21:58:33 +0200 | [diff] [blame] | 181 | var qlField = document.getElementById('ql-field'); |
| 182 | if (qlField !== null) { |
| 183 | selectMenuClass.create( |
| 184 | document.getElementById('ql-field').parentNode |
| 185 | ).limit(5); |
| 186 | }; |
Akron | 6bb7158 | 2016-06-10 20:41:08 +0200 | [diff] [blame] | 187 | |
Akron | 179c8ac | 2015-06-30 19:30:50 +0200 | [diff] [blame] | 188 | var result = document.getElementById('resultinfo'); |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 189 | var resultButton = null; |
Akron | 9a5b1e1 | 2016-12-06 18:18:23 +0100 | [diff] [blame] | 190 | var leftButton = null; |
Akron | 179c8ac | 2015-06-30 19:30:50 +0200 | [diff] [blame] | 191 | if (result != null) { |
Akron | 9a5b1e1 | 2016-12-06 18:18:23 +0100 | [diff] [blame] | 192 | |
| 193 | // Add right buttons |
Akron | 179c8ac | 2015-06-30 19:30:50 +0200 | [diff] [blame] | 194 | resultButton = result.appendChild(document.createElement('div')); |
Akron | 9a5b1e1 | 2016-12-06 18:18:23 +0100 | [diff] [blame] | 195 | resultButton.classList.add('result', 'button'); |
| 196 | |
| 197 | leftButton = result.appendChild(document.createElement('div')); |
| 198 | leftButton.classList.add('result', 'button', 'left'); |
Akron | 179c8ac | 2015-06-30 19:30:50 +0200 | [diff] [blame] | 199 | }; |
| 200 | |
| 201 | // There is a koralQuery |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 202 | if (KorAP.koralQuery !== undefined) { |
Akron | 179c8ac | 2015-06-30 19:30:50 +0200 | [diff] [blame] | 203 | |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 204 | if (resultButton !== null) { |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 205 | var kq; |
| 206 | var toggle = document.createElement('a'); |
| 207 | toggle.setAttribute('title', loc.SHOW_KQ) |
| 208 | toggle.classList.add('show-kq', 'button'); |
| 209 | toggle.appendChild(document.createElement('span')) |
| 210 | .appendChild(document.createTextNode(loc.SHOW_KQ)); |
| 211 | resultButton.appendChild(toggle); |
Akron | 179c8ac | 2015-06-30 19:30:50 +0200 | [diff] [blame] | 212 | |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 213 | var showKQ = function () { |
| 214 | if (kq === undefined) { |
| 215 | kq = document.createElement('div'); |
| 216 | kq.setAttribute('id', 'koralquery'); |
| 217 | kq.style.display = 'none'; |
| 218 | var kqInner = document.createElement('div'); |
| 219 | kq.appendChild(kqInner); |
| 220 | kqInner.innerHTML = JSON.stringify(KorAP.koralQuery, null, ' '); |
| 221 | hljs.highlightBlock(kqInner); |
| 222 | var sb = document.getElementById('search'); |
| 223 | sb.insertBefore(kq, sb.firstChild); |
| 224 | }; |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 225 | |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 226 | if (kq.style.display === 'none') { |
| 227 | kq.style.display = 'block'; |
| 228 | show['kq'] = true; |
| 229 | } |
| 230 | else { |
| 231 | kq.style.display = 'none'; |
| 232 | delete show['kq']; |
| 233 | }; |
| 234 | }; |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 235 | |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 236 | if (toggle !== undefined) { |
| 237 | |
| 238 | // Show koralquery |
| 239 | toggle.addEventListener('click', showKQ); |
| 240 | }; |
Akron | 179c8ac | 2015-06-30 19:30:50 +0200 | [diff] [blame] | 241 | }; |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 242 | |
Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame] | 243 | if (KorAP.koralQuery["errors"]) { |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 244 | var errors = KorAP.koralQuery["errors"]; |
| 245 | for (var i in errors) { |
Akron | f0c31ed | 2016-06-11 11:27:01 +0200 | [diff] [blame] | 246 | |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 247 | // Malformed query |
| 248 | if (errors[i][0] === 302 && errors[i][2]) { |
| 249 | obj.hint = hintClass.create(); |
| 250 | obj.hint.alert(errors[i][2], errors[i][1]); |
| 251 | break; |
| 252 | } |
Akron | f0c31ed | 2016-06-11 11:27:01 +0200 | [diff] [blame] | 253 | |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 254 | // no query |
| 255 | else if (errors[i][0] === 301) { |
| 256 | obj.hint = hintClass.create(); |
| 257 | obj.hint.alert(0, errors[i][1]); |
| 258 | } |
| 259 | } |
Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame] | 260 | }; |
| 261 | |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 262 | // Session has KQ visibility stored |
| 263 | if (show["kq"]) |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 264 | showKQ.apply(); |
Akron | 179c8ac | 2015-06-30 19:30:50 +0200 | [diff] [blame] | 265 | }; |
| 266 | |
Akron | 9a5b1e1 | 2016-12-06 18:18:23 +0100 | [diff] [blame] | 267 | // There is more than 0 matches and there is a resultButton |
| 268 | if (i > 0) { |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 269 | |
Akron | 9a5b1e1 | 2016-12-06 18:18:23 +0100 | [diff] [blame] | 270 | if (resultButton !== null) { |
| 271 | |
| 272 | /** |
| 273 | * Toggle the alignment (left <=> right) |
| 274 | */ |
| 275 | // querySelector('div.button.right'); |
| 276 | |
| 277 | var toggle = document.createElement('a'); |
| 278 | toggle.setAttribute('title', loc.TOGGLE_ALIGN); |
| 279 | // Todo: Reuse old alignment from query |
| 280 | var cl = toggle.classList; |
| 281 | cl.add('align', 'right', 'button'); |
| 282 | toggle.addEventListener( |
| 283 | 'click', |
| 284 | function (e) { |
| 285 | var ol = document.querySelector('#search > ol'); |
| 286 | ol.toggleClass("align-left", "align-right"); |
| 287 | this.toggleClass("left", "right"); |
| 288 | }); |
| 289 | toggle.appendChild(document.createElement('span')) |
| 290 | .appendChild(document.createTextNode(loc.TOGGLE_ALIGN)); |
| 291 | resultButton.appendChild(toggle); |
| 292 | }; |
| 293 | |
| 294 | /* |
| 295 | // Not ready yet |
| 296 | if (leftButton !== null) { |
| 297 | var metaInfo = document.createElement('a'); |
| 298 | metaInfo.setAttribute('title', loc.SHOW_META) |
| 299 | |
| 300 | var cl = metaInfo.classList; |
| 301 | cl.add('show-meta', 'button'); |
| 302 | metaInfo.appendChild(document.createElement('span')) |
| 303 | .appendChild(document.createTextNode(loc.SHOW_META)); |
| 304 | leftButton.appendChild(metaInfo); |
| 305 | }; |
| 306 | */ |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 307 | }; |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 308 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 309 | /** |
| 310 | * Toggle the Virtual Collection builder |
| 311 | */ |
| 312 | if (vcname) { |
Nils Diewald | 6283d69 | 2015-04-23 20:32:53 +0000 | [diff] [blame] | 313 | var vc; |
Akron | 179c8ac | 2015-06-30 19:30:50 +0200 | [diff] [blame] | 314 | var vcclick = function () { |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 315 | var view = document.getElementById('vc-view'); |
Nils Diewald | 6283d69 | 2015-04-23 20:32:53 +0000 | [diff] [blame] | 316 | |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 317 | // The vc is visible |
| 318 | if (vcname.classList.contains('active')) { |
| 319 | view.removeChild(vc.element()); |
| 320 | vcname.classList.remove('active'); |
| 321 | delete show['collection']; |
| 322 | } |
Nils Diewald | 6283d69 | 2015-04-23 20:32:53 +0000 | [diff] [blame] | 323 | |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 324 | // The vc is not visible |
| 325 | else { |
| 326 | if (vc === undefined) |
| 327 | vc = _getCurrentVC(vcClass, vcArray); |
| 328 | view.appendChild(vc.element()); |
| 329 | vcname.classList.add('active'); |
| 330 | show['collection'] = true; |
| 331 | }; |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 332 | }; |
Akron | 179c8ac | 2015-06-30 19:30:50 +0200 | [diff] [blame] | 333 | vcname.onclick = vcclick; |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 334 | if (show['collection']) { |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 335 | vcclick.apply(); |
Akron | 179c8ac | 2015-06-30 19:30:50 +0200 | [diff] [blame] | 336 | }; |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 337 | }; |
| 338 | |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 339 | |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 340 | /** |
| 341 | * Init Tutorial view |
| 342 | */ |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 343 | if (document.getElementById('view-tutorial')) { |
| 344 | window.tutorial = tutClass.create( |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 345 | document.getElementById('view-tutorial'), |
| 346 | session |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 347 | ); |
| 348 | obj.tutorial = window.tutorial; |
| 349 | } |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 350 | |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 351 | // Tutorial is in parent |
| 352 | else if (window.parent) { |
| 353 | obj.tutorial = window.parent.tutorial; |
| 354 | }; |
| 355 | |
| 356 | // Initialize queries for document |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 357 | if (obj.tutorial) { |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 358 | obj.tutorial.initQueries(document); |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 359 | |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 360 | // Initialize documentation links |
| 361 | obj.tutorial.initDocLinks(document); |
| 362 | }; |
Nils Diewald | 61e6ff5 | 2015-05-07 17:26:50 +0000 | [diff] [blame] | 363 | |
Nils Diewald | 845282c | 2015-05-14 07:53:03 +0000 | [diff] [blame] | 364 | |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 365 | /** |
Akron | c1457bf | 2015-06-11 19:24:00 +0200 | [diff] [blame] | 366 | * Add VC creation on submission. |
| 367 | */ |
| 368 | var form = document.getElementById('searchform'); |
Akron | 792f58b | 2015-07-08 18:59:36 +0200 | [diff] [blame] | 369 | if (form !== null) { |
Akron | c1457bf | 2015-06-11 19:24:00 +0200 | [diff] [blame] | 370 | form.addEventListener('submit', function (e) { |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 371 | var qf = document.getElementById('q-field'); |
Akron | c1457bf | 2015-06-11 19:24:00 +0200 | [diff] [blame] | 372 | |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 373 | // No query was defined |
| 374 | if (qf.value === undefined || qf.value === '') { |
| 375 | qf.focus(); |
| 376 | e.halt(); |
| 377 | KorAP.log(700, "No query given"); |
| 378 | return; |
| 379 | }; |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 380 | |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 381 | // Store session information |
| 382 | session.set("show", show); |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 383 | |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 384 | // Set Virtual collection |
| 385 | if (vc === undefined) { |
| 386 | vc = _getCurrentVC(vcClass, vcArray); |
| 387 | }; |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 388 | |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 389 | if (vc !== undefined) { |
| 390 | input.value = vc.toQuery(); |
| 391 | } |
| 392 | else { |
| 393 | delete input['value']; |
| 394 | }; |
Akron | c1457bf | 2015-06-11 19:24:00 +0200 | [diff] [blame] | 395 | }); |
| 396 | }; |
| 397 | |
| 398 | /** |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 399 | * Init hint helper |
| 400 | * has to be final because of |
| 401 | * reposition |
| 402 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 403 | // Todo: Pass an element, so this works with |
| 404 | // tutorial pages as well! |
Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame] | 405 | if (obj.hint === undefined) |
| 406 | obj.hint = hintClass.create(); |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 407 | |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 408 | return obj; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 409 | }); |
| 410 | }); |
Akron | c1457bf | 2015-06-11 19:24:00 +0200 | [diff] [blame] | 411 | |
| 412 | // Render Virtual collection |
Akron | 27ae9ec | 2015-06-23 00:43:21 +0200 | [diff] [blame] | 413 | function _getCurrentVC (vcClass, vcArray) { |
| 414 | var vc = vcClass.create(vcArray); |
| 415 | if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) { |
| 416 | vc.fromJson(KorAP.koralQuery["collection"]); |
| 417 | }; |
Akron | c1457bf | 2015-06-11 19:24:00 +0200 | [diff] [blame] | 418 | return vc; |
| 419 | }; |