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