Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 1 | define([ |
| 2 | 'match', |
| 3 | 'hint', |
| 4 | 'vc', |
| 5 | 'tutorial', |
| 6 | 'lib/domReady', |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 7 | 'hint/array', |
Akron | 27ae9ec | 2015-06-23 00:43:21 +0200 | [diff] [blame] | 8 | 'vc/array', |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 9 | 'lib/alertify', |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 10 | 'session', |
Akron | be105b9 | 2016-01-04 14:13:20 +0100 | [diff] [blame] | 11 | 'tagger', |
Akron | 6bb7158 | 2016-06-10 20:41:08 +0200 | [diff] [blame] | 12 | 'selectMenu', |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 13 | 'api', |
Nils Diewald | c46003b | 2015-05-07 15:55:35 +0000 | [diff] [blame] | 14 | 'mailToChiffre', |
Nils Diewald | 845282c | 2015-05-14 07:53:03 +0000 | [diff] [blame] | 15 | 'lib/highlight/highlight.pack', |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 16 | 'util' |
| 17 | ], function (matchClass, |
| 18 | hintClass, |
| 19 | vcClass, |
| 20 | tutClass, |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 21 | domReady, |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 22 | hintArray, |
Akron | 27ae9ec | 2015-06-23 00:43:21 +0200 | [diff] [blame] | 23 | vcArray, |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 24 | alertifyClass, |
Akron | be105b9 | 2016-01-04 14:13:20 +0100 | [diff] [blame] | 25 | sessionClass, |
Akron | 6bb7158 | 2016-06-10 20:41:08 +0200 | [diff] [blame] | 26 | tagger, |
| 27 | selectMenuClass) { |
Nils Diewald | a0defc4 | 2015-05-07 23:54:17 +0000 | [diff] [blame] | 28 | |
| 29 | // Set hint array for hint helper |
| 30 | KorAP.hintArray = hintArray; |
| 31 | |
Akron | 9cc3eaf | 2015-06-10 22:15:52 +0200 | [diff] [blame] | 32 | // Localization values |
| 33 | var loc = KorAP.Locale; |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 34 | loc.VC_allCorpora = loc.VC_allCorpora || 'all Corpora'; |
| 35 | loc.VC_oneCollection = loc.VC_oneCollection || 'one Collection'; |
| 36 | loc.TOGGLE_ALIGN = loc.TOGGLE_ALIGN || 'toggle Alignment'; |
| 37 | loc.SHOW_KQ = loc.SHOW_KQ || 'show KoralQuery'; |
Akron | 9cc3eaf | 2015-06-10 22:15:52 +0200 | [diff] [blame] | 38 | |
Nils Diewald | a0defc4 | 2015-05-07 23:54:17 +0000 | [diff] [blame] | 39 | // Override KorAP.log |
| 40 | window.alertify = alertifyClass; |
Akron | f55504a | 2015-06-18 16:42:55 +0200 | [diff] [blame] | 41 | KorAP.log = function (code, msg) { |
Nils Diewald | a0defc4 | 2015-05-07 23:54:17 +0000 | [diff] [blame] | 42 | |
| 43 | // Use alertify to log errors |
| 44 | alertifyClass.log( |
Akron | f55504a | 2015-06-18 16:42:55 +0200 | [diff] [blame] | 45 | (code === 0 ? '' : code + ': ') + |
Nils Diewald | a0defc4 | 2015-05-07 23:54:17 +0000 | [diff] [blame] | 46 | msg, |
| 47 | 'error', |
Akron | f55504a | 2015-06-18 16:42:55 +0200 | [diff] [blame] | 48 | 10000 |
Nils Diewald | a0defc4 | 2015-05-07 23:54:17 +0000 | [diff] [blame] | 49 | ); |
| 50 | }; |
| 51 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 52 | domReady(function (event) { |
| 53 | var obj = {}; |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 54 | var session = sessionClass.create('KalamarJS'); |
| 55 | |
| 56 | // What should be visible? |
| 57 | var show = session.get('show') || {}; |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 58 | |
| 59 | /** |
Akron | f55504a | 2015-06-18 16:42:55 +0200 | [diff] [blame] | 60 | * Release notifications |
| 61 | */ |
| 62 | if (KorAP.Notifications !== undefined) { |
| 63 | var n = KorAP.Notifications; |
| 64 | for (var i = 0; i < n.length; i++) { |
| 65 | alertifyClass.log(n[i][1], n[i][0], 10000); |
| 66 | }; |
| 67 | }; |
| 68 | |
| 69 | /** |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 70 | * Replace Virtual Collection field |
| 71 | */ |
| 72 | var vcname; |
Akron | c1457bf | 2015-06-11 19:24:00 +0200 | [diff] [blame] | 73 | var input = document.getElementById('collection'); |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 74 | if (input) { |
| 75 | input.style.display = 'none'; |
| 76 | vcname = document.createElement('span'); |
| 77 | vcname.setAttribute('id', 'vc-choose'); |
Akron | 6bb7158 | 2016-06-10 20:41:08 +0200 | [diff] [blame] | 78 | vcname.classList.add('select'); |
Akron | 941551e | 2015-06-11 16:06:22 +0200 | [diff] [blame] | 79 | |
Akron | 27ae9ec | 2015-06-23 00:43:21 +0200 | [diff] [blame] | 80 | var currentVC = loc.VC_allCorpora; |
| 81 | if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) { |
| 82 | currentVC = loc.VC_oneCollection; |
| 83 | }; |
| 84 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 85 | vcname.appendChild( |
| 86 | document.createTextNode( |
Akron | 179c8ac | 2015-06-30 19:30:50 +0200 | [diff] [blame] | 87 | document.getElementById('collection-name').value || currentVC |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 88 | ) |
| 89 | ); |
Akron | 27ae9ec | 2015-06-23 00:43:21 +0200 | [diff] [blame] | 90 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 91 | input.parentNode.insertBefore(vcname, input); |
| 92 | }; |
| 93 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 94 | /** |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 95 | * Add actions to match entries |
| 96 | */ |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 97 | var inactiveLi = document.querySelectorAll( |
| 98 | '#search > ol > li:not(.active)' |
| 99 | ); |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 100 | var i = 0; |
Akron | 6a535d4 | 2015-08-26 20:16:58 +0200 | [diff] [blame] | 101 | |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 102 | for (i = 0; i < inactiveLi.length; i++) { |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 103 | inactiveLi[i].addEventListener('click', function (e) { |
| 104 | if (this._match !== undefined) |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 105 | this._match.open(); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 106 | else { |
| 107 | matchClass.create(this).open(); |
| 108 | }; |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 109 | e.halt(); |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 110 | }); |
Akron | 6a535d4 | 2015-08-26 20:16:58 +0200 | [diff] [blame] | 111 | inactiveLi[i].addEventListener('keydown', function (e) { |
| 112 | var code = _codeFromEvent(e); |
| 113 | |
| 114 | switch (code) { |
| 115 | case 32: |
| 116 | if (this._match !== undefined) |
| 117 | this._match.toggle(); |
| 118 | else { |
| 119 | matchClass.create(this).open(); |
| 120 | }; |
| 121 | e.halt(); |
| 122 | break; |
| 123 | }; |
| 124 | }); |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 125 | }; |
| 126 | |
Akron | 6bb7158 | 2016-06-10 20:41:08 +0200 | [diff] [blame] | 127 | // Replace QL select menus with KorAP menus |
| 128 | selectMenuClass.create( |
| 129 | document.getElementById('ql-field').parentNode |
| 130 | ).limit(5); |
| 131 | |
Akron | 179c8ac | 2015-06-30 19:30:50 +0200 | [diff] [blame] | 132 | var result = document.getElementById('resultinfo'); |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 133 | var resultButton = null; |
Akron | 179c8ac | 2015-06-30 19:30:50 +0200 | [diff] [blame] | 134 | if (result != null) { |
| 135 | resultButton = result.appendChild(document.createElement('div')); |
| 136 | resultButton.classList.add('result', 'button'); |
| 137 | }; |
| 138 | |
| 139 | // There is a koralQuery |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 140 | if (KorAP.koralQuery !== undefined) { |
Akron | 179c8ac | 2015-06-30 19:30:50 +0200 | [diff] [blame] | 141 | |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 142 | if (resultButton !== null) { |
| 143 | var kq; |
| 144 | var toggle = document.createElement('a'); |
| 145 | toggle.setAttribute('title', loc.SHOW_KQ) |
| 146 | toggle.classList.add('show-kq', 'button'); |
| 147 | toggle.appendChild(document.createElement('span')) |
| 148 | .appendChild(document.createTextNode(loc.SHOW_KQ)); |
| 149 | resultButton.appendChild(toggle); |
Akron | 179c8ac | 2015-06-30 19:30:50 +0200 | [diff] [blame] | 150 | |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 151 | var showKQ = function () { |
| 152 | if (kq === undefined) { |
| 153 | kq = document.createElement('div'); |
| 154 | kq.setAttribute('id', 'koralquery'); |
| 155 | kq.style.display = 'none'; |
| 156 | var kqInner = document.createElement('div'); |
| 157 | kq.appendChild(kqInner); |
| 158 | kqInner.innerHTML = JSON.stringify(KorAP.koralQuery, null, ' '); |
| 159 | hljs.highlightBlock(kqInner); |
| 160 | var sb = document.getElementById('search'); |
| 161 | sb.insertBefore(kq, sb.firstChild); |
| 162 | }; |
| 163 | |
| 164 | if (kq.style.display === 'none') { |
| 165 | kq.style.display = 'block'; |
| 166 | show['kq'] = true; |
Akron | 179c8ac | 2015-06-30 19:30:50 +0200 | [diff] [blame] | 167 | } |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 168 | else { |
| 169 | kq.style.display = 'none'; |
| 170 | delete show['kq']; |
| 171 | }; |
| 172 | }; |
| 173 | |
| 174 | if (toggle !== undefined) { |
| 175 | |
| 176 | // Show koralquery |
| 177 | toggle.addEventListener('click', showKQ); |
| 178 | }; |
Akron | 179c8ac | 2015-06-30 19:30:50 +0200 | [diff] [blame] | 179 | }; |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 180 | |
| 181 | // Session has KQ visibility stored |
| 182 | if (show["kq"]) |
| 183 | showKQ.apply(); |
Akron | 179c8ac | 2015-06-30 19:30:50 +0200 | [diff] [blame] | 184 | }; |
| 185 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 186 | |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 187 | /** |
| 188 | * Toggle the alignment (left <=> right) |
| 189 | */ |
Akron | 179c8ac | 2015-06-30 19:30:50 +0200 | [diff] [blame] | 190 | // querySelector('div.button.right'); |
| 191 | if (i > 0 && resultButton !== null) { |
| 192 | var toggle = document.createElement('a'); |
| 193 | toggle.setAttribute('title', loc.TOGGLE_ALIGN); |
| 194 | // Todo: Reuse old alignment from query |
| 195 | var cl = toggle.classList; |
| 196 | cl.add('align', 'right', 'button'); |
| 197 | toggle.addEventListener( |
| 198 | 'click', |
| 199 | function (e) { |
| 200 | var ol = document.querySelector('#search > ol'); |
| 201 | ol.toggleClass("align-left", "align-right"); |
| 202 | this.toggleClass("left", "right"); |
| 203 | }); |
| 204 | toggle.appendChild(document.createElement('span')) |
| 205 | .appendChild(document.createTextNode(loc.TOGGLE_ALIGN)); |
| 206 | resultButton.appendChild(toggle); |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 207 | }; |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 208 | |
Nils Diewald | 6283d69 | 2015-04-23 20:32:53 +0000 | [diff] [blame] | 209 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 210 | /** |
| 211 | * Toggle the Virtual Collection builder |
| 212 | */ |
| 213 | if (vcname) { |
Nils Diewald | 6283d69 | 2015-04-23 20:32:53 +0000 | [diff] [blame] | 214 | var vc; |
Akron | 179c8ac | 2015-06-30 19:30:50 +0200 | [diff] [blame] | 215 | var vcclick = function () { |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 216 | var view = document.getElementById('vc-view'); |
Nils Diewald | 6283d69 | 2015-04-23 20:32:53 +0000 | [diff] [blame] | 217 | |
| 218 | // The vc is visible |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 219 | if (vcname.classList.contains('active')) { |
Nils Diewald | 6283d69 | 2015-04-23 20:32:53 +0000 | [diff] [blame] | 220 | view.removeChild(vc.element()); |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 221 | vcname.classList.remove('active'); |
| 222 | delete show['collection']; |
Nils Diewald | 6283d69 | 2015-04-23 20:32:53 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | // The vc is not visible |
| 226 | else { |
Akron | c1457bf | 2015-06-11 19:24:00 +0200 | [diff] [blame] | 227 | if (vc === undefined) |
Akron | 27ae9ec | 2015-06-23 00:43:21 +0200 | [diff] [blame] | 228 | vc = _getCurrentVC(vcClass, vcArray); |
Nils Diewald | 6283d69 | 2015-04-23 20:32:53 +0000 | [diff] [blame] | 229 | view.appendChild(vc.element()); |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 230 | vcname.classList.add('active'); |
| 231 | show['collection'] = true; |
Nils Diewald | 6283d69 | 2015-04-23 20:32:53 +0000 | [diff] [blame] | 232 | }; |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 233 | }; |
Akron | 179c8ac | 2015-06-30 19:30:50 +0200 | [diff] [blame] | 234 | vcname.onclick = vcclick; |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 235 | if (show['collection']) { |
Akron | 179c8ac | 2015-06-30 19:30:50 +0200 | [diff] [blame] | 236 | vcclick.apply(); |
| 237 | }; |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 238 | }; |
| 239 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 240 | |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 241 | /** |
| 242 | * Init Tutorial view |
| 243 | */ |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 244 | if (document.getElementById('view-tutorial')) { |
| 245 | window.tutorial = tutClass.create( |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 246 | document.getElementById('view-tutorial'), |
| 247 | session |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 248 | ); |
| 249 | obj.tutorial = window.tutorial; |
| 250 | } |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 251 | |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 252 | // Tutorial is in parent |
| 253 | else if (window.parent) { |
| 254 | obj.tutorial = window.parent.tutorial; |
| 255 | }; |
| 256 | |
| 257 | // Initialize queries for document |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 258 | if (obj.tutorial) { |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 259 | obj.tutorial.initQueries(document); |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 260 | |
Akron | 6ed1399 | 2016-05-23 18:06:05 +0200 | [diff] [blame] | 261 | // Initialize documentation links |
| 262 | obj.tutorial.initDocLinks(document); |
| 263 | }; |
Nils Diewald | 61e6ff5 | 2015-05-07 17:26:50 +0000 | [diff] [blame] | 264 | |
Nils Diewald | 845282c | 2015-05-14 07:53:03 +0000 | [diff] [blame] | 265 | |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 266 | /** |
Akron | c1457bf | 2015-06-11 19:24:00 +0200 | [diff] [blame] | 267 | * Add VC creation on submission. |
| 268 | */ |
| 269 | var form = document.getElementById('searchform'); |
Akron | 792f58b | 2015-07-08 18:59:36 +0200 | [diff] [blame] | 270 | if (form !== null) { |
Akron | c1457bf | 2015-06-11 19:24:00 +0200 | [diff] [blame] | 271 | form.addEventListener('submit', function (e) { |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 272 | var qf = document.getElementById('q-field'); |
Akron | c1457bf | 2015-06-11 19:24:00 +0200 | [diff] [blame] | 273 | |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 274 | // No query was defined |
| 275 | if (qf.value === undefined || qf.value === '') { |
| 276 | qf.focus(); |
| 277 | e.halt(); |
| 278 | KorAP.log(700, "No query given"); |
| 279 | return; |
| 280 | }; |
| 281 | |
| 282 | // Store session information |
| 283 | session.set("show", show); |
| 284 | |
| 285 | // Set Virtual collection |
| 286 | if (vc === undefined) { |
| 287 | vc = _getCurrentVC(vcClass, vcArray); |
| 288 | }; |
| 289 | |
| 290 | if (vc !== undefined) { |
Akron | c1457bf | 2015-06-11 19:24:00 +0200 | [diff] [blame] | 291 | input.value = vc.toQuery(); |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 292 | } |
| 293 | else { |
| 294 | delete input['value']; |
| 295 | }; |
Akron | c1457bf | 2015-06-11 19:24:00 +0200 | [diff] [blame] | 296 | }); |
| 297 | }; |
| 298 | |
| 299 | /** |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 300 | * Init hint helper |
| 301 | * has to be final because of |
| 302 | * reposition |
| 303 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 304 | // Todo: Pass an element, so this works with |
| 305 | // tutorial pages as well! |
| 306 | obj.hint = hintClass.create(); |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 307 | |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 308 | return obj; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 309 | }); |
| 310 | }); |
Akron | c1457bf | 2015-06-11 19:24:00 +0200 | [diff] [blame] | 311 | |
| 312 | // Render Virtual collection |
Akron | 27ae9ec | 2015-06-23 00:43:21 +0200 | [diff] [blame] | 313 | function _getCurrentVC (vcClass, vcArray) { |
| 314 | var vc = vcClass.create(vcArray); |
| 315 | if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) { |
| 316 | vc.fromJson(KorAP.koralQuery["collection"]); |
| 317 | }; |
Akron | c1457bf | 2015-06-11 19:24:00 +0200 | [diff] [blame] | 318 | return vc; |
| 319 | }; |