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