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