blob: 5af4b3587ca8471ebb88cc8ae4c65871f2d3a715 [file] [log] [blame]
Nils Diewald0e6992a2015-04-14 20:13:52 +00001/**
2 * Implementation of rewrite objects.
3 */
Akron88d237e2020-10-21 08:05:18 +02004"use strict";
5
Nils Diewald0e6992a2015-04-14 20:13:52 +00006define(['vc/jsonld', 'util'], function (jsonldClass) {
7
Akron7ae79aa2017-06-01 16:14:29 +02008 // injection, modification, and deletion should probably be enough
Akron0b489ad2018-02-02 16:49:32 +01009 const _validRewriteOpRE =
10 new RegExp("^(operation:)?(?:injec|inser|modifica|dele)tion|override$");
Nils Diewald0e6992a2015-04-14 20:13:52 +000011
12 return {
Akron88d237e2020-10-21 08:05:18 +020013
Nils Diewald0e6992a2015-04-14 20:13:52 +000014 // Construction method
15 create : function (json) {
Akron88d237e2020-10-21 08:05:18 +020016 return Object(jsonldClass).
17 create().
18 upgradeTo(this).
19 fromJson(json);
Nils Diewald0e6992a2015-04-14 20:13:52 +000020 },
21
Akron88d237e2020-10-21 08:05:18 +020022
Nils Diewald0e6992a2015-04-14 20:13:52 +000023 // Get or set source
24 src : function (string) {
25 if (arguments.length === 1)
Akron625fe0c2017-05-03 16:15:08 +020026 this._src = string;
Nils Diewald0e6992a2015-04-14 20:13:52 +000027 return this._src;
28 },
29
Akron88d237e2020-10-21 08:05:18 +020030
Nils Diewald0e6992a2015-04-14 20:13:52 +000031 // Get or set operation
32 operation : function (op) {
33 if (arguments.length === 1) {
Akron625fe0c2017-05-03 16:15:08 +020034 if (_validRewriteOpRE.test(op)) {
35 this._op = op;
36 }
Akron4ae45c12017-05-02 16:13:08 +020037 else {
38 KorAP.log(814, "Unknown rewrite operation");
Akron7ae79aa2017-06-01 16:14:29 +020039 return;
40 };
Nils Diewald0e6992a2015-04-14 20:13:52 +000041 };
42 return this._op || 'injection';
43 },
44
Akron88d237e2020-10-21 08:05:18 +020045
Nils Diewald0e6992a2015-04-14 20:13:52 +000046 // Get or set scope
47 scope : function (attr) {
48 if (arguments.length === 1)
Akron7ae79aa2017-06-01 16:14:29 +020049 this._scope = attr;
Nils Diewald0e6992a2015-04-14 20:13:52 +000050 return this._scope;
51 },
52
Akron88d237e2020-10-21 08:05:18 +020053
Nils Diewald0e6992a2015-04-14 20:13:52 +000054 // Serialize from Json
55 fromJson : function (json) {
56 if (json === undefined)
Akron7ae79aa2017-06-01 16:14:29 +020057 return this;
58
Nils Diewald0e6992a2015-04-14 20:13:52 +000059 // Missing @type
60 if (json["@type"] === undefined) {
Akron7ae79aa2017-06-01 16:14:29 +020061 KorAP.log(701, "JSON-LD group has no @type attribute");
62 return;
Nils Diewald0e6992a2015-04-14 20:13:52 +000063 };
64
65 // Missing source
66 if (json["src"] === undefined ||
Akron7ae79aa2017-06-01 16:14:29 +020067 typeof json["src"] !== 'string') {
68 KorAP.log(815, "Rewrite expects source");
69 return;
Nils Diewald0e6992a2015-04-14 20:13:52 +000070 };
71
72 // Set source
73 this.src(json["src"]);
74
75 // Set operation
76 if (json["operation"] !== undefined) {
Akron88d237e2020-10-21 08:05:18 +020077 this.operation(json["operation"].replace(/^operation:/,''));
Nils Diewald0e6992a2015-04-14 20:13:52 +000078 };
79
80 // Set scope
81 if (json["scope"] !== undefined &&
Akron7ae79aa2017-06-01 16:14:29 +020082 typeof json["scope"] === 'string')
83 this.scope(json["scope"]);
Nils Diewald0e6992a2015-04-14 20:13:52 +000084
85 return this;
86 },
87
Akron88d237e2020-10-21 08:05:18 +020088
Nils Diewald0e6992a2015-04-14 20:13:52 +000089 toString : function () {
Akron88d237e2020-10-21 08:05:18 +020090 let str = '';
91 const op = this.operation();
Nils Diewald0e6992a2015-04-14 20:13:52 +000092 str += op.charAt(0).toUpperCase() + op.slice(1);
93 str += ' of ' + (
Akron7ae79aa2017-06-01 16:14:29 +020094 this._scope === null ?
95 'object' :
Akron0c4cd222019-07-19 16:33:34 +020096 this.scope().quote()
Nils Diewald0e6992a2015-04-14 20:13:52 +000097 );
Akron0c4cd222019-07-19 16:33:34 +020098 str += ' by ' + this.src().quote();
Nils Diewald0e6992a2015-04-14 20:13:52 +000099 return str;
100 }
101 };
102});