blob: fbdc118ffffd7ae170daded09bb1088ddc93883e [file] [log] [blame]
Nils Diewald3a2d8022014-12-16 02:45:41 +00001var KorAP = KorAP || {};
Nils Diewald3a2d8022014-12-16 02:45:41 +00002
Nils Diewald8f4e2542014-12-19 04:42:09 +00003// TODO: Implement a working localization solution!
4// TODO: Support 'update' method to update elements on change
Nils Diewald966abf12014-12-20 02:27:45 +00005// TODO: Implement "toQuery"
Nils Diewaldd0770492014-12-19 03:55:00 +00006
7/*
8 * Error codes:
9 701: "JSON-LD group has no @type attribute"
10 704: "Operation needs operand list"
Nils Diewald3a2d8022014-12-16 02:45:41 +000011 802: "Match type is not supported by value type"
12 804: "Unknown value type"
13 805: "Value is invalid"
14 806: "Value is not a valid date string"
15 807: "Value is not a valid regular expression"
Nils Diewald3a2d8022014-12-16 02:45:41 +000016 810: "Unknown document group operation" (like 711)
17 811: "Document group expects operation" (like 703)
18 812: "Operand not supported in document group" (like 744)
Nils Diewaldd0770492014-12-19 03:55:00 +000019 813: "Collection type is not supported" (like 713)
20*/
21
Nils Diewald3a2d8022014-12-16 02:45:41 +000022(function (KorAP) {
23 "use strict";
24
25 // Default log message
26 KorAP.log = KorAP.log || function (type, msg) {
27 console.log(type + ": " + msg);
28 };
29
30 KorAP._validStringMatchRE = new RegExp("^(?:eq|ne|contains)$");
31 KorAP._validRegexMatchRE = new RegExp("^(?:eq|ne)$");
Nils Diewaldd0770492014-12-19 03:55:00 +000032 KorAP._validDateMatchRE = new RegExp("^[lg]?eq$");
Nils Diewald3a2d8022014-12-16 02:45:41 +000033 KorAP._validDateRE = new RegExp("^(?:\\d{4})(?:-\\d\\d(?:-\\d\\d)?)?$");
34 KorAP._validGroupOpRE = new RegExp("^(?:and|or)$");
35
Nils Diewaldd0770492014-12-19 03:55:00 +000036 var loc = (KorAP.Locale = KorAP.Locale || {} );
37 loc.AND = loc.AND || 'and';
38 loc.OR = loc.OR || 'or';
39 loc.DEL = loc.DEL || '×';
40
Nils Diewald966abf12014-12-20 02:27:45 +000041 function _bool (bool) {
42 return (bool === undefined || bool === false) ? false : true;
43 };
44
45 function _removeChildren (node) {
46 // Remove everything underneath
47 while (node.firstChild) {
48 node.removeChild(node.firstChild);
49 };
50 };
51
Nils Diewald3a2d8022014-12-16 02:45:41 +000052 KorAP.VirtualCollection = {
Nils Diewaldd0770492014-12-19 03:55:00 +000053 _root : undefined,
Nils Diewald3a2d8022014-12-16 02:45:41 +000054 create : function () {
55 return Object.create(KorAP.VirtualCollection);
56 },
Nils Diewaldd0770492014-12-19 03:55:00 +000057 render : function (json) {
58 var obj = Object.create(KorAP.VirtualCollection);
59
60 if (json !== undefined) {
61 // Root object
62 if (json['@type'] == 'korap:doc') {
Nils Diewald8f4e2542014-12-19 04:42:09 +000063 obj._root = KorAP.Doc.create(undefined, json);
Nils Diewaldd0770492014-12-19 03:55:00 +000064 }
65 else if (json['@type'] == 'korap:docGroup') {
Nils Diewald8f4e2542014-12-19 04:42:09 +000066 obj._root = KorAP.DocGroup.create(undefined, json);
Nils Diewaldd0770492014-12-19 03:55:00 +000067 }
68 else {
69 KorAP.log(813, "Collection type is not supported");
70 return;
71 };
72 }
73
74 else {
75 // Add unspecified object
Nils Diewald8f4e2542014-12-19 04:42:09 +000076 obj._root = KorAP.UnspecifiedDoc.create();
Nils Diewaldd0770492014-12-19 03:55:00 +000077 };
78
79 // Add root element to root node
80 obj.element().appendChild(
81 obj._root.element()
82 );
83
84 return obj;
Nils Diewald3a2d8022014-12-16 02:45:41 +000085 },
Nils Diewaldd0770492014-12-19 03:55:00 +000086 root : function () {
87 return this._root;
Nils Diewald3a2d8022014-12-16 02:45:41 +000088 },
Nils Diewaldd0770492014-12-19 03:55:00 +000089 element : function () {
90 if (this._element !== undefined)
91 return this._element;
92
93 this._element = document.createElement('div');
94 this._element.setAttribute('class', 'vc');
95 return this._element;
Nils Diewald3a2d8022014-12-16 02:45:41 +000096 }
97 };
98
Nils Diewald0297ba12015-01-05 21:56:12 +000099 KorAP._or = function (e) {
100 var obj = this.parentNode.refTo;
101 };
102
103 KorAP._and = function (e) {
104 var obj = this.parentNode.refTo;
105 };
106
107 KorAP._delete = function (e) {
108 var obj = this.parentNode.refTo;
109 obj.parent().delOperand(obj);
110 // Todo: CLEAR ALL THE THINGS!
111 };
112
Nils Diewald8f4e2542014-12-19 04:42:09 +0000113 /**
114 * Operators for criteria
115 */
Nils Diewaldd0770492014-12-19 03:55:00 +0000116 KorAP.Operators = {
117 create : function (and, or, del) {
118 var op = Object.create(KorAP.Operators);
119 op.and(and);
120 op.or(or);
121 op.del(del);
122 return op;
123 },
124 update : function () {
125
126 // Init the element
127 if (this._element === undefined)
128 return this.element();
129
130 var op = this._element;
131
Nils Diewald0297ba12015-01-05 21:56:12 +0000132 op.refTo = this.parent();
133
Nils Diewaldd0770492014-12-19 03:55:00 +0000134 // Remove everything underneath
Nils Diewald966abf12014-12-20 02:27:45 +0000135 _removeChildren(op);
Nils Diewaldd0770492014-12-19 03:55:00 +0000136
137 // Add and button
138 if (this._and === true) {
139 var andE = document.createElement('span');
140 andE.setAttribute('class', 'and');
Nils Diewald0297ba12015-01-05 21:56:12 +0000141 andE.addEventListener('click', KorAP._and, false);
Nils Diewaldd0770492014-12-19 03:55:00 +0000142 andE.appendChild(document.createTextNode(KorAP.Locale.AND));
143 op.appendChild(andE);
144 };
145
146 // Add or button
147 if (this._or === true) {
148 var orE = document.createElement('span');
149 orE.setAttribute('class', 'or');
Nils Diewald0297ba12015-01-05 21:56:12 +0000150 orE.addEventListener('click', KorAP._or, false);
Nils Diewaldd0770492014-12-19 03:55:00 +0000151 orE.appendChild(document.createTextNode(KorAP.Locale.OR));
152 op.appendChild(orE);
153 };
154
155 // Add delete button
156 if (this._del === true) {
157 var delE = document.createElement('span');
158 delE.setAttribute('class', 'delete');
159 delE.appendChild(document.createTextNode(KorAP.Locale.DEL));
Nils Diewald0297ba12015-01-05 21:56:12 +0000160 delE.addEventListener('click', KorAP._delete, false);
Nils Diewaldd0770492014-12-19 03:55:00 +0000161 op.appendChild(delE);
162 };
Nils Diewald966abf12014-12-20 02:27:45 +0000163
164 return op;
Nils Diewaldd0770492014-12-19 03:55:00 +0000165 },
Nils Diewald0297ba12015-01-05 21:56:12 +0000166
167 // Be aware! This may be cyclic
168 parent : function (obj) {
169 if (arguments.length === 1)
170 this._parent = obj;
171 return this._parent;
172 },
Nils Diewaldd0770492014-12-19 03:55:00 +0000173 element : function () {
174
175 // Return existing element
176 if (this._element !== undefined)
177 return this._element;
178
179 this._element = document.createElement('div');
180 this._element.setAttribute('class', 'operators');
181
182 // Init elements
183 this.update();
184 return this._element;
185 },
186 and : function (bool) {
187 if (arguments.length === 1)
Nils Diewald966abf12014-12-20 02:27:45 +0000188 this._and = _bool(bool);
Nils Diewaldd0770492014-12-19 03:55:00 +0000189 return this._and;
190 },
191 or : function (bool) {
192 if (arguments.length === 1)
Nils Diewald966abf12014-12-20 02:27:45 +0000193 this._or = _bool(bool);
Nils Diewaldd0770492014-12-19 03:55:00 +0000194 return this._or;
195 },
196 del : function (bool) {
197 if (arguments.length === 1)
Nils Diewald966abf12014-12-20 02:27:45 +0000198 this._del = _bool(bool);
Nils Diewaldd0770492014-12-19 03:55:00 +0000199 return this._del;
200 }
201 };
202
203
204 /**
205 * Unspecified criterion
206 */
Nils Diewald8f4e2542014-12-19 04:42:09 +0000207 KorAP.UnspecifiedDoc = {
Nils Diewald966abf12014-12-20 02:27:45 +0000208 _ldType : "doc",
Nils Diewaldd0770492014-12-19 03:55:00 +0000209 create : function (parent) {
Nils Diewald8f4e2542014-12-19 04:42:09 +0000210 var obj = Object.create(KorAP.JsonLD).upgradeTo(KorAP.UnspecifiedDoc);
Nils Diewaldd0770492014-12-19 03:55:00 +0000211 if (parent !== undefined)
212 obj._parent = parent;
213 return obj;
214 },
Nils Diewald966abf12014-12-20 02:27:45 +0000215 update : function () {
216 if (this._element === undefined)
217 return this.element();
218
219 _removeChildren(this._element);
220
221 var ellipsis = document.createElement('span');
222 ellipsis.appendChild(document.createTextNode('⋯'));
223 this._element.appendChild(ellipsis);
224
225 // Set operators
226 var op = this.operators(
227 false,
228 false,
229 // No delete object, if this is the root
230 this._parent !== undefined ? true : false
231 );
232
233 this._element.appendChild(
234 op.element()
235 );
236
Nils Diewald966abf12014-12-20 02:27:45 +0000237 return this.element();
238 },
Nils Diewaldd0770492014-12-19 03:55:00 +0000239 element : function () {
240 if (this._element !== undefined)
241 return this._element;
Nils Diewald966abf12014-12-20 02:27:45 +0000242
Nils Diewaldd0770492014-12-19 03:55:00 +0000243 this._element = document.createElement('div');
Nils Diewald966abf12014-12-20 02:27:45 +0000244 this._element.setAttribute('class', 'unspecified');
245
246 this.update();
Nils Diewaldd0770492014-12-19 03:55:00 +0000247 return this._element;
248 }
249 };
250
251
Nils Diewald8f4e2542014-12-19 04:42:09 +0000252 /**
253 * Virtual collection doc criterion.
254 */
255 KorAP.Doc = {
256 _ldType : "doc",
257 _obj : function () { return KorAP.Doc; },
Nils Diewaldd0770492014-12-19 03:55:00 +0000258
259 create : function (parent, json) {
Nils Diewald8f4e2542014-12-19 04:42:09 +0000260 var obj = Object(KorAP.JsonLD).create().upgradeTo(KorAP.Doc).fromJson(json);
Nils Diewaldd0770492014-12-19 03:55:00 +0000261 if (parent !== undefined)
262 obj._parent = parent;
263 return obj;
264 },
Nils Diewald966abf12014-12-20 02:27:45 +0000265 update : function () {
266 if (this._element === undefined)
267 return this.element();
Nils Diewaldd0770492014-12-19 03:55:00 +0000268
269 // Added key
270 var key = document.createElement('span');
271 key.setAttribute('class', 'key');
272 if (this.key())
273 key.appendChild(document.createTextNode(this.key()));
274
275 // Added match operator
276 var matchop = document.createElement('span');
277 matchop.setAttribute('data-type', this.type());
278 matchop.setAttribute('class', 'match');
279 matchop.appendChild(document.createTextNode(this.matchop()));
280
281 // Added match operator
282 var value = document.createElement('span');
283 value.setAttribute('data-type', this.type());
284 value.setAttribute('class', 'value');
285 if (this.value())
286 value.appendChild(document.createTextNode(this.value()));
287
Nils Diewald966abf12014-12-20 02:27:45 +0000288 var e = this._element;
289
Nils Diewaldd0770492014-12-19 03:55:00 +0000290 // Add spans
291 e.appendChild(key);
292 e.appendChild(matchop);
293 e.appendChild(value);
Nils Diewald966abf12014-12-20 02:27:45 +0000294
295 // Set operators
296 var op = this.operators(true, true, true);
297
298 e.appendChild(op.element());
299
Nils Diewaldd0770492014-12-19 03:55:00 +0000300 return e;
301 },
302
Nils Diewald966abf12014-12-20 02:27:45 +0000303 element : function () {
304 if (this._element !== undefined)
305 return this._element;
306
307 this._element = document.createElement('div');
308 this._element.setAttribute('class', 'doc');
309
310 this.update();
311 return this._element;
312 },
313
Nils Diewaldd0770492014-12-19 03:55:00 +0000314 // Wrap a new operation around the doc element
315 wrap : function (op) {
Nils Diewald8f4e2542014-12-19 04:42:09 +0000316 var group = KorAP.DocGroup.create(undefined);
Nils Diewald966abf12014-12-20 02:27:45 +0000317 group.append(this);
318 group.append(null);
Nils Diewaldd0770492014-12-19 03:55:00 +0000319 this.parent(group);
320/*
321 var div = document.createElement('div');
322 div.setAttribute('data-operation', op);
323 var parent = this.element.parent;
324 parent.removeChild(this.element);
325 parent.appendChild(div);
326 div.appendChild(this.element);
327 return div;
328*/
Nils Diewaldd0770492014-12-19 03:55:00 +0000329 },
Nils Diewald3a2d8022014-12-16 02:45:41 +0000330 // Deserialize from json
331 fromJson : function (json) {
Nils Diewald3a2d8022014-12-16 02:45:41 +0000332 if (json === undefined)
Nils Diewaldd0770492014-12-19 03:55:00 +0000333 return this;
Nils Diewald3a2d8022014-12-16 02:45:41 +0000334
Nils Diewald3a2d8022014-12-16 02:45:41 +0000335 if (json["@type"] !== "korap:doc") {
336 KorAP.log(701, "JSON-LD group has no @type attribute");
337 return;
338 };
339
Nils Diewald3a2d8022014-12-16 02:45:41 +0000340 if (json["value"] === undefined ||
341 typeof json["value"] != 'string') {
342 KorAP.log(805, "Value is invalid");
343 return;
344 };
345
346 // There is a defined key
347 if (json["key"] !== undefined &&
348 typeof json["key"] === 'string') {
349
350 // Set key
Nils Diewaldd0770492014-12-19 03:55:00 +0000351 this.key(json["key"]);
Nils Diewald3a2d8022014-12-16 02:45:41 +0000352
353 // Set match operation
354 if (json["match"] !== undefined) {
Nils Diewaldd0770492014-12-19 03:55:00 +0000355 if (typeof json["match"] === 'string') {
356 this.matchop(json["match"]);
357 }
Nils Diewald3a2d8022014-12-16 02:45:41 +0000358 else {
359 KorAP.log(802, "Match type is not supported by value type");
360 return;
361 };
362 };
363
364 // Key is a string
365 if (json["type"] === undefined ||
366 json["type"] == "type:string") {
Nils Diewaldd0770492014-12-19 03:55:00 +0000367 this.type("string");
Nils Diewald3a2d8022014-12-16 02:45:41 +0000368
369 // Check match type
Nils Diewaldd0770492014-12-19 03:55:00 +0000370 if (!KorAP._validStringMatchRE.test(this.matchop())) {
Nils Diewald3a2d8022014-12-16 02:45:41 +0000371 KorAP.log(802, "Match type is not supported by value type");
372 return;
373 };
374
375 // Set string value
Nils Diewaldd0770492014-12-19 03:55:00 +0000376 this.value(json["value"]);
Nils Diewald3a2d8022014-12-16 02:45:41 +0000377 }
378
379 // Key is a date
380 else if (json["type"] === "type:date") {
Nils Diewaldd0770492014-12-19 03:55:00 +0000381 this.type("date");
Nils Diewald3a2d8022014-12-16 02:45:41 +0000382
383 if (json["value"] !== undefined &&
384 KorAP._validDateRE.test(json["value"])) {
385
Nils Diewaldd0770492014-12-19 03:55:00 +0000386 if (!KorAP._validDateMatchRE.test(this.matchop())) {
Nils Diewald3a2d8022014-12-16 02:45:41 +0000387 KorAP.log(802, "Match type is not supported by value type");
388 return;
389 };
390
391 // Set value
Nils Diewaldd0770492014-12-19 03:55:00 +0000392 this.value(json["value"]);
Nils Diewald3a2d8022014-12-16 02:45:41 +0000393 }
394 else {
395 KorAP.log(806, "Value is not a valid date string");
396 return;
397 };
398 }
399
400 // Key is a regular expression
401 else if (json["type"] === "type:regex") {
Nils Diewaldd0770492014-12-19 03:55:00 +0000402 this.type("regex");
Nils Diewald3a2d8022014-12-16 02:45:41 +0000403
404 try {
405
406 // Try to create a regular expression
407 var check = new RegExp(json["value"]);
408
Nils Diewaldd0770492014-12-19 03:55:00 +0000409 if (!KorAP._validRegexMatchRE.test(this.matchop())) {
Nils Diewald3a2d8022014-12-16 02:45:41 +0000410 KorAP.log(802, "Match type is not supported by value type");
411 return;
412 };
413
Nils Diewaldd0770492014-12-19 03:55:00 +0000414 this.value(json["value"]);
Nils Diewald3a2d8022014-12-16 02:45:41 +0000415 }
Nils Diewaldd0770492014-12-19 03:55:00 +0000416
Nils Diewald3a2d8022014-12-16 02:45:41 +0000417 catch (e) {
418 KorAP.log(807, "Value is not a valid regular expression");
419 return;
420 };
Nils Diewaldd0770492014-12-19 03:55:00 +0000421 this.type("regex");
Nils Diewald3a2d8022014-12-16 02:45:41 +0000422 }
423
424 else {
425 KorAP.log(804, "Unknown value type");
426 return;
427 };
428 };
429
430 return this;
431 },
Nils Diewaldd0770492014-12-19 03:55:00 +0000432 key : function (value) {
433 if (arguments.length === 1)
434 this._key = value;
Nils Diewald3a2d8022014-12-16 02:45:41 +0000435 return this._key;
436 },
Nils Diewaldd0770492014-12-19 03:55:00 +0000437 matchop : function (match) {
438 if (arguments.length === 1)
439 this._matchop = match.replace(/^match:/, '');
Nils Diewald3a2d8022014-12-16 02:45:41 +0000440 return this._matchop || "eq";
441 },
Nils Diewaldd0770492014-12-19 03:55:00 +0000442 type : function (type) {
443 if (arguments.length === 1)
444 this._type = type;
Nils Diewald3a2d8022014-12-16 02:45:41 +0000445 return this._type || "string";
446 },
Nils Diewaldd0770492014-12-19 03:55:00 +0000447 value : function (value) {
448 if (arguments.length === 1)
449 this._value = value;
Nils Diewald3a2d8022014-12-16 02:45:41 +0000450 return this._value;
451 },
Nils Diewald3a2d8022014-12-16 02:45:41 +0000452 toJson : function () {
Nils Diewaldd0770492014-12-19 03:55:00 +0000453 if (!this.matchop() || !this.key())
Nils Diewald3a2d8022014-12-16 02:45:41 +0000454 return {};
455
456 return {
Nils Diewaldd0770492014-12-19 03:55:00 +0000457 "@type" : "korap:" + this.ldType(),
458 "key" : this.key(),
459 "match" : "match:" + this.matchop(),
460 "value" : this.value() || '',
461 "type" : "type:" + this.type()
Nils Diewald3a2d8022014-12-16 02:45:41 +0000462 };
463 }
464 };
Nils Diewald8f4e2542014-12-19 04:42:09 +0000465
466 /**
467 * Virtual collection group criterion.
468 */
469 KorAP.DocGroup = {
470 _ldType : "docGroup",
471
472 create : function (parent, json) {
473 var obj = Object.create(KorAP.JsonLD).upgradeTo(KorAP.DocGroup);
474 obj._operands = [];
475 obj.fromJson(json);
476 if (parent !== undefined)
477 obj._parent = parent;
478 return obj;
479 },
Nils Diewald966abf12014-12-20 02:27:45 +0000480 append : function (operand) {
481
482 // Append unspecified object
483 if (operand === undefined) {
484
485 // Be aware of cyclic structures!
486 operand = KorAP.UnspecifiedDoc.create(this);
487 this._operands.push(operand);
488 this.update();
489 return operand;
490 };
491
Nils Diewald8f4e2542014-12-19 04:42:09 +0000492 switch (operand["@type"]) {
493 case undefined:
494 if (operand["ldType"] !== undefined) {
495 if (operand.ldType() !== 'doc' &&
Nils Diewald966abf12014-12-20 02:27:45 +0000496 operand.ldType() !== 'docGroup') {
Nils Diewald8f4e2542014-12-19 04:42:09 +0000497 KorAP.log(812, "Operand not supported in document group");
498 return;
499 };
Nils Diewald966abf12014-12-20 02:27:45 +0000500 // Be aware of cyclic structures!
501 operand.parent(this);
Nils Diewald8f4e2542014-12-19 04:42:09 +0000502 this._operands.push(operand);
Nils Diewald966abf12014-12-20 02:27:45 +0000503 this.update();
Nils Diewald8f4e2542014-12-19 04:42:09 +0000504 return operand;
505 };
506
507 KorAP.log(701, "JSON-LD group has no @type attribute");
508 return;
509
510 case "korap:doc":
Nils Diewald966abf12014-12-20 02:27:45 +0000511 // Be aware of cyclic structures!
512 var doc = KorAP.Doc.create(this, operand);
Nils Diewald8f4e2542014-12-19 04:42:09 +0000513 if (doc === undefined)
514 return;
515 this._operands.push(doc);
Nils Diewald966abf12014-12-20 02:27:45 +0000516 this.update();
Nils Diewald8f4e2542014-12-19 04:42:09 +0000517 return doc;
518
519 case "korap:docGroup":
Nils Diewald966abf12014-12-20 02:27:45 +0000520 // Be aware of cyclic structures!
521 var docGroup = KorAP.DocGroup.create(this, operand);
Nils Diewald8f4e2542014-12-19 04:42:09 +0000522 if (docGroup === undefined)
523 return;
524 this._operands.push(docGroup);
Nils Diewald966abf12014-12-20 02:27:45 +0000525 this.update();
Nils Diewald8f4e2542014-12-19 04:42:09 +0000526 return docGroup;
527
528 default:
529 KorAP.log(812, "Operand not supported in document group");
530 return;
531 };
532 },
Nils Diewald966abf12014-12-20 02:27:45 +0000533 update : function () {
534 if (this._element === undefined)
535 return this.element();
536
537 var op = this._element;
538 op.setAttribute('data-operation', this.operation());
539
540 _removeChildren(op);
541
542 // Append operands
543 for (var i in this.operands()) {
544 op.appendChild(
545 this.getOperand(i).element()
546 );
547 };
548
549 // Set operators
550 var op = this.operators(
551 this.operation() == 'and' ? false : true,
552 this.operation() == 'or' ? false : true,
553 true
554 );
555
556 this._element.appendChild(
557 op.element()
558 );
559
560 return op;
561 },
Nils Diewald8f4e2542014-12-19 04:42:09 +0000562 element : function () {
563 if (this._element !== undefined)
564 return this._element;
565
566 this._element = document.createElement('div');
567 this._element.setAttribute('class', 'docGroup');
Nils Diewald8f4e2542014-12-19 04:42:09 +0000568
Nils Diewald966abf12014-12-20 02:27:45 +0000569 this.update();
Nils Diewald8f4e2542014-12-19 04:42:09 +0000570
571 return this._element;
572 },
573 operation : function (op) {
574 if (arguments.length === 1) {
575 if (KorAP._validGroupOpRE.test(op)) {
576 this._op = op;
577 }
578 else {
579 KorAP.log(810, "Unknown operation type");
580 return;
581 };
582 };
583 return this._op || 'and';
584 },
585 operands : function () {
586 return this._operands;
587 },
588 getOperand : function (index) {
589 return this._operands[index];
590 },
591
Nils Diewald0297ba12015-01-05 21:56:12 +0000592 // Delete operand from group
593 delOperand : function (obj) {
594 for (var i in this._operands) {
595 if (this._operands[i] === obj) {
596 this._operands.splice(i,1);
597
598 // Todo: Update has to check
599 // that this may mean the group is empty etc.
600 this.update();
601 return true;
602 };
603 };
604 return false;
605 },
606
Nils Diewald8f4e2542014-12-19 04:42:09 +0000607 // Deserialize from json
608 fromJson : function (json) {
609 if (json === undefined)
610 return this;
611
612 if (json["@type"] !== "korap:docGroup") {
613 KorAP.log(701, "JSON-LD group has no @type attribute");
614 return;
615 };
616
617 if (json["operation"] === undefined ||
618 typeof json["operation"] !== 'string') {
619 KorAP.log(811, "Document group expects operation");
620 return;
621 };
622
623 var operation = json["operation"];
624
625 this.operation(operation.replace(/^operation:/,''));
626
627 if (json["operands"] === undefined ||
628 !(json["operands"] instanceof Array)) {
629 KorAP.log(704, "Operation needs operand list")
630 return;
631 };
632
633 // Add all documents
634 for (var i in json["operands"]) {
635 var operand = json["operands"][i];
Nils Diewald966abf12014-12-20 02:27:45 +0000636 this.append(operand);
Nils Diewald8f4e2542014-12-19 04:42:09 +0000637 };
638
639 return this;
640 },
641 toJson : function () {
642 var opArray = new Array();
643 for (var i in this._operands) {
644 opArray.push(this._operands[i].toJson());
645 };
646 return {
647 "@type" : "korap:" + this.ldType(),
648 "operation" : "operation:" + this.operation(),
649 "operands" : opArray
650 };
651 }
652 };
653
654
655 // Abstract JsonLD object
656 KorAP.JsonLD = {
657 create : function () {
658 return Object.create(KorAP.JsonLD);
659 },
660
661 /**
662 * Upgrade this object to another object
663 * while private data stays intact
664 */
665 upgradeTo : function (props) {
666 for (var prop in props) {
667 this[prop] = props[prop];
668 };
669 return this;
670 },
671 ldType : function (type) {
672 if (arguments.length === 1)
673 this._ldType = type;
674 return this._ldType;
675 },
676 parent : function (obj) {
677 if (arguments.length === 1)
678 this._parent = obj;
679 return this._parent;
Nils Diewald966abf12014-12-20 02:27:45 +0000680 },
Nils Diewald0297ba12015-01-05 21:56:12 +0000681
682 // Be aware! This may be cyclic
Nils Diewald966abf12014-12-20 02:27:45 +0000683 operators : function (and, or, del) {
684 if (arguments === 0)
685 return this._ops;
686 this._ops = KorAP.Operators.create(
687 and, or, del
688 );
Nils Diewald0297ba12015-01-05 21:56:12 +0000689 this._ops.parent(this);
Nils Diewald966abf12014-12-20 02:27:45 +0000690 return this._ops;
691 },
692 toJson : function () {
693 return {
694 // Unspecified object
695 "@type" : "korap:" + this.ldType()
696 };
Nils Diewald8f4e2542014-12-19 04:42:09 +0000697 }
698 };
699
Nils Diewald3a2d8022014-12-16 02:45:41 +0000700}(this.KorAP));