Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 1 | /** |
| 2 | * Implementation of rewrite objects. |
| 3 | */ |
| 4 | define(['vc/jsonld', 'util'], function (jsonldClass) { |
| 5 | |
Akron | 7ae79aa | 2017-06-01 16:14:29 +0200 | [diff] [blame] | 6 | // injection, modification, and deletion should probably be enough |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 7 | const _validRewriteOpRE = |
| 8 | new RegExp("^(operation:)?(?:injec|inser|modifica|dele)tion|override$"); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 9 | |
| 10 | return { |
| 11 | // Construction method |
| 12 | create : function (json) { |
| 13 | var obj = Object(jsonldClass). |
Akron | 625fe0c | 2017-05-03 16:15:08 +0200 | [diff] [blame] | 14 | create(). |
| 15 | upgradeTo(this). |
| 16 | fromJson(json); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 17 | return obj; |
| 18 | }, |
| 19 | |
| 20 | // Get or set source |
| 21 | src : function (string) { |
| 22 | if (arguments.length === 1) |
Akron | 625fe0c | 2017-05-03 16:15:08 +0200 | [diff] [blame] | 23 | this._src = string; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 24 | return this._src; |
| 25 | }, |
| 26 | |
| 27 | // Get or set operation |
| 28 | operation : function (op) { |
| 29 | if (arguments.length === 1) { |
Akron | 625fe0c | 2017-05-03 16:15:08 +0200 | [diff] [blame] | 30 | if (_validRewriteOpRE.test(op)) { |
| 31 | this._op = op; |
| 32 | } |
Akron | 4ae45c1 | 2017-05-02 16:13:08 +0200 | [diff] [blame] | 33 | else { |
| 34 | KorAP.log(814, "Unknown rewrite operation"); |
Akron | 7ae79aa | 2017-06-01 16:14:29 +0200 | [diff] [blame] | 35 | return; |
| 36 | }; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 37 | }; |
| 38 | return this._op || 'injection'; |
| 39 | }, |
| 40 | |
| 41 | // Get or set scope |
| 42 | scope : function (attr) { |
| 43 | if (arguments.length === 1) |
Akron | 7ae79aa | 2017-06-01 16:14:29 +0200 | [diff] [blame] | 44 | this._scope = attr; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 45 | return this._scope; |
| 46 | }, |
| 47 | |
| 48 | // Serialize from Json |
| 49 | fromJson : function (json) { |
| 50 | if (json === undefined) |
Akron | 7ae79aa | 2017-06-01 16:14:29 +0200 | [diff] [blame] | 51 | return this; |
| 52 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 53 | // Missing @type |
| 54 | if (json["@type"] === undefined) { |
Akron | 7ae79aa | 2017-06-01 16:14:29 +0200 | [diff] [blame] | 55 | KorAP.log(701, "JSON-LD group has no @type attribute"); |
| 56 | return; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | // Missing source |
| 60 | if (json["src"] === undefined || |
Akron | 7ae79aa | 2017-06-01 16:14:29 +0200 | [diff] [blame] | 61 | typeof json["src"] !== 'string') { |
| 62 | KorAP.log(815, "Rewrite expects source"); |
| 63 | return; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | // Set source |
| 67 | this.src(json["src"]); |
| 68 | |
| 69 | // Set operation |
| 70 | if (json["operation"] !== undefined) { |
Akron | 7ae79aa | 2017-06-01 16:14:29 +0200 | [diff] [blame] | 71 | var operation = json["operation"]; |
| 72 | this.operation(operation.replace(/^operation:/,'')); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | // Set scope |
| 76 | if (json["scope"] !== undefined && |
Akron | 7ae79aa | 2017-06-01 16:14:29 +0200 | [diff] [blame] | 77 | typeof json["scope"] === 'string') |
| 78 | this.scope(json["scope"]); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 79 | |
| 80 | return this; |
| 81 | }, |
| 82 | |
| 83 | toString : function () { |
| 84 | var str = ''; |
| 85 | var op = this.operation(); |
| 86 | str += op.charAt(0).toUpperCase() + op.slice(1); |
| 87 | str += ' of ' + ( |
Akron | 7ae79aa | 2017-06-01 16:14:29 +0200 | [diff] [blame] | 88 | this._scope === null ? |
| 89 | 'object' : |
Akron | 0c4cd22 | 2019-07-19 16:33:34 +0200 | [diff] [blame] | 90 | this.scope().quote() |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 91 | ); |
Akron | 0c4cd22 | 2019-07-19 16:33:34 +0200 | [diff] [blame] | 92 | str += ' by ' + this.src().quote(); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 93 | return str; |
| 94 | } |
| 95 | }; |
| 96 | }); |