Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 1 | /** |
| 2 | * Abstract JsonLD criterion object |
| 3 | */ |
| 4 | define(['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) { |
Akron | 8778f5d | 2017-06-30 21:25:55 +0200 | [diff] [blame] | 18 | this[prop] = props[prop]; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 19 | }; |
| 20 | return this; |
| 21 | }, |
| 22 | |
| 23 | ldType : function (type) { |
| 24 | if (arguments.length === 1) |
Akron | 8778f5d | 2017-06-30 21:25:55 +0200 | [diff] [blame] | 25 | this._ldType = type; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 26 | return this._ldType; |
| 27 | }, |
| 28 | |
| 29 | parent : function (obj) { |
| 30 | if (arguments.length === 1) { |
Akron | 8778f5d | 2017-06-30 21:25:55 +0200 | [diff] [blame] | 31 | this._parent = obj; |
| 32 | this.__changed = true; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 33 | }; |
| 34 | return this._parent; |
| 35 | }, |
| 36 | |
| 37 | // Destroy object - especially for |
| 38 | // acyclic structures! |
| 39 | // I'm paranoid! |
| 40 | destroy : function () { |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 41 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 42 | if (this._ops != undefined) { |
Akron | 8778f5d | 2017-06-30 21:25:55 +0200 | [diff] [blame] | 43 | this._ops._parent = undefined; |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 44 | if (this._ops._element !== undefined) { |
Akron | 8778f5d | 2017-06-30 21:25:55 +0200 | [diff] [blame] | 45 | this._ops._element.refTo = undefined; |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 46 | }; |
Akron | 8778f5d | 2017-06-30 21:25:55 +0200 | [diff] [blame] | 47 | this._ops = undefined; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 48 | }; |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 49 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 50 | if (this._element !== undefined) |
Akron | 8778f5d | 2017-06-30 21:25:55 +0200 | [diff] [blame] | 51 | this._element = undefined; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 52 | |
| 53 | // In case of a group, destroy all operands |
| 54 | if (this._operands !== undefined) { |
Akron | b50964a | 2020-10-12 11:44:37 +0200 | [diff] [blame^] | 55 | this._operands.forEach(i => i.destroy()); |
Akron | 8778f5d | 2017-06-30 21:25:55 +0200 | [diff] [blame] | 56 | this._operands = []; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 57 | }; |
| 58 | }, |
| 59 | |
| 60 | // Wrap a new operation around the root group element |
| 61 | wrapOnRoot : function (op) { |
| 62 | var parent = this.parent(); |
| 63 | |
| 64 | var group = require('vc/docgroup').create(parent); |
| 65 | if (arguments.length === 1) |
Akron | 8778f5d | 2017-06-30 21:25:55 +0200 | [diff] [blame] | 66 | group.operation(op); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 67 | else |
Akron | 8778f5d | 2017-06-30 21:25:55 +0200 | [diff] [blame] | 68 | group.operation( |
| 69 | this.operation() === 'and' ? 'or' : 'and' |
| 70 | ); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 71 | group.append(this); |
| 72 | this.parent(group); |
| 73 | group.append(); |
| 74 | group.element(); // Init (seems to be necessary) |
| 75 | parent.root(group); |
| 76 | return this.parent(); |
| 77 | }, |
| 78 | |
| 79 | // Be aware! This may be cyclic |
| 80 | operators : function (and, or, del) { |
| 81 | if (arguments === 0) |
Akron | 8778f5d | 2017-06-30 21:25:55 +0200 | [diff] [blame] | 82 | return this._ops; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 83 | this._ops = operatorsClass.create( |
Akron | 8778f5d | 2017-06-30 21:25:55 +0200 | [diff] [blame] | 84 | and, or, del |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 85 | ); |
| 86 | this._ops.parent(this); |
| 87 | return this._ops; |
| 88 | }, |
| 89 | |
| 90 | toJson : function () { |
| 91 | return { |
Akron | 8778f5d | 2017-06-30 21:25:55 +0200 | [diff] [blame] | 92 | // Unspecified object |
| 93 | "@type" : "koral:" + this.ldType() |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 94 | }; |
| 95 | }, |
| 96 | |
Akron | d2474aa | 2018-08-28 12:06:27 +0200 | [diff] [blame] | 97 | rewrites : function () { |
| 98 | return null; |
| 99 | }, |
| 100 | |
hebasta | a0282be | 2018-12-05 16:58:00 +0100 | [diff] [blame] | 101 | incomplete : function () { |
| 102 | return false; |
| 103 | }, |
| 104 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 105 | toQuery : function () { |
| 106 | return ''; |
| 107 | } |
| 108 | }; |
| 109 | }); |