blob: 3b7bd6db7d50336fcd897aa1a71f4641f952d456 [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 };
hebasta48842cf2018-12-11 12:57:38 +0100112
113 var vcchevent = new CustomEvent('vcChange', {'detail':this});
114 document.dispatchEvent(vcchevent);
115
Akronb19803c2018-08-16 16:39:42 +0200116 return this.element();
117 },
118
119
120 /**
121 * Get the associated element
122 */
123 element : function () {
124 if (this._element !== undefined)
125 return this._element;
126 this._element = document.createElement('div');
127 this._element.setAttribute('class', 'doc groupref');
128 this.update();
129 return this._element;
130 },
131
132
133 /**
134 * Get or set the value
135 */
136 ref : function (ref) {
137 if (arguments.length === 1) {
138 this._ref = ref;
139 this._changed();
140 return this;
141 };
142 return this._ref;
143 },
144
145
146 // Click on the reference operator, show me the option
147 _changeRef : function (e) {
148 var that = this;
149
150 var str = stringValClass.create(this.ref(), false, false);
151 var strElem = str.element();
152
153 str.store = function (ref, regex) {
154 that.ref(ref);
155
156 that._element.removeChild(
157 this._element
158 );
159 that.update();
160 };
161
162 // Insert element
163 this._element.insertBefore(
164 strElem,
165 this._refE
166 );
167
168 str.focus();
169 },
170
171
172 /**
173 * Wrap a new operation around the doc element.
174 * This is copypasta from doc.js
175 */
176 wrap : function (op) {
177 var parent = this.parent();
178 var group = require('vc/docgroup').create(parent);
179 group.operation(op);
180 group.append(this);
181 group.append();
182 return parent.replaceOperand(this, group).update();
183 },
184
185 /**
186 * Deserialize from json
187 */
188 fromJson : function (json) {
189 if (json === undefined)
190 return this;
191
192 if (json["@type"] === undefined) {
193 KorAP.log(701, "JSON-LD group has no @type attribute");
194 return;
195 };
196
197 if (json["ref"] === undefined ||
198 typeof json["ref"] != 'string') {
199 KorAP.log(821, "Reference is missing");
200 return;
201 };
202
203 this.ref(json["ref"]);
204
205 // Rewrite coming from the server
206 if (json["rewrites"] !== undefined) {
207 this.rewrite(json["rewrites"]);
208 };
209
210 return this;
211 },
212
Akronb19803c2018-08-16 16:39:42 +0200213 /**
214 * Click on the unspecified object
215 */
216 onclick : function () {
217 console.log("Do not support click on this");
218 },
219
220 // TODO: This is identical to doc.js
221 rewrites : function () {
222 return this._rewrites;
223 },
224
225 // TODO: This is identical to doc.js
226 rewrite : function (value) {
227 if (typeof value === 'string') {
228 value = [{
229 "@type" : "koral:rewrite",
230 "operation" : "operation:" + value,
231 "src" : "Kalamar"
232 }];
233 };
234 this._rewrites = rewriteListClass.create(value);
235 },
236
237
238 // Mark the underlying data as being changed.
239 // This is important for rerendering the dom.
240 // This will also remove rewrite markers, when the data
241 // change happened by the user
242 _changed : function () {
243 this.__changed = true;
244
245 if (this._rewrites === undefined)
246 return;
247
248 delete this["_rewrites"];
249
250 if (this._element === undefined)
251 return;
252
253 this._element.classList.remove("rewritten");
254 },
255
256 toJson : function () {
257 if (!this.ref)
258 return {};
259
260 return {
261 "@type" : "koral:" + this.ldType(),
262 "ref" : this.ref()
263 };
264 },
265
hebastaa0282be2018-12-05 16:58:00 +0100266
267 incomplete : function () {
268 return this.ref() ? false : true
269 },
Akronb19803c2018-08-16 16:39:42 +0200270
271 toQuery : function () {
hebastaa0282be2018-12-05 16:58:00 +0100272 if (this.incomplete())
Akronb19803c2018-08-16 16:39:42 +0200273 return "";
274
275 // Build doc string based on key
276 return 'referTo "' + this.ref().quote() + '"';
277 }
278 };
279});