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