blob: ee178ce98cc0a0665588c0e925ce5ad5eee21d19 [file] [log] [blame]
Akrone4da7ef2017-06-27 19:22:15 +02001(function () {
2 "use strict";
3
4 var qc = {
5 create : function (matchInfo) {
6 return Object.create(this)._init(matchInfo);
7 },
8
9 // Initialize query creator
10 _init : function (matchInfo) {
11
12 // This may be probably a hint helper
13 this._query = []
14 this._matchInfo = matchInfo;
15
16 // Listen on the match table
17 this._matchInfo.addEventListener(
18 "click", this.clickOnAnno.bind(this), false
19 );
20 return this;
21 },
22
23 clickOnAnno : function (event) {
24
25 // Listen for clicks on table cells
26 if (event.target !== event.currentTarget) {
27
28 // Get target event
29 var target = event.target;
30
31 if (target.tagName == 'TD') {
32
33 // Check foundry and layer
34 var head = target.parentNode.getElementsByTagName('th');
35 var foundry = head[0].innerText;
36 var layer = head[1].innerText;
37
38 // Check index position:
39 var i = -2;
Akronc863a382017-06-28 16:12:08 +020040 var sib = target;
41 while((sib = sib.previousSibling) != null) {
42 if (sib.nodeType === 1)
Akrone4da7ef2017-06-27 19:22:15 +020043 i++;
44 };
Akronc863a382017-06-28 16:12:08 +020045
46 this.toggleInToken(target, i, foundry + '/' + layer + '=' + target.innerText);
Akrone4da7ef2017-06-27 19:22:15 +020047 }
48
49 // Get orth values
50 else if (target.tagName == 'TH') {
51
52 // The head is in the top row
53 if (target.parentNode.parentNode.tagName == 'THEAD') {
54
55 var i = -2;
Akronc863a382017-06-28 16:12:08 +020056 var sib = target;
57 while ((sib = sib.previousSibling) != null) {
58 if (sib.nodeType === 1)
Akrone4da7ef2017-06-27 19:22:15 +020059 i++;
60 };
61
62 // Target is an orth
63 if (i >= 0) {
Akronc863a382017-06-28 16:12:08 +020064 this.toggleInToken(target, i, 'orth=' + target.innerText);
65 }
66 }
Akrone4da7ef2017-06-27 19:22:15 +020067
Akronc863a382017-06-28 16:12:08 +020068 // The head refers to the complete row
69 else {
70
71 // Check foundry and layer
72 var head = target.parentNode.getElementsByTagName('th');
73 var foundry = head[0].innerText;
74 var layer = head[1].innerText;
75 var prefix = foundry + '/' + layer + '=';
76
77 // Iterate over all siblings
78 var i = 0;
79 var sib = target;
80 while ((sib = sib.nextSibling) != null) {
81 if (sib.nodeType !== 1 || sib.tagName === 'TH')
82 continue;
83 this.addToToken(i, prefix + sib.innerText);
84 sib.className = 'chosen';
85 i++;
86 };
87 };
Akrone4da7ef2017-06-27 19:22:15 +020088 };
89 };
90
91 event.stopPropagation();
92 },
93
Akronc863a382017-06-28 16:12:08 +020094 // Add term to token
95 addToToken : function (index, term) {
Akrone4da7ef2017-06-27 19:22:15 +020096
97 var token = this._query[index];
98
Akronc863a382017-06-28 16:12:08 +020099 // Initialize token
Akrone4da7ef2017-06-27 19:22:15 +0200100 if (token === undefined) {
101 token = this._query[index] = [];
102 };
103
Akronc863a382017-06-28 16:12:08 +0200104 // Push to token array
105 token.push(term);
Akrone4da7ef2017-06-27 19:22:15 +0200106
107 // Make terms unique
108 this._query[index] = token.filter(
109 function (e, i, arr) {
110 return arr.lastIndexOf(e) === i;
111 }
112 );
113
114 this.show();
115 },
Akronc863a382017-06-28 16:12:08 +0200116
117 // Remove term from token
118 removeFromToken : function (index, term) {
119 var token = this._query[index];
120
121 if (token === undefined)
122 return;
123
124 token.splice(token.indexOf(term), 1);
125
126 if (token.length > 0)
127 this._query[index] = token;
128 else
129 this._query[index] = undefined;
130
131 this.show();
132 },
133
134 // Get element representing annotation line
Akrone4da7ef2017-06-27 19:22:15 +0200135 element : function () {
136 return this._element;
137 },
Akronc863a382017-06-28 16:12:08 +0200138
139 // Show annotation fragment
Akrone4da7ef2017-06-27 19:22:15 +0200140 show : function () {
141 var str = '';
142 this._query.forEach(function (token, index) {
143 if (token !== undefined) {
144 str += _createToken(token);
145 };
146 });
147
148 // Element is not yet defined
149 if (this._element === undefined) {
Akronc863a382017-06-28 16:12:08 +0200150
151 // Better create a div
Akrone4da7ef2017-06-27 19:22:15 +0200152 this._element = document.createElement('input');
153 this._element.setAttribute('type', 'text');
154 this._matchInfo.appendChild(this._element);
155 };
156
Akronc863a382017-06-28 16:12:08 +0200157 if (str === '') {
158 this._matchInfo.removeChild(this._element);
159 this._element = undefined;
160 }
161 else {
162 this._element.value = str;
163 };
164 },
165
166 // Add term to token if not yet chosen, otherwise remove
167 toggleInToken : function (node, index, term) {
168 if (node.className == 'chosen') {
169 this.removeFromToken(index, term);
170 node.className = '';
171 }
172 else {
173 this.addToToken(index, term);
174 node.className = 'chosen';
175 };
Akrone4da7ef2017-06-27 19:22:15 +0200176 }
177 };
178
179 function _createToken (token) {
180 var str = '[';
Akronc863a382017-06-28 16:12:08 +0200181 str += token.sort().join(" & ");
Akrone4da7ef2017-06-27 19:22:15 +0200182 return str + ']';
183 };
184
185 qc.create(document.getElementsByClassName('matchinfo')[0]);
186
187})();