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', |
| 10 | 'api', |
Nils Diewald | c46003b | 2015-05-07 15:55:35 +0000 | [diff] [blame] | 11 | 'mailToChiffre', |
Nils Diewald | 845282c | 2015-05-14 07:53:03 +0000 | [diff] [blame] | 12 | 'lib/highlight/highlight.pack', |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 13 | 'util' |
| 14 | ], function (matchClass, |
| 15 | hintClass, |
| 16 | vcClass, |
| 17 | tutClass, |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 18 | domReady, |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 19 | hintArray, |
Akron | 27ae9ec | 2015-06-23 00:43:21 +0200 | [diff] [blame^] | 20 | vcArray, |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 21 | alertifyClass) { |
Nils Diewald | a0defc4 | 2015-05-07 23:54:17 +0000 | [diff] [blame] | 22 | |
| 23 | // Set hint array for hint helper |
| 24 | KorAP.hintArray = hintArray; |
| 25 | |
Akron | 9cc3eaf | 2015-06-10 22:15:52 +0200 | [diff] [blame] | 26 | // Localization values |
| 27 | var loc = KorAP.Locale; |
Akron | 941551e | 2015-06-11 16:06:22 +0200 | [diff] [blame] | 28 | loc.VC_allCorpora = loc.VC_allCorpora || 'all Corpora'; |
| 29 | loc.VC_oneCollection = loc.VC_oneCollection || 'one Collection'; |
Akron | 9cc3eaf | 2015-06-10 22:15:52 +0200 | [diff] [blame] | 30 | |
Nils Diewald | a0defc4 | 2015-05-07 23:54:17 +0000 | [diff] [blame] | 31 | // Override KorAP.log |
| 32 | window.alertify = alertifyClass; |
Akron | f55504a | 2015-06-18 16:42:55 +0200 | [diff] [blame] | 33 | KorAP.log = function (code, msg) { |
Nils Diewald | a0defc4 | 2015-05-07 23:54:17 +0000 | [diff] [blame] | 34 | |
| 35 | // Use alertify to log errors |
| 36 | alertifyClass.log( |
Akron | f55504a | 2015-06-18 16:42:55 +0200 | [diff] [blame] | 37 | (code === 0 ? '' : code + ': ') + |
Nils Diewald | a0defc4 | 2015-05-07 23:54:17 +0000 | [diff] [blame] | 38 | msg, |
| 39 | 'error', |
Akron | f55504a | 2015-06-18 16:42:55 +0200 | [diff] [blame] | 40 | 10000 |
Nils Diewald | a0defc4 | 2015-05-07 23:54:17 +0000 | [diff] [blame] | 41 | ); |
| 42 | }; |
| 43 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 44 | domReady(function (event) { |
| 45 | var obj = {}; |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 46 | |
| 47 | /** |
Akron | f55504a | 2015-06-18 16:42:55 +0200 | [diff] [blame] | 48 | * Release notifications |
| 49 | */ |
| 50 | if (KorAP.Notifications !== undefined) { |
| 51 | var n = KorAP.Notifications; |
| 52 | for (var i = 0; i < n.length; i++) { |
| 53 | alertifyClass.log(n[i][1], n[i][0], 10000); |
| 54 | }; |
| 55 | }; |
| 56 | |
| 57 | /** |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 58 | * Replace Virtual Collection field |
| 59 | */ |
| 60 | var vcname; |
Akron | c1457bf | 2015-06-11 19:24:00 +0200 | [diff] [blame] | 61 | var input = document.getElementById('collection'); |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 62 | if (input) { |
| 63 | input.style.display = 'none'; |
| 64 | vcname = document.createElement('span'); |
| 65 | vcname.setAttribute('id', 'vc-choose'); |
Akron | 941551e | 2015-06-11 16:06:22 +0200 | [diff] [blame] | 66 | |
Akron | 27ae9ec | 2015-06-23 00:43:21 +0200 | [diff] [blame^] | 67 | var currentVC = loc.VC_allCorpora; |
| 68 | if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) { |
| 69 | currentVC = loc.VC_oneCollection; |
| 70 | }; |
| 71 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 72 | vcname.appendChild( |
| 73 | document.createTextNode( |
Akron | 27ae9ec | 2015-06-23 00:43:21 +0200 | [diff] [blame^] | 74 | document.getElementById('vc-name').value || currentVC |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 75 | ) |
| 76 | ); |
Akron | 27ae9ec | 2015-06-23 00:43:21 +0200 | [diff] [blame^] | 77 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 78 | input.parentNode.insertBefore(vcname, input); |
| 79 | }; |
| 80 | |
| 81 | |
| 82 | /** |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 83 | * Add actions to match entries |
| 84 | */ |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 85 | var inactiveLi = document.querySelectorAll( |
| 86 | '#search > ol > li:not(.active)' |
| 87 | ); |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 88 | var i = 0; |
| 89 | for (i = 0; i < inactiveLi.length; i++) { |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 90 | inactiveLi[i].addEventListener('click', function (e) { |
| 91 | if (this._match !== undefined) |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 92 | this._match.open(); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 93 | else { |
| 94 | matchClass.create(this).open(); |
| 95 | }; |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 96 | e.halt(); |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 97 | }); |
| 98 | }; |
| 99 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 100 | |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 101 | /** |
| 102 | * Toggle the alignment (left <=> right) |
| 103 | */ |
| 104 | if (i > 0) { |
Nils Diewald | d0711ca | 2015-05-19 22:25:12 +0000 | [diff] [blame] | 105 | var br = document.querySelector('div.button.right'); |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 106 | if (br !== null) { |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 107 | var toggle = document.createElement('a'); |
| 108 | toggle.setAttribute('title', 'toggle Alignment'); |
| 109 | // Todo: Reuse old alignment from cookie! |
| 110 | var cl = toggle.classList; |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 111 | cl.add('align', 'right'); |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 112 | toggle.addEventListener( |
| 113 | 'click', |
| 114 | function (e) { |
| 115 | var ol = document.querySelector('#search > ol'); |
| 116 | ol.toggleClass("align-left", "align-right"); |
| 117 | this.toggleClass("left", "right"); |
| 118 | }); |
| 119 | toggle.appendChild(document.createElement('span')) |
| 120 | .appendChild(document.createTextNode('Alignment')); |
| 121 | br.appendChild(toggle); |
| 122 | }; |
| 123 | }; |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 124 | |
Nils Diewald | 6283d69 | 2015-04-23 20:32:53 +0000 | [diff] [blame] | 125 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 126 | /** |
| 127 | * Toggle the Virtual Collection builder |
| 128 | */ |
| 129 | if (vcname) { |
Nils Diewald | 6283d69 | 2015-04-23 20:32:53 +0000 | [diff] [blame] | 130 | var vc; |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 131 | vcname.onclick = function () { |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 132 | var view = document.getElementById('vc-view'); |
Nils Diewald | 6283d69 | 2015-04-23 20:32:53 +0000 | [diff] [blame] | 133 | |
| 134 | // The vc is visible |
| 135 | if (this.classList.contains('active')) { |
| 136 | view.removeChild(vc.element()); |
| 137 | this.classList.remove('active'); |
| 138 | } |
| 139 | |
| 140 | // The vc is not visible |
| 141 | else { |
Akron | c1457bf | 2015-06-11 19:24:00 +0200 | [diff] [blame] | 142 | if (vc === undefined) |
Akron | 27ae9ec | 2015-06-23 00:43:21 +0200 | [diff] [blame^] | 143 | vc = _getCurrentVC(vcClass, vcArray); |
Nils Diewald | 6283d69 | 2015-04-23 20:32:53 +0000 | [diff] [blame] | 144 | view.appendChild(vc.element()); |
| 145 | this.classList.add('active'); |
| 146 | }; |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 147 | }; |
| 148 | }; |
| 149 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 150 | |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 151 | /** |
| 152 | * Init Tutorial view |
| 153 | */ |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 154 | if (document.getElementById('view-tutorial')) { |
| 155 | window.tutorial = tutClass.create( |
| 156 | document.getElementById('view-tutorial') |
| 157 | ); |
| 158 | obj.tutorial = window.tutorial; |
| 159 | } |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 160 | |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 161 | // Tutorial is in parent |
| 162 | else if (window.parent) { |
| 163 | obj.tutorial = window.parent.tutorial; |
| 164 | }; |
| 165 | |
| 166 | // Initialize queries for document |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 167 | if (obj.tutorial) |
| 168 | obj.tutorial.initQueries(document); |
Nils Diewald | fccfbcb | 2015-04-29 20:48:19 +0000 | [diff] [blame] | 169 | |
Nils Diewald | 61e6ff5 | 2015-05-07 17:26:50 +0000 | [diff] [blame] | 170 | // Initialize documentation links |
| 171 | obj.tutorial.initDocLinks(document); |
| 172 | |
Akron | 48b1e4d | 2015-06-17 18:47:01 +0200 | [diff] [blame] | 173 | // There is a currentQuery |
Akron | 27ae9ec | 2015-06-23 00:43:21 +0200 | [diff] [blame^] | 174 | /* |
| 175 | if (KorAP.koralQuery !== undefined) { |
Nils Diewald | 845282c | 2015-05-14 07:53:03 +0000 | [diff] [blame] | 176 | var kq = document.createElement('div'); |
| 177 | kq.setAttribute('id', 'koralquery'); |
Akron | 48b1e4d | 2015-06-17 18:47:01 +0200 | [diff] [blame] | 178 | |
| 179 | var kqInner = document.createElement('div'); |
| 180 | kq.appendChild(kqInner); |
Akron | 27ae9ec | 2015-06-23 00:43:21 +0200 | [diff] [blame^] | 181 | kqInner.innerHTML = JSON.stringify(KorAP.koralQuery, null, ' '); |
Akron | 48b1e4d | 2015-06-17 18:47:01 +0200 | [diff] [blame] | 182 | hljs.highlightBlock(kqInner); |
| 183 | |
| 184 | var sb = document.getElementById('search'); |
| 185 | sb.insertBefore(kq, sb.firstChild); |
Nils Diewald | 845282c | 2015-05-14 07:53:03 +0000 | [diff] [blame] | 186 | }; |
Akron | 27ae9ec | 2015-06-23 00:43:21 +0200 | [diff] [blame^] | 187 | */ |
Nils Diewald | 845282c | 2015-05-14 07:53:03 +0000 | [diff] [blame] | 188 | |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 189 | /** |
Akron | c1457bf | 2015-06-11 19:24:00 +0200 | [diff] [blame] | 190 | * Add VC creation on submission. |
| 191 | */ |
| 192 | var form = document.getElementById('searchform'); |
| 193 | if (form !== undefined) { |
| 194 | form.addEventListener('submit', function (e) { |
| 195 | if (vc === undefined) |
Akron | 27ae9ec | 2015-06-23 00:43:21 +0200 | [diff] [blame^] | 196 | vc = _getCurrentVC(vcClass, vcArray); |
Akron | c1457bf | 2015-06-11 19:24:00 +0200 | [diff] [blame] | 197 | |
| 198 | if (vc !== undefined) |
| 199 | input.value = vc.toQuery(); |
| 200 | }); |
| 201 | }; |
| 202 | |
| 203 | /** |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 204 | * Init hint helper |
| 205 | * has to be final because of |
| 206 | * reposition |
| 207 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 208 | // Todo: Pass an element, so this works with |
| 209 | // tutorial pages as well! |
| 210 | obj.hint = hintClass.create(); |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 211 | |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 212 | return obj; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 213 | }); |
| 214 | }); |
Akron | c1457bf | 2015-06-11 19:24:00 +0200 | [diff] [blame] | 215 | |
| 216 | // Render Virtual collection |
Akron | 27ae9ec | 2015-06-23 00:43:21 +0200 | [diff] [blame^] | 217 | function _getCurrentVC (vcClass, vcArray) { |
| 218 | var vc = vcClass.create(vcArray); |
| 219 | if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) { |
| 220 | vc.fromJson(KorAP.koralQuery["collection"]); |
| 221 | }; |
Akron | c1457bf | 2015-06-11 19:24:00 +0200 | [diff] [blame] | 222 | return vc; |
| 223 | }; |
| 224 | |