blob: ecaad6f51d98126bee7751c24640cc8968b919bb [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 () {
Nils Diewald58141332015-04-07 16:18:45 +000039 var obj = Object.create(KorAP.init);
Nils Diewalda297f062015-04-02 00:23:46 +000040
41 /**
42 * Add actions to match entries
43 */
Nils Diewald5c5a7472015-04-02 22:13:38 +000044 var inactiveLi = document.querySelectorAll(
45 '#search > ol > li:not(.active)'
46 );
Nils Diewalda297f062015-04-02 00:23:46 +000047 var i = 0;
48 for (i = 0; i < inactiveLi.length; i++) {
Nils Diewald5c5a7472015-04-02 22:13:38 +000049 inactiveLi[i].addEventListener('click', function (e) {
50 if (this._match !== undefined)
Nils Diewalda297f062015-04-02 00:23:46 +000051 this._match.open();
Nils Diewald5c5a7472015-04-02 22:13:38 +000052 else
Nils Diewalda297f062015-04-02 00:23:46 +000053 KorAP.Match.create(this).open();
Nils Diewald5c5a7472015-04-02 22:13:38 +000054 e.halt();
Nils Diewalda297f062015-04-02 00:23:46 +000055 });
56 };
57
Nils Diewald5c5a7472015-04-02 22:13:38 +000058
Nils Diewalda297f062015-04-02 00:23:46 +000059 /**
60 * Toggle the alignment (left <=> right)
61 */
62 if (i > 0) {
63 var br = document.getElementById('button-right');
Nils Diewald5c5a7472015-04-02 22:13:38 +000064 if (br !== null) {
Nils Diewalda297f062015-04-02 00:23:46 +000065 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 Diewald5c5a7472015-04-02 22:13:38 +000083
84 /**
Nils Diewald58141332015-04-07 16:18:45 +000085 * Init vc
Nils Diewald5c5a7472015-04-02 22:13:38 +000086 */
Nils Diewald58141332015-04-07 16:18:45 +000087 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 Diewalda297f062015-04-02 00:23:46 +0000122 };
123
Nils Diewalda297f062015-04-02 00:23:46 +0000124}(this.KorAP));