Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 1 | /** |
| 2 | * Document group criterion |
| 3 | */ |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 4 | /* |
| 5 | * TODO: Let the UPDATE event bubble up through parents! |
| 6 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 7 | define([ |
| 8 | 'vc/jsonld', |
| 9 | 'vc/unspecified', |
| 10 | 'vc/doc', |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 11 | 'vc/docgroupref', |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 12 | 'util' |
| 13 | ], function (jsonldClass, |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 14 | unspecClass, |
| 15 | docClass, |
Akron | adab5e5 | 2018-08-20 13:50:53 +0200 | [diff] [blame] | 16 | docGroupRefClass) { |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 17 | |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 18 | const _validGroupOpRE = new RegExp("^(?:and|or)$"); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 19 | |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 20 | const docGroupClass = { |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 21 | _ldType : "docGroup", |
| 22 | |
| 23 | create : function (parent, json) { |
| 24 | var obj = Object.create(jsonldClass).upgradeTo(this); |
| 25 | obj._operands = []; |
| 26 | obj.fromJson(json); |
| 27 | if (parent !== undefined) |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 28 | obj._parent = parent; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 29 | return obj; |
| 30 | }, |
| 31 | |
| 32 | newAfter : function (obj) { |
Akron | b50964a | 2020-10-12 11:44:37 +0200 | [diff] [blame] | 33 | this._operands.forEach(function (op, i) { |
| 34 | if (op === obj) { |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 35 | var operand = unspecClass.create(this); |
| 36 | this._operands.splice(i + 1, 0, operand); |
| 37 | return this.update(); |
| 38 | }; |
Akron | b50964a | 2020-10-12 11:44:37 +0200 | [diff] [blame] | 39 | }, this); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 40 | }, |
| 41 | |
| 42 | // The doc is already set in the group |
| 43 | _duplicate : function (operand) { |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 44 | |
| 45 | // TODO: |
| 46 | // Also check for duplicate docGroupRefs! |
| 47 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 48 | if (operand.ldType() !== 'doc') |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 49 | return null; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 50 | |
Akron | b50964a | 2020-10-12 11:44:37 +0200 | [diff] [blame] | 51 | const f = this._operands.find( |
| 52 | op => |
| 53 | op.ldType() === 'doc' |
| 54 | && operand.key() === op.key() |
| 55 | && operand.matchop() === op.matchop() |
| 56 | && operand.value() === op.value() |
| 57 | ); |
| 58 | |
| 59 | if (f) |
| 60 | return f; |
| 61 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 62 | return null; |
| 63 | }, |
| 64 | |
| 65 | append : function (operand) { |
| 66 | |
| 67 | // Append unspecified object |
| 68 | if (operand === undefined) { |
| 69 | |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 70 | // Be aware of cyclic structures! |
| 71 | operand = unspecClass.create(this); |
| 72 | this._operands.push(operand); |
| 73 | return operand; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | switch (operand["@type"]) { |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 77 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 78 | case undefined: |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 79 | // No @type defined |
| 80 | if (operand["ldType"] !== undefined) { |
| 81 | if (operand.ldType() !== 'doc' && |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 82 | operand.ldType() !== 'docGroup' && |
| 83 | operand.ldType() !== 'docGroupRef') { |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 84 | KorAP.log(812, "Operand not supported in document group"); |
| 85 | return; |
| 86 | }; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 87 | |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 88 | // Be aware of cyclic structures! |
| 89 | operand.parent(this); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 90 | |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 91 | var dupl = this._duplicate(operand); |
| 92 | if (dupl === null) { |
| 93 | this._operands.push(operand); |
| 94 | return operand; |
| 95 | }; |
| 96 | return dupl; |
| 97 | }; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 98 | |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 99 | KorAP.log(701, "JSON-LD group has no @type attribute"); |
| 100 | return; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 101 | |
| 102 | case "koral:doc": |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 103 | // Be aware of cyclic structures! |
| 104 | var doc = docClass.create(this, operand); |
| 105 | if (doc === undefined) |
| 106 | return; |
| 107 | var dupl = this._duplicate(doc); |
| 108 | if (dupl === null) { |
| 109 | this._operands.push(doc); |
| 110 | return doc; |
| 111 | }; |
| 112 | return dupl; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 113 | |
| 114 | case "koral:docGroup": |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 115 | // Be aware of cyclic structures! |
| 116 | var docGroup = docGroupClass.create(this, operand); |
| 117 | if (docGroup === undefined) |
| 118 | return; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 119 | |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 120 | // Flatten group |
| 121 | if (docGroup.operation() === this.operation()) { |
Akron | 678c26f | 2020-10-09 08:52:50 +0200 | [diff] [blame] | 122 | docGroup.operands().forEach(function(op) { |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 123 | var dupl = this._duplicate(op); |
| 124 | if (dupl === null) { |
| 125 | this._operands.push(op); |
| 126 | op.parent(this); |
| 127 | }; |
Akron | 678c26f | 2020-10-09 08:52:50 +0200 | [diff] [blame] | 128 | }, this); |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 129 | docGroup._operands = []; |
| 130 | docGroup.destroy(); |
| 131 | return this; |
| 132 | }; |
| 133 | this._operands.push(docGroup); |
| 134 | return docGroup; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 135 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 136 | case "koral:docGroupRef": |
| 137 | |
| 138 | var docGroupRef = docGroupRefClass.create(this, operand); |
| 139 | |
| 140 | if (docGroupRef === undefined) { |
| 141 | return |
| 142 | }; |
| 143 | |
| 144 | // TODO: |
| 145 | // Currently this doesn't do anything meaningful, |
| 146 | // as duplicate only checks on docs for the moment |
| 147 | /* |
| 148 | var dupl = this._duplicate(doc); |
| 149 | if (dupl === null) { |
| 150 | this._operands.push(doc); |
| 151 | return doc; |
| 152 | }; |
| 153 | return dupl; |
| 154 | */ |
| 155 | this._operands.push(docGroupRef); |
| 156 | return docGroupRef; |
| 157 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 158 | default: |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 159 | KorAP.log(812, "Operand not supported in document group"); |
| 160 | return; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 161 | }; |
| 162 | }, |
| 163 | |
| 164 | update : function () { |
| 165 | // There is only one operand in group |
| 166 | |
| 167 | if (this._operands.length === 1) { |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 168 | |
| 169 | var parent = this.parent(); |
| 170 | var op = this.getOperand(0); |
| 171 | |
| 172 | // This will prevent destruction of |
| 173 | // the operand |
| 174 | this._operands = []; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 175 | |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 176 | // Parent is a group |
| 177 | if (parent.ldType() !== null) |
| 178 | return parent.replaceOperand(this, op).update(); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 179 | |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 180 | // Parent is vc |
| 181 | else { |
| 182 | this.destroy(); |
| 183 | // Cyclic madness |
| 184 | parent.root(op); |
| 185 | op.parent(parent); |
| 186 | return parent.root(); |
| 187 | }; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 188 | }; |
| 189 | |
| 190 | if (this._element === undefined) |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 191 | return this; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 192 | |
| 193 | var group = this._element; |
| 194 | group.setAttribute('data-operation', this.operation()); |
| 195 | |
| 196 | _removeChildren(group); |
| 197 | |
| 198 | // Append operands |
Akron | b50964a | 2020-10-12 11:44:37 +0200 | [diff] [blame] | 199 | this._operands.forEach( |
| 200 | op => group.appendChild(op.element()) |
| 201 | ); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 202 | |
| 203 | // Set operators |
| 204 | var op = this.operators( |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 205 | this.operation() == 'and' ? false : true, |
| 206 | this.operation() == 'or' ? false : true, |
| 207 | true |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 208 | ); |
| 209 | |
| 210 | group.appendChild(op.element()); |
| 211 | |
Akron | 13af2f4 | 2019-07-25 15:06:21 +0200 | [diff] [blame] | 212 | if (KorAP.vc) { |
| 213 | var vcchevent = new CustomEvent('vcChange', {'detail':this}); |
| 214 | KorAP.vc.element().dispatchEvent(vcchevent); |
| 215 | }; |
hebasta | de595b4 | 2019-02-05 13:53:10 +0100 | [diff] [blame] | 216 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 217 | return this; |
| 218 | }, |
| 219 | |
| 220 | element : function () { |
| 221 | if (this._element !== undefined) |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 222 | return this._element; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 223 | |
| 224 | this._element = document.createElement('div'); |
| 225 | this._element.setAttribute('class', 'docGroup'); |
| 226 | |
| 227 | // Update the object - including optimization |
| 228 | this.update(); |
| 229 | |
| 230 | return this._element; |
| 231 | }, |
| 232 | |
| 233 | operation : function (op) { |
| 234 | if (arguments.length === 1) { |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 235 | if (_validGroupOpRE.test(op)) { |
| 236 | this._op = op; |
| 237 | } |
| 238 | else { |
| 239 | KorAP.log(810, "Unknown operation type"); |
| 240 | return; |
| 241 | }; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 242 | }; |
| 243 | return this._op || 'and'; |
| 244 | }, |
| 245 | |
| 246 | operands : function () { |
| 247 | return this._operands; |
| 248 | }, |
| 249 | |
| 250 | getOperand : function (index) { |
| 251 | return this._operands[index]; |
| 252 | }, |
| 253 | |
| 254 | // Replace operand |
| 255 | replaceOperand : function (oldOp, newOp) { |
| 256 | |
| 257 | for (var i = 0; i < this._operands.length; i++) { |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 258 | if (this._operands[i] === oldOp) { |
| 259 | |
| 260 | // Just insert a doc or ... |
| 261 | if (newOp.ldType() === "doc" || |
| 262 | newOp.ldType() === "non" || |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 263 | newOp.ldType() === 'docGroupRef' || |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 264 | // ... insert a group of a different operation |
| 265 | // (i.e. "and" in "or"/"or" in "and") |
| 266 | newOp.operation() != this.operation()) { |
| 267 | this._operands[i] = newOp; |
| 268 | newOp.parent(this); |
| 269 | } |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 270 | |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 271 | // Flatten group |
| 272 | else { |
| 273 | // Remove old group |
| 274 | this._operands.splice(i, 1); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 275 | |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 276 | // Inject new operands |
Akron | 678c26f | 2020-10-09 08:52:50 +0200 | [diff] [blame] | 277 | newOp.operands().reverse().forEach(function(op) { |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 278 | this._operands.splice(i, 0, op); |
| 279 | op.parent(this); |
Akron | 678c26f | 2020-10-09 08:52:50 +0200 | [diff] [blame] | 280 | }, this); |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 281 | // Prevent destruction of operands |
| 282 | newOp._operands = []; |
| 283 | newOp.destroy(); |
| 284 | }; |
| 285 | oldOp.destroy(); |
| 286 | return this; |
| 287 | } |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 288 | }; |
| 289 | return false; |
| 290 | }, |
| 291 | |
| 292 | // Delete operand from group |
| 293 | delOperand : function (obj) { |
| 294 | for (var i = 0; i < this._operands.length; i++) { |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 295 | if (this._operands[i] === obj) { |
| 296 | |
| 297 | // Delete identified operand |
| 298 | this._operands.splice(i,1); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 299 | |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 300 | // Destroy object for cyclic references |
| 301 | obj.destroy(); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 302 | |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 303 | return this; |
| 304 | }; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 305 | }; |
| 306 | |
| 307 | // Operand not found |
| 308 | return undefined; |
| 309 | }, |
| 310 | |
| 311 | // Deserialize from json |
| 312 | fromJson : function (json) { |
| 313 | if (json === undefined) |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 314 | return this; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 315 | |
| 316 | if (json["@type"] === undefined) { |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 317 | KorAP.log(701, "JSON-LD group has no @type attribute"); |
| 318 | return; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 319 | }; |
| 320 | |
| 321 | if (json["operation"] === undefined || |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 322 | typeof json["operation"] !== 'string') { |
| 323 | KorAP.log(811, "Document group expects operation"); |
| 324 | return; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 325 | }; |
| 326 | |
| 327 | var operation = json["operation"]; |
| 328 | |
| 329 | this.operation(operation.replace(/^operation:/,'')); |
| 330 | |
| 331 | if (json["operands"] === undefined || |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 332 | !(json["operands"] instanceof Array)) { |
| 333 | KorAP.log(704, "Operation needs operand list") |
| 334 | return; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 335 | }; |
| 336 | |
| 337 | // Add all documents |
Akron | 678c26f | 2020-10-09 08:52:50 +0200 | [diff] [blame] | 338 | json["operands"].forEach(i => this.append(i)); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 339 | |
| 340 | return this; |
| 341 | }, |
| 342 | |
| 343 | toJson : function () { |
| 344 | var opArray = new Array(); |
Akron | b50964a | 2020-10-12 11:44:37 +0200 | [diff] [blame] | 345 | this._operands.forEach(function(op) { |
| 346 | if (op.ldType() !== 'non') |
| 347 | opArray.push(op.toJson()); |
| 348 | }); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 349 | return { |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 350 | "@type" : "koral:" + this.ldType(), |
| 351 | "operation" : "operation:" + this.operation(), |
| 352 | "operands" : opArray |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 353 | }; |
| 354 | }, |
| 355 | |
| 356 | toQuery : function (brackets) { |
| 357 | var list = this._operands |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 358 | .filter(function (op) { |
hebasta | a0282be | 2018-12-05 16:58:00 +0100 | [diff] [blame] | 359 | return !op.incomplete(); |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 360 | }) |
| 361 | .map(function (op) { |
| 362 | return (op.ldType() === 'docGroup') ? |
| 363 | op.toQuery(true) : |
| 364 | op.toQuery(); |
| 365 | }); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 366 | |
| 367 | if (list.length === 1) |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 368 | return list.join(''); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 369 | else { |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 370 | var str = list.join(this.operation() === 'or' ? ' | ' : ' & '); |
| 371 | return brackets ? '(' + str + ')' : str; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 372 | }; |
| 373 | } |
| 374 | }; |
| 375 | return docGroupClass; |
| 376 | }); |