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