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