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', |
Akron | bec4a6a | 2018-07-10 14:45:15 +0200 | [diff] [blame] | 18 | 'buttongroup', |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 19 | 'lib/domReady', |
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 | 6bb7158 | 2016-06-10 20:41:08 +0200 | [diff] [blame] | 23 | 'selectMenu', |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 24 | 'panel', |
| 25 | 'view/koralquery', |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 26 | 'api', |
Nils Diewald | c46003b | 2015-05-07 15:55:35 +0000 | [diff] [blame] | 27 | 'mailToChiffre', |
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, |
Akron | bec4a6a | 2018-07-10 14:45:15 +0200 | [diff] [blame] | 33 | buttonGroupClass, |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 34 | domReady, |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 35 | vcArray, |
| 36 | alertifyClass, |
| 37 | sessionClass, |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 38 | selectMenuClass, |
| 39 | panelClass, |
| 40 | kqClass) { |
Nils Diewald | a0defc4 | 2015-05-07 23:54:17 +0000 | [diff] [blame] | 41 | |
Akron | 9cc3eaf | 2015-06-10 22:15:52 +0200 | [diff] [blame] | 42 | // Localization values |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 43 | const loc = KorAP.Locale; |
Akron | d3903fb | 2017-11-13 11:13:44 +0100 | [diff] [blame] | 44 | loc.VC_allCorpora = loc.VC_allCorpora || 'all corpora'; |
| 45 | loc.VC_oneCollection = loc.VC_oneCollection || 'a virtual corpus'; |
| 46 | loc.TOGGLE_ALIGN = loc.TOGGLE_ALIGN || 'toggle alignment'; |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 47 | loc.SHOW_KQ = loc.SHOW_KQ || 'show KoralQuery'; |
Akron | 9a5b1e1 | 2016-12-06 18:18:23 +0100 | [diff] [blame] | 48 | |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 49 | const d = document; |
Akron | 9cc3eaf | 2015-06-10 22:15:52 +0200 | [diff] [blame] | 50 | |
Akron | f803559 | 2018-05-24 20:40:51 +0200 | [diff] [blame] | 51 | KorAP.session = sessionClass.create('KalamarJS'); |
| 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 | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 69 | // What should be visible in the beginning? |
Akron | f803559 | 2018-05-24 20:40:51 +0200 | [diff] [blame] | 70 | var show = KorAP.session.get('show') || {}; |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 71 | |
| 72 | /** |
Akron | f55504a | 2015-06-18 16:42:55 +0200 | [diff] [blame] | 73 | * Release notifications |
| 74 | */ |
| 75 | if (KorAP.Notifications !== undefined) { |
| 76 | var n = KorAP.Notifications; |
| 77 | for (var i = 0; i < n.length; i++) { |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 78 | alertifyClass.log(n[i][1], n[i][0], 10000); |
Akron | f55504a | 2015-06-18 16:42:55 +0200 | [diff] [blame] | 79 | }; |
| 80 | }; |
| 81 | |
| 82 | /** |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 83 | * Replace Virtual Collection field |
| 84 | */ |
Akron | 5c829e9 | 2017-05-12 18:10:00 +0200 | [diff] [blame] | 85 | var vcname, vcchoose; |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 86 | var input = d.getElementById('collection'); |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 87 | if (input) { |
| 88 | input.style.display = 'none'; |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 89 | vcname = d.createElement('span'); |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 90 | vcname.setAttribute('id', 'vc-choose'); |
Akron | 6bb7158 | 2016-06-10 20:41:08 +0200 | [diff] [blame] | 91 | vcname.classList.add('select'); |
Akron | 941551e | 2015-06-11 16:06:22 +0200 | [diff] [blame] | 92 | |
Akron | 27ae9ec | 2015-06-23 00:43:21 +0200 | [diff] [blame] | 93 | var currentVC = loc.VC_allCorpora; |
| 94 | if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) { |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 95 | currentVC = loc.VC_oneCollection; |
Akron | 27ae9ec | 2015-06-23 00:43:21 +0200 | [diff] [blame] | 96 | }; |
| 97 | |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 98 | vcchoose = vcname.addE('span'); |
Akron | 5c829e9 | 2017-05-12 18:10:00 +0200 | [diff] [blame] | 99 | |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 100 | vcchoose.addT( |
| 101 | d.getElementById('collection-name').value || currentVC |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 102 | ); |
Akron | 27ae9ec | 2015-06-23 00:43:21 +0200 | [diff] [blame] | 103 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 104 | input.parentNode.insertBefore(vcname, input); |
| 105 | }; |
| 106 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 107 | /** |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 108 | * Add actions to match entries |
| 109 | */ |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 110 | var inactiveLi = d.querySelectorAll( |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 111 | '#search > ol > li:not(.active)' |
| 112 | ); |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 113 | var i = 0; |
Akron | 6a535d4 | 2015-08-26 20:16:58 +0200 | [diff] [blame] | 114 | |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 115 | for (i = 0; i < inactiveLi.length; i++) { |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 116 | inactiveLi[i].addEventListener('click', function (e) { |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 117 | if (this._match !== undefined) |
| 118 | this._match.open(); |
| 119 | else { |
| 120 | // lazyLoad |
| 121 | matchClass.create(this).open(); |
| 122 | }; |
Akron | e0c32c7 | 2017-04-25 22:38:23 +0200 | [diff] [blame] | 123 | // This would prevent the sidebar to go back |
| 124 | // e.halt(); |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 125 | }); |
Akron | 6a535d4 | 2015-08-26 20:16:58 +0200 | [diff] [blame] | 126 | inactiveLi[i].addEventListener('keydown', function (e) { |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 127 | var code = _codeFromEvent(e); |
| 128 | |
| 129 | switch (code) { |
| 130 | case 32: |
| 131 | if (this._match !== undefined) |
| 132 | this._match.toggle(); |
| 133 | else { |
| 134 | // lazyLoad |
| 135 | matchClass.create(this).open(); |
| 136 | }; |
| 137 | e.halt(); |
| 138 | break; |
| 139 | }; |
Akron | 6a535d4 | 2015-08-26 20:16:58 +0200 | [diff] [blame] | 140 | }); |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 141 | }; |
| 142 | |
Akron | e0c32c7 | 2017-04-25 22:38:23 +0200 | [diff] [blame] | 143 | // Add focus listener to aside |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 144 | var aside = d.getElementsByTagName('aside')[0]; |
Akron | e0c32c7 | 2017-04-25 22:38:23 +0200 | [diff] [blame] | 145 | |
| 146 | if (aside && aside.classList.contains('active') == false) { |
Akron | 1885ce9 | 2017-04-26 23:10:01 +0200 | [diff] [blame] | 147 | |
Akron | 5258d46 | 2017-04-26 23:32:57 +0200 | [diff] [blame] | 148 | // Horrible lock to deal with sidebar clicks |
| 149 | var asideClicked = false; |
| 150 | |
Akron | 1885ce9 | 2017-04-26 23:10:01 +0200 | [diff] [blame] | 151 | // Make aside active on focus |
| 152 | aside.addEventListener('focus', function(e) { |
Akron | e0c32c7 | 2017-04-25 22:38:23 +0200 | [diff] [blame] | 153 | this.classList.add('active'); |
| 154 | }); |
| 155 | |
Akron | 1885ce9 | 2017-04-26 23:10:01 +0200 | [diff] [blame] | 156 | // Deactivate focus when clicking anywhere else |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 157 | var body = d.getElementsByTagName('body')[0]; |
Akron | 1885ce9 | 2017-04-26 23:10:01 +0200 | [diff] [blame] | 158 | if (body !== null) { |
| 159 | body.addEventListener('click', function() { |
Akron | 5258d46 | 2017-04-26 23:32:57 +0200 | [diff] [blame] | 160 | if (!asideClicked) { |
| 161 | aside.classList.remove('active'); |
| 162 | } |
| 163 | else { |
| 164 | asideClicked = false; |
| 165 | }; |
Akron | 1885ce9 | 2017-04-26 23:10:01 +0200 | [diff] [blame] | 166 | }); |
| 167 | }; |
| 168 | |
| 169 | /* Stop click event on aside |
| 170 | * (to not trickle down to body) |
| 171 | */ |
| 172 | aside.addEventListener('click', function(e) { |
Akron | 5258d46 | 2017-04-26 23:32:57 +0200 | [diff] [blame] | 173 | asideClicked = true; |
Akron | e0c32c7 | 2017-04-25 22:38:23 +0200 | [diff] [blame] | 174 | }); |
| 175 | }; |
Akron | b9cdb10 | 2017-04-25 00:52:31 +0200 | [diff] [blame] | 176 | |
| 177 | |
Akron | 6bb7158 | 2016-06-10 20:41:08 +0200 | [diff] [blame] | 178 | // Replace QL select menus with KorAP menus |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 179 | var qlField = d.getElementById('ql-field'); |
Akron | aba7a5a | 2016-08-15 21:58:33 +0200 | [diff] [blame] | 180 | if (qlField !== null) { |
Akron | 086fe5d | 2017-11-13 14:01:45 +0100 | [diff] [blame] | 181 | KorAP.QLmenu = selectMenuClass.create( |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 182 | d.getElementById('ql-field').parentNode |
Akron | aba7a5a | 2016-08-15 21:58:33 +0200 | [diff] [blame] | 183 | ).limit(5); |
| 184 | }; |
Akron | 6bb7158 | 2016-06-10 20:41:08 +0200 | [diff] [blame] | 185 | |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 186 | var resultInfo = d.getElementById('resultinfo'); |
| 187 | |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 188 | /** |
| 189 | * Add result panel |
| 190 | */ |
| 191 | var resultPanel; |
| 192 | if (resultInfo != null) { |
| 193 | resultPanel = panelClass.create(['result']); |
| 194 | |
| 195 | // Move buttons to resultinfo |
| 196 | resultInfo.appendChild(resultPanel.actions.element()); |
| 197 | |
Akron | e6538cd | 2018-07-16 17:52:33 +0200 | [diff] [blame] | 198 | // The views are at the top of the search results |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 199 | var sb = d.getElementById('search'); |
| 200 | sb.insertBefore(resultPanel.element(), sb.firstChild); |
| 201 | |
| 202 | }; |
| 203 | |
| 204 | |
Akron | 179c8ac | 2015-06-30 19:30:50 +0200 | [diff] [blame] | 205 | // There is a koralQuery |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 206 | if (KorAP.koralQuery !== undefined) { |
| 207 | |
| 208 | if (resultInfo !== null) { |
Akron | 179c8ac | 2015-06-30 19:30:50 +0200 | [diff] [blame] | 209 | |
Akron | e6538cd | 2018-07-16 17:52:33 +0200 | [diff] [blame] | 210 | // Open KoralQuery view |
| 211 | var kqButton = resultPanel.actions.add(loc.SHOW_KQ, ['show-kq','button-icon'], function () { |
Akron | 179c8ac | 2015-06-30 19:30:50 +0200 | [diff] [blame] | 212 | |
Akron | e6538cd | 2018-07-16 17:52:33 +0200 | [diff] [blame] | 213 | // Show only once - otherwise toggle |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 214 | if (this._kq && this._kq.shown()) { |
Akron | e6538cd | 2018-07-16 17:52:33 +0200 | [diff] [blame] | 215 | this._kq.close(); |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 216 | return; |
| 217 | }; |
| 218 | |
| 219 | this._kq = kqClass.create(); |
Akron | e6538cd | 2018-07-16 17:52:33 +0200 | [diff] [blame] | 220 | |
| 221 | // On close, remove session info on KQ |
| 222 | this._kq.onClose = function () { |
| 223 | delete show['kq']; |
| 224 | }; |
| 225 | |
| 226 | show['kq'] = true; |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 227 | this.add(this._kq); |
| 228 | }); |
Akron | e6538cd | 2018-07-16 17:52:33 +0200 | [diff] [blame] | 229 | |
| 230 | // Show KoralQuery in case it's meant to be shown |
| 231 | if (show['kq']) |
| 232 | kqButton.click(); |
Akron | 179c8ac | 2015-06-30 19:30:50 +0200 | [diff] [blame] | 233 | }; |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 234 | |
Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame] | 235 | if (KorAP.koralQuery["errors"]) { |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 236 | var errors = KorAP.koralQuery["errors"]; |
| 237 | for (var i in errors) { |
Akron | f0c31ed | 2016-06-11 11:27:01 +0200 | [diff] [blame] | 238 | |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 239 | // Malformed query |
| 240 | if (errors[i][0] === 302 && errors[i][2]) { |
| 241 | obj.hint = hintClass.create(); |
| 242 | obj.hint.alert(errors[i][2], errors[i][1]); |
| 243 | break; |
| 244 | } |
Akron | f0c31ed | 2016-06-11 11:27:01 +0200 | [diff] [blame] | 245 | |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 246 | // no query |
| 247 | else if (errors[i][0] === 301) { |
| 248 | obj.hint = hintClass.create(); |
| 249 | obj.hint.alert(0, errors[i][1]); |
| 250 | } |
| 251 | } |
Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame] | 252 | }; |
Akron | 179c8ac | 2015-06-30 19:30:50 +0200 | [diff] [blame] | 253 | }; |
| 254 | |
Akron | 9a5b1e1 | 2016-12-06 18:18:23 +0100 | [diff] [blame] | 255 | // There is more than 0 matches and there is a resultButton |
| 256 | if (i > 0) { |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 257 | |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 258 | if (resultPanel !== null) { |
Akron | 9a5b1e1 | 2016-12-06 18:18:23 +0100 | [diff] [blame] | 259 | /** |
| 260 | * Toggle the alignment (left <=> right) |
| 261 | */ |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 262 | resultPanel.actions.add(loc.TOGGLE_ALIGN, ['align','right','button-icon'], function (e) { |
| 263 | var ol = d.querySelector('#search > ol'); |
| 264 | ol.toggleClass("align-left", "align-right"); |
| 265 | this.button.toggleClass("left", "right"); |
| 266 | }); |
Akron | 9a5b1e1 | 2016-12-06 18:18:23 +0100 | [diff] [blame] | 267 | }; |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 268 | }; |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 269 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 270 | /** |
| 271 | * Toggle the Virtual Collection builder |
| 272 | */ |
| 273 | if (vcname) { |
Nils Diewald | 6283d69 | 2015-04-23 20:32:53 +0000 | [diff] [blame] | 274 | var vc; |
Akron | 179c8ac | 2015-06-30 19:30:50 +0200 | [diff] [blame] | 275 | var vcclick = function () { |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 276 | var view = d.getElementById('vc-view'); |
Nils Diewald | 6283d69 | 2015-04-23 20:32:53 +0000 | [diff] [blame] | 277 | |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 278 | // The vc is visible |
| 279 | if (vcname.classList.contains('active')) { |
| 280 | view.removeChild(vc.element()); |
| 281 | vcname.classList.remove('active'); |
| 282 | delete show['collection']; |
| 283 | } |
Nils Diewald | 6283d69 | 2015-04-23 20:32:53 +0000 | [diff] [blame] | 284 | |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 285 | // The vc is not visible |
| 286 | else { |
| 287 | if (vc === undefined) |
| 288 | vc = _getCurrentVC(vcClass, vcArray); |
| 289 | view.appendChild(vc.element()); |
| 290 | vcname.classList.add('active'); |
| 291 | show['collection'] = true; |
| 292 | }; |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 293 | }; |
Akron | 04671e7 | 2017-05-11 20:47:32 +0200 | [diff] [blame] | 294 | |
Akron | 179c8ac | 2015-06-30 19:30:50 +0200 | [diff] [blame] | 295 | vcname.onclick = vcclick; |
Akron | 5c829e9 | 2017-05-12 18:10:00 +0200 | [diff] [blame] | 296 | |
| 297 | // Click, if the VC should be shown |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 298 | if (show['collection']) { |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 299 | vcclick.apply(); |
Akron | 5c829e9 | 2017-05-12 18:10:00 +0200 | [diff] [blame] | 300 | } |
Akron | 04671e7 | 2017-05-11 20:47:32 +0200 | [diff] [blame] | 301 | |
Akron | 5c829e9 | 2017-05-12 18:10:00 +0200 | [diff] [blame] | 302 | // else |
| 303 | else if (_checkVCrewrite(vcClass)) { |
| 304 | vcchoose.classList.add('rewritten'); |
Akron | 04671e7 | 2017-05-11 20:47:32 +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 | */ |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 312 | if (d.getElementById('view-tutorial')) { |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 313 | window.tutorial = tutClass.create( |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 314 | d.getElementById('view-tutorial'), |
Akron | f803559 | 2018-05-24 20:40:51 +0200 | [diff] [blame] | 315 | KorAP.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 | |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 325 | // Initialize queries for d |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 326 | if (obj.tutorial) { |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 327 | obj.tutorial.initQueries(d); |
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 |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 330 | obj.tutorial.initDocLinks(d); |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 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 | */ |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 337 | var form = d.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 | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 340 | var qf = d.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 |
Akron | f803559 | 2018-05-24 20:40:51 +0200 | [diff] [blame] | 351 | KorAP.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 | |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 377 | // Add the hinthelper to the KorAP object to make it manipulatable globally |
Akron | 72f7357 | 2017-12-05 12:31:09 +0100 | [diff] [blame] | 378 | KorAP.Hint = obj.hint; |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 379 | |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 380 | return obj; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 381 | }); |
| 382 | }); |
Akron | c1457bf | 2015-06-11 19:24:00 +0200 | [diff] [blame] | 383 | |
| 384 | // Render Virtual collection |
Akron | 27ae9ec | 2015-06-23 00:43:21 +0200 | [diff] [blame] | 385 | function _getCurrentVC (vcClass, vcArray) { |
| 386 | var vc = vcClass.create(vcArray); |
| 387 | if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) { |
| 388 | vc.fromJson(KorAP.koralQuery["collection"]); |
| 389 | }; |
Akron | c1457bf | 2015-06-11 19:24:00 +0200 | [diff] [blame] | 390 | return vc; |
| 391 | }; |
Akron | 04671e7 | 2017-05-11 20:47:32 +0200 | [diff] [blame] | 392 | |
| 393 | function _checkVCrewrite (vcClass) { |
| 394 | if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) { |
| 395 | return vcClass.checkRewrite(KorAP.koralQuery["collection"]); |
| 396 | }; |
| 397 | return false; |
| 398 | } |