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 () { |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 39 | var obj = Object.create(KorAP.init); |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Add actions to match entries |
| 43 | */ |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 44 | var inactiveLi = document.querySelectorAll( |
| 45 | '#search > ol > li:not(.active)' |
| 46 | ); |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 47 | var i = 0; |
| 48 | for (i = 0; i < inactiveLi.length; i++) { |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 49 | inactiveLi[i].addEventListener('click', function (e) { |
| 50 | if (this._match !== undefined) |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 51 | this._match.open(); |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 52 | else |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 53 | KorAP.Match.create(this).open(); |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 54 | e.halt(); |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 55 | }); |
| 56 | }; |
| 57 | |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 58 | |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 59 | /** |
| 60 | * Toggle the alignment (left <=> right) |
| 61 | */ |
| 62 | if (i > 0) { |
| 63 | var br = document.getElementById('button-right'); |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 64 | if (br !== null) { |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 65 | var toggle = document.createElement('a'); |
| 66 | toggle.setAttribute('title', 'toggle Alignment'); |
| 67 | // Todo: Reuse old alignment from cookie! |
| 68 | var cl = toggle.classList; |
| 69 | cl.add('align'); |
| 70 | cl.add('right'); |
| 71 | toggle.addEventListener( |
| 72 | 'click', |
| 73 | function (e) { |
| 74 | var ol = document.querySelector('#search > ol'); |
| 75 | ol.toggleClass("align-left", "align-right"); |
| 76 | this.toggleClass("left", "right"); |
| 77 | }); |
| 78 | toggle.appendChild(document.createElement('span')) |
| 79 | .appendChild(document.createTextNode('Alignment')); |
| 80 | br.appendChild(toggle); |
| 81 | }; |
| 82 | }; |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 83 | |
| 84 | /** |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 85 | * Init vc |
Nils Diewald | 5c5a747 | 2015-04-02 22:13:38 +0000 | [diff] [blame] | 86 | */ |
Nils Diewald | 5814133 | 2015-04-07 16:18:45 +0000 | [diff] [blame] | 87 | var input = document.getElementById('vc'); |
| 88 | if (input) { |
| 89 | input.style.display = 'none'; |
| 90 | var vcname = document.createElement('span'); |
| 91 | vcname.setAttribute('id', 'vc-choose'); |
| 92 | vcname.appendChild( |
| 93 | document.createTextNode( |
| 94 | document.getElementById('vc-name').value |
| 95 | ) |
| 96 | ); |
| 97 | input.parentNode.insertBefore(vcname, input); |
| 98 | |
| 99 | vcname.onclick = function () { |
| 100 | var vc = KorAP.VirtualCollection.render(vcExample); |
| 101 | var view = document.getElementById('vc-view'); |
| 102 | view.appendChild(vc.element()); |
| 103 | }; |
| 104 | }; |
| 105 | |
| 106 | /** |
| 107 | * Init Tutorial view |
| 108 | */ |
| 109 | obj.tutorial = KorAP.Tutorial.create( |
| 110 | document.getElementById('view-tutorial') |
| 111 | ); |
| 112 | |
| 113 | /** |
| 114 | * Init hint helper |
| 115 | * has to be final because of |
| 116 | * reposition |
| 117 | */ |
| 118 | // Todo: Pass an element, so this works with |
| 119 | // tutorial pages as well! |
| 120 | obj.hint = KorAP.Hint.create(); |
| 121 | return obj; |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 122 | }; |
| 123 | |
Nils Diewald | a297f06 | 2015-04-02 00:23:46 +0000 | [diff] [blame] | 124 | }(this.KorAP)); |