blob: b68f0aa8f2db6c3be319caafd6e2c67c1c9ee50e [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!
Nils Diewald52f7eb12015-01-12 17:30:04 +00004// TODO: Remove "and" or "or" in case it's followed
5// by an unspecified document
6// TODO: Add 'or' or 'and' on root
Nils Diewaldd0770492014-12-19 03:55:00 +00007
8/*
Nils Diewaldd599d542015-01-08 20:41:34 +00009 Error codes:
Nils Diewaldd0770492014-12-19 03:55:00 +000010 701: "JSON-LD group has no @type attribute"
11 704: "Operation needs operand list"
Nils Diewald3a2d8022014-12-16 02:45:41 +000012 802: "Match type is not supported by value type"
13 804: "Unknown value type"
14 805: "Value is invalid"
15 806: "Value is not a valid date string"
16 807: "Value is not a valid regular expression"
Nils Diewald3a2d8022014-12-16 02:45:41 +000017 810: "Unknown document group operation" (like 711)
18 811: "Document group expects operation" (like 703)
19 812: "Operand not supported in document group" (like 744)
Nils Diewaldd0770492014-12-19 03:55:00 +000020 813: "Collection type is not supported" (like 713)
21*/
22
Nils Diewald3a2d8022014-12-16 02:45:41 +000023(function (KorAP) {
24 "use strict";
25
26 // Default log message
27 KorAP.log = KorAP.log || function (type, msg) {
28 console.log(type + ": " + msg);
29 };
30
31 KorAP._validStringMatchRE = new RegExp("^(?:eq|ne|contains)$");
32 KorAP._validRegexMatchRE = new RegExp("^(?:eq|ne)$");
Nils Diewaldd0770492014-12-19 03:55:00 +000033 KorAP._validDateMatchRE = new RegExp("^[lg]?eq$");
Nils Diewald3a2d8022014-12-16 02:45:41 +000034 KorAP._validDateRE = new RegExp("^(?:\\d{4})(?:-\\d\\d(?:-\\d\\d)?)?$");
35 KorAP._validGroupOpRE = new RegExp("^(?:and|or)$");
Nils Diewaldf219eb82015-01-07 20:15:42 +000036 KorAP._quote = new RegExp("([\"\\\\])", 'g');
Nils Diewald3a2d8022014-12-16 02:45:41 +000037
Nils Diewalde15b7a22015-01-09 21:50:21 +000038 // Localization values
Nils Diewaldf219eb82015-01-07 20:15:42 +000039 var loc = (KorAP.Locale = KorAP.Locale || {} );
40 loc.AND = loc.AND || 'and';
41 loc.OR = loc.OR || 'or';
42 loc.DEL = loc.DEL || '×';
43 loc.EMPTY = loc.EMPTY || '⋯'
Nils Diewaldd0770492014-12-19 03:55:00 +000044
Nils Diewaldd599d542015-01-08 20:41:34 +000045
Nils Diewalde15b7a22015-01-09 21:50:21 +000046 // Utility for analysing boolean values
Nils Diewald966abf12014-12-20 02:27:45 +000047 function _bool (bool) {
Nils Diewalde15b7a22015-01-09 21:50:21 +000048 return (bool === undefined || bool === null || bool === false) ? false : true;
Nils Diewald966abf12014-12-20 02:27:45 +000049 };
50
Nils Diewaldd599d542015-01-08 20:41:34 +000051
Nils Diewalde15b7a22015-01-09 21:50:21 +000052 // Utility for removing all children of a node
Nils Diewald966abf12014-12-20 02:27:45 +000053 function _removeChildren (node) {
54 // Remove everything underneath
Nils Diewald8f6b6102015-01-08 18:25:33 +000055 while (node.firstChild)
Nils Diewald966abf12014-12-20 02:27:45 +000056 node.removeChild(node.firstChild);
Nils Diewald966abf12014-12-20 02:27:45 +000057 };
58
Nils Diewald4019bd22015-01-08 19:57:50 +000059
Nils Diewald52f7eb12015-01-12 17:30:04 +000060 // Add new unspecified document
Nils Diewald9fcc1712015-01-09 14:24:32 +000061 KorAP._add = function (obj, type) {
62 var ref = obj.parentNode.refTo;
Nils Diewaldfda29d92015-01-22 17:28:01 +000063 console.log("DEBUG: " + type + " on " + ref.toQuery());
Nils Diewaldd5070b02015-01-11 01:44:47 +000064 var parent = ref.parent();
Nils Diewald52f7eb12015-01-12 17:30:04 +000065
Nils Diewald9fcc1712015-01-09 14:24:32 +000066 if (ref.ldType() === 'docGroup') {
Nils Diewaldd5070b02015-01-11 01:44:47 +000067
68 // Check that the action differs from the type
69 if (ref.operation() === type)
70 return;
71
72 if (parent.ldType() !== null) {
73 return parent.newAfter(ref);
Nils Diewald9fcc1712015-01-09 14:24:32 +000074 }
75 else {
Nils Diewaldd5070b02015-01-11 01:44:47 +000076 // The group is on root - wrap
77 return ref.wrapOnRoot();
78 };
79 }
80 else if (ref.ldType() === 'doc') {
Nils Diewald52f7eb12015-01-12 17:30:04 +000081
82 if (parent.ldType() === null) {
83 return ref.wrapOnRoot(type);
84 }
85 else if (parent.operation() === type) {
Nils Diewaldd5070b02015-01-11 01:44:47 +000086 return parent.newAfter(ref);
87 }
88 else {
89 return ref.wrap(type);
Nils Diewald9fcc1712015-01-09 14:24:32 +000090 };
Nils Diewaldd599d542015-01-08 20:41:34 +000091 };
Nils Diewald4019bd22015-01-08 19:57:50 +000092 };
93
Nils Diewaldd599d542015-01-08 20:41:34 +000094
Nils Diewalde15b7a22015-01-09 21:50:21 +000095 // Add doc with 'and' relation
96 KorAP._and = function () {
97 return KorAP._add(this, 'and');
98 };
99
100
101 // Add doc with 'or' relation
102 KorAP._or = function () {
103 return KorAP._add(this, 'or');
104 };
105
Nils Diewald52f7eb12015-01-12 17:30:04 +0000106
Nils Diewald4019bd22015-01-08 19:57:50 +0000107 // Remove doc or docGroup
Nils Diewalde15b7a22015-01-09 21:50:21 +0000108 KorAP._delete = function () {
Nils Diewald9fcc1712015-01-09 14:24:32 +0000109 var ref = this.parentNode.refTo;
Nils Diewaldfda29d92015-01-22 17:28:01 +0000110console.log("DEBUG: delete " + ref.toQuery());
Nils Diewald52f7eb12015-01-12 17:30:04 +0000111 if (ref.parent().ldType() !== null) {
112 return ref.parent().delOperand(ref).update();
113 }
114 else {
Nils Diewald9fcc1712015-01-09 14:24:32 +0000115 ref.parent().clean();
Nils Diewald52f7eb12015-01-12 17:30:04 +0000116 };
Nils Diewald4019bd22015-01-08 19:57:50 +0000117 };
118
Nils Diewaldd599d542015-01-08 20:41:34 +0000119
120 /**
121 * Virtual Collection
122 */
Nils Diewald3a2d8022014-12-16 02:45:41 +0000123 KorAP.VirtualCollection = {
Nils Diewaldf219eb82015-01-07 20:15:42 +0000124 ldType : function () {
125 return null;
126 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000127
Nils Diewald3a2d8022014-12-16 02:45:41 +0000128 create : function () {
129 return Object.create(KorAP.VirtualCollection);
130 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000131
Nils Diewald4019bd22015-01-08 19:57:50 +0000132 clean : function () {
133 if (this._root.ldType() !== "non") {
134 this._root.destroy();
135 this.root(KorAP.UnspecifiedDoc.create(this));
136 };
137 return this;
138 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000139
Nils Diewaldd0770492014-12-19 03:55:00 +0000140 render : function (json) {
141 var obj = Object.create(KorAP.VirtualCollection);
142
143 if (json !== undefined) {
144 // Root object
145 if (json['@type'] == 'korap:doc') {
Nils Diewaldf219eb82015-01-07 20:15:42 +0000146 obj._root = KorAP.Doc.create(obj, json);
Nils Diewaldd0770492014-12-19 03:55:00 +0000147 }
148 else if (json['@type'] == 'korap:docGroup') {
Nils Diewaldf219eb82015-01-07 20:15:42 +0000149 obj._root = KorAP.DocGroup.create(obj, json);
Nils Diewaldd0770492014-12-19 03:55:00 +0000150 }
151 else {
152 KorAP.log(813, "Collection type is not supported");
153 return;
154 };
155 }
156
157 else {
158 // Add unspecified object
Nils Diewaldf219eb82015-01-07 20:15:42 +0000159 obj._root = KorAP.UnspecifiedDoc.create(obj);
Nils Diewaldd0770492014-12-19 03:55:00 +0000160 };
161
Nils Diewald8e7182e2015-01-08 15:02:07 +0000162 // Init element and update
163 obj.update();
Nils Diewaldd0770492014-12-19 03:55:00 +0000164
165 return obj;
Nils Diewald3a2d8022014-12-16 02:45:41 +0000166 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000167
Nils Diewaldf219eb82015-01-07 20:15:42 +0000168 root : function (obj) {
Nils Diewald8e7182e2015-01-08 15:02:07 +0000169 if (arguments.length === 1) {
Nils Diewald8f6b6102015-01-08 18:25:33 +0000170 var e = this.element();
171 if (e.firstChild !== null) {
Nils Diewaldd5070b02015-01-11 01:44:47 +0000172 if (e.firstChild !== obj.element()) {
Nils Diewald8f6b6102015-01-08 18:25:33 +0000173 e.replaceChild(obj.element(), e.firstChild);
Nils Diewaldd5070b02015-01-11 01:44:47 +0000174 };
Nils Diewald8f6b6102015-01-08 18:25:33 +0000175 }
176
177 // Append root element
178 else {
179 e.appendChild(obj.element());
180 };
181
182 // Update parent child relations
Nils Diewaldf219eb82015-01-07 20:15:42 +0000183 this._root = obj;
Nils Diewald8f6b6102015-01-08 18:25:33 +0000184 obj.parent(this);
185
Nils Diewald8e7182e2015-01-08 15:02:07 +0000186 this.update();
187 };
Nils Diewaldd0770492014-12-19 03:55:00 +0000188 return this._root;
Nils Diewald3a2d8022014-12-16 02:45:41 +0000189 },
Nils Diewald8f6b6102015-01-08 18:25:33 +0000190
Nils Diewaldd0770492014-12-19 03:55:00 +0000191 element : function () {
192 if (this._element !== undefined)
193 return this._element;
194
195 this._element = document.createElement('div');
196 this._element.setAttribute('class', 'vc');
Nils Diewald8e7182e2015-01-08 15:02:07 +0000197
Nils Diewald8f6b6102015-01-08 18:25:33 +0000198 // Initialize root
199 this._element.appendChild(this._root.element());
200
Nils Diewaldd0770492014-12-19 03:55:00 +0000201 return this._element;
Nils Diewaldf219eb82015-01-07 20:15:42 +0000202 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000203
204 update : function () {
205 this._root.update();
206 return this;
207 },
208
Nils Diewaldf219eb82015-01-07 20:15:42 +0000209 toJson : function () {
210 return this._root.toJson();
211 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000212
213 toQuery : function () {
214 return this._root.toQuery();
Nils Diewald3a2d8022014-12-16 02:45:41 +0000215 }
216 };
217
Nils Diewaldd599d542015-01-08 20:41:34 +0000218
Nils Diewald8f4e2542014-12-19 04:42:09 +0000219 /**
220 * Operators for criteria
221 */
Nils Diewaldd0770492014-12-19 03:55:00 +0000222 KorAP.Operators = {
223 create : function (and, or, del) {
224 var op = Object.create(KorAP.Operators);
225 op.and(and);
226 op.or(or);
227 op.del(del);
228 return op;
229 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000230
Nils Diewaldd0770492014-12-19 03:55:00 +0000231 update : function () {
Nils Diewaldd0770492014-12-19 03:55:00 +0000232 // Init the element
233 if (this._element === undefined)
234 return this.element();
235
236 var op = this._element;
237
Nils Diewald0297ba12015-01-05 21:56:12 +0000238 op.refTo = this.parent();
239
Nils Diewaldd0770492014-12-19 03:55:00 +0000240 // Remove everything underneath
Nils Diewald966abf12014-12-20 02:27:45 +0000241 _removeChildren(op);
Nils Diewaldd0770492014-12-19 03:55:00 +0000242
243 // Add and button
244 if (this._and === true) {
245 var andE = document.createElement('span');
246 andE.setAttribute('class', 'and');
Nils Diewalde15b7a22015-01-09 21:50:21 +0000247 andE.addEventListener('click', KorAP._and, false);
Nils Diewald8f6b6102015-01-08 18:25:33 +0000248 andE.appendChild(
249 document.createTextNode(KorAP.Locale.AND)
250 );
Nils Diewaldd0770492014-12-19 03:55:00 +0000251 op.appendChild(andE);
252 };
253
254 // Add or button
255 if (this._or === true) {
256 var orE = document.createElement('span');
257 orE.setAttribute('class', 'or');
Nils Diewalde15b7a22015-01-09 21:50:21 +0000258 orE.addEventListener('click', KorAP._or, false);
Nils Diewaldd0770492014-12-19 03:55:00 +0000259 orE.appendChild(document.createTextNode(KorAP.Locale.OR));
260 op.appendChild(orE);
261 };
262
263 // Add delete button
264 if (this._del === true) {
265 var delE = document.createElement('span');
266 delE.setAttribute('class', 'delete');
267 delE.appendChild(document.createTextNode(KorAP.Locale.DEL));
Nils Diewald0297ba12015-01-05 21:56:12 +0000268 delE.addEventListener('click', KorAP._delete, false);
Nils Diewaldd0770492014-12-19 03:55:00 +0000269 op.appendChild(delE);
270 };
Nils Diewald966abf12014-12-20 02:27:45 +0000271
272 return op;
Nils Diewaldd0770492014-12-19 03:55:00 +0000273 },
Nils Diewald0297ba12015-01-05 21:56:12 +0000274
275 // Be aware! This may be cyclic
276 parent : function (obj) {
277 if (arguments.length === 1)
278 this._parent = obj;
279 return this._parent;
280 },
Nils Diewald5c817a42015-01-06 01:08:56 +0000281
Nils Diewaldd0770492014-12-19 03:55:00 +0000282 element : function () {
283
284 // Return existing element
285 if (this._element !== undefined)
286 return this._element;
287
288 this._element = document.createElement('div');
289 this._element.setAttribute('class', 'operators');
290
291 // Init elements
292 this.update();
293 return this._element;
294 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000295
Nils Diewaldd0770492014-12-19 03:55:00 +0000296 and : function (bool) {
297 if (arguments.length === 1)
Nils Diewald966abf12014-12-20 02:27:45 +0000298 this._and = _bool(bool);
Nils Diewaldd0770492014-12-19 03:55:00 +0000299 return this._and;
300 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000301
Nils Diewaldd0770492014-12-19 03:55:00 +0000302 or : function (bool) {
303 if (arguments.length === 1)
Nils Diewald966abf12014-12-20 02:27:45 +0000304 this._or = _bool(bool);
Nils Diewaldd0770492014-12-19 03:55:00 +0000305 return this._or;
306 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000307
Nils Diewaldd0770492014-12-19 03:55:00 +0000308 del : function (bool) {
309 if (arguments.length === 1)
Nils Diewald966abf12014-12-20 02:27:45 +0000310 this._del = _bool(bool);
Nils Diewaldd0770492014-12-19 03:55:00 +0000311 return this._del;
312 }
313 };
314
315
316 /**
317 * Unspecified criterion
318 */
Nils Diewald8f4e2542014-12-19 04:42:09 +0000319 KorAP.UnspecifiedDoc = {
Nils Diewald4019bd22015-01-08 19:57:50 +0000320 _ldType : "non",
Nils Diewaldd0770492014-12-19 03:55:00 +0000321 create : function (parent) {
Nils Diewald8f6b6102015-01-08 18:25:33 +0000322 var obj = Object.create(KorAP.JsonLD).
323 upgradeTo(KorAP.UnspecifiedDoc);
324
Nils Diewaldd0770492014-12-19 03:55:00 +0000325 if (parent !== undefined)
326 obj._parent = parent;
Nils Diewald8f6b6102015-01-08 18:25:33 +0000327
Nils Diewaldd0770492014-12-19 03:55:00 +0000328 return obj;
329 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000330
Nils Diewaldfda29d92015-01-22 17:28:01 +0000331 // Set key - replace
332 key : function (v) {
333
334 // Not replaceable
335 if (this._parent === undefined)
336 return null;
337
338 // Set JSON-LD type
339 var newDoc = KorAP.Doc.create(this._parent, {
340 "@type" : "korap:doc",
341 "value" : "",
342 "key" : v
343 });
344
345 // Unspecified document on root
346 if (this._parent.ldType() === null) {
347 this._parent.root(newDoc);
348 this.destroy();
349 }
350
351 // Unspecified document in group
352 else {
353 this._parent.replaceOperand(this, newDoc);
354 };
355 this._parent.update();
356 return newDoc;
357 },
358
Nils Diewald966abf12014-12-20 02:27:45 +0000359 update : function () {
Nils Diewald4019bd22015-01-08 19:57:50 +0000360
Nils Diewald966abf12014-12-20 02:27:45 +0000361 if (this._element === undefined)
362 return this.element();
363
Nils Diewald8f6b6102015-01-08 18:25:33 +0000364 // Remove element content
Nils Diewald966abf12014-12-20 02:27:45 +0000365 _removeChildren(this._element);
366
367 var ellipsis = document.createElement('span');
Nils Diewaldf219eb82015-01-07 20:15:42 +0000368 ellipsis.appendChild(document.createTextNode(loc.EMPTY));
Nils Diewald966abf12014-12-20 02:27:45 +0000369 this._element.appendChild(ellipsis);
370
371 // Set operators
Nils Diewalde15b7a22015-01-09 21:50:21 +0000372 if (this._parent !== undefined && this.parent().ldType() !== null) {
373 var op = this.operators(
374 false,
375 false,
376 true
377 );
Nils Diewald966abf12014-12-20 02:27:45 +0000378
Nils Diewalde15b7a22015-01-09 21:50:21 +0000379 this._element.appendChild(
380 op.element()
381 );
382 };
Nils Diewald966abf12014-12-20 02:27:45 +0000383
Nils Diewald966abf12014-12-20 02:27:45 +0000384 return this.element();
385 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000386
Nils Diewaldd0770492014-12-19 03:55:00 +0000387 element : function () {
388 if (this._element !== undefined)
389 return this._element;
390 this._element = document.createElement('div');
Nils Diewaldd599d542015-01-08 20:41:34 +0000391 this._element.setAttribute('class', 'doc unspecified');
Nils Diewald966abf12014-12-20 02:27:45 +0000392 this.update();
Nils Diewaldd0770492014-12-19 03:55:00 +0000393 return this._element;
Nils Diewaldfda29d92015-01-22 17:28:01 +0000394 },
395
396
Nils Diewaldd0770492014-12-19 03:55:00 +0000397 };
398
399
Nils Diewald8f4e2542014-12-19 04:42:09 +0000400 /**
Nils Diewaldd599d542015-01-08 20:41:34 +0000401 * Document criterion
Nils Diewald8f4e2542014-12-19 04:42:09 +0000402 */
403 KorAP.Doc = {
404 _ldType : "doc",
Nils Diewaldfda29d92015-01-22 17:28:01 +0000405 _obj : function () { return KorAP.Doc },
Nils Diewaldd0770492014-12-19 03:55:00 +0000406
407 create : function (parent, json) {
Nils Diewald8f6b6102015-01-08 18:25:33 +0000408 var obj = Object(KorAP.JsonLD).
409 create().
410 upgradeTo(KorAP.Doc).
411 fromJson(json);
412
Nils Diewaldd0770492014-12-19 03:55:00 +0000413 if (parent !== undefined)
414 obj._parent = parent;
Nils Diewald8f6b6102015-01-08 18:25:33 +0000415
416 obj._changed = true;
Nils Diewaldd0770492014-12-19 03:55:00 +0000417 return obj;
418 },
Nils Diewald5c817a42015-01-06 01:08:56 +0000419
Nils Diewald966abf12014-12-20 02:27:45 +0000420 update : function () {
421 if (this._element === undefined)
422 return this.element();
Nils Diewaldd0770492014-12-19 03:55:00 +0000423
Nils Diewald8f6b6102015-01-08 18:25:33 +0000424 // Get element
Nils Diewald966abf12014-12-20 02:27:45 +0000425 var e = this._element;
426
Nils Diewald8f6b6102015-01-08 18:25:33 +0000427 // Check if there is a change
428 if (this._changed) {
Nils Diewald966abf12014-12-20 02:27:45 +0000429
Nils Diewald8f6b6102015-01-08 18:25:33 +0000430 // Added key
431 var key = document.createElement('span');
432 key.setAttribute('class', 'key');
433 if (this.key())
434 key.appendChild(document.createTextNode(this.key()));
Nils Diewald966abf12014-12-20 02:27:45 +0000435
Nils Diewald8f6b6102015-01-08 18:25:33 +0000436 // Added match operator
437 var matchop = document.createElement('span');
438 matchop.setAttribute('data-type', this.type());
439 matchop.setAttribute('class', 'match');
440 matchop.appendChild(
441 document.createTextNode(this.matchop())
442 );
443
444 // Added match operator
445 var value = document.createElement('span');
446 value.setAttribute('data-type', this.type());
447 value.setAttribute('class', 'value');
448 if (this.value())
449 value.appendChild(
450 document.createTextNode(this.value())
451 );
452
453 // Remove all element children
454 _removeChildren(e);
455
456 // Add spans
457 e.appendChild(key);
458 e.appendChild(matchop);
459 e.appendChild(value);
460
461 this._changed = false;
462 };
463
464 if (this._parent !== undefined) {
465 // Set operators
466 var op = this.operators(
467 true,
468 true,
Nils Diewald4019bd22015-01-08 19:57:50 +0000469 true
Nils Diewald8f6b6102015-01-08 18:25:33 +0000470 );
471
472 // Append new operators
473 e.appendChild(op.element());
474 };
Nils Diewald966abf12014-12-20 02:27:45 +0000475
Nils Diewaldd0770492014-12-19 03:55:00 +0000476 return e;
477 },
478
Nils Diewald966abf12014-12-20 02:27:45 +0000479 element : function () {
480 if (this._element !== undefined)
481 return this._element;
482
483 this._element = document.createElement('div');
484 this._element.setAttribute('class', 'doc');
485
486 this.update();
487 return this._element;
488 },
489
Nils Diewaldd0770492014-12-19 03:55:00 +0000490 // Wrap a new operation around the doc element
491 wrap : function (op) {
Nils Diewald9fcc1712015-01-09 14:24:32 +0000492 var parent = this.parent();
493 var group = KorAP.DocGroup.create(parent);
494 group.operation(op);
Nils Diewald966abf12014-12-20 02:27:45 +0000495 group.append(this);
Nils Diewald9fcc1712015-01-09 14:24:32 +0000496 group.append();
497 return parent.replaceOperand(this, group).update();
Nils Diewaldd0770492014-12-19 03:55:00 +0000498 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000499
Nils Diewald3a2d8022014-12-16 02:45:41 +0000500 // Deserialize from json
501 fromJson : function (json) {
Nils Diewald3a2d8022014-12-16 02:45:41 +0000502 if (json === undefined)
Nils Diewaldd0770492014-12-19 03:55:00 +0000503 return this;
Nils Diewald3a2d8022014-12-16 02:45:41 +0000504
Nils Diewald3a2d8022014-12-16 02:45:41 +0000505 if (json["@type"] !== "korap:doc") {
506 KorAP.log(701, "JSON-LD group has no @type attribute");
507 return;
508 };
509
Nils Diewald3a2d8022014-12-16 02:45:41 +0000510 if (json["value"] === undefined ||
511 typeof json["value"] != 'string') {
512 KorAP.log(805, "Value is invalid");
513 return;
514 };
515
516 // There is a defined key
517 if (json["key"] !== undefined &&
518 typeof json["key"] === 'string') {
519
520 // Set key
Nils Diewaldd0770492014-12-19 03:55:00 +0000521 this.key(json["key"]);
Nils Diewald3a2d8022014-12-16 02:45:41 +0000522
523 // Set match operation
524 if (json["match"] !== undefined) {
Nils Diewaldd0770492014-12-19 03:55:00 +0000525 if (typeof json["match"] === 'string') {
526 this.matchop(json["match"]);
527 }
Nils Diewald3a2d8022014-12-16 02:45:41 +0000528 else {
529 KorAP.log(802, "Match type is not supported by value type");
530 return;
531 };
532 };
533
534 // Key is a string
535 if (json["type"] === undefined ||
536 json["type"] == "type:string") {
Nils Diewaldd0770492014-12-19 03:55:00 +0000537 this.type("string");
Nils Diewald3a2d8022014-12-16 02:45:41 +0000538
539 // Check match type
Nils Diewaldd0770492014-12-19 03:55:00 +0000540 if (!KorAP._validStringMatchRE.test(this.matchop())) {
Nils Diewald3a2d8022014-12-16 02:45:41 +0000541 KorAP.log(802, "Match type is not supported by value type");
542 return;
543 };
544
545 // Set string value
Nils Diewaldd0770492014-12-19 03:55:00 +0000546 this.value(json["value"]);
Nils Diewald3a2d8022014-12-16 02:45:41 +0000547 }
548
549 // Key is a date
550 else if (json["type"] === "type:date") {
Nils Diewaldd0770492014-12-19 03:55:00 +0000551 this.type("date");
Nils Diewald3a2d8022014-12-16 02:45:41 +0000552
553 if (json["value"] !== undefined &&
554 KorAP._validDateRE.test(json["value"])) {
555
Nils Diewaldd0770492014-12-19 03:55:00 +0000556 if (!KorAP._validDateMatchRE.test(this.matchop())) {
Nils Diewald3a2d8022014-12-16 02:45:41 +0000557 KorAP.log(802, "Match type is not supported by value type");
558 return;
559 };
560
561 // Set value
Nils Diewaldd0770492014-12-19 03:55:00 +0000562 this.value(json["value"]);
Nils Diewald3a2d8022014-12-16 02:45:41 +0000563 }
564 else {
565 KorAP.log(806, "Value is not a valid date string");
566 return;
567 };
568 }
569
570 // Key is a regular expression
571 else if (json["type"] === "type:regex") {
Nils Diewaldd0770492014-12-19 03:55:00 +0000572 this.type("regex");
Nils Diewald3a2d8022014-12-16 02:45:41 +0000573
574 try {
575
576 // Try to create a regular expression
577 var check = new RegExp(json["value"]);
578
Nils Diewaldd0770492014-12-19 03:55:00 +0000579 if (!KorAP._validRegexMatchRE.test(this.matchop())) {
Nils Diewald3a2d8022014-12-16 02:45:41 +0000580 KorAP.log(802, "Match type is not supported by value type");
581 return;
582 };
583
Nils Diewaldd0770492014-12-19 03:55:00 +0000584 this.value(json["value"]);
Nils Diewald3a2d8022014-12-16 02:45:41 +0000585 }
Nils Diewaldd0770492014-12-19 03:55:00 +0000586
Nils Diewald3a2d8022014-12-16 02:45:41 +0000587 catch (e) {
588 KorAP.log(807, "Value is not a valid regular expression");
589 return;
590 };
Nils Diewaldd0770492014-12-19 03:55:00 +0000591 this.type("regex");
Nils Diewald3a2d8022014-12-16 02:45:41 +0000592 }
593
594 else {
595 KorAP.log(804, "Unknown value type");
596 return;
597 };
598 };
599
600 return this;
601 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000602
Nils Diewaldd0770492014-12-19 03:55:00 +0000603 key : function (value) {
Nils Diewald8f6b6102015-01-08 18:25:33 +0000604 if (arguments.length === 1) {
Nils Diewaldd0770492014-12-19 03:55:00 +0000605 this._key = value;
Nils Diewald8f6b6102015-01-08 18:25:33 +0000606 this._changed = true;
Nils Diewaldfda29d92015-01-22 17:28:01 +0000607 return this;
Nils Diewald8f6b6102015-01-08 18:25:33 +0000608 };
Nils Diewald3a2d8022014-12-16 02:45:41 +0000609 return this._key;
610 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000611
Nils Diewaldd0770492014-12-19 03:55:00 +0000612 matchop : function (match) {
Nils Diewald8f6b6102015-01-08 18:25:33 +0000613 if (arguments.length === 1) {
Nils Diewaldd0770492014-12-19 03:55:00 +0000614 this._matchop = match.replace(/^match:/, '');
Nils Diewald8f6b6102015-01-08 18:25:33 +0000615 this._changed = true;
Nils Diewaldfda29d92015-01-22 17:28:01 +0000616 return this;
Nils Diewald8f6b6102015-01-08 18:25:33 +0000617 };
Nils Diewald3a2d8022014-12-16 02:45:41 +0000618 return this._matchop || "eq";
619 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000620
Nils Diewaldd0770492014-12-19 03:55:00 +0000621 type : function (type) {
Nils Diewald8f6b6102015-01-08 18:25:33 +0000622 if (arguments.length === 1) {
Nils Diewaldd0770492014-12-19 03:55:00 +0000623 this._type = type;
Nils Diewald8f6b6102015-01-08 18:25:33 +0000624 this._changed = true;
Nils Diewaldfda29d92015-01-22 17:28:01 +0000625 return this;
Nils Diewald8f6b6102015-01-08 18:25:33 +0000626 };
Nils Diewald3a2d8022014-12-16 02:45:41 +0000627 return this._type || "string";
628 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000629
Nils Diewaldd0770492014-12-19 03:55:00 +0000630 value : function (value) {
Nils Diewald8f6b6102015-01-08 18:25:33 +0000631 if (arguments.length === 1) {
Nils Diewaldd0770492014-12-19 03:55:00 +0000632 this._value = value;
Nils Diewald8f6b6102015-01-08 18:25:33 +0000633 this._changed = true;
Nils Diewaldfda29d92015-01-22 17:28:01 +0000634 return this;
Nils Diewald8f6b6102015-01-08 18:25:33 +0000635 };
Nils Diewald3a2d8022014-12-16 02:45:41 +0000636 return this._value;
637 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000638
Nils Diewald3a2d8022014-12-16 02:45:41 +0000639 toJson : function () {
Nils Diewaldd0770492014-12-19 03:55:00 +0000640 if (!this.matchop() || !this.key())
Nils Diewald3a2d8022014-12-16 02:45:41 +0000641 return {};
642
643 return {
Nils Diewaldd0770492014-12-19 03:55:00 +0000644 "@type" : "korap:" + this.ldType(),
645 "key" : this.key(),
646 "match" : "match:" + this.matchop(),
647 "value" : this.value() || '',
648 "type" : "type:" + this.type()
Nils Diewald3a2d8022014-12-16 02:45:41 +0000649 };
Nils Diewaldf219eb82015-01-07 20:15:42 +0000650 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000651
652 toQuery : function () {
Nils Diewaldf219eb82015-01-07 20:15:42 +0000653 if (!this.matchop() || !this.key())
654 return "";
655
656 // Build doc string based on key
657 var string = this.key() + ' ';
658
659 // Add match operator
660 switch (this.matchop()) {
661 case "ne":
662 string += '!=';
663 break;
664 case "contains":
665 string += '~';
666 break;
667 case "geq":
668 string += 'since';
669 break;
670 case "leq":
671 string += 'until';
672 break;
673 default:
674 string += (this.type() == 'date') ? 'in' : '=';
675 break;
676 };
677
678 string += ' ';
679
680 // Add value
681 switch (this.type()) {
682 case "date":
683 return string + this.value();
684 break;
685 case "regex":
686 return string + '/' + this.value() + '/';
687 break;
688 case "string":
689 return string + '"' + this.value().replace(KorAP._quote, '\\$1') + '"';
690 break;
691 };
692
Nils Diewaldd599d542015-01-08 20:41:34 +0000693 return "";
Nils Diewald3a2d8022014-12-16 02:45:41 +0000694 }
695 };
Nils Diewald8f4e2542014-12-19 04:42:09 +0000696
Nils Diewaldd599d542015-01-08 20:41:34 +0000697
Nils Diewald8f4e2542014-12-19 04:42:09 +0000698 /**
Nils Diewaldd599d542015-01-08 20:41:34 +0000699 * Document group criterion
Nils Diewald8f4e2542014-12-19 04:42:09 +0000700 */
701 KorAP.DocGroup = {
702 _ldType : "docGroup",
703
704 create : function (parent, json) {
705 var obj = Object.create(KorAP.JsonLD).upgradeTo(KorAP.DocGroup);
706 obj._operands = [];
707 obj.fromJson(json);
708 if (parent !== undefined)
709 obj._parent = parent;
710 return obj;
711 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000712
713 newAfter : function (obj) {
Nils Diewald52f7eb12015-01-12 17:30:04 +0000714 for (var i = 0; i < this._operands.length; i++) {
Nils Diewaldd599d542015-01-08 20:41:34 +0000715 if (this._operands[i] === obj) {
716 var operand = KorAP.UnspecifiedDoc.create(this);
717 this._operands.splice(i + 1, 0, operand);
718 return this.update();
719 };
720 };
721 },
722
Nils Diewald966abf12014-12-20 02:27:45 +0000723 append : function (operand) {
724
725 // Append unspecified object
726 if (operand === undefined) {
727
728 // Be aware of cyclic structures!
729 operand = KorAP.UnspecifiedDoc.create(this);
730 this._operands.push(operand);
Nils Diewald966abf12014-12-20 02:27:45 +0000731 return operand;
732 };
733
Nils Diewald8f4e2542014-12-19 04:42:09 +0000734 switch (operand["@type"]) {
735 case undefined:
736 if (operand["ldType"] !== undefined) {
737 if (operand.ldType() !== 'doc' &&
Nils Diewald966abf12014-12-20 02:27:45 +0000738 operand.ldType() !== 'docGroup') {
Nils Diewald8f4e2542014-12-19 04:42:09 +0000739 KorAP.log(812, "Operand not supported in document group");
740 return;
741 };
Nils Diewald966abf12014-12-20 02:27:45 +0000742 // Be aware of cyclic structures!
743 operand.parent(this);
Nils Diewald8f4e2542014-12-19 04:42:09 +0000744 this._operands.push(operand);
745 return operand;
746 };
747
748 KorAP.log(701, "JSON-LD group has no @type attribute");
749 return;
750
751 case "korap:doc":
Nils Diewald966abf12014-12-20 02:27:45 +0000752 // Be aware of cyclic structures!
753 var doc = KorAP.Doc.create(this, operand);
Nils Diewald8f4e2542014-12-19 04:42:09 +0000754 if (doc === undefined)
755 return;
756 this._operands.push(doc);
757 return doc;
758
759 case "korap:docGroup":
Nils Diewald966abf12014-12-20 02:27:45 +0000760 // Be aware of cyclic structures!
761 var docGroup = KorAP.DocGroup.create(this, operand);
Nils Diewald8f4e2542014-12-19 04:42:09 +0000762 if (docGroup === undefined)
763 return;
764 this._operands.push(docGroup);
765 return docGroup;
766
767 default:
768 KorAP.log(812, "Operand not supported in document group");
769 return;
770 };
771 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000772
Nils Diewald966abf12014-12-20 02:27:45 +0000773 update : function () {
Nils Diewaldf219eb82015-01-07 20:15:42 +0000774 // There is only one operand in group
Nils Diewald52f7eb12015-01-12 17:30:04 +0000775
Nils Diewaldf219eb82015-01-07 20:15:42 +0000776 if (this._operands.length === 1) {
Nils Diewald52f7eb12015-01-12 17:30:04 +0000777
Nils Diewaldf219eb82015-01-07 20:15:42 +0000778 var parent = this.parent();
Nils Diewald8e7182e2015-01-08 15:02:07 +0000779 var op = this.getOperand(0);
780
Nils Diewald8f6b6102015-01-08 18:25:33 +0000781 // This will prevent destruction of
782 // the operand
783 this._operands = [];
Nils Diewaldf219eb82015-01-07 20:15:42 +0000784
785 // Parent is a group
Nils Diewald52f7eb12015-01-12 17:30:04 +0000786 if (parent.ldType() !== null)
Nils Diewald8e7182e2015-01-08 15:02:07 +0000787 return parent.replaceOperand(this, op).update();
Nils Diewaldf219eb82015-01-07 20:15:42 +0000788
Nils Diewald8f6b6102015-01-08 18:25:33 +0000789 // Parent is vc
Nils Diewaldf219eb82015-01-07 20:15:42 +0000790 else {
Nils Diewaldf219eb82015-01-07 20:15:42 +0000791 this.destroy();
Nils Diewald8f6b6102015-01-08 18:25:33 +0000792 // Cyclic madness
793 parent.root(op);
794 op.parent(parent);
Nils Diewaldf219eb82015-01-07 20:15:42 +0000795 return parent.root();
Nils Diewald5c817a42015-01-06 01:08:56 +0000796 };
797 };
798
Nils Diewald966abf12014-12-20 02:27:45 +0000799 if (this._element === undefined)
Nils Diewald5c817a42015-01-06 01:08:56 +0000800 return this;
Nils Diewald966abf12014-12-20 02:27:45 +0000801
Nils Diewald5c817a42015-01-06 01:08:56 +0000802 var group = this._element;
803 group.setAttribute('data-operation', this.operation());
Nils Diewald966abf12014-12-20 02:27:45 +0000804
Nils Diewald5c817a42015-01-06 01:08:56 +0000805 _removeChildren(group);
Nils Diewald966abf12014-12-20 02:27:45 +0000806
807 // Append operands
Nils Diewald52f7eb12015-01-12 17:30:04 +0000808 for (var i = 0; i < this._operands.length; i++) {
Nils Diewald5c817a42015-01-06 01:08:56 +0000809 group.appendChild(
Nils Diewald966abf12014-12-20 02:27:45 +0000810 this.getOperand(i).element()
811 );
812 };
813
814 // Set operators
815 var op = this.operators(
816 this.operation() == 'and' ? false : true,
817 this.operation() == 'or' ? false : true,
818 true
819 );
820
Nils Diewald5c817a42015-01-06 01:08:56 +0000821 group.appendChild(
Nils Diewald966abf12014-12-20 02:27:45 +0000822 op.element()
823 );
824
Nils Diewald5c817a42015-01-06 01:08:56 +0000825 return this;
Nils Diewald966abf12014-12-20 02:27:45 +0000826 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000827
Nils Diewald8f4e2542014-12-19 04:42:09 +0000828 element : function () {
829 if (this._element !== undefined)
830 return this._element;
831
832 this._element = document.createElement('div');
833 this._element.setAttribute('class', 'docGroup');
Nils Diewald8f4e2542014-12-19 04:42:09 +0000834
Nils Diewaldf219eb82015-01-07 20:15:42 +0000835 // Update the object - including optimization
Nils Diewald966abf12014-12-20 02:27:45 +0000836 this.update();
Nils Diewald8f4e2542014-12-19 04:42:09 +0000837
838 return this._element;
839 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000840
Nils Diewald8f4e2542014-12-19 04:42:09 +0000841 operation : function (op) {
842 if (arguments.length === 1) {
843 if (KorAP._validGroupOpRE.test(op)) {
844 this._op = op;
845 }
846 else {
847 KorAP.log(810, "Unknown operation type");
848 return;
849 };
850 };
851 return this._op || 'and';
852 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000853
Nils Diewald8f4e2542014-12-19 04:42:09 +0000854 operands : function () {
855 return this._operands;
856 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000857
Nils Diewald8f4e2542014-12-19 04:42:09 +0000858 getOperand : function (index) {
859 return this._operands[index];
860 },
861
Nils Diewald5c817a42015-01-06 01:08:56 +0000862 // Replace operand
Nils Diewaldf219eb82015-01-07 20:15:42 +0000863 replaceOperand : function (oldOp, newOp) {
Nils Diewald52f7eb12015-01-12 17:30:04 +0000864
865 for (var i = 0; i < this._operands.length; i++) {
Nils Diewaldf219eb82015-01-07 20:15:42 +0000866 if (this._operands[i] === oldOp) {
867
Nils Diewalde15b7a22015-01-09 21:50:21 +0000868 // Just insert a doc or ...
869 if (newOp.ldType() === "doc" ||
Nils Diewald52f7eb12015-01-12 17:30:04 +0000870 newOp.ldType() === "non" ||
Nils Diewalde15b7a22015-01-09 21:50:21 +0000871 // ... insert a group of a different operation
872 // (i.e. "and" in "or"/"or" in "and")
873 newOp.operation() != this.operation()) {
Nils Diewaldf219eb82015-01-07 20:15:42 +0000874 this._operands[i] = newOp;
Nils Diewald8f6b6102015-01-08 18:25:33 +0000875 newOp.parent(this);
Nils Diewaldf219eb82015-01-07 20:15:42 +0000876 }
877
878 // Flatten the group
879 else {
Nils Diewald8e7182e2015-01-08 15:02:07 +0000880 // Remove old group
881 this._operands.splice(i, 1);
882
883 // Inject new operands
Nils Diewald8f6b6102015-01-08 18:25:33 +0000884 for (var op in newOp.operands().reverse()) {
885 this._operands.splice(i, 0, newOp.getOperand(op));
886 newOp.getOperand(0).parent(this);
887 };
888 // Prevent destruction of operands
889 newOp._operands = [];
890 newOp.destroy();
Nils Diewaldf219eb82015-01-07 20:15:42 +0000891 };
892 oldOp.destroy();
893 return this;
Nils Diewald5c817a42015-01-06 01:08:56 +0000894 }
895 };
896 return false;
897 },
898
Nils Diewald0297ba12015-01-05 21:56:12 +0000899 // Delete operand from group
900 delOperand : function (obj) {
Nils Diewald52f7eb12015-01-12 17:30:04 +0000901 for (var i = 0; i < this._operands.length; i++) {
Nils Diewald0297ba12015-01-05 21:56:12 +0000902 if (this._operands[i] === obj) {
Nils Diewald8e7182e2015-01-08 15:02:07 +0000903
Nils Diewald8f6b6102015-01-08 18:25:33 +0000904 // Delete identified operand
Nils Diewald0297ba12015-01-05 21:56:12 +0000905 this._operands.splice(i,1);
906
Nils Diewald5c817a42015-01-06 01:08:56 +0000907 // Destroy object for cyclic references
908 obj.destroy();
909
Nils Diewaldf219eb82015-01-07 20:15:42 +0000910 return this;
Nils Diewald0297ba12015-01-05 21:56:12 +0000911 };
912 };
Nils Diewald5c817a42015-01-06 01:08:56 +0000913
914 // Operand not found
915 return undefined;
Nils Diewald0297ba12015-01-05 21:56:12 +0000916 },
917
Nils Diewald8f4e2542014-12-19 04:42:09 +0000918 // Deserialize from json
919 fromJson : function (json) {
920 if (json === undefined)
921 return this;
922
923 if (json["@type"] !== "korap:docGroup") {
924 KorAP.log(701, "JSON-LD group has no @type attribute");
925 return;
926 };
927
928 if (json["operation"] === undefined ||
929 typeof json["operation"] !== 'string') {
930 KorAP.log(811, "Document group expects operation");
931 return;
932 };
933
934 var operation = json["operation"];
935
936 this.operation(operation.replace(/^operation:/,''));
937
938 if (json["operands"] === undefined ||
939 !(json["operands"] instanceof Array)) {
940 KorAP.log(704, "Operation needs operand list")
941 return;
942 };
943
944 // Add all documents
945 for (var i in json["operands"]) {
946 var operand = json["operands"][i];
Nils Diewald966abf12014-12-20 02:27:45 +0000947 this.append(operand);
Nils Diewald8f4e2542014-12-19 04:42:09 +0000948 };
949
950 return this;
951 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000952
Nils Diewald8f4e2542014-12-19 04:42:09 +0000953 toJson : function () {
954 var opArray = new Array();
Nils Diewald52f7eb12015-01-12 17:30:04 +0000955 for (var i = 0; i < this._operands.length; i++) {
Nils Diewalde15b7a22015-01-09 21:50:21 +0000956 if (this._operands[i].ldType() !== 'non')
957 opArray.push(this._operands[i].toJson());
Nils Diewald8f4e2542014-12-19 04:42:09 +0000958 };
959 return {
960 "@type" : "korap:" + this.ldType(),
961 "operation" : "operation:" + this.operation(),
962 "operands" : opArray
963 };
Nils Diewaldf219eb82015-01-07 20:15:42 +0000964 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000965
Nils Diewalde15b7a22015-01-09 21:50:21 +0000966 toQuery : function (brackets) {
967 var list = this._operands
968 .filter(function (op) {
969 return op.ldType() !== 'non';
970 })
971 .map(function (op) {
Nils Diewaldd599d542015-01-08 20:41:34 +0000972 return (op.ldType() === 'docGroup') ?
Nils Diewalde15b7a22015-01-09 21:50:21 +0000973 op.toQuery(true) :
Nils Diewaldd599d542015-01-08 20:41:34 +0000974 op.toQuery();
Nils Diewalde15b7a22015-01-09 21:50:21 +0000975 });
976
977 if (list.length === 1)
978 return list.join('');
979 else {
980 var str = list.join(this.operation() === 'or' ? ' | ' : ' & ');
981 return brackets ? '(' + str + ')' : str;
982 };
Nils Diewald8f4e2542014-12-19 04:42:09 +0000983 }
984 };
985
986
Nils Diewaldd599d542015-01-08 20:41:34 +0000987 /**
988 * Abstract JsonLD criterion object
989 */
Nils Diewald8f4e2542014-12-19 04:42:09 +0000990 KorAP.JsonLD = {
Nils Diewald8f6b6102015-01-08 18:25:33 +0000991 _changed : false,
992
Nils Diewald8f4e2542014-12-19 04:42:09 +0000993 create : function () {
994 return Object.create(KorAP.JsonLD);
995 },
996
997 /**
998 * Upgrade this object to another object
999 * while private data stays intact
1000 */
1001 upgradeTo : function (props) {
1002 for (var prop in props) {
1003 this[prop] = props[prop];
1004 };
1005 return this;
1006 },
Nils Diewald8f6b6102015-01-08 18:25:33 +00001007
Nils Diewald8f4e2542014-12-19 04:42:09 +00001008 ldType : function (type) {
1009 if (arguments.length === 1)
1010 this._ldType = type;
1011 return this._ldType;
1012 },
Nils Diewald8f6b6102015-01-08 18:25:33 +00001013
Nils Diewald8f4e2542014-12-19 04:42:09 +00001014 parent : function (obj) {
Nils Diewald8f6b6102015-01-08 18:25:33 +00001015 if (arguments.length === 1) {
Nils Diewald8f4e2542014-12-19 04:42:09 +00001016 this._parent = obj;
Nils Diewald8f6b6102015-01-08 18:25:33 +00001017 this._changed = true;
1018 };
Nils Diewald8f4e2542014-12-19 04:42:09 +00001019 return this._parent;
Nils Diewald966abf12014-12-20 02:27:45 +00001020 },
Nils Diewald0297ba12015-01-05 21:56:12 +00001021
Nils Diewald5c817a42015-01-06 01:08:56 +00001022 // Destroy object - especially for
1023 // acyclic structures!
1024 // I'm a paranoid chicken!
1025 destroy : function () {
1026 if (this._ops != undefined) {
1027 this._ops._parent = undefined;
1028 if (this._ops._element !== undefined)
1029 this._ops._element.refTo = undefined;
1030 this._ops = undefined;
1031 };
1032 if (this._element !== undefined)
1033 this._element = undefined;
1034
1035 // In case of a group, destroy all operands
1036 if (this._operands !== undefined) {
Nils Diewald52f7eb12015-01-12 17:30:04 +00001037 for (var i = 0; i < this._operands.length; i++)
Nils Diewald5c817a42015-01-06 01:08:56 +00001038 this.getOperand(i).destroy();
Nils Diewaldf219eb82015-01-07 20:15:42 +00001039 this._operands = [];
Nils Diewald5c817a42015-01-06 01:08:56 +00001040 };
1041 },
1042
Nils Diewald52f7eb12015-01-12 17:30:04 +00001043 // Wrap a new operation around the root group element
1044 wrapOnRoot : function (op) {
1045 var parent = this.parent();
1046
1047 var group = KorAP.DocGroup.create(parent);
1048 if (arguments.length === 1)
1049 group.operation(op);
1050 else
1051 group.operation(
1052 this.operation() === 'and' ? 'or' : 'and'
1053 );
1054 group.append(this);
1055 this.parent(group);
1056 group.append();
1057 group.element(); // Init (seems to be necessary)
1058 parent.root(group);
1059 return this.parent();
1060 },
1061
Nils Diewald0297ba12015-01-05 21:56:12 +00001062 // Be aware! This may be cyclic
Nils Diewald966abf12014-12-20 02:27:45 +00001063 operators : function (and, or, del) {
1064 if (arguments === 0)
1065 return this._ops;
1066 this._ops = KorAP.Operators.create(
1067 and, or, del
1068 );
Nils Diewald0297ba12015-01-05 21:56:12 +00001069 this._ops.parent(this);
Nils Diewald966abf12014-12-20 02:27:45 +00001070 return this._ops;
1071 },
Nils Diewald8f6b6102015-01-08 18:25:33 +00001072
Nils Diewald966abf12014-12-20 02:27:45 +00001073 toJson : function () {
1074 return {
1075 // Unspecified object
1076 "@type" : "korap:" + this.ldType()
1077 };
Nils Diewald8f6b6102015-01-08 18:25:33 +00001078 },
Nils Diewaldd599d542015-01-08 20:41:34 +00001079
1080 toQuery : function () {
Nils Diewaldfda29d92015-01-22 17:28:01 +00001081 return '';
Nils Diewald8f4e2542014-12-19 04:42:09 +00001082 }
1083 };
1084
Nils Diewald3a2d8022014-12-16 02:45:41 +00001085}(this.KorAP));