Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 1 | /** |
| 2 | * Create a virtual corpus fragment, |
Akron | 889ec29 | 2018-11-19 17:56:01 +0100 | [diff] [blame] | 3 | * that can be shown and merged with the |
Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 4 | * main VC. |
| 5 | * |
| 6 | * @author Nils Diewald |
| 7 | */ |
| 8 | |
| 9 | define(['util'], function () { |
| 10 | "use strict"; |
| 11 | |
Akron | d45a170 | 2018-11-19 18:15:17 +0100 | [diff] [blame] | 12 | const loc = KorAP.Locale; |
| 13 | loc.NEW_CONSTRAINT = loc.NEW_CONSTRAINT || 'New Constraint'; |
| 14 | |
Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 15 | // Create a VC doc |
| 16 | function _doc (op) { |
| 17 | var doc = document.createElement('div'); |
| 18 | doc.setAttribute('class','doc'); |
| 19 | |
| 20 | var key = doc.addE('span'); |
| 21 | key.setAttribute('class','key'); |
| 22 | key.addT(op[0]); |
| 23 | |
| 24 | var match = doc.addE('span'); |
| 25 | match.setAttribute('class','match'); |
| 26 | match.addT('eq'); |
| 27 | |
| 28 | var value = doc.addE('span'); |
| 29 | value.setAttribute('class', 'value'); |
| 30 | value.addT(op[1]); |
| 31 | |
| 32 | return doc; |
| 33 | }; |
| 34 | |
Akron | 889ec29 | 2018-11-19 17:56:01 +0100 | [diff] [blame] | 35 | // Return object |
Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 36 | return { |
| 37 | |
| 38 | create : function () { |
Akron | 889ec29 | 2018-11-19 17:56:01 +0100 | [diff] [blame] | 39 | const obj = Object.create(this); |
Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 40 | obj._operands = []; |
| 41 | return obj; |
| 42 | }, |
| 43 | |
| 44 | |
| 45 | /** |
| 46 | * Add document constraint to fragment |
| 47 | */ |
| 48 | add : function (key, value, type) { |
Akron | 889ec29 | 2018-11-19 17:56:01 +0100 | [diff] [blame] | 49 | for (let i in this._operands) { |
| 50 | let op = this._operands[i]; |
Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 51 | if (op[0] === key && op[1] === value) { |
| 52 | array.splice(index, 1); |
| 53 | }; |
| 54 | }; |
| 55 | this._operands.push([key, value, type]); |
| 56 | this.update(); |
| 57 | }, |
| 58 | |
| 59 | |
| 60 | /** |
| 61 | * Remove document constraint from fragment |
| 62 | */ |
| 63 | remove : function (key, value) { |
Akron | 889ec29 | 2018-11-19 17:56:01 +0100 | [diff] [blame] | 64 | for (let i in this._operands) { |
| 65 | let op = this._operands[i]; |
Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 66 | if (op[0] === key && op[1] === value) { |
| 67 | this._operands.splice(i, 1); |
| 68 | this.update(); |
| 69 | return; |
| 70 | }; |
| 71 | }; |
| 72 | return; |
| 73 | }, |
| 74 | |
Akron | 889ec29 | 2018-11-19 17:56:01 +0100 | [diff] [blame] | 75 | /** |
| 76 | * Check, if the fragment contains any constraints |
| 77 | */ |
| 78 | isEmpty : function () { |
| 79 | return this._operands.length > 0 ? false : true; |
| 80 | }, |
Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 81 | |
| 82 | /** |
| 83 | * Add fragment constraints to VC. |
| 84 | */ |
| 85 | mergeWithVC : function () { |
| 86 | }, |
| 87 | |
| 88 | |
| 89 | /** |
| 90 | * Get the element associated with the virtual corpus |
| 91 | */ |
| 92 | element : function () { |
| 93 | if (this._element !== undefined) { |
| 94 | return this._element; |
| 95 | }; |
| 96 | |
Akron | d45a170 | 2018-11-19 18:15:17 +0100 | [diff] [blame] | 97 | // Initialize element |
Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 98 | this._element = document.createElement('div'); |
| 99 | this._element.classList.add('vc', 'fragment'); |
Akron | d45a170 | 2018-11-19 18:15:17 +0100 | [diff] [blame] | 100 | |
| 101 | // Prepend info text |
| 102 | this._element.addE('span').addT(loc.NEW_CONSTRAINT + ':'); |
| 103 | this._frag = this._element.addE('div'); |
| 104 | |
Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 105 | return this._element; |
| 106 | }, |
| 107 | |
| 108 | |
| 109 | /** |
| 110 | * Update the whole object based on the underlying data structure |
| 111 | */ |
| 112 | update : function() { |
| 113 | |
| 114 | // <div class="docGroup" data-operation="and"> |
| 115 | // <div class="doc"> |
| 116 | // <span class="key">author</span> |
| 117 | // <span class="match">eq</span> |
| 118 | // <span class="value">Baum</span> |
| 119 | // </div> |
| 120 | // </div> |
Akron | 889ec29 | 2018-11-19 17:56:01 +0100 | [diff] [blame] | 121 | let root; |
| 122 | let l = this._operands.length; |
Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 123 | if (l > 1) { |
| 124 | |
| 125 | root = document.createElement('div'); |
| 126 | root.setAttribute('class','docGroup'); |
| 127 | root.setAttribute('data-operation', 'and'); |
| 128 | |
Akron | 889ec29 | 2018-11-19 17:56:01 +0100 | [diff] [blame] | 129 | for (let i in this._operands) { |
Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 130 | root.appendChild(_doc(this._operands[i])); |
| 131 | }; |
| 132 | } |
| 133 | else if (l == 1) { |
| 134 | root = _doc(this._operands[0]); |
| 135 | }; |
| 136 | |
Akron | d45a170 | 2018-11-19 18:15:17 +0100 | [diff] [blame] | 137 | this.element(); |
| 138 | const e = this._frag; |
Akron | 889ec29 | 2018-11-19 17:56:01 +0100 | [diff] [blame] | 139 | if (l === 0) { |
| 140 | _removeChildren(e); |
| 141 | } |
| 142 | else if (e.firstChild) |
Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 143 | e.replaceChild(root, e.firstChild); |
| 144 | else |
| 145 | e.appendChild(root); |
| 146 | |
| 147 | return this; |
Akron | fa32c9d | 2018-11-20 15:39:18 +0100 | [diff] [blame] | 148 | }, |
| 149 | |
| 150 | toQuery : function () { |
| 151 | if (this._operands.length === 0) |
| 152 | return ''; |
| 153 | |
| 154 | let str = '(' + this._operands.map( |
| 155 | function (item) { |
| 156 | return item[0] + ' = "' + new String(item[1]).quote() + '"'; |
| 157 | } |
| 158 | ).join(" & "); |
| 159 | |
| 160 | return str + ')'; |
| 161 | } |
Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 162 | } |
| 163 | }); |