blob: fa82a4b85e69b26902738001a5b497de30460e95 [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 };
hebastaa0282be2018-12-05 16:58:00 +0100112 KorAP.vc.checkGrayingStat();
Akronb19803c2018-08-16 16:39:42 +0200113 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
Akronb19803c2018-08-16 16:39:42 +0200210 /**
211 * Click on the unspecified object
212 */
213 onclick : function () {
214 console.log("Do not support click on this");
215 },
216
217 // TODO: This is identical to doc.js
218 rewrites : function () {
219 return this._rewrites;
220 },
221
222 // TODO: This is identical to doc.js
223 rewrite : function (value) {
224 if (typeof value === 'string') {
225 value = [{
226 "@type" : "koral:rewrite",
227 "operation" : "operation:" + value,
228 "src" : "Kalamar"
229 }];
230 };
231 this._rewrites = rewriteListClass.create(value);
232 },
233
234
235 // Mark the underlying data as being changed.
236 // This is important for rerendering the dom.
237 // This will also remove rewrite markers, when the data
238 // change happened by the user
239 _changed : function () {
240 this.__changed = true;
241
242 if (this._rewrites === undefined)
243 return;
244
245 delete this["_rewrites"];
246
247 if (this._element === undefined)
248 return;
249
250 this._element.classList.remove("rewritten");
251 },
252
253 toJson : function () {
254 if (!this.ref)
255 return {};
256
257 return {
258 "@type" : "koral:" + this.ldType(),
259 "ref" : this.ref()
260 };
261 },
262
hebastaa0282be2018-12-05 16:58:00 +0100263
264 incomplete : function () {
265 return this.ref() ? false : true
266 },
Akronb19803c2018-08-16 16:39:42 +0200267
268 toQuery : function () {
hebastaa0282be2018-12-05 16:58:00 +0100269 if (this.incomplete())
Akronb19803c2018-08-16 16:39:42 +0200270 return "";
271
272 // Build doc string based on key
273 return 'referTo "' + this.ref().quote() + '"';
274 }
275 };
276});