Akron | e4da7ef | 2017-06-27 19:22:15 +0200 | [diff] [blame] | 1 | (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; |
Akron | c863a38 | 2017-06-28 16:12:08 +0200 | [diff] [blame^] | 40 | var sib = target; |
| 41 | while((sib = sib.previousSibling) != null) { |
| 42 | if (sib.nodeType === 1) |
Akron | e4da7ef | 2017-06-27 19:22:15 +0200 | [diff] [blame] | 43 | i++; |
| 44 | }; |
Akron | c863a38 | 2017-06-28 16:12:08 +0200 | [diff] [blame^] | 45 | |
| 46 | this.toggleInToken(target, i, foundry + '/' + layer + '=' + target.innerText); |
Akron | e4da7ef | 2017-06-27 19:22:15 +0200 | [diff] [blame] | 47 | } |
| 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; |
Akron | c863a38 | 2017-06-28 16:12:08 +0200 | [diff] [blame^] | 56 | var sib = target; |
| 57 | while ((sib = sib.previousSibling) != null) { |
| 58 | if (sib.nodeType === 1) |
Akron | e4da7ef | 2017-06-27 19:22:15 +0200 | [diff] [blame] | 59 | i++; |
| 60 | }; |
| 61 | |
| 62 | // Target is an orth |
| 63 | if (i >= 0) { |
Akron | c863a38 | 2017-06-28 16:12:08 +0200 | [diff] [blame^] | 64 | this.toggleInToken(target, i, 'orth=' + target.innerText); |
| 65 | } |
| 66 | } |
Akron | e4da7ef | 2017-06-27 19:22:15 +0200 | [diff] [blame] | 67 | |
Akron | c863a38 | 2017-06-28 16:12:08 +0200 | [diff] [blame^] | 68 | // 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 | }; |
Akron | e4da7ef | 2017-06-27 19:22:15 +0200 | [diff] [blame] | 88 | }; |
| 89 | }; |
| 90 | |
| 91 | event.stopPropagation(); |
| 92 | }, |
| 93 | |
Akron | c863a38 | 2017-06-28 16:12:08 +0200 | [diff] [blame^] | 94 | // Add term to token |
| 95 | addToToken : function (index, term) { |
Akron | e4da7ef | 2017-06-27 19:22:15 +0200 | [diff] [blame] | 96 | |
| 97 | var token = this._query[index]; |
| 98 | |
Akron | c863a38 | 2017-06-28 16:12:08 +0200 | [diff] [blame^] | 99 | // Initialize token |
Akron | e4da7ef | 2017-06-27 19:22:15 +0200 | [diff] [blame] | 100 | if (token === undefined) { |
| 101 | token = this._query[index] = []; |
| 102 | }; |
| 103 | |
Akron | c863a38 | 2017-06-28 16:12:08 +0200 | [diff] [blame^] | 104 | // Push to token array |
| 105 | token.push(term); |
Akron | e4da7ef | 2017-06-27 19:22:15 +0200 | [diff] [blame] | 106 | |
| 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 | }, |
Akron | c863a38 | 2017-06-28 16:12:08 +0200 | [diff] [blame^] | 116 | |
| 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 |
Akron | e4da7ef | 2017-06-27 19:22:15 +0200 | [diff] [blame] | 135 | element : function () { |
| 136 | return this._element; |
| 137 | }, |
Akron | c863a38 | 2017-06-28 16:12:08 +0200 | [diff] [blame^] | 138 | |
| 139 | // Show annotation fragment |
Akron | e4da7ef | 2017-06-27 19:22:15 +0200 | [diff] [blame] | 140 | 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) { |
Akron | c863a38 | 2017-06-28 16:12:08 +0200 | [diff] [blame^] | 150 | |
| 151 | // Better create a div |
Akron | e4da7ef | 2017-06-27 19:22:15 +0200 | [diff] [blame] | 152 | this._element = document.createElement('input'); |
| 153 | this._element.setAttribute('type', 'text'); |
| 154 | this._matchInfo.appendChild(this._element); |
| 155 | }; |
| 156 | |
Akron | c863a38 | 2017-06-28 16:12:08 +0200 | [diff] [blame^] | 157 | 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 | }; |
Akron | e4da7ef | 2017-06-27 19:22:15 +0200 | [diff] [blame] | 176 | } |
| 177 | }; |
| 178 | |
| 179 | function _createToken (token) { |
| 180 | var str = '['; |
Akron | c863a38 | 2017-06-28 16:12:08 +0200 | [diff] [blame^] | 181 | str += token.sort().join(" & "); |
Akron | e4da7ef | 2017-06-27 19:22:15 +0200 | [diff] [blame] | 182 | return str + ']'; |
| 183 | }; |
| 184 | |
| 185 | qc.create(document.getElementsByClassName('matchinfo')[0]); |
| 186 | |
| 187 | })(); |