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; |
| 40 | var child = target; |
| 41 | while((child = child.previousSibling) != null) { |
| 42 | if (child.nodeType === 1) |
| 43 | i++; |
| 44 | }; |
| 45 | |
| 46 | this.addToToken(i, foundry + '/' + layer + '=' + target.innerText); |
| 47 | target.style.backgroundColor = 'red'; |
| 48 | } |
| 49 | |
| 50 | // Get orth values |
| 51 | else if (target.tagName == 'TH') { |
| 52 | |
| 53 | // The head is in the top row |
| 54 | if (target.parentNode.parentNode.tagName == 'THEAD') { |
| 55 | |
| 56 | var i = -2; |
| 57 | var child = target; |
| 58 | while((child = child.previousSibling) != null) { |
| 59 | if (child.nodeType === 1) |
| 60 | i++; |
| 61 | }; |
| 62 | |
| 63 | // Target is an orth |
| 64 | if (i >= 0) { |
| 65 | |
| 66 | this.addToToken(i, 'orth=' + target.innerText); |
| 67 | target.style.backgroundColor = 'red'; |
| 68 | } |
| 69 | |
| 70 | }; |
| 71 | }; |
| 72 | }; |
| 73 | |
| 74 | event.stopPropagation(); |
| 75 | }, |
| 76 | |
| 77 | addToToken : function (index, annotation) { |
| 78 | |
| 79 | var token = this._query[index]; |
| 80 | |
| 81 | if (token === undefined) { |
| 82 | token = this._query[index] = []; |
| 83 | }; |
| 84 | |
| 85 | token.push(annotation); |
| 86 | |
| 87 | // Make terms unique |
| 88 | this._query[index] = token.filter( |
| 89 | function (e, i, arr) { |
| 90 | return arr.lastIndexOf(e) === i; |
| 91 | } |
| 92 | ); |
| 93 | |
| 94 | this.show(); |
| 95 | }, |
| 96 | element : function () { |
| 97 | return this._element; |
| 98 | }, |
| 99 | show : function () { |
| 100 | var str = ''; |
| 101 | this._query.forEach(function (token, index) { |
| 102 | if (token !== undefined) { |
| 103 | str += _createToken(token); |
| 104 | }; |
| 105 | }); |
| 106 | |
| 107 | // Element is not yet defined |
| 108 | if (this._element === undefined) { |
| 109 | this._element = document.createElement('input'); |
| 110 | this._element.setAttribute('type', 'text'); |
| 111 | this._matchInfo.appendChild(this._element); |
| 112 | }; |
| 113 | |
| 114 | this._element.value = str; |
| 115 | } |
| 116 | }; |
| 117 | |
| 118 | function _createToken (token) { |
| 119 | var str = '['; |
| 120 | str += token.join(" & "); |
| 121 | return str + ']'; |
| 122 | }; |
| 123 | |
| 124 | qc.create(document.getElementsByClassName('matchinfo')[0]); |
| 125 | |
| 126 | })(); |