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