blob: 3adf5a0afbd908abd985c5aedf2b6a7068a5527f [file] [log] [blame]
Nils Diewalda297f062015-04-02 00:23:46 +00001/**
2 * These are utility functions for the frontend
3 */
4
5// Add toggleClass method similar to jquery
6HTMLElement.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
20if (Event.halt === undefined) {
21 // Don't let events bubble up
22 Event.prototype.halt = function () {
23 this.stopPropagation();
24 this.preventDefault();
25 };
26};
27
28var KorAP = KorAP || {};
29
30
31(function (KorAP) {
32 "use strict";
33
Nils Diewald5c5a7472015-04-02 22:13:38 +000034
35 /**
36 * Initialize user interface elements
37 */
Nils Diewalda297f062015-04-02 00:23:46 +000038 KorAP.init = function () {
39
40 /**
41 * Add actions to match entries
42 */
Nils Diewald5c5a7472015-04-02 22:13:38 +000043 var inactiveLi = document.querySelectorAll(
44 '#search > ol > li:not(.active)'
45 );
Nils Diewalda297f062015-04-02 00:23:46 +000046 var i = 0;
47 for (i = 0; i < inactiveLi.length; i++) {
Nils Diewald5c5a7472015-04-02 22:13:38 +000048 inactiveLi[i].addEventListener('click', function (e) {
49 if (this._match !== undefined)
Nils Diewalda297f062015-04-02 00:23:46 +000050 this._match.open();
Nils Diewald5c5a7472015-04-02 22:13:38 +000051 else
Nils Diewalda297f062015-04-02 00:23:46 +000052 KorAP.Match.create(this).open();
Nils Diewald5c5a7472015-04-02 22:13:38 +000053 e.halt();
Nils Diewalda297f062015-04-02 00:23:46 +000054 });
55 };
56
Nils Diewald5c5a7472015-04-02 22:13:38 +000057
Nils Diewalda297f062015-04-02 00:23:46 +000058 /**
59 * Toggle the alignment (left <=> right)
60 */
61 if (i > 0) {
62 var br = document.getElementById('button-right');
Nils Diewald5c5a7472015-04-02 22:13:38 +000063 if (br !== null) {
Nils Diewalda297f062015-04-02 00:23:46 +000064 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 Diewald5c5a7472015-04-02 22:13:38 +000082
83 /**
84 * Init hint helper
85 */
86 KorAP.Hint.create();
Nils Diewalda297f062015-04-02 00:23:46 +000087 };
88
Nils Diewalda297f062015-04-02 00:23:46 +000089}(this.KorAP));