Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 1 | /** |
| 2 | * These are utility functions for the frontend |
| 3 | */ |
| 4 | |
| 5 | // Add toggleClass method similar to jquery |
| 6 | HTMLElement.prototype.toggleClass = function (c1, c2) { |
| 7 | var cl = this.classList; |
| 8 | if (cl.contains(c1)) { |
| 9 | cl.add(c2); |
| 10 | cl.remove(c1); |
| 11 | } |
| 12 | else { |
| 13 | cl.remove(c2); |
| 14 | cl.add(c1); |
| 15 | }; |
| 16 | }; |
| 17 | |
| 18 | |
| 19 | // Don't let events bubble up |
| 20 | if (Event.halt === undefined) { |
| 21 | // Don't let events bubble up |
| 22 | Event.prototype.halt = function () { |
| 23 | this.stopPropagation(); |
| 24 | this.preventDefault(); |
| 25 | }; |
| 26 | }; |
| 27 | |
| 28 | var KorAP = KorAP || {}; |
| 29 | |
| 30 | |
| 31 | (function (KorAP) { |
| 32 | "use strict"; |
| 33 | |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame^] | 34 | |
| 35 | /** |
| 36 | * Initialize user interface elements |
| 37 | */ |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 38 | KorAP.init = function () { |
| 39 | |
| 40 | /** |
| 41 | * Add actions to match entries |
| 42 | */ |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame^] | 43 | var inactiveLi = document.querySelectorAll( |
| 44 | '#search > ol > li:not(.active)' |
| 45 | ); |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 46 | var i = 0; |
| 47 | for (i = 0; i < inactiveLi.length; i++) { |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame^] | 48 | inactiveLi[i].addEventListener('click', function (e) { |
| 49 | if (this._match !== undefined) |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 50 | this._match.open(); |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame^] | 51 | else |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 52 | KorAP.Match.create(this).open(); |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame^] | 53 | e.halt(); |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 54 | }); |
| 55 | }; |
| 56 | |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame^] | 57 | |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 58 | /** |
| 59 | * Toggle the alignment (left <=> right) |
| 60 | */ |
| 61 | if (i > 0) { |
| 62 | var br = document.getElementById('button-right'); |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame^] | 63 | if (br !== null) { |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 64 | var toggle = document.createElement('a'); |
| 65 | toggle.setAttribute('title', 'toggle Alignment'); |
| 66 | // Todo: Reuse old alignment from cookie! |
| 67 | var cl = toggle.classList; |
| 68 | cl.add('align'); |
| 69 | cl.add('right'); |
| 70 | toggle.addEventListener( |
| 71 | 'click', |
| 72 | function (e) { |
| 73 | var ol = document.querySelector('#search > ol'); |
| 74 | ol.toggleClass("align-left", "align-right"); |
| 75 | this.toggleClass("left", "right"); |
| 76 | }); |
| 77 | toggle.appendChild(document.createElement('span')) |
| 78 | .appendChild(document.createTextNode('Alignment')); |
| 79 | br.appendChild(toggle); |
| 80 | }; |
| 81 | }; |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame^] | 82 | |
| 83 | /** |
| 84 | * Init hint helper |
| 85 | */ |
| 86 | KorAP.Hint.create(); |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 87 | }; |
| 88 | |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 89 | }(this.KorAP)); |