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