blob: 600a5bb86eb575d6299c6fcc1f9257d24f1768ca [file] [log] [blame]
Nils Diewald0e6992a2015-04-14 20:13:52 +00001/**
2 * Abstract JsonLD criterion object
3 */
4define(['vc/operators'], function (operatorsClass) {
5 return {
6 __changed : false,
7
8 create : function () {
9 return Object.create(this);
10 },
11
12 /**
13 * Upgrade this object to another object
14 * while private data stays intact
15 */
16 upgradeTo : function (props) {
17 for (var prop in props) {
Akron8778f5d2017-06-30 21:25:55 +020018 this[prop] = props[prop];
Nils Diewald0e6992a2015-04-14 20:13:52 +000019 };
20 return this;
21 },
22
23 ldType : function (type) {
24 if (arguments.length === 1)
Akron8778f5d2017-06-30 21:25:55 +020025 this._ldType = type;
Nils Diewald0e6992a2015-04-14 20:13:52 +000026 return this._ldType;
27 },
28
29 parent : function (obj) {
30 if (arguments.length === 1) {
Akron8778f5d2017-06-30 21:25:55 +020031 this._parent = obj;
32 this.__changed = true;
Nils Diewald0e6992a2015-04-14 20:13:52 +000033 };
34 return this._parent;
35 },
36
37 // Destroy object - especially for
38 // acyclic structures!
39 // I'm paranoid!
40 destroy : function () {
Akronb19803c2018-08-16 16:39:42 +020041
Nils Diewald0e6992a2015-04-14 20:13:52 +000042 if (this._ops != undefined) {
Akron8778f5d2017-06-30 21:25:55 +020043 this._ops._parent = undefined;
Akronb19803c2018-08-16 16:39:42 +020044 if (this._ops._element !== undefined) {
Akron8778f5d2017-06-30 21:25:55 +020045 this._ops._element.refTo = undefined;
Akronb19803c2018-08-16 16:39:42 +020046 };
Akron8778f5d2017-06-30 21:25:55 +020047 this._ops = undefined;
Nils Diewald0e6992a2015-04-14 20:13:52 +000048 };
Akronb19803c2018-08-16 16:39:42 +020049
Nils Diewald0e6992a2015-04-14 20:13:52 +000050 if (this._element !== undefined)
Akron8778f5d2017-06-30 21:25:55 +020051 this._element = undefined;
Nils Diewald0e6992a2015-04-14 20:13:52 +000052
53 // In case of a group, destroy all operands
54 if (this._operands !== undefined) {
Akron8778f5d2017-06-30 21:25:55 +020055 for (var i = 0; i < this._operands.length; i++)
56 this.getOperand(i).destroy();
57 this._operands = [];
Nils Diewald0e6992a2015-04-14 20:13:52 +000058 };
59 },
60
61 // Wrap a new operation around the root group element
62 wrapOnRoot : function (op) {
63 var parent = this.parent();
64
65 var group = require('vc/docgroup').create(parent);
66 if (arguments.length === 1)
Akron8778f5d2017-06-30 21:25:55 +020067 group.operation(op);
Nils Diewald0e6992a2015-04-14 20:13:52 +000068 else
Akron8778f5d2017-06-30 21:25:55 +020069 group.operation(
70 this.operation() === 'and' ? 'or' : 'and'
71 );
Nils Diewald0e6992a2015-04-14 20:13:52 +000072 group.append(this);
73 this.parent(group);
74 group.append();
75 group.element(); // Init (seems to be necessary)
76 parent.root(group);
77 return this.parent();
78 },
79
80 // Be aware! This may be cyclic
81 operators : function (and, or, del) {
82 if (arguments === 0)
Akron8778f5d2017-06-30 21:25:55 +020083 return this._ops;
Nils Diewald0e6992a2015-04-14 20:13:52 +000084 this._ops = operatorsClass.create(
Akron8778f5d2017-06-30 21:25:55 +020085 and, or, del
Nils Diewald0e6992a2015-04-14 20:13:52 +000086 );
87 this._ops.parent(this);
88 return this._ops;
89 },
90
91 toJson : function () {
92 return {
Akron8778f5d2017-06-30 21:25:55 +020093 // Unspecified object
94 "@type" : "koral:" + this.ldType()
Nils Diewald0e6992a2015-04-14 20:13:52 +000095 };
96 },
97
Akrond2474aa2018-08-28 12:06:27 +020098 rewrites : function () {
99 return null;
100 },
101
Nils Diewald0e6992a2015-04-14 20:13:52 +0000102 toQuery : function () {
103 return '';
104 }
105 };
106});