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; |
| 97 | for (i = 0; i < inactiveLi.length; i++) { |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 98 | inactiveLi[i].addEventListener('click', function (e) { |
| 99 | if (this._match !== undefined) |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 100 | this._match.open(); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 101 | else { |
| 102 | matchClass.create(this).open(); |
| 103 | }; |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 104 | e.halt(); |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 105 | }); |
| 106 | }; |
| 107 | |
Akron | 179c8ac | 2015-06-30 19:30:50 +0200 | [diff] [blame] | 108 | var result = document.getElementById('resultinfo'); |
| 109 | var resultButton; |
| 110 | if (result != null) { |
| 111 | resultButton = result.appendChild(document.createElement('div')); |
| 112 | resultButton.classList.add('result', 'button'); |
| 113 | }; |
| 114 | |
| 115 | // There is a koralQuery |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 116 | if (KorAP.koralQuery !== undefined) { |
Akron | 179c8ac | 2015-06-30 19:30:50 +0200 | [diff] [blame] | 117 | |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 118 | if (resultButton !== null) { |
| 119 | var kq; |
| 120 | var toggle = document.createElement('a'); |
| 121 | toggle.setAttribute('title', loc.SHOW_KQ) |
| 122 | toggle.classList.add('show-kq', 'button'); |
| 123 | toggle.appendChild(document.createElement('span')) |
| 124 | .appendChild(document.createTextNode(loc.SHOW_KQ)); |
| 125 | resultButton.appendChild(toggle); |
Akron | 179c8ac | 2015-06-30 19:30:50 +0200 | [diff] [blame] | 126 | |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 127 | var showKQ = function () { |
| 128 | if (kq === undefined) { |
| 129 | kq = document.createElement('div'); |
| 130 | kq.setAttribute('id', 'koralquery'); |
| 131 | kq.style.display = 'none'; |
| 132 | var kqInner = document.createElement('div'); |
| 133 | kq.appendChild(kqInner); |
| 134 | kqInner.innerHTML = JSON.stringify(KorAP.koralQuery, null, ' '); |
| 135 | hljs.highlightBlock(kqInner); |
| 136 | var sb = document.getElementById('search'); |
| 137 | sb.insertBefore(kq, sb.firstChild); |
| 138 | }; |
| 139 | |
| 140 | if (kq.style.display === 'none') { |
| 141 | kq.style.display = 'block'; |
| 142 | show['kq'] = true; |
Akron | 179c8ac | 2015-06-30 19:30:50 +0200 | [diff] [blame] | 143 | } |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 144 | else { |
| 145 | kq.style.display = 'none'; |
| 146 | delete show['kq']; |
| 147 | }; |
| 148 | }; |
| 149 | |
| 150 | if (toggle !== undefined) { |
| 151 | |
| 152 | // Show koralquery |
| 153 | toggle.addEventListener('click', showKQ); |
| 154 | }; |
Akron | 179c8ac | 2015-06-30 19:30:50 +0200 | [diff] [blame] | 155 | }; |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 156 | |
| 157 | // Session has KQ visibility stored |
| 158 | if (show["kq"]) |
| 159 | showKQ.apply(); |
Akron | 179c8ac | 2015-06-30 19:30:50 +0200 | [diff] [blame] | 160 | }; |
| 161 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 162 | |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 163 | /** |
| 164 | * Toggle the alignment (left <=> right) |
| 165 | */ |
Akron | 179c8ac | 2015-06-30 19:30:50 +0200 | [diff] [blame] | 166 | // querySelector('div.button.right'); |
| 167 | if (i > 0 && resultButton !== null) { |
| 168 | var toggle = document.createElement('a'); |
| 169 | toggle.setAttribute('title', loc.TOGGLE_ALIGN); |
| 170 | // Todo: Reuse old alignment from query |
| 171 | var cl = toggle.classList; |
| 172 | cl.add('align', 'right', 'button'); |
| 173 | toggle.addEventListener( |
| 174 | 'click', |
| 175 | function (e) { |
| 176 | var ol = document.querySelector('#search > ol'); |
| 177 | ol.toggleClass("align-left", "align-right"); |
| 178 | this.toggleClass("left", "right"); |
| 179 | }); |
| 180 | toggle.appendChild(document.createElement('span')) |
| 181 | .appendChild(document.createTextNode(loc.TOGGLE_ALIGN)); |
| 182 | resultButton.appendChild(toggle); |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 183 | }; |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 184 | |
Nils Diewald | 6283d69 | 2015-04-23 20:32:53 +0000 | [diff] [blame] | 185 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 186 | /** |
| 187 | * Toggle the Virtual Collection builder |
| 188 | */ |
| 189 | if (vcname) { |
Nils Diewald | 6283d69 | 2015-04-23 20:32:53 +0000 | [diff] [blame] | 190 | var vc; |
Akron | 179c8ac | 2015-06-30 19:30:50 +0200 | [diff] [blame] | 191 | var vcclick = function () { |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 192 | var view = document.getElementById('vc-view'); |
Nils Diewald | 6283d69 | 2015-04-23 20:32:53 +0000 | [diff] [blame] | 193 | |
| 194 | // The vc is visible |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 195 | if (vcname.classList.contains('active')) { |
Nils Diewald | 6283d69 | 2015-04-23 20:32:53 +0000 | [diff] [blame] | 196 | view.removeChild(vc.element()); |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 197 | vcname.classList.remove('active'); |
| 198 | delete show['collection']; |
Nils Diewald | 6283d69 | 2015-04-23 20:32:53 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | // The vc is not visible |
| 202 | else { |
Akron | c1457bf | 2015-06-11 19:24:00 +0200 | [diff] [blame] | 203 | if (vc === undefined) |
Akron | 27ae9ec | 2015-06-23 00:43:21 +0200 | [diff] [blame] | 204 | vc = _getCurrentVC(vcClass, vcArray); |
Nils Diewald | 6283d69 | 2015-04-23 20:32:53 +0000 | [diff] [blame] | 205 | view.appendChild(vc.element()); |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 206 | vcname.classList.add('active'); |
| 207 | show['collection'] = true; |
Nils Diewald | 6283d69 | 2015-04-23 20:32:53 +0000 | [diff] [blame] | 208 | }; |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 209 | }; |
Akron | 179c8ac | 2015-06-30 19:30:50 +0200 | [diff] [blame] | 210 | vcname.onclick = vcclick; |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 211 | if (show['collection']) { |
Akron | 179c8ac | 2015-06-30 19:30:50 +0200 | [diff] [blame] | 212 | vcclick.apply(); |
| 213 | }; |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 214 | }; |
| 215 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 216 | |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 217 | /** |
| 218 | * Init Tutorial view |
| 219 | */ |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 220 | if (document.getElementById('view-tutorial')) { |
| 221 | window.tutorial = tutClass.create( |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 222 | document.getElementById('view-tutorial'), |
| 223 | session |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 224 | ); |
| 225 | obj.tutorial = window.tutorial; |
| 226 | } |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 227 | |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 228 | // Tutorial is in parent |
| 229 | else if (window.parent) { |
| 230 | obj.tutorial = window.parent.tutorial; |
| 231 | }; |
| 232 | |
| 233 | // Initialize queries for document |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 234 | if (obj.tutorial) |
| 235 | obj.tutorial.initQueries(document); |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 236 | |
Nils Diewald | 61e6ff5 | 2015-05-07 17:26:50 +0000 | [diff] [blame] | 237 | // Initialize documentation links |
| 238 | obj.tutorial.initDocLinks(document); |
| 239 | |
Nils Diewald | 845282c | 2015-05-14 07:53:03 +0000 | [diff] [blame] | 240 | |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 241 | /** |
Akron | c1457bf | 2015-06-11 19:24:00 +0200 | [diff] [blame] | 242 | * Add VC creation on submission. |
| 243 | */ |
| 244 | var form = document.getElementById('searchform'); |
| 245 | if (form !== undefined) { |
| 246 | form.addEventListener('submit', function (e) { |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 247 | var qf = document.getElementById('q-field'); |
Akron | c1457bf | 2015-06-11 19:24:00 +0200 | [diff] [blame] | 248 | |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 249 | // No query was defined |
| 250 | if (qf.value === undefined || qf.value === '') { |
| 251 | qf.focus(); |
| 252 | e.halt(); |
| 253 | KorAP.log(700, "No query given"); |
| 254 | return; |
| 255 | }; |
| 256 | |
| 257 | // Store session information |
| 258 | session.set("show", show); |
| 259 | |
| 260 | // Set Virtual collection |
| 261 | if (vc === undefined) { |
| 262 | vc = _getCurrentVC(vcClass, vcArray); |
| 263 | }; |
| 264 | |
| 265 | if (vc !== undefined) { |
Akron | c1457bf | 2015-06-11 19:24:00 +0200 | [diff] [blame] | 266 | input.value = vc.toQuery(); |
Akron | 7716f01 | 2015-07-01 20:38:32 +0200 | [diff] [blame] | 267 | } |
| 268 | else { |
| 269 | delete input['value']; |
| 270 | }; |
Akron | c1457bf | 2015-06-11 19:24:00 +0200 | [diff] [blame] | 271 | }); |
| 272 | }; |
| 273 | |
| 274 | /** |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 275 | * Init hint helper |
| 276 | * has to be final because of |
| 277 | * reposition |
| 278 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 279 | // Todo: Pass an element, so this works with |
| 280 | // tutorial pages as well! |
| 281 | obj.hint = hintClass.create(); |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 282 | |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 283 | return obj; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 284 | }); |
| 285 | }); |
Akron | c1457bf | 2015-06-11 19:24:00 +0200 | [diff] [blame] | 286 | |
| 287 | // Render Virtual collection |
Akron | 27ae9ec | 2015-06-23 00:43:21 +0200 | [diff] [blame] | 288 | function _getCurrentVC (vcClass, vcArray) { |
| 289 | var vc = vcClass.create(vcArray); |
| 290 | if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) { |
| 291 | vc.fromJson(KorAP.koralQuery["collection"]); |
| 292 | }; |
Akron | c1457bf | 2015-06-11 19:24:00 +0200 | [diff] [blame] | 293 | return vc; |
| 294 | }; |
| 295 | |