blob: b577f148f568055e7df60e4e0bc58f14faf1bf2f [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
7 var _validRewriteOpRE = new RegExp("^(operation:)?(?:injec|inser|modifica|dele)tion|override$");
Nils Diewald0e6992a2015-04-14 20:13:52 +00008
9 return {
10 // Construction method
11 create : function (json) {
12 var obj = Object(jsonldClass).
Akron625fe0c2017-05-03 16:15:08 +020013 create().
14 upgradeTo(this).
15 fromJson(json);
Nils Diewald0e6992a2015-04-14 20:13:52 +000016 return obj;
17 },
18
19 // Get or set source
20 src : function (string) {
21 if (arguments.length === 1)
Akron625fe0c2017-05-03 16:15:08 +020022 this._src = string;
Nils Diewald0e6992a2015-04-14 20:13:52 +000023 return this._src;
24 },
25
26 // Get or set operation
27 operation : function (op) {
28 if (arguments.length === 1) {
Akron625fe0c2017-05-03 16:15:08 +020029 if (_validRewriteOpRE.test(op)) {
30 this._op = op;
31 }
Akron4ae45c12017-05-02 16:13:08 +020032 else {
33 KorAP.log(814, "Unknown rewrite operation");
Akron7ae79aa2017-06-01 16:14:29 +020034 return;
35 };
Nils Diewald0e6992a2015-04-14 20:13:52 +000036 };
37 return this._op || 'injection';
38 },
39
40 // Get or set scope
41 scope : function (attr) {
42 if (arguments.length === 1)
Akron7ae79aa2017-06-01 16:14:29 +020043 this._scope = attr;
Nils Diewald0e6992a2015-04-14 20:13:52 +000044 return this._scope;
45 },
46
47 // Serialize from Json
48 fromJson : function (json) {
49 if (json === undefined)
Akron7ae79aa2017-06-01 16:14:29 +020050 return this;
51
Nils Diewald0e6992a2015-04-14 20:13:52 +000052 // Missing @type
53 if (json["@type"] === undefined) {
Akron7ae79aa2017-06-01 16:14:29 +020054 KorAP.log(701, "JSON-LD group has no @type attribute");
55 return;
Nils Diewald0e6992a2015-04-14 20:13:52 +000056 };
57
58 // Missing source
59 if (json["src"] === undefined ||
Akron7ae79aa2017-06-01 16:14:29 +020060 typeof json["src"] !== 'string') {
61 KorAP.log(815, "Rewrite expects source");
62 return;
Nils Diewald0e6992a2015-04-14 20:13:52 +000063 };
64
65 // Set source
66 this.src(json["src"]);
67
68 // Set operation
69 if (json["operation"] !== undefined) {
Akron7ae79aa2017-06-01 16:14:29 +020070 var operation = json["operation"];
71 this.operation(operation.replace(/^operation:/,''));
Nils Diewald0e6992a2015-04-14 20:13:52 +000072 };
73
74 // Set scope
75 if (json["scope"] !== undefined &&
Akron7ae79aa2017-06-01 16:14:29 +020076 typeof json["scope"] === 'string')
77 this.scope(json["scope"]);
Nils Diewald0e6992a2015-04-14 20:13:52 +000078
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 ' + (
Akron7ae79aa2017-06-01 16:14:29 +020087 this._scope === null ?
88 'object' :
89 '"' +
90 this.scope().quote() +
91 '"'
Nils Diewald0e6992a2015-04-14 20:13:52 +000092 );
93 str += ' by ' +
Akron7ae79aa2017-06-01 16:14:29 +020094 '"' +
95 this.src().quote() +
96 '"';
Nils Diewald0e6992a2015-04-14 20:13:52 +000097 return str;
98 }
99 };
100});