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