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