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