Akron | 7524be1 | 2016-06-01 17:31:33 +0200 | [diff] [blame] | 1 | /* |
Akron | 5cb9b2b | 2018-07-24 17:01:09 +0200 | [diff] [blame] | 2 | * Initialize The JS frontend part and decorate |
| 3 | * the static HTML data. |
| 4 | * |
| 5 | * @author Nils Diewald |
| 6 | * |
Akron | 7524be1 | 2016-06-01 17:31:33 +0200 | [diff] [blame] | 7 | * TODO: Create lazy loading of objects including |
| 8 | * - obj.hint() |
| 9 | * - obj.alertify() |
| 10 | * - obj.session() |
| 11 | * - obj.tutorial() |
| 12 | * - obj.vc() // toggle |
| 13 | * - obj.matchCreate() (using webpack) |
| 14 | * - obj.koral() (show result, parse for errors ...) |
| 15 | * - obj.alignment() // toggle |
| 16 | */ |
| 17 | |
Akron | e51eaa3 | 2020-11-10 09:35:53 +0100 | [diff] [blame] | 18 | "use strict"; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 19 | define([ |
| 20 | 'match', |
| 21 | 'hint', |
| 22 | 'vc', |
| 23 | 'tutorial', |
| 24 | 'lib/domReady', |
Akron | 27ae9ec | 2015-06-23 00:43:21 +0200 | [diff] [blame] | 25 | 'vc/array', |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 26 | 'lib/alertify', |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 27 | 'session', |
Akron | da32e7a | 2021-11-16 17:28:57 +0100 | [diff] [blame] | 28 | 'state/manager', |
Akron | 6bb7158 | 2016-06-10 20:41:08 +0200 | [diff] [blame] | 29 | 'selectMenu', |
Akron | 5cb9b2b | 2018-07-24 17:01:09 +0200 | [diff] [blame] | 30 | 'panel/result', |
Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 31 | 'panel/query', |
Akron | 644ad9f | 2021-07-26 16:12:59 +0200 | [diff] [blame] | 32 | 'panel/pagination', |
hebasta | 75cfca5 | 2019-02-19 13:15:27 +0100 | [diff] [blame] | 33 | 'tour/tours', |
Akron | 24f48ea | 2020-07-01 09:37:19 +0200 | [diff] [blame] | 34 | 'plugin/server', |
| 35 | 'pipe', |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 36 | 'api', |
Nils Diewald | c46003b | 2015-05-07 15:55:35 +0000 | [diff] [blame] | 37 | 'mailToChiffre', |
Akron | 858cbc8 | 2019-12-05 16:53:13 +0100 | [diff] [blame] | 38 | 'util', |
| 39 | 'state' |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 40 | ], function (matchClass, |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 41 | hintClass, |
| 42 | vcClass, |
| 43 | tutClass, |
| 44 | domReady, |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 45 | vcArray, |
| 46 | alertifyClass, |
| 47 | sessionClass, |
Akron | da32e7a | 2021-11-16 17:28:57 +0100 | [diff] [blame] | 48 | stateManagerClass, |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 49 | selectMenuClass, |
hebasta | 75cfca5 | 2019-02-19 13:15:27 +0100 | [diff] [blame] | 50 | resultPanelClass, |
Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 51 | queryPanelClass, |
Akron | 644ad9f | 2021-07-26 16:12:59 +0200 | [diff] [blame] | 52 | paginationPanelClass, |
Akron | 24f48ea | 2020-07-01 09:37:19 +0200 | [diff] [blame] | 53 | tourClass, |
| 54 | pluginClass, |
| 55 | pipeClass) { |
Nils Diewald | a0defc4 | 2015-05-07 23:54:17 +0000 | [diff] [blame] | 56 | |
Nils Diewald | a0defc4 | 2015-05-07 23:54:17 +0000 | [diff] [blame] | 57 | // Override KorAP.log |
| 58 | window.alertify = alertifyClass; |
Akron | e71bd6d | 2024-06-11 15:47:39 +0200 | [diff] [blame] | 59 | KorAP.log = function (code, msg, src, type) { |
Akron | c0a2da8 | 2018-07-04 15:27:37 +0200 | [diff] [blame] | 60 | |
| 61 | if (src) { |
| 62 | msg += '<code class="src">'+src+'</code>'; |
| 63 | }; |
Nils Diewald | a0defc4 | 2015-05-07 23:54:17 +0000 | [diff] [blame] | 64 | |
| 65 | // Use alertify to log errors |
| 66 | alertifyClass.log( |
Akron | f55504a | 2015-06-18 16:42:55 +0200 | [diff] [blame] | 67 | (code === 0 ? '' : code + ': ') + |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 68 | msg, |
Akron | e71bd6d | 2024-06-11 15:47:39 +0200 | [diff] [blame] | 69 | (type ? type : 'error'), |
Akron | f55504a | 2015-06-18 16:42:55 +0200 | [diff] [blame] | 70 | 10000 |
Nils Diewald | a0defc4 | 2015-05-07 23:54:17 +0000 | [diff] [blame] | 71 | ); |
| 72 | }; |
| 73 | |
hebasta | 2758b58 | 2018-11-19 15:59:42 +0100 | [diff] [blame] | 74 | KorAP.vc = vcClass.create(vcArray); |
Akron | 690066c | 2021-01-22 17:39:18 +0100 | [diff] [blame] | 75 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 76 | domReady(function (event) { |
Akron | 006ddc6 | 2024-02-19 08:49:43 +0100 | [diff] [blame] | 77 | |
| 78 | const d = document; |
| 79 | |
| 80 | // Set base URL |
| 81 | KorAP.URL = d.body.getAttribute('data-korap-url') || ""; |
| 82 | |
| 83 | // Create suffix if KorAP is run in a subfolder |
| 84 | KorAP.session = sessionClass.create( |
| 85 | KorAP.URL.length > 0 ? 'kalamarJS-' + KorAP.URL.slugify() : 'kalamarJS' |
| 86 | ); |
| 87 | |
| 88 | // Get koralQuery response |
| 89 | const kqe = d.getElementById('koralQuery'); |
| 90 | if (kqe !== null) { |
| 91 | KorAP.koralQuery = JSON.parse(kqe.getAttribute('data-koralquery') || ""); |
| 92 | }; |
| 93 | |
Helge | 0d3630c | 2024-10-16 17:19:40 +0200 | [diff] [blame^] | 94 | let gt = document.getElementsByClassName('link-guided-tour'); |
| 95 | if (gt.length != null){ |
| 96 | for(let j = 0; j < gt.length; j++){ |
| 97 | gt[j].setAttribute('href', '#'); |
| 98 | gt[j].addEventListener('click', function(){ |
Akron | 006ddc6 | 2024-02-19 08:49:43 +0100 | [diff] [blame] | 99 | tourClass.gTstartSearch().start(); |
| 100 | }); |
Helge | 0d3630c | 2024-10-16 17:19:40 +0200 | [diff] [blame^] | 101 | } |
| 102 | |
| 103 | KorAP.tourshowR = function(){ |
| 104 | tourClass.gTshowResults().start(); |
Akron | 006ddc6 | 2024-02-19 08:49:43 +0100 | [diff] [blame] | 105 | }; |
| 106 | }; |
| 107 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 108 | var obj = {}; |
Akron | 71b91e4 | 2016-06-01 22:12:43 +0200 | [diff] [blame] | 109 | |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 110 | // What should be visible in the beginning? |
Akron | f803559 | 2018-05-24 20:40:51 +0200 | [diff] [blame] | 111 | var show = KorAP.session.get('show') || {}; |
hebasta | 043e96f | 2019-11-28 12:33:00 +0100 | [diff] [blame] | 112 | |
| 113 | KorAP.Panel = KorAP.Panel || {} |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 114 | |
| 115 | /** |
Akron | f55504a | 2015-06-18 16:42:55 +0200 | [diff] [blame] | 116 | * Release notifications |
| 117 | */ |
Akron | cb5c171 | 2021-01-26 18:01:04 +0100 | [diff] [blame] | 118 | d.querySelectorAll('#notifications div.notify').forEach( |
| 119 | function(e) { |
| 120 | let msg = e.textContent; |
| 121 | |
| 122 | let src = e.getAttribute('data-src'); |
| 123 | if (src) { |
| 124 | msg += '<code class="src">'+src+'</code>'; |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 125 | }; |
Akron | cb5c171 | 2021-01-26 18:01:04 +0100 | [diff] [blame] | 126 | |
| 127 | let type = e.getAttribute('data-type') || "error"; |
| 128 | alertifyClass.log(msg, type, 10000); |
| 129 | } |
| 130 | ); |
Akron | f55504a | 2015-06-18 16:42:55 +0200 | [diff] [blame] | 131 | |
| 132 | /** |
Akron | cd42a14 | 2019-07-12 18:55:37 +0200 | [diff] [blame] | 133 | * Replace Virtual Corpus field |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 134 | */ |
Akron | 5c829e9 | 2017-05-12 18:10:00 +0200 | [diff] [blame] | 135 | var vcname, vcchoose; |
Akron | cd42a14 | 2019-07-12 18:55:37 +0200 | [diff] [blame] | 136 | var input = d.getElementById('cq'); |
Akron | 1f0521b | 2018-08-28 13:01:24 +0200 | [diff] [blame] | 137 | |
hebasta | 2758b58 | 2018-11-19 15:59:42 +0100 | [diff] [blame] | 138 | var vc = KorAP.vc; |
hebasta | 48842cf | 2018-12-11 12:57:38 +0100 | [diff] [blame] | 139 | |
Akron | 1f0521b | 2018-08-28 13:01:24 +0200 | [diff] [blame] | 140 | // Add vc name object |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 141 | if (input) { |
| 142 | input.style.display = 'none'; |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 143 | vcname = d.createElement('span'); |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 144 | vcname.setAttribute('id', 'vc-choose'); |
Akron | 6bb7158 | 2016-06-10 20:41:08 +0200 | [diff] [blame] | 145 | vcname.classList.add('select'); |
Akron | 941551e | 2015-06-11 16:06:22 +0200 | [diff] [blame] | 146 | |
Akron | 1f0521b | 2018-08-28 13:01:24 +0200 | [diff] [blame] | 147 | // Load virtual corpus object |
Akron | cd42a14 | 2019-07-12 18:55:37 +0200 | [diff] [blame] | 148 | // Supports "collection" for legacy reasons |
| 149 | if (KorAP.koralQuery !== undefined && (KorAP.koralQuery["collection"] || KorAP.koralQuery["corpus"])) { |
Akron | 1f0521b | 2018-08-28 13:01:24 +0200 | [diff] [blame] | 150 | try { |
Akron | cd42a14 | 2019-07-12 18:55:37 +0200 | [diff] [blame] | 151 | vc.fromJson(KorAP.koralQuery["collection"] || KorAP.koralQuery["corpus"]); |
Akron | 1f0521b | 2018-08-28 13:01:24 +0200 | [diff] [blame] | 152 | } |
| 153 | catch (e) { |
| 154 | KorAP.log(0,e); |
| 155 | } |
Akron | 27ae9ec | 2015-06-23 00:43:21 +0200 | [diff] [blame] | 156 | }; |
| 157 | |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 158 | vcchoose = vcname.addE('span'); |
Akron | ec6bb8e | 2018-08-29 13:07:56 +0200 | [diff] [blame] | 159 | vcchoose.addT(vc.getName()); |
Akron | 27ae9ec | 2015-06-23 00:43:21 +0200 | [diff] [blame] | 160 | |
Akron | 1f0521b | 2018-08-28 13:01:24 +0200 | [diff] [blame] | 161 | if (vc.wasRewritten()) { |
| 162 | vcchoose.classList.add('rewritten'); |
| 163 | }; |
| 164 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 165 | input.parentNode.insertBefore(vcname, input); |
| 166 | }; |
| 167 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 168 | /** |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 169 | * Add actions to match entries |
| 170 | */ |
Akron | b50964a | 2020-10-12 11:44:37 +0200 | [diff] [blame] | 171 | var matchElements = d.querySelectorAll( |
Akron | 3c390c4 | 2020-03-30 09:06:21 +0200 | [diff] [blame] | 172 | '#search > ol > li' |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 173 | ); |
Akron | 6a535d4 | 2015-08-26 20:16:58 +0200 | [diff] [blame] | 174 | |
Akron | b50964a | 2020-10-12 11:44:37 +0200 | [diff] [blame] | 175 | matchElements.forEach(function(e) { |
Akron | 3c390c4 | 2020-03-30 09:06:21 +0200 | [diff] [blame] | 176 | |
| 177 | // Define class for active elements |
| 178 | if (e.classList.contains('active')) { |
Akron | d769d70 | 2021-08-16 11:09:08 +0200 | [diff] [blame] | 179 | if (e._match === undefined) { |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 180 | // lazyLoad |
Akron | 3c390c4 | 2020-03-30 09:06:21 +0200 | [diff] [blame] | 181 | matchClass.create(e).init(); |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 182 | }; |
Akron | 3c390c4 | 2020-03-30 09:06:21 +0200 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | // Define class for inactive elements |
| 186 | else { |
| 187 | e.addEventListener('click', function (e) { |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 188 | if (this._match !== undefined) |
Akron | 3c390c4 | 2020-03-30 09:06:21 +0200 | [diff] [blame] | 189 | this._match.open(); |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 190 | else { |
| 191 | // lazyLoad |
| 192 | matchClass.create(this).open(); |
| 193 | }; |
Akron | 3c390c4 | 2020-03-30 09:06:21 +0200 | [diff] [blame] | 194 | // This would prevent the sidebar to go back |
| 195 | // e.halt(); |
| 196 | }); |
| 197 | e.addEventListener('keydown', function (e) { |
| 198 | var code = _codeFromEvent(e); |
| 199 | |
| 200 | switch (code) { |
| 201 | case 32: |
| 202 | if (this._match !== undefined) |
| 203 | this._match.toggle(); |
| 204 | else { |
| 205 | // lazyLoad |
| 206 | matchClass.create(this).open(); |
| 207 | }; |
| 208 | e.halt(); |
| 209 | break; |
| 210 | }; |
| 211 | }); |
| 212 | }; |
Akron | d769d70 | 2021-08-16 11:09:08 +0200 | [diff] [blame] | 213 | }); |
Akron | 3c390c4 | 2020-03-30 09:06:21 +0200 | [diff] [blame] | 214 | |
Akron | e0c32c7 | 2017-04-25 22:38:23 +0200 | [diff] [blame] | 215 | // Add focus listener to aside |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 216 | var aside = d.getElementsByTagName('aside')[0]; |
Akron | e0c32c7 | 2017-04-25 22:38:23 +0200 | [diff] [blame] | 217 | |
| 218 | if (aside && aside.classList.contains('active') == false) { |
Akron | 1885ce9 | 2017-04-26 23:10:01 +0200 | [diff] [blame] | 219 | |
Akron | 5258d46 | 2017-04-26 23:32:57 +0200 | [diff] [blame] | 220 | // Horrible lock to deal with sidebar clicks |
| 221 | var asideClicked = false; |
| 222 | |
Akron | 1885ce9 | 2017-04-26 23:10:01 +0200 | [diff] [blame] | 223 | // Make aside active on focus |
| 224 | aside.addEventListener('focus', function(e) { |
Akron | e0c32c7 | 2017-04-25 22:38:23 +0200 | [diff] [blame] | 225 | this.classList.add('active'); |
| 226 | }); |
| 227 | |
Akron | 1885ce9 | 2017-04-26 23:10:01 +0200 | [diff] [blame] | 228 | // Deactivate focus when clicking anywhere else |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 229 | var body = d.getElementsByTagName('body')[0]; |
Akron | 1885ce9 | 2017-04-26 23:10:01 +0200 | [diff] [blame] | 230 | if (body !== null) { |
| 231 | body.addEventListener('click', function() { |
Akron | 5258d46 | 2017-04-26 23:32:57 +0200 | [diff] [blame] | 232 | if (!asideClicked) { |
| 233 | aside.classList.remove('active'); |
| 234 | } |
| 235 | else { |
| 236 | asideClicked = false; |
| 237 | }; |
Akron | 1885ce9 | 2017-04-26 23:10:01 +0200 | [diff] [blame] | 238 | }); |
| 239 | }; |
| 240 | |
| 241 | /* Stop click event on aside |
| 242 | * (to not trickle down to body) |
| 243 | */ |
| 244 | aside.addEventListener('click', function(e) { |
Akron | 5258d46 | 2017-04-26 23:32:57 +0200 | [diff] [blame] | 245 | asideClicked = true; |
Akron | e0c32c7 | 2017-04-25 22:38:23 +0200 | [diff] [blame] | 246 | }); |
| 247 | }; |
Akron | b9cdb10 | 2017-04-25 00:52:31 +0200 | [diff] [blame] | 248 | |
| 249 | |
Akron | 6bb7158 | 2016-06-10 20:41:08 +0200 | [diff] [blame] | 250 | // Replace QL select menus with KorAP menus |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 251 | var qlField = d.getElementById('ql-field'); |
Akron | aba7a5a | 2016-08-15 21:58:33 +0200 | [diff] [blame] | 252 | if (qlField !== null) { |
Akron | 086fe5d | 2017-11-13 14:01:45 +0100 | [diff] [blame] | 253 | KorAP.QLmenu = selectMenuClass.create( |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 254 | d.getElementById('ql-field').parentNode |
Marc Kupietz | 9545582 | 2023-09-19 20:14:31 +0200 | [diff] [blame] | 255 | ).limit(10); |
Akron | aba7a5a | 2016-08-15 21:58:33 +0200 | [diff] [blame] | 256 | }; |
Akron | 6bb7158 | 2016-06-10 20:41:08 +0200 | [diff] [blame] | 257 | |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 258 | var resultInfo = d.getElementById('resultinfo'); |
| 259 | |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 260 | /** |
| 261 | * Add result panel |
| 262 | */ |
Akron | 5cb9b2b | 2018-07-24 17:01:09 +0200 | [diff] [blame] | 263 | var resultPanel = resultPanelClass.create(show); |
Akron | 644ad9f | 2021-07-26 16:12:59 +0200 | [diff] [blame] | 264 | |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 265 | if (resultInfo != null) { |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 266 | |
| 267 | // Move buttons to resultinfo |
Akron | 37ea119 | 2021-07-28 10:40:14 +0200 | [diff] [blame] | 268 | resultInfo.appendChild(resultPanel.actions().element()); |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 269 | |
Akron | e6538cd | 2018-07-16 17:52:33 +0200 | [diff] [blame] | 270 | // The views are at the top of the search results |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 271 | var sb = d.getElementById('search'); |
| 272 | sb.insertBefore(resultPanel.element(), sb.firstChild); |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 273 | }; |
| 274 | |
| 275 | |
Akron | 179c8ac | 2015-06-30 19:30:50 +0200 | [diff] [blame] | 276 | // There is a koralQuery |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 277 | if (KorAP.koralQuery !== undefined) { |
Akron | 5cb9b2b | 2018-07-24 17:01:09 +0200 | [diff] [blame] | 278 | |
| 279 | // Add KoralQuery view to result panel |
Akron | 4d926f1 | 2018-07-16 15:30:25 +0200 | [diff] [blame] | 280 | if (resultInfo !== null) { |
Akron | 5cb9b2b | 2018-07-24 17:01:09 +0200 | [diff] [blame] | 281 | resultPanel.addKqAction() |
Akron | 179c8ac | 2015-06-30 19:30:50 +0200 | [diff] [blame] | 282 | }; |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 283 | |
Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame] | 284 | if (KorAP.koralQuery["errors"]) { |
Akron | 678c26f | 2020-10-09 08:52:50 +0200 | [diff] [blame] | 285 | KorAP.koralQuery["errors"].forEach(function(e) { |
Akron | f0c31ed | 2016-06-11 11:27:01 +0200 | [diff] [blame] | 286 | |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 287 | // Malformed query |
Akron | 4a24b72 | 2020-10-13 12:44:25 +0200 | [diff] [blame] | 288 | if (e[0] === 302 && e[2] !== undefined) { |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 289 | obj.hint = hintClass.create(); |
Akron | 678c26f | 2020-10-09 08:52:50 +0200 | [diff] [blame] | 290 | obj.hint.alert(e[2], e[1]); |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 291 | } |
Akron | f0c31ed | 2016-06-11 11:27:01 +0200 | [diff] [blame] | 292 | |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 293 | // no query |
Akron | 678c26f | 2020-10-09 08:52:50 +0200 | [diff] [blame] | 294 | else if (e[0] === 301) { |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 295 | obj.hint = hintClass.create(); |
Akron | 678c26f | 2020-10-09 08:52:50 +0200 | [diff] [blame] | 296 | obj.hint.alert(0, e[1]); |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 297 | } |
Akron | 678c26f | 2020-10-09 08:52:50 +0200 | [diff] [blame] | 298 | }); |
Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame] | 299 | }; |
Akron | 179c8ac | 2015-06-30 19:30:50 +0200 | [diff] [blame] | 300 | }; |
| 301 | |
Akron | 5cb9b2b | 2018-07-24 17:01:09 +0200 | [diff] [blame] | 302 | |
| 303 | /* |
| 304 | * There is more than 0 matches, so allow for |
| 305 | * alignment toggling (left <=> right) |
| 306 | */ |
Akron | b50964a | 2020-10-12 11:44:37 +0200 | [diff] [blame] | 307 | if (matchElements.length > 0) |
Akron | 5cb9b2b | 2018-07-24 17:01:09 +0200 | [diff] [blame] | 308 | resultPanel.addAlignAction(); |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 309 | |
hebasta | 043e96f | 2019-11-28 12:33:00 +0100 | [diff] [blame] | 310 | KorAP.Panel['result'] = resultPanel; |
Akron | 5cb9b2b | 2018-07-24 17:01:09 +0200 | [diff] [blame] | 311 | /* |
Akron | cd42a14 | 2019-07-12 18:55:37 +0200 | [diff] [blame] | 312 | * Toggle the Virtual Corpus builder |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 313 | */ |
| 314 | if (vcname) { |
Akron | ec6bb8e | 2018-08-29 13:07:56 +0200 | [diff] [blame] | 315 | vc.onMinimize = function () { |
| 316 | vcname.classList.remove('active'); |
Akron | cd42a14 | 2019-07-12 18:55:37 +0200 | [diff] [blame] | 317 | delete show['vc']; |
Akron | ec6bb8e | 2018-08-29 13:07:56 +0200 | [diff] [blame] | 318 | }; |
Nils Diewald | 6283d69 | 2015-04-23 20:32:53 +0000 | [diff] [blame] | 319 | |
Akron | ec6bb8e | 2018-08-29 13:07:56 +0200 | [diff] [blame] | 320 | vc.onOpen = function () { |
| 321 | vcname.classList.add('active'); |
Akron | cfe8ecc | 2018-11-20 18:46:16 +0100 | [diff] [blame] | 322 | |
| 323 | var view = d.getElementById('vc-view'); |
| 324 | if (!view.firstChild) |
| 325 | view.appendChild(this.element()); |
| 326 | |
Akron | cd42a14 | 2019-07-12 18:55:37 +0200 | [diff] [blame] | 327 | show['vc'] = true; |
Akron | ec6bb8e | 2018-08-29 13:07:56 +0200 | [diff] [blame] | 328 | }; |
| 329 | |
| 330 | var vcclick = function () { |
Akron | ec6bb8e | 2018-08-29 13:07:56 +0200 | [diff] [blame] | 331 | if (vc.isOpen()) { |
| 332 | vc.minimize() |
| 333 | } |
| 334 | else { |
Akron | ec6bb8e | 2018-08-29 13:07:56 +0200 | [diff] [blame] | 335 | vc.open(); |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 336 | }; |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 337 | }; |
Akron | 04671e7 | 2017-05-11 20:47:32 +0200 | [diff] [blame] | 338 | |
Akron | 179c8ac | 2015-06-30 19:30:50 +0200 | [diff] [blame] | 339 | vcname.onclick = vcclick; |
Akron | 5c829e9 | 2017-05-12 18:10:00 +0200 | [diff] [blame] | 340 | |
| 341 | // Click, if the VC should be shown |
Akron | cd42a14 | 2019-07-12 18:55:37 +0200 | [diff] [blame] | 342 | if (show['vc']) { |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 343 | vcclick.apply(); |
Akron | 04671e7 | 2017-05-11 20:47:32 +0200 | [diff] [blame] | 344 | }; |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 345 | }; |
| 346 | |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 347 | |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 348 | /** |
| 349 | * Init Tutorial view |
| 350 | */ |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 351 | if (d.getElementById('view-tutorial')) { |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 352 | window.tutorial = tutClass.create( |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 353 | d.getElementById('view-tutorial'), |
Akron | f803559 | 2018-05-24 20:40:51 +0200 | [diff] [blame] | 354 | KorAP.session |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 355 | ); |
| 356 | obj.tutorial = window.tutorial; |
| 357 | } |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 358 | |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 359 | // Tutorial is in parent |
| 360 | else if (window.parent) { |
| 361 | obj.tutorial = window.parent.tutorial; |
| 362 | }; |
| 363 | |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 364 | // Initialize queries for d |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 365 | if (obj.tutorial) { |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 366 | obj.tutorial.initQueries(d); |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 367 | |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 368 | // Initialize documentation links |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 369 | obj.tutorial.initDocLinks(d); |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 370 | }; |
Nils Diewald | 61e6ff5 | 2015-05-07 17:26:50 +0000 | [diff] [blame] | 371 | |
Nils Diewald | 845282c | 2015-05-14 07:53:03 +0000 | [diff] [blame] | 372 | |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 373 | /** |
Akron | c1457bf | 2015-06-11 19:24:00 +0200 | [diff] [blame] | 374 | * Add VC creation on submission. |
| 375 | */ |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 376 | var form = d.getElementById('searchform'); |
Akron | 792f58b | 2015-07-08 18:59:36 +0200 | [diff] [blame] | 377 | if (form !== null) { |
Akron | c1457bf | 2015-06-11 19:24:00 +0200 | [diff] [blame] | 378 | form.addEventListener('submit', function (e) { |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 379 | var qf = d.getElementById('q-field'); |
Akron | 1be6c1c | 2020-01-07 15:29:58 +0100 | [diff] [blame] | 380 | |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 381 | // No query was defined |
| 382 | if (qf.value === undefined || qf.value === '') { |
| 383 | qf.focus(); |
| 384 | e.halt(); |
| 385 | KorAP.log(700, "No query given"); |
| 386 | return; |
| 387 | }; |
Akron | 1be6c1c | 2020-01-07 15:29:58 +0100 | [diff] [blame] | 388 | |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 389 | // Store session information |
Akron | f803559 | 2018-05-24 20:40:51 +0200 | [diff] [blame] | 390 | KorAP.session.set("show", show); |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 391 | |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 392 | if (vc !== undefined) { |
| 393 | input.value = vc.toQuery(); |
Akron | d7ad907 | 2019-12-09 07:08:20 +0100 | [diff] [blame] | 394 | if (input.value == '') |
| 395 | input.removeAttribute('name'); |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 396 | } |
| 397 | else { |
Akron | d7ad907 | 2019-12-09 07:08:20 +0100 | [diff] [blame] | 398 | input.removeAttribute('value'); |
| 399 | input.removeAttribute('name'); |
Akron | 19d97fe | 2016-09-06 20:47:05 +0200 | [diff] [blame] | 400 | }; |
Akron | 1be6c1c | 2020-01-07 15:29:58 +0100 | [diff] [blame] | 401 | |
| 402 | // This would preferably set the query to be "disabled", |
| 403 | // but in that case the query wouldn't be submitted |
| 404 | // at all. |
| 405 | // Setting the cursor to "progress" fails in current versions |
| 406 | // of webkit. |
| 407 | qf.classList.add("loading"); |
| 408 | d.getElementById('qsubmit').classList.add("loading"); |
Akron | c1457bf | 2015-06-11 19:24:00 +0200 | [diff] [blame] | 409 | }); |
| 410 | }; |
hebasta | 5df796f | 2019-05-21 15:27:12 +0200 | [diff] [blame] | 411 | |
| 412 | |
| 413 | //Starts the guided tour at the next page |
| 414 | if(KorAP.session.get("tour")){ |
| 415 | tourClass.gTshowResults().start(); |
| 416 | } |
| 417 | |
Akron | c1457bf | 2015-06-11 19:24:00 +0200 | [diff] [blame] | 418 | /** |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 419 | * Init hint helper |
| 420 | * has to be final because of |
| 421 | * reposition |
| 422 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 423 | // Todo: Pass an element, so this works with |
| 424 | // tutorial pages as well! |
Akron | 00cd4d1 | 2016-05-31 21:01:11 +0200 | [diff] [blame] | 425 | if (obj.hint === undefined) |
| 426 | obj.hint = hintClass.create(); |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 427 | |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 428 | // Add the hinthelper to the KorAP object to make it manipulatable globally |
Akron | 72f7357 | 2017-12-05 12:31:09 +0100 | [diff] [blame] | 429 | KorAP.Hint = obj.hint; |
Akron | 99713ef | 2017-06-28 18:19:28 +0200 | [diff] [blame] | 430 | |
Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 431 | |
| 432 | /** |
| 433 | * Add query panel |
| 434 | */ |
| 435 | var queryPanel = queryPanelClass.create(); |
| 436 | |
| 437 | // Get input field |
Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 438 | var vcView = d.getElementById('vc-view') |
Akron | 96b97d6 | 2023-11-07 15:56:54 +0100 | [diff] [blame] | 439 | if (form && vcView) { |
Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 440 | // The views are below the query bar |
Akron | 96b97d6 | 2023-11-07 15:56:54 +0100 | [diff] [blame] | 441 | form.insertBefore(queryPanel.element(), vcView); |
Akron | 2d0d96d | 2019-11-18 19:49:50 +0100 | [diff] [blame] | 442 | KorAP.Panel['query'] = queryPanel; |
Akron | 644ad9f | 2021-07-26 16:12:59 +0200 | [diff] [blame] | 443 | }; |
| 444 | |
| 445 | |
| 446 | /** |
| 447 | * Add pagination panel |
| 448 | */ |
| 449 | const paginationPanel = paginationPanelClass.create(); |
| 450 | |
| 451 | if (paginationPanel) { |
| 452 | paginationPanel.addRandomPage(); |
| 453 | KorAP.Panel['pagination'] = paginationPanel; |
| 454 | }; |
Akron | 24f48ea | 2020-07-01 09:37:19 +0200 | [diff] [blame] | 455 | |
Akron | a9c5580 | 2021-06-15 11:41:29 +0200 | [diff] [blame] | 456 | |
| 457 | /** |
| 458 | * Initialize password toggle. |
| 459 | */ |
| 460 | initCopyToClipboard(d); |
| 461 | |
| 462 | |
Akron | 24f48ea | 2020-07-01 09:37:19 +0200 | [diff] [blame] | 463 | /** |
Akron | 116eace | 2021-06-14 18:02:37 +0200 | [diff] [blame] | 464 | * Initialize password toggle. |
| 465 | */ |
Akron | 1cfde27 | 2021-06-14 18:32:39 +0200 | [diff] [blame] | 466 | initTogglePwdVisibility(d); |
Akron | 116eace | 2021-06-14 18:02:37 +0200 | [diff] [blame] | 467 | |
| 468 | /** |
Akron | 24f48ea | 2020-07-01 09:37:19 +0200 | [diff] [blame] | 469 | * Initialize Plugin registry. |
| 470 | */ |
Akron | 8dda1c6 | 2021-01-20 10:27:32 +0100 | [diff] [blame] | 471 | let pe; |
| 472 | if (pe = d.getElementById("kalamar-plugins")) { |
| 473 | let url = pe.getAttribute('data-plugins'); |
| 474 | if (url !== undefined) { |
| 475 | KorAP.API.getPluginList(url, function (json) { |
| 476 | if (json && json.length > 0) { |
Akron | da32e7a | 2021-11-16 17:28:57 +0100 | [diff] [blame] | 477 | |
| 478 | // Add state manager |
Akron | 96b97d6 | 2023-11-07 15:56:54 +0100 | [diff] [blame] | 479 | form = d.getElementById("searchform"); |
| 480 | if (!form) { |
| 481 | return; |
| 482 | }; |
| 483 | |
| 484 | const input = form.addE("input"); |
Akron | da32e7a | 2021-11-16 17:28:57 +0100 | [diff] [blame] | 485 | input.setAttribute("name","state"); |
| 486 | KorAP.States = stateManagerClass.create(input); |
| 487 | |
Akron | 8dda1c6 | 2021-01-20 10:27:32 +0100 | [diff] [blame] | 488 | // Load Plugin Server first |
| 489 | KorAP.Plugin = pluginClass.create(); |
Akron | 24f48ea | 2020-07-01 09:37:19 +0200 | [diff] [blame] | 490 | |
Akron | 8dda1c6 | 2021-01-20 10:27:32 +0100 | [diff] [blame] | 491 | // Add services container to head |
| 492 | d.head.appendChild(KorAP.Plugin.element()); |
Akron | 24f48ea | 2020-07-01 09:37:19 +0200 | [diff] [blame] | 493 | |
Akron | 8dda1c6 | 2021-01-20 10:27:32 +0100 | [diff] [blame] | 494 | // Add pipe form |
| 495 | KorAP.Pipe = pipeClass.create(); |
| 496 | d.getElementById("searchform").appendChild(KorAP.Pipe.element()); |
Akron | da32e7a | 2021-11-16 17:28:57 +0100 | [diff] [blame] | 497 | |
Akron | 8dda1c6 | 2021-01-20 10:27:32 +0100 | [diff] [blame] | 498 | try { |
| 499 | |
| 500 | // Register all plugins |
| 501 | json.forEach(i => KorAP.Plugin.register(i)); |
| 502 | } |
| 503 | catch (e) { |
| 504 | KorAP.log(0, e); |
| 505 | } |
| 506 | } |
| 507 | }); |
| 508 | }; |
Akron | 24f48ea | 2020-07-01 09:37:19 +0200 | [diff] [blame] | 509 | }; |
Akron | f7f75a9 | 2024-09-24 11:15:43 +0200 | [diff] [blame] | 510 | |
| 511 | window.dispatchEvent(new Event("ui-ready")); |
Akron | 8dda1c6 | 2021-01-20 10:27:32 +0100 | [diff] [blame] | 512 | |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 513 | return obj; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 514 | }); |
hebasta | 75cfca5 | 2019-02-19 13:15:27 +0100 | [diff] [blame] | 515 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 516 | }); |