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