blob: 7d3ecea6e1436465b714de050f7f31335d64a4aa [file] [log] [blame]
Akronb19803c2018-08-16 16:39:42 +02001/**
2 * A reference to another VC.
3 * Inherits everything from jsonld
4 */
5define([
6 'vc/jsonld',
7 'vc/rewritelist',
8 'vc/stringval',
9 'util'
10], function (jsonldClass, rewriteListClass, stringValClass) {
11
Akrond2474aa2018-08-28 12:06:27 +020012 // TODO:
13 // Does not support rewrites currently
14
Akronb19803c2018-08-16 16:39:42 +020015 const loc = KorAP.Locale;
16 loc.EMPTY = loc.EMPTY || '⋯';
17
18 return {
19
20 // The ld-type
21 _ldType : "docGroupRef",
22
23 /**
24 * Create new unspecified criterion
25 * with a link to the parent object
26 */
27 create : function (parent, json) {
28 var obj = Object(jsonldClass)
29 .create().
30 upgradeTo(this)
31 .fromJson(json);
32
33 if (obj === undefined) {
34 console.log(json);
35 };
36
37 if (parent !== undefined)
38 obj._parent = parent;
39
40 obj.__changed = true;
41 return obj;
42 },
43
44
45 /**
46 * Update the element
47 */
48 update : function () {
49 if (this._element === undefined)
50 return this.element();
51
52 var e = this._element;
53
54 // Check if there is a change in the underlying data
55 if (!this.__changed)
56 return e;
57
58 // Set ref - TODO: Cleanup!
59 e.refTo = this;
60
61 // Was rewritten
62 if (this.rewrites() !== undefined) {
63 e.classList.add("rewritten");
64 };
65
66 var refTitle = document.createElement('span');
Akron3ad46942018-08-22 16:47:14 +020067 refTitle.classList.add('key','fixed', 'ref');
68 refTitle.addT('referTo');
Akronb19803c2018-08-16 16:39:42 +020069
70 // Added value operator
71 this._refE = document.createElement('span');
72 this._refE.setAttribute('data-type', "string");
73 this._refE.setAttribute('class', 'value');
74 if (this.ref()) {
75 this._refE.addT(this.ref());
76 }
77 else {
78 this._refE.addT(loc.EMPTY);
Akron62ac95b2018-08-30 18:08:25 +020079 this._refE.classList.add('unspecified');
Akronb19803c2018-08-16 16:39:42 +020080 };
81
82 // Change value
83 this._refE.addEventListener(
84 'click',
85 this._changeRef.bind(this)
86 );
87
88 // Remove all element children
89 _removeChildren(e);
90
91 // Add spans
92 e.appendChild(refTitle);
93 e.appendChild(this._refE);
94
95 this.__changed = false;
96
97 if (this._rewrites !== undefined) {
98 e.appendChild(this._rewrites.element());
99 };
100
101 if (this._parent !== undefined) {
102 // Set operators
103 var op = this.operators(
104 true,
105 true,
106 true
107 );
108
109 // Append new operators
110 e.appendChild(op.element());
111 };
112
113 return this.element();
114 },
115
116
117 /**
118 * Get the associated element
119 */
120 element : function () {
121 if (this._element !== undefined)
122 return this._element;
123 this._element = document.createElement('div');
124 this._element.setAttribute('class', 'doc groupref');
125 this.update();
126 return this._element;
127 },
128
129
130 /**
131 * Get or set the value
132 */
133 ref : function (ref) {
134 if (arguments.length === 1) {
135 this._ref = ref;
136 this._changed();
137 return this;
138 };
139 return this._ref;
140 },
141
142
143 // Click on the reference operator, show me the option
144 _changeRef : function (e) {
145 var that = this;
146
147 var str = stringValClass.create(this.ref(), false, false);
148 var strElem = str.element();
149
150 str.store = function (ref, regex) {
151 that.ref(ref);
152
153 that._element.removeChild(
154 this._element
155 );
156 that.update();
157 };
158
159 // Insert element
160 this._element.insertBefore(
161 strElem,
162 this._refE
163 );
164
165 str.focus();
166 },
167
168
169 /**
170 * Wrap a new operation around the doc element.
171 * This is copypasta from doc.js
172 */
173 wrap : function (op) {
174 var parent = this.parent();
175 var group = require('vc/docgroup').create(parent);
176 group.operation(op);
177 group.append(this);
178 group.append();
179 return parent.replaceOperand(this, group).update();
180 },
181
182 /**
183 * Deserialize from json
184 */
185 fromJson : function (json) {
186 if (json === undefined)
187 return this;
188
189 if (json["@type"] === undefined) {
190 KorAP.log(701, "JSON-LD group has no @type attribute");
191 return;
192 };
193
194 if (json["ref"] === undefined ||
195 typeof json["ref"] != 'string') {
196 KorAP.log(821, "Reference is missing");
197 return;
198 };
199
200 this.ref(json["ref"]);
201
202 // Rewrite coming from the server
203 if (json["rewrites"] !== undefined) {
204 this.rewrite(json["rewrites"]);
205 };
206
207 return this;
208 },
209
210
211 /**
212 * Click on the unspecified object
213 */
214 onclick : function () {
215 console.log("Do not support click on this");
216 },
217
218 // TODO: This is identical to doc.js
219 rewrites : function () {
220 return this._rewrites;
221 },
222
223 // TODO: This is identical to doc.js
224 rewrite : function (value) {
225 if (typeof value === 'string') {
226 value = [{
227 "@type" : "koral:rewrite",
228 "operation" : "operation:" + value,
229 "src" : "Kalamar"
230 }];
231 };
232 this._rewrites = rewriteListClass.create(value);
233 },
234
235
236 // Mark the underlying data as being changed.
237 // This is important for rerendering the dom.
238 // This will also remove rewrite markers, when the data
239 // change happened by the user
240 _changed : function () {
241 this.__changed = true;
242
243 if (this._rewrites === undefined)
244 return;
245
246 delete this["_rewrites"];
247
248 if (this._element === undefined)
249 return;
250
251 this._element.classList.remove("rewritten");
252 },
253
254 toJson : function () {
255 if (!this.ref)
256 return {};
257
258 return {
259 "@type" : "koral:" + this.ldType(),
260 "ref" : this.ref()
261 };
262 },
263
264
265 toQuery : function () {
266 if (!this.ref())
267 return "";
268
269 // Build doc string based on key
270 return 'referTo "' + this.ref().quote() + '"';
271 }
272 };
273});