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