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