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