blob: 150ee8746d964b170453847c33f86abdfc9d7738 [file] [log] [blame]
Nils Diewald0e6992a2015-04-14 20:13:52 +00001/**
2 * Implementation of rewrite objects.
3 */
4define(['vc/jsonld', 'util'], function (jsonldClass) {
5
Akron7ae79aa2017-06-01 16:14:29 +02006 // injection, modification, and deletion should probably be enough
Akron0b489ad2018-02-02 16:49:32 +01007 const _validRewriteOpRE =
8 new RegExp("^(operation:)?(?:injec|inser|modifica|dele)tion|override$");
Nils Diewald0e6992a2015-04-14 20:13:52 +00009
10 return {
11 // Construction method
12 create : function (json) {
13 var obj = Object(jsonldClass).
Akron625fe0c2017-05-03 16:15:08 +020014 create().
15 upgradeTo(this).
16 fromJson(json);
Nils Diewald0e6992a2015-04-14 20:13:52 +000017 return obj;
18 },
19
20 // Get or set source
21 src : function (string) {
22 if (arguments.length === 1)
Akron625fe0c2017-05-03 16:15:08 +020023 this._src = string;
Nils Diewald0e6992a2015-04-14 20:13:52 +000024 return this._src;
25 },
26
27 // Get or set operation
28 operation : function (op) {
29 if (arguments.length === 1) {
Akron625fe0c2017-05-03 16:15:08 +020030 if (_validRewriteOpRE.test(op)) {
31 this._op = op;
32 }
Akron4ae45c12017-05-02 16:13:08 +020033 else {
34 KorAP.log(814, "Unknown rewrite operation");
Akron7ae79aa2017-06-01 16:14:29 +020035 return;
36 };
Nils Diewald0e6992a2015-04-14 20:13:52 +000037 };
38 return this._op || 'injection';
39 },
40
41 // Get or set scope
42 scope : function (attr) {
43 if (arguments.length === 1)
Akron7ae79aa2017-06-01 16:14:29 +020044 this._scope = attr;
Nils Diewald0e6992a2015-04-14 20:13:52 +000045 return this._scope;
46 },
47
48 // Serialize from Json
49 fromJson : function (json) {
50 if (json === undefined)
Akron7ae79aa2017-06-01 16:14:29 +020051 return this;
52
Nils Diewald0e6992a2015-04-14 20:13:52 +000053 // Missing @type
54 if (json["@type"] === undefined) {
Akron7ae79aa2017-06-01 16:14:29 +020055 KorAP.log(701, "JSON-LD group has no @type attribute");
56 return;
Nils Diewald0e6992a2015-04-14 20:13:52 +000057 };
58
59 // Missing source
60 if (json["src"] === undefined ||
Akron7ae79aa2017-06-01 16:14:29 +020061 typeof json["src"] !== 'string') {
62 KorAP.log(815, "Rewrite expects source");
63 return;
Nils Diewald0e6992a2015-04-14 20:13:52 +000064 };
65
66 // Set source
67 this.src(json["src"]);
68
69 // Set operation
70 if (json["operation"] !== undefined) {
Akron7ae79aa2017-06-01 16:14:29 +020071 var operation = json["operation"];
72 this.operation(operation.replace(/^operation:/,''));
Nils Diewald0e6992a2015-04-14 20:13:52 +000073 };
74
75 // Set scope
76 if (json["scope"] !== undefined &&
Akron7ae79aa2017-06-01 16:14:29 +020077 typeof json["scope"] === 'string')
78 this.scope(json["scope"]);
Nils Diewald0e6992a2015-04-14 20:13:52 +000079
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 ' + (
Akron7ae79aa2017-06-01 16:14:29 +020088 this._scope === null ?
89 'object' :
Akron0c4cd222019-07-19 16:33:34 +020090 this.scope().quote()
Nils Diewald0e6992a2015-04-14 20:13:52 +000091 );
Akron0c4cd222019-07-19 16:33:34 +020092 str += ' by ' + this.src().quote();
Nils Diewald0e6992a2015-04-14 20:13:52 +000093 return str;
94 }
95 };
96});