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 | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 9 | "use strict"; |
Akron | 2761d88 | 2020-10-13 10:35:09 +0200 | [diff] [blame] | 10 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 11 | define(['vc/doc', 'util'], function (docClass) { |
Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 12 | |
Akron | d45a170 | 2018-11-19 18:15:17 +0100 | [diff] [blame] | 13 | const loc = KorAP.Locale; |
| 14 | loc.NEW_CONSTRAINT = loc.NEW_CONSTRAINT || 'New Constraint'; |
| 15 | |
Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 16 | // Create a VC doc |
| 17 | function _doc (op) { |
Akron | 2761d88 | 2020-10-13 10:35:09 +0200 | [diff] [blame] | 18 | const doc = document.createElement('div'); |
Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 19 | doc.setAttribute('class','doc'); |
| 20 | |
Akron | 2761d88 | 2020-10-13 10:35:09 +0200 | [diff] [blame] | 21 | const key = doc.addE('span'); |
Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 22 | key.setAttribute('class','key'); |
| 23 | key.addT(op[0]); |
| 24 | |
Akron | 2761d88 | 2020-10-13 10:35:09 +0200 | [diff] [blame] | 25 | const match = doc.addE('span'); |
Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 26 | match.setAttribute('class','match'); |
| 27 | match.addT('eq'); |
| 28 | |
Akron | 2761d88 | 2020-10-13 10:35:09 +0200 | [diff] [blame] | 29 | const value = doc.addE('span'); |
Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 30 | value.setAttribute('class', 'value'); |
| 31 | value.addT(op[1]); |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 32 | |
Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 33 | return doc; |
| 34 | }; |
| 35 | |
Akron | 2761d88 | 2020-10-13 10:35:09 +0200 | [diff] [blame] | 36 | |
Akron | 889ec29 | 2018-11-19 17:56:01 +0100 | [diff] [blame] | 37 | // Return object |
Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 38 | return { |
| 39 | |
Akron | 2761d88 | 2020-10-13 10:35:09 +0200 | [diff] [blame] | 40 | /** |
| 41 | * Construct a new VC fragment. |
| 42 | */ |
Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 43 | create : function () { |
Akron | 889ec29 | 2018-11-19 17:56:01 +0100 | [diff] [blame] | 44 | const obj = Object.create(this); |
Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 45 | obj._operands = []; |
| 46 | return obj; |
| 47 | }, |
| 48 | |
| 49 | |
| 50 | /** |
| 51 | * Add document constraint to fragment |
| 52 | */ |
| 53 | add : function (key, value, type) { |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 54 | this._operands.forEach(function (op, i, arr) { |
Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 55 | if (op[0] === key && op[1] === value) { |
Akron | 2761d88 | 2020-10-13 10:35:09 +0200 | [diff] [blame] | 56 | arr.splice(i,1); |
| 57 | } |
| 58 | }); |
Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 59 | this._operands.push([key, value, type]); |
| 60 | this.update(); |
| 61 | }, |
| 62 | |
| 63 | |
| 64 | /** |
| 65 | * Remove document constraint from fragment |
| 66 | */ |
| 67 | remove : function (key, value) { |
Akron | 2761d88 | 2020-10-13 10:35:09 +0200 | [diff] [blame] | 68 | for (let i = 0; i < this._operands.length; i++) { |
Akron | 889ec29 | 2018-11-19 17:56:01 +0100 | [diff] [blame] | 69 | let op = this._operands[i]; |
Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 70 | if (op[0] === key && op[1] === value) { |
| 71 | this._operands.splice(i, 1); |
| 72 | this.update(); |
| 73 | return; |
| 74 | }; |
| 75 | }; |
| 76 | return; |
| 77 | }, |
| 78 | |
Akron | cfe8ecc | 2018-11-20 18:46:16 +0100 | [diff] [blame] | 79 | |
Akron | 889ec29 | 2018-11-19 17:56:01 +0100 | [diff] [blame] | 80 | /** |
| 81 | * Check, if the fragment contains any constraints |
| 82 | */ |
| 83 | isEmpty : function () { |
| 84 | return this._operands.length > 0 ? false : true; |
| 85 | }, |
Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 86 | |
Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 87 | |
| 88 | /** |
| 89 | * Get the element associated with the virtual corpus |
| 90 | */ |
| 91 | element : function () { |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame^] | 92 | if (this._el !== undefined) { |
| 93 | return this._el; |
Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 94 | }; |
| 95 | |
Akron | d45a170 | 2018-11-19 18:15:17 +0100 | [diff] [blame] | 96 | // Initialize element |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame^] | 97 | const e = this._el = document.createElement('div'); |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 98 | e.classList.add('vc', 'fragment'); |
Akron | d45a170 | 2018-11-19 18:15:17 +0100 | [diff] [blame] | 99 | |
| 100 | // Prepend info text |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 101 | e.addE('span').addT(loc.NEW_CONSTRAINT + ':'); |
| 102 | this._frag = e.addE('div'); |
Akron | d45a170 | 2018-11-19 18:15:17 +0100 | [diff] [blame] | 103 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 104 | return e; |
Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 105 | }, |
| 106 | |
| 107 | |
| 108 | /** |
Akron | cfe8ecc | 2018-11-20 18:46:16 +0100 | [diff] [blame] | 109 | * Return operands as document objects |
| 110 | */ |
| 111 | documents : function () { |
| 112 | return this._operands.map( |
| 113 | function (item) { |
Akron | 2761d88 | 2020-10-13 10:35:09 +0200 | [diff] [blame] | 114 | const doc = docClass.create(); |
Akron | cfe8ecc | 2018-11-20 18:46:16 +0100 | [diff] [blame] | 115 | doc.key(item[0]); |
| 116 | doc.matchop("eq"); |
| 117 | doc.value(item[1]); |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 118 | doc.type(item[2] === "date" ? "date" : "string"); |
Akron | cfe8ecc | 2018-11-20 18:46:16 +0100 | [diff] [blame] | 119 | return doc; |
| 120 | } |
| 121 | ); |
| 122 | }, |
| 123 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 124 | |
Akron | cfe8ecc | 2018-11-20 18:46:16 +0100 | [diff] [blame] | 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; |
Akron | 2761d88 | 2020-10-13 10:35:09 +0200 | [diff] [blame] | 138 | const l = this._operands.length; |
Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 139 | |
Akron | 2761d88 | 2020-10-13 10:35:09 +0200 | [diff] [blame] | 140 | if (l > 1) { |
Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 141 | root = document.createElement('div'); |
| 142 | root.setAttribute('class','docGroup'); |
| 143 | root.setAttribute('data-operation', 'and'); |
Akron | 678c26f | 2020-10-09 08:52:50 +0200 | [diff] [blame] | 144 | this._operands.forEach(i => root.appendChild(_doc(i))); |
Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 145 | } |
Akron | 2761d88 | 2020-10-13 10:35:09 +0200 | [diff] [blame] | 146 | |
Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 147 | else if (l == 1) { |
| 148 | root = _doc(this._operands[0]); |
| 149 | }; |
| 150 | |
Akron | 2761d88 | 2020-10-13 10:35:09 +0200 | [diff] [blame] | 151 | // Init element |
Akron | d45a170 | 2018-11-19 18:15:17 +0100 | [diff] [blame] | 152 | this.element(); |
Akron | 2761d88 | 2020-10-13 10:35:09 +0200 | [diff] [blame] | 153 | |
Akron | d45a170 | 2018-11-19 18:15:17 +0100 | [diff] [blame] | 154 | const e = this._frag; |
Akron | 889ec29 | 2018-11-19 17:56:01 +0100 | [diff] [blame] | 155 | if (l === 0) { |
| 156 | _removeChildren(e); |
| 157 | } |
| 158 | else if (e.firstChild) |
Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 159 | e.replaceChild(root, e.firstChild); |
| 160 | else |
| 161 | e.appendChild(root); |
| 162 | |
| 163 | return this; |
Akron | fa32c9d | 2018-11-20 15:39:18 +0100 | [diff] [blame] | 164 | }, |
| 165 | |
Akron | cfe8ecc | 2018-11-20 18:46:16 +0100 | [diff] [blame] | 166 | |
| 167 | /** |
| 168 | * Stringification |
| 169 | */ |
Akron | fa32c9d | 2018-11-20 15:39:18 +0100 | [diff] [blame] | 170 | toQuery : function () { |
Akron | 2761d88 | 2020-10-13 10:35:09 +0200 | [diff] [blame] | 171 | |
Akron | fa32c9d | 2018-11-20 15:39:18 +0100 | [diff] [blame] | 172 | if (this._operands.length === 0) |
| 173 | return ''; |
| 174 | |
Akron | cfe8ecc | 2018-11-20 18:46:16 +0100 | [diff] [blame] | 175 | return this._operands.map( |
Akron | fa32c9d | 2018-11-20 15:39:18 +0100 | [diff] [blame] | 176 | function (item) { |
Akron | cfe8ecc | 2018-11-20 18:46:16 +0100 | [diff] [blame] | 177 | if (item[2] === "date") { |
| 178 | return item[0] + ' in ' + item[1]; |
| 179 | }; |
Akron | 0c4cd22 | 2019-07-19 16:33:34 +0200 | [diff] [blame] | 180 | return item[0] + ' = ' + new String(item[1]).quote(); |
Akron | fa32c9d | 2018-11-20 15:39:18 +0100 | [diff] [blame] | 181 | } |
| 182 | ).join(" & "); |
Akron | fa32c9d | 2018-11-20 15:39:18 +0100 | [diff] [blame] | 183 | } |
Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 184 | } |
| 185 | }); |