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