blob: 4d6b8e426ec9e8e1afa571d930a5af5082715a52 [file] [log] [blame]
Nils Diewald0e6992a2015-04-14 20:13:52 +00001/**
2 * Abstract JsonLD criterion object
3 */
Akron88d237e2020-10-21 08:05:18 +02004"use strict";
5
Nils Diewald0e6992a2015-04-14 20:13:52 +00006define(['vc/operators'], function (operatorsClass) {
Akron88d237e2020-10-21 08:05:18 +02007
Nils Diewald0e6992a2015-04-14 20:13:52 +00008 return {
Akron88d237e2020-10-21 08:05:18 +02009
Nils Diewald0e6992a2015-04-14 20:13:52 +000010 __changed : false,
11
12 create : function () {
13 return Object.create(this);
14 },
15
Akron88d237e2020-10-21 08:05:18 +020016
Nils Diewald0e6992a2015-04-14 20:13:52 +000017 ldType : function (type) {
18 if (arguments.length === 1)
Akron8778f5d2017-06-30 21:25:55 +020019 this._ldType = type;
Nils Diewald0e6992a2015-04-14 20:13:52 +000020 return this._ldType;
21 },
22
Akron88d237e2020-10-21 08:05:18 +020023
Nils Diewald0e6992a2015-04-14 20:13:52 +000024 parent : function (obj) {
25 if (arguments.length === 1) {
Akron8778f5d2017-06-30 21:25:55 +020026 this._parent = obj;
27 this.__changed = true;
Nils Diewald0e6992a2015-04-14 20:13:52 +000028 };
29 return this._parent;
30 },
31
Akron88d237e2020-10-21 08:05:18 +020032
Nils Diewald0e6992a2015-04-14 20:13:52 +000033 // Destroy object - especially for
34 // acyclic structures!
35 // I'm paranoid!
36 destroy : function () {
Akron88d237e2020-10-21 08:05:18 +020037 const t = this;
38 if (t._ops != undefined) {
39 t._ops._parent = undefined;
Akron24aa0052020-11-10 11:00:34 +010040 if (t._ops._el !== undefined) {
41 t._ops._el.refTo = undefined;
Akronb19803c2018-08-16 16:39:42 +020042 };
Akron88d237e2020-10-21 08:05:18 +020043 t._ops = undefined;
Nils Diewald0e6992a2015-04-14 20:13:52 +000044 };
Akronb19803c2018-08-16 16:39:42 +020045
Akron24aa0052020-11-10 11:00:34 +010046 if (t._el !== undefined)
47 t._el = undefined;
Nils Diewald0e6992a2015-04-14 20:13:52 +000048
49 // In case of a group, destroy all operands
Akron88d237e2020-10-21 08:05:18 +020050 if (t._operands !== undefined) {
51 t._operands.forEach(i => i.destroy());
52 t._operands = [];
Nils Diewald0e6992a2015-04-14 20:13:52 +000053 };
54 },
55
56 // Wrap a new operation around the root group element
57 wrapOnRoot : function (op) {
Akron88d237e2020-10-21 08:05:18 +020058 const parent = this.parent();
59 const group = require('vc/docgroup').create(parent);
60
Nils Diewald0e6992a2015-04-14 20:13:52 +000061 if (arguments.length === 1)
Akron8778f5d2017-06-30 21:25:55 +020062 group.operation(op);
Nils Diewald0e6992a2015-04-14 20:13:52 +000063 else
Akron8778f5d2017-06-30 21:25:55 +020064 group.operation(
65 this.operation() === 'and' ? 'or' : 'and'
66 );
Nils Diewald0e6992a2015-04-14 20:13:52 +000067 group.append(this);
68 this.parent(group);
69 group.append();
70 group.element(); // Init (seems to be necessary)
71 parent.root(group);
72 return this.parent();
73 },
74
Akron88d237e2020-10-21 08:05:18 +020075
Nils Diewald0e6992a2015-04-14 20:13:52 +000076 // Be aware! This may be cyclic
77 operators : function (and, or, del) {
78 if (arguments === 0)
Akron8778f5d2017-06-30 21:25:55 +020079 return this._ops;
Akron88d237e2020-10-21 08:05:18 +020080
Nils Diewald0e6992a2015-04-14 20:13:52 +000081 this._ops = operatorsClass.create(
Akron8778f5d2017-06-30 21:25:55 +020082 and, or, del
Nils Diewald0e6992a2015-04-14 20:13:52 +000083 );
Akron88d237e2020-10-21 08:05:18 +020084
Nils Diewald0e6992a2015-04-14 20:13:52 +000085 this._ops.parent(this);
86 return this._ops;
87 },
88
Akron88d237e2020-10-21 08:05:18 +020089
Nils Diewald0e6992a2015-04-14 20:13:52 +000090 toJson : function () {
91 return {
Akron8778f5d2017-06-30 21:25:55 +020092 // Unspecified object
93 "@type" : "koral:" + this.ldType()
Nils Diewald0e6992a2015-04-14 20:13:52 +000094 };
95 },
96
Akron88d237e2020-10-21 08:05:18 +020097
Akrond2474aa2018-08-28 12:06:27 +020098 rewrites : function () {
99 return null;
100 },
101
Akron88d237e2020-10-21 08:05:18 +0200102
hebastaa0282be2018-12-05 16:58:00 +0100103 incomplete : function () {
104 return false;
105 },
106
Akron88d237e2020-10-21 08:05:18 +0200107
Nils Diewald0e6992a2015-04-14 20:13:52 +0000108 toQuery : function () {
109 return '';
110 }
111 };
112});