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