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