blob: af4919117891f41e06a914e3babc66fc05bc8c7a [file] [log] [blame]
Nils Diewald0e6992a2015-04-14 20:13:52 +00001/**
2 * Document group criterion
3 */
Nils Diewald4347ee92015-05-04 20:32:48 +00004/*
5 * TODO: Let the UPDATE event bubble up through parents!
6 */
Nils Diewald0e6992a2015-04-14 20:13:52 +00007define([
8 'vc/jsonld',
9 'vc/unspecified',
10 'vc/doc',
Akronb19803c2018-08-16 16:39:42 +020011 'vc/docgroupref',
Nils Diewald0e6992a2015-04-14 20:13:52 +000012 'util'
13], function (jsonldClass,
Akronb19803c2018-08-16 16:39:42 +020014 unspecClass,
15 docClass,
Akronadab5e52018-08-20 13:50:53 +020016 docGroupRefClass) {
Nils Diewald0e6992a2015-04-14 20:13:52 +000017
Akron0b489ad2018-02-02 16:49:32 +010018 const _validGroupOpRE = new RegExp("^(?:and|or)$");
Nils Diewald0e6992a2015-04-14 20:13:52 +000019
Akron0b489ad2018-02-02 16:49:32 +010020 const docGroupClass = {
Nils Diewald0e6992a2015-04-14 20:13:52 +000021 _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)
Akron0b489ad2018-02-02 16:49:32 +010028 obj._parent = parent;
Nils Diewald0e6992a2015-04-14 20:13:52 +000029 return obj;
30 },
31
32 newAfter : function (obj) {
33 for (var i = 0; i < this._operands.length; i++) {
Akron0b489ad2018-02-02 16:49:32 +010034 if (this._operands[i] === obj) {
35 var operand = unspecClass.create(this);
36 this._operands.splice(i + 1, 0, operand);
37 return this.update();
38 };
Nils Diewald0e6992a2015-04-14 20:13:52 +000039 };
40 },
41
42 // The doc is already set in the group
43 _duplicate : function (operand) {
Akronb19803c2018-08-16 16:39:42 +020044
45 // TODO:
46 // Also check for duplicate docGroupRefs!
47
Nils Diewald0e6992a2015-04-14 20:13:52 +000048 if (operand.ldType() !== 'doc')
Akron0b489ad2018-02-02 16:49:32 +010049 return null;
Nils Diewald0e6992a2015-04-14 20:13:52 +000050
51 for (var i = 0; i < this._operands.length; i++) {
Akron0b489ad2018-02-02 16:49:32 +010052 var op = this.getOperand(i);
53 if (op.ldType() === 'doc'
54 && operand.key() === op.key()
55 && operand.matchop() === op.matchop()
56 && operand.value() === op.value()) {
57 return op;
58 };
Nils Diewald0e6992a2015-04-14 20:13:52 +000059 };
60 return null;
61 },
62
63 append : function (operand) {
64
65 // Append unspecified object
66 if (operand === undefined) {
67
Akron0b489ad2018-02-02 16:49:32 +010068 // Be aware of cyclic structures!
69 operand = unspecClass.create(this);
70 this._operands.push(operand);
71 return operand;
Nils Diewald0e6992a2015-04-14 20:13:52 +000072 };
73
74 switch (operand["@type"]) {
Akron0b489ad2018-02-02 16:49:32 +010075
Nils Diewald0e6992a2015-04-14 20:13:52 +000076 case undefined:
Akron0b489ad2018-02-02 16:49:32 +010077 // No @type defined
78 if (operand["ldType"] !== undefined) {
79 if (operand.ldType() !== 'doc' &&
Akronb19803c2018-08-16 16:39:42 +020080 operand.ldType() !== 'docGroup' &&
81 operand.ldType() !== 'docGroupRef') {
Akron0b489ad2018-02-02 16:49:32 +010082 KorAP.log(812, "Operand not supported in document group");
83 return;
84 };
Nils Diewald0e6992a2015-04-14 20:13:52 +000085
Akron0b489ad2018-02-02 16:49:32 +010086 // Be aware of cyclic structures!
87 operand.parent(this);
Nils Diewald0e6992a2015-04-14 20:13:52 +000088
Akron0b489ad2018-02-02 16:49:32 +010089 var dupl = this._duplicate(operand);
90 if (dupl === null) {
91 this._operands.push(operand);
92 return operand;
93 };
94 return dupl;
95 };
Nils Diewald0e6992a2015-04-14 20:13:52 +000096
Akron0b489ad2018-02-02 16:49:32 +010097 KorAP.log(701, "JSON-LD group has no @type attribute");
98 return;
Nils Diewald0e6992a2015-04-14 20:13:52 +000099
100 case "koral:doc":
Akron0b489ad2018-02-02 16:49:32 +0100101 // Be aware of cyclic structures!
102 var doc = docClass.create(this, operand);
103 if (doc === undefined)
104 return;
105 var dupl = this._duplicate(doc);
106 if (dupl === null) {
107 this._operands.push(doc);
108 return doc;
109 };
110 return dupl;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000111
112 case "koral:docGroup":
Akron0b489ad2018-02-02 16:49:32 +0100113 // Be aware of cyclic structures!
114 var docGroup = docGroupClass.create(this, operand);
115 if (docGroup === undefined)
116 return;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000117
Akron0b489ad2018-02-02 16:49:32 +0100118 // Flatten group
119 if (docGroup.operation() === this.operation()) {
Akron678c26f2020-10-09 08:52:50 +0200120 docGroup.operands().forEach(function(op) {
Akron0b489ad2018-02-02 16:49:32 +0100121 var dupl = this._duplicate(op);
122 if (dupl === null) {
123 this._operands.push(op);
124 op.parent(this);
125 };
Akron678c26f2020-10-09 08:52:50 +0200126 }, this);
Akron0b489ad2018-02-02 16:49:32 +0100127 docGroup._operands = [];
128 docGroup.destroy();
129 return this;
130 };
131 this._operands.push(docGroup);
132 return docGroup;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000133
Akronb19803c2018-08-16 16:39:42 +0200134 case "koral:docGroupRef":
135
136 var docGroupRef = docGroupRefClass.create(this, operand);
137
138 if (docGroupRef === undefined) {
139 return
140 };
141
142 // TODO:
143 // Currently this doesn't do anything meaningful,
144 // as duplicate only checks on docs for the moment
145 /*
146 var dupl = this._duplicate(doc);
147 if (dupl === null) {
148 this._operands.push(doc);
149 return doc;
150 };
151 return dupl;
152 */
153 this._operands.push(docGroupRef);
154 return docGroupRef;
155
Nils Diewald0e6992a2015-04-14 20:13:52 +0000156 default:
Akron0b489ad2018-02-02 16:49:32 +0100157 KorAP.log(812, "Operand not supported in document group");
158 return;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000159 };
160 },
161
162 update : function () {
163 // There is only one operand in group
164
165 if (this._operands.length === 1) {
Akron0b489ad2018-02-02 16:49:32 +0100166
167 var parent = this.parent();
168 var op = this.getOperand(0);
169
170 // This will prevent destruction of
171 // the operand
172 this._operands = [];
Nils Diewald0e6992a2015-04-14 20:13:52 +0000173
Akron0b489ad2018-02-02 16:49:32 +0100174 // Parent is a group
175 if (parent.ldType() !== null)
176 return parent.replaceOperand(this, op).update();
Nils Diewald0e6992a2015-04-14 20:13:52 +0000177
Akron0b489ad2018-02-02 16:49:32 +0100178 // Parent is vc
179 else {
180 this.destroy();
181 // Cyclic madness
182 parent.root(op);
183 op.parent(parent);
184 return parent.root();
185 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000186 };
187
188 if (this._element === undefined)
Akron0b489ad2018-02-02 16:49:32 +0100189 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000190
191 var group = this._element;
192 group.setAttribute('data-operation', this.operation());
193
194 _removeChildren(group);
195
196 // Append operands
197 for (var i = 0; i < this._operands.length; i++) {
Akron0b489ad2018-02-02 16:49:32 +0100198 group.appendChild(
199 this.getOperand(i).element()
200 );
Nils Diewald0e6992a2015-04-14 20:13:52 +0000201 };
202
203 // Set operators
204 var op = this.operators(
Akron0b489ad2018-02-02 16:49:32 +0100205 this.operation() == 'and' ? false : true,
206 this.operation() == 'or' ? false : true,
207 true
Nils Diewald0e6992a2015-04-14 20:13:52 +0000208 );
209
210 group.appendChild(op.element());
211
Akron13af2f42019-07-25 15:06:21 +0200212 if (KorAP.vc) {
213 var vcchevent = new CustomEvent('vcChange', {'detail':this});
214 KorAP.vc.element().dispatchEvent(vcchevent);
215 };
hebastade595b42019-02-05 13:53:10 +0100216
Nils Diewald0e6992a2015-04-14 20:13:52 +0000217 return this;
218 },
219
220 element : function () {
221 if (this._element !== undefined)
Akron0b489ad2018-02-02 16:49:32 +0100222 return this._element;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000223
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) {
Akron0b489ad2018-02-02 16:49:32 +0100235 if (_validGroupOpRE.test(op)) {
236 this._op = op;
237 }
238 else {
239 KorAP.log(810, "Unknown operation type");
240 return;
241 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000242 };
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++) {
Akron0b489ad2018-02-02 16:49:32 +0100258 if (this._operands[i] === oldOp) {
259
260 // Just insert a doc or ...
261 if (newOp.ldType() === "doc" ||
262 newOp.ldType() === "non" ||
Akronb19803c2018-08-16 16:39:42 +0200263 newOp.ldType() === 'docGroupRef' ||
Akron0b489ad2018-02-02 16:49:32 +0100264 // ... 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 Diewald0e6992a2015-04-14 20:13:52 +0000270
Akron0b489ad2018-02-02 16:49:32 +0100271 // Flatten group
272 else {
273 // Remove old group
274 this._operands.splice(i, 1);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000275
Akron0b489ad2018-02-02 16:49:32 +0100276 // Inject new operands
Akron678c26f2020-10-09 08:52:50 +0200277 newOp.operands().reverse().forEach(function(op) {
Akron0b489ad2018-02-02 16:49:32 +0100278 this._operands.splice(i, 0, op);
279 op.parent(this);
Akron678c26f2020-10-09 08:52:50 +0200280 }, this);
Akron0b489ad2018-02-02 16:49:32 +0100281 // Prevent destruction of operands
282 newOp._operands = [];
283 newOp.destroy();
284 };
285 oldOp.destroy();
286 return this;
287 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000288 };
289 return false;
290 },
291
292 // Delete operand from group
293 delOperand : function (obj) {
294 for (var i = 0; i < this._operands.length; i++) {
Akron0b489ad2018-02-02 16:49:32 +0100295 if (this._operands[i] === obj) {
296
297 // Delete identified operand
298 this._operands.splice(i,1);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000299
Akron0b489ad2018-02-02 16:49:32 +0100300 // Destroy object for cyclic references
301 obj.destroy();
Nils Diewald0e6992a2015-04-14 20:13:52 +0000302
Akron0b489ad2018-02-02 16:49:32 +0100303 return this;
304 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000305 };
306
307 // Operand not found
308 return undefined;
309 },
310
311 // Deserialize from json
312 fromJson : function (json) {
313 if (json === undefined)
Akron0b489ad2018-02-02 16:49:32 +0100314 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000315
316 if (json["@type"] === undefined) {
Akron0b489ad2018-02-02 16:49:32 +0100317 KorAP.log(701, "JSON-LD group has no @type attribute");
318 return;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000319 };
320
321 if (json["operation"] === undefined ||
Akron0b489ad2018-02-02 16:49:32 +0100322 typeof json["operation"] !== 'string') {
323 KorAP.log(811, "Document group expects operation");
324 return;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000325 };
326
327 var operation = json["operation"];
328
329 this.operation(operation.replace(/^operation:/,''));
330
331 if (json["operands"] === undefined ||
Akron0b489ad2018-02-02 16:49:32 +0100332 !(json["operands"] instanceof Array)) {
333 KorAP.log(704, "Operation needs operand list")
334 return;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000335 };
336
337 // Add all documents
Akron678c26f2020-10-09 08:52:50 +0200338 json["operands"].forEach(i => this.append(i));
Nils Diewald0e6992a2015-04-14 20:13:52 +0000339
340 return this;
341 },
342
343 toJson : function () {
344 var opArray = new Array();
345 for (var i = 0; i < this._operands.length; i++) {
Akron0b489ad2018-02-02 16:49:32 +0100346 if (this._operands[i].ldType() !== 'non')
347 opArray.push(this._operands[i].toJson());
Nils Diewald0e6992a2015-04-14 20:13:52 +0000348 };
349 return {
Akron0b489ad2018-02-02 16:49:32 +0100350 "@type" : "koral:" + this.ldType(),
351 "operation" : "operation:" + this.operation(),
352 "operands" : opArray
Nils Diewald0e6992a2015-04-14 20:13:52 +0000353 };
354 },
355
356 toQuery : function (brackets) {
357 var list = this._operands
Akron0b489ad2018-02-02 16:49:32 +0100358 .filter(function (op) {
hebastaa0282be2018-12-05 16:58:00 +0100359 return !op.incomplete();
Akron0b489ad2018-02-02 16:49:32 +0100360 })
361 .map(function (op) {
362 return (op.ldType() === 'docGroup') ?
363 op.toQuery(true) :
364 op.toQuery();
365 });
Nils Diewald0e6992a2015-04-14 20:13:52 +0000366
367 if (list.length === 1)
Akron0b489ad2018-02-02 16:49:32 +0100368 return list.join('');
Nils Diewald0e6992a2015-04-14 20:13:52 +0000369 else {
Akron0b489ad2018-02-02 16:49:32 +0100370 var str = list.join(this.operation() === 'or' ? ' | ' : ' & ');
371 return brackets ? '(' + str + ')' : str;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000372 };
373 }
374 };
375 return docGroupClass;
376});