blob: 78d2627869522486ece5f5742b154c7bd00ec333 [file] [log] [blame]
Nils Diewald0e6992a2015-04-14 20:13:52 +00001/**
2 * Implementation of rewrite objects.
3 */
4define(['vc/jsonld', 'util'], function (jsonldClass) {
5
Akron625fe0c2017-05-03 16:15:08 +02006 var _validRewriteOpRE = new RegExp("^(operation:)?(?:injec|inser|modifica)tion$");
Nils Diewald0e6992a2015-04-14 20:13:52 +00007
8 return {
9 // Construction method
10 create : function (json) {
11 var obj = Object(jsonldClass).
Akron625fe0c2017-05-03 16:15:08 +020012 create().
13 upgradeTo(this).
14 fromJson(json);
Nils Diewald0e6992a2015-04-14 20:13:52 +000015 return obj;
16 },
17
18 // Get or set source
19 src : function (string) {
20 if (arguments.length === 1)
Akron625fe0c2017-05-03 16:15:08 +020021 this._src = string;
Nils Diewald0e6992a2015-04-14 20:13:52 +000022 return this._src;
23 },
24
25 // Get or set operation
26 operation : function (op) {
27 if (arguments.length === 1) {
Akron625fe0c2017-05-03 16:15:08 +020028 if (_validRewriteOpRE.test(op)) {
29 this._op = op;
30 }
Akron4ae45c12017-05-02 16:13:08 +020031 else {
32 KorAP.log(814, "Unknown rewrite operation");
Nils Diewald0e6992a2015-04-14 20:13:52 +000033 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 Diewald7c8ced22015-04-15 19:21:00 +000089 this.scope().quote() +
Nils Diewald0e6992a2015-04-14 20:13:52 +000090 '"'
91 );
92 str += ' by ' +
93 '"' +
Nils Diewald7c8ced22015-04-15 19:21:00 +000094 this.src().quote() +
Nils Diewald0e6992a2015-04-14 20:13:52 +000095 '"';
96 return str;
97 }
98 };
99});