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 | |
Akron | cfe8ecc | 2018-11-20 18:46:16 +0100 | [diff] [blame] | 9 | define(['vc/doc', 'util'], function (docClass) { |
Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 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 | cfe8ecc | 2018-11-20 18:46:16 +0100 | [diff] [blame] | 75 | |
Akron | 889ec29 | 2018-11-19 17:56:01 +0100 | [diff] [blame] | 76 | /** |
| 77 | * Check, if the fragment contains any constraints |
| 78 | */ |
| 79 | isEmpty : function () { |
| 80 | return this._operands.length > 0 ? false : true; |
| 81 | }, |
Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 82 | |
Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 83 | |
| 84 | /** |
| 85 | * Get the element associated with the virtual corpus |
| 86 | */ |
| 87 | element : function () { |
| 88 | if (this._element !== undefined) { |
| 89 | return this._element; |
| 90 | }; |
| 91 | |
Akron | d45a170 | 2018-11-19 18:15:17 +0100 | [diff] [blame] | 92 | // Initialize element |
Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 93 | this._element = document.createElement('div'); |
| 94 | this._element.classList.add('vc', 'fragment'); |
Akron | d45a170 | 2018-11-19 18:15:17 +0100 | [diff] [blame] | 95 | |
| 96 | // Prepend info text |
| 97 | this._element.addE('span').addT(loc.NEW_CONSTRAINT + ':'); |
| 98 | this._frag = this._element.addE('div'); |
| 99 | |
Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 100 | return this._element; |
| 101 | }, |
| 102 | |
| 103 | |
| 104 | /** |
Akron | cfe8ecc | 2018-11-20 18:46:16 +0100 | [diff] [blame] | 105 | * Return operands as document objects |
| 106 | */ |
| 107 | documents : function () { |
| 108 | return this._operands.map( |
| 109 | function (item) { |
| 110 | let doc = docClass.create(); |
| 111 | doc.key(item[0]); |
| 112 | doc.matchop("eq"); |
| 113 | doc.value(item[1]); |
| 114 | if (item[2] === "date") { |
| 115 | doc.type("date"); |
| 116 | } |
| 117 | else { |
| 118 | doc.type("string"); |
| 119 | }; |
| 120 | return doc; |
| 121 | } |
| 122 | ); |
| 123 | }, |
| 124 | |
| 125 | /** |
Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 126 | * Update the whole object based on the underlying data structure |
| 127 | */ |
| 128 | update : function() { |
| 129 | |
| 130 | // <div class="docGroup" data-operation="and"> |
| 131 | // <div class="doc"> |
| 132 | // <span class="key">author</span> |
| 133 | // <span class="match">eq</span> |
| 134 | // <span class="value">Baum</span> |
| 135 | // </div> |
| 136 | // </div> |
Akron | 889ec29 | 2018-11-19 17:56:01 +0100 | [diff] [blame] | 137 | let root; |
| 138 | let l = this._operands.length; |
Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 139 | if (l > 1) { |
| 140 | |
| 141 | root = document.createElement('div'); |
| 142 | root.setAttribute('class','docGroup'); |
| 143 | root.setAttribute('data-operation', 'and'); |
| 144 | |
Akron | 678c26f | 2020-10-09 08:52:50 +0200 | [diff] [blame] | 145 | this._operands.forEach(i => root.appendChild(_doc(i))); |
Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 146 | } |
| 147 | else if (l == 1) { |
| 148 | root = _doc(this._operands[0]); |
| 149 | }; |
| 150 | |
Akron | d45a170 | 2018-11-19 18:15:17 +0100 | [diff] [blame] | 151 | this.element(); |
| 152 | const e = this._frag; |
Akron | 889ec29 | 2018-11-19 17:56:01 +0100 | [diff] [blame] | 153 | if (l === 0) { |
| 154 | _removeChildren(e); |
| 155 | } |
| 156 | else if (e.firstChild) |
Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 157 | e.replaceChild(root, e.firstChild); |
| 158 | else |
| 159 | e.appendChild(root); |
| 160 | |
| 161 | return this; |
Akron | fa32c9d | 2018-11-20 15:39:18 +0100 | [diff] [blame] | 162 | }, |
| 163 | |
Akron | cfe8ecc | 2018-11-20 18:46:16 +0100 | [diff] [blame] | 164 | |
| 165 | /** |
| 166 | * Stringification |
| 167 | */ |
Akron | fa32c9d | 2018-11-20 15:39:18 +0100 | [diff] [blame] | 168 | toQuery : function () { |
| 169 | if (this._operands.length === 0) |
| 170 | return ''; |
| 171 | |
Akron | cfe8ecc | 2018-11-20 18:46:16 +0100 | [diff] [blame] | 172 | return this._operands.map( |
Akron | fa32c9d | 2018-11-20 15:39:18 +0100 | [diff] [blame] | 173 | function (item) { |
Akron | cfe8ecc | 2018-11-20 18:46:16 +0100 | [diff] [blame] | 174 | if (item[2] === "date") { |
| 175 | return item[0] + ' in ' + item[1]; |
| 176 | }; |
Akron | 0c4cd22 | 2019-07-19 16:33:34 +0200 | [diff] [blame] | 177 | return item[0] + ' = ' + new String(item[1]).quote(); |
Akron | fa32c9d | 2018-11-20 15:39:18 +0100 | [diff] [blame] | 178 | } |
| 179 | ).join(" & "); |
Akron | fa32c9d | 2018-11-20 15:39:18 +0100 | [diff] [blame] | 180 | } |
Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 181 | } |
| 182 | }); |