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