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 () { |
| 41 | if (this._ops != undefined) { |
Akron | 8778f5d | 2017-06-30 21:25:55 +0200 | [diff] [blame^] | 42 | this._ops._parent = undefined; |
| 43 | if (this._ops._element !== undefined) |
| 44 | this._ops._element.refTo = undefined; |
| 45 | this._ops = undefined; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 46 | }; |
| 47 | if (this._element !== undefined) |
Akron | 8778f5d | 2017-06-30 21:25:55 +0200 | [diff] [blame^] | 48 | this._element = undefined; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 49 | |
| 50 | // In case of a group, destroy all operands |
| 51 | if (this._operands !== undefined) { |
Akron | 8778f5d | 2017-06-30 21:25:55 +0200 | [diff] [blame^] | 52 | for (var i = 0; i < this._operands.length; i++) |
| 53 | this.getOperand(i).destroy(); |
| 54 | this._operands = []; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 55 | }; |
| 56 | }, |
| 57 | |
| 58 | // Wrap a new operation around the root group element |
| 59 | wrapOnRoot : function (op) { |
| 60 | var parent = this.parent(); |
| 61 | |
| 62 | var group = require('vc/docgroup').create(parent); |
| 63 | if (arguments.length === 1) |
Akron | 8778f5d | 2017-06-30 21:25:55 +0200 | [diff] [blame^] | 64 | group.operation(op); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 65 | else |
Akron | 8778f5d | 2017-06-30 21:25:55 +0200 | [diff] [blame^] | 66 | group.operation( |
| 67 | this.operation() === 'and' ? 'or' : 'and' |
| 68 | ); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 69 | group.append(this); |
| 70 | this.parent(group); |
| 71 | group.append(); |
| 72 | group.element(); // Init (seems to be necessary) |
| 73 | parent.root(group); |
| 74 | return this.parent(); |
| 75 | }, |
| 76 | |
| 77 | // Be aware! This may be cyclic |
| 78 | operators : function (and, or, del) { |
| 79 | if (arguments === 0) |
Akron | 8778f5d | 2017-06-30 21:25:55 +0200 | [diff] [blame^] | 80 | return this._ops; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 81 | this._ops = operatorsClass.create( |
Akron | 8778f5d | 2017-06-30 21:25:55 +0200 | [diff] [blame^] | 82 | and, or, del |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 83 | ); |
| 84 | this._ops.parent(this); |
| 85 | return this._ops; |
| 86 | }, |
| 87 | |
| 88 | toJson : function () { |
| 89 | return { |
Akron | 8778f5d | 2017-06-30 21:25:55 +0200 | [diff] [blame^] | 90 | // Unspecified object |
| 91 | "@type" : "koral:" + this.ldType() |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 92 | }; |
| 93 | }, |
| 94 | |
| 95 | toQuery : function () { |
| 96 | return ''; |
| 97 | } |
| 98 | }; |
| 99 | }); |