blob: 06be1e06260d6d092f915b15bb63995748bddcd9 [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 /**
18 * Upgrade this object to another object
19 * while private data stays intact
20 */
21 upgradeTo : function (props) {
Akron88d237e2020-10-21 08:05:18 +020022 for (let prop in props) {
Akron8778f5d2017-06-30 21:25:55 +020023 this[prop] = props[prop];
Nils Diewald0e6992a2015-04-14 20:13:52 +000024 };
25 return this;
26 },
27
Akron88d237e2020-10-21 08:05:18 +020028
Nils Diewald0e6992a2015-04-14 20:13:52 +000029 ldType : function (type) {
30 if (arguments.length === 1)
Akron8778f5d2017-06-30 21:25:55 +020031 this._ldType = type;
Nils Diewald0e6992a2015-04-14 20:13:52 +000032 return this._ldType;
33 },
34
Akron88d237e2020-10-21 08:05:18 +020035
Nils Diewald0e6992a2015-04-14 20:13:52 +000036 parent : function (obj) {
37 if (arguments.length === 1) {
Akron8778f5d2017-06-30 21:25:55 +020038 this._parent = obj;
39 this.__changed = true;
Nils Diewald0e6992a2015-04-14 20:13:52 +000040 };
41 return this._parent;
42 },
43
Akron88d237e2020-10-21 08:05:18 +020044
Nils Diewald0e6992a2015-04-14 20:13:52 +000045 // Destroy object - especially for
46 // acyclic structures!
47 // I'm paranoid!
48 destroy : function () {
Akron88d237e2020-10-21 08:05:18 +020049 const t = this;
50 if (t._ops != undefined) {
51 t._ops._parent = undefined;
Akron24aa0052020-11-10 11:00:34 +010052 if (t._ops._el !== undefined) {
53 t._ops._el.refTo = undefined;
Akronb19803c2018-08-16 16:39:42 +020054 };
Akron88d237e2020-10-21 08:05:18 +020055 t._ops = undefined;
Nils Diewald0e6992a2015-04-14 20:13:52 +000056 };
Akronb19803c2018-08-16 16:39:42 +020057
Akron24aa0052020-11-10 11:00:34 +010058 if (t._el !== undefined)
59 t._el = undefined;
Nils Diewald0e6992a2015-04-14 20:13:52 +000060
61 // In case of a group, destroy all operands
Akron88d237e2020-10-21 08:05:18 +020062 if (t._operands !== undefined) {
63 t._operands.forEach(i => i.destroy());
64 t._operands = [];
Nils Diewald0e6992a2015-04-14 20:13:52 +000065 };
66 },
67
68 // Wrap a new operation around the root group element
69 wrapOnRoot : function (op) {
Akron88d237e2020-10-21 08:05:18 +020070 const parent = this.parent();
71 const group = require('vc/docgroup').create(parent);
72
Nils Diewald0e6992a2015-04-14 20:13:52 +000073 if (arguments.length === 1)
Akron8778f5d2017-06-30 21:25:55 +020074 group.operation(op);
Nils Diewald0e6992a2015-04-14 20:13:52 +000075 else
Akron8778f5d2017-06-30 21:25:55 +020076 group.operation(
77 this.operation() === 'and' ? 'or' : 'and'
78 );
Nils Diewald0e6992a2015-04-14 20:13:52 +000079 group.append(this);
80 this.parent(group);
81 group.append();
82 group.element(); // Init (seems to be necessary)
83 parent.root(group);
84 return this.parent();
85 },
86
Akron88d237e2020-10-21 08:05:18 +020087
Nils Diewald0e6992a2015-04-14 20:13:52 +000088 // Be aware! This may be cyclic
89 operators : function (and, or, del) {
90 if (arguments === 0)
Akron8778f5d2017-06-30 21:25:55 +020091 return this._ops;
Akron88d237e2020-10-21 08:05:18 +020092
Nils Diewald0e6992a2015-04-14 20:13:52 +000093 this._ops = operatorsClass.create(
Akron8778f5d2017-06-30 21:25:55 +020094 and, or, del
Nils Diewald0e6992a2015-04-14 20:13:52 +000095 );
Akron88d237e2020-10-21 08:05:18 +020096
Nils Diewald0e6992a2015-04-14 20:13:52 +000097 this._ops.parent(this);
98 return this._ops;
99 },
100
Akron88d237e2020-10-21 08:05:18 +0200101
Nils Diewald0e6992a2015-04-14 20:13:52 +0000102 toJson : function () {
103 return {
Akron8778f5d2017-06-30 21:25:55 +0200104 // Unspecified object
105 "@type" : "koral:" + this.ldType()
Nils Diewald0e6992a2015-04-14 20:13:52 +0000106 };
107 },
108
Akron88d237e2020-10-21 08:05:18 +0200109
Akrond2474aa2018-08-28 12:06:27 +0200110 rewrites : function () {
111 return null;
112 },
113
Akron88d237e2020-10-21 08:05:18 +0200114
hebastaa0282be2018-12-05 16:58:00 +0100115 incomplete : function () {
116 return false;
117 },
118
Akron88d237e2020-10-21 08:05:18 +0200119
Nils Diewald0e6992a2015-04-14 20:13:52 +0000120 toQuery : function () {
121 return '';
122 }
123 };
124});