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