blob: 3709c63ea19b69d5f66e52601168436cae87b4ea [file] [log] [blame]
Akronb19803c2018-08-16 16:39:42 +02001/**
2 * A reference to another VC.
3 * Inherits everything from jsonld
4 */
Akrone51eaa32020-11-10 09:35:53 +01005"use strict";
6
Akronb19803c2018-08-16 16:39:42 +02007define([
8 'vc/jsonld',
9 'vc/rewritelist',
10 'vc/stringval',
11 'util'
12], function (jsonldClass, rewriteListClass, stringValClass) {
13
Akrond2474aa2018-08-28 12:06:27 +020014 // TODO:
15 // Does not support rewrites currently
16
Akronb19803c2018-08-16 16:39:42 +020017 const loc = KorAP.Locale;
18 loc.EMPTY = loc.EMPTY || '⋯';
19
20 return {
21
22 // The ld-type
23 _ldType : "docGroupRef",
24
25 /**
26 * Create new unspecified criterion
27 * with a link to the parent object
28 */
29 create : function (parent, json) {
Akron88d237e2020-10-21 08:05:18 +020030 const obj = Object(jsonldClass).
31 create().
32 upgradeTo(this).
33 fromJson(json);
34
35 if (obj === undefined)
Akronb19803c2018-08-16 16:39:42 +020036 console.log(json);
Akronb19803c2018-08-16 16:39:42 +020037
38 if (parent !== undefined)
39 obj._parent = parent;
Akron88d237e2020-10-21 08:05:18 +020040
Akronb19803c2018-08-16 16:39:42 +020041 obj.__changed = true;
42 return obj;
43 },
44
45
46 /**
47 * Update the element
48 */
49 update : function () {
Akron88d237e2020-10-21 08:05:18 +020050 const t = this;
Akronb19803c2018-08-16 16:39:42 +020051
Akron24aa0052020-11-10 11:00:34 +010052 if (t._el === undefined)
Akron88d237e2020-10-21 08:05:18 +020053 return t.element();
54
Akron24aa0052020-11-10 11:00:34 +010055 const e = t._el;
Akronb19803c2018-08-16 16:39:42 +020056
57 // Check if there is a change in the underlying data
Akron88d237e2020-10-21 08:05:18 +020058 if (!t.__changed)
Akronb19803c2018-08-16 16:39:42 +020059 return e;
60
61 // Set ref - TODO: Cleanup!
Akron88d237e2020-10-21 08:05:18 +020062 e.refTo = t;
Akron0f00b772020-12-02 17:32:13 +010063
Akronb19803c2018-08-16 16:39:42 +020064 // Was rewritten
Akron88d237e2020-10-21 08:05:18 +020065 if (t.rewrites() !== undefined) {
Akronb19803c2018-08-16 16:39:42 +020066 e.classList.add("rewritten");
67 };
Akron0f00b772020-12-02 17:32:13 +010068
Akron88d237e2020-10-21 08:05:18 +020069 const refTitle = document.createElement('span');
Akron3ad46942018-08-22 16:47:14 +020070 refTitle.classList.add('key','fixed', 'ref');
71 refTitle.addT('referTo');
Akronb19803c2018-08-16 16:39:42 +020072
73 // Added value operator
Akron88d237e2020-10-21 08:05:18 +020074 const refE = t._refE = document.createElement('span');
75 refE.setAttribute('data-type', "string");
76 refE.setAttribute('class', 'value');
77
Akronb19803c2018-08-16 16:39:42 +020078 if (this.ref()) {
Akron88d237e2020-10-21 08:05:18 +020079 refE.addT(t.ref());
Akronb19803c2018-08-16 16:39:42 +020080 }
81 else {
Akron88d237e2020-10-21 08:05:18 +020082 refE.addT(loc.EMPTY);
83 refE.classList.add('unspecified');
Akronb19803c2018-08-16 16:39:42 +020084 };
85
86 // Change value
Akron88d237e2020-10-21 08:05:18 +020087 refE.addEventListener(
Akronb19803c2018-08-16 16:39:42 +020088 'click',
Akron88d237e2020-10-21 08:05:18 +020089 t._changeRef.bind(t)
Akronb19803c2018-08-16 16:39:42 +020090 );
91
92 // Remove all element children
93 _removeChildren(e);
94
95 // Add spans
96 e.appendChild(refTitle);
Akron88d237e2020-10-21 08:05:18 +020097 e.appendChild(refE);
Akronb19803c2018-08-16 16:39:42 +020098
Akron88d237e2020-10-21 08:05:18 +020099 t.__changed = false;
Akronb19803c2018-08-16 16:39:42 +0200100
Akron88d237e2020-10-21 08:05:18 +0200101 if (t._rewrites !== undefined) {
102 e.appendChild(t._rewrites.element());
Akronb19803c2018-08-16 16:39:42 +0200103 };
Akron0f00b772020-12-02 17:32:13 +0100104
Akron88d237e2020-10-21 08:05:18 +0200105 if (t._parent !== undefined) {
106
Akronb19803c2018-08-16 16:39:42 +0200107 // Set operators
Akron88d237e2020-10-21 08:05:18 +0200108 // Append new operators
109 e.appendChild(t.operators(
Akronb19803c2018-08-16 16:39:42 +0200110 true,
111 true,
112 true
Akron88d237e2020-10-21 08:05:18 +0200113 ).element());
Akronb19803c2018-08-16 16:39:42 +0200114 };
Akron0f00b772020-12-02 17:32:13 +0100115
116 if (KorAP.vc !== undefined) {
117 KorAP.vc.element().dispatchEvent(
118 new CustomEvent('vcChange', { 'detail' : t })
119 );
120 };
hebasta48842cf2018-12-11 12:57:38 +0100121
Akron88d237e2020-10-21 08:05:18 +0200122 return t.element();
Akronb19803c2018-08-16 16:39:42 +0200123 },
124
125
126 /**
127 * Get the associated element
128 */
129 element : function () {
Akron88d237e2020-10-21 08:05:18 +0200130
Akron24aa0052020-11-10 11:00:34 +0100131 if (this._el !== undefined)
132 return this._el;
Akron88d237e2020-10-21 08:05:18 +0200133
Akron24aa0052020-11-10 11:00:34 +0100134 const e = this._el = document.createElement('div');
Akron88d237e2020-10-21 08:05:18 +0200135 e.setAttribute('class', 'doc groupref');
Akronb19803c2018-08-16 16:39:42 +0200136 this.update();
Akron88d237e2020-10-21 08:05:18 +0200137 return e;
Akronb19803c2018-08-16 16:39:42 +0200138 },
139
140
141 /**
142 * Get or set the value
143 */
144 ref : function (ref) {
145 if (arguments.length === 1) {
146 this._ref = ref;
147 this._changed();
148 return this;
149 };
150 return this._ref;
151 },
152
153
154 // Click on the reference operator, show me the option
155 _changeRef : function (e) {
Akron88d237e2020-10-21 08:05:18 +0200156 const that = this;
157 const str = stringValClass.create(this.ref(), false, false);
158 const strElem = str.element();
Akronb19803c2018-08-16 16:39:42 +0200159
160 str.store = function (ref, regex) {
161 that.ref(ref);
162
Akron24aa0052020-11-10 11:00:34 +0100163 that._el.removeChild(
164 this._el
Akronb19803c2018-08-16 16:39:42 +0200165 );
166 that.update();
167 };
168
169 // Insert element
Akron24aa0052020-11-10 11:00:34 +0100170 this._el.insertBefore(
Akronb19803c2018-08-16 16:39:42 +0200171 strElem,
172 this._refE
173 );
174
175 str.focus();
176 },
177
178
179 /**
180 * Wrap a new operation around the doc element.
181 * This is copypasta from doc.js
182 */
183 wrap : function (op) {
Akron88d237e2020-10-21 08:05:18 +0200184 const parent = this.parent();
185 const group = require('vc/docgroup').create(parent);
Akronb19803c2018-08-16 16:39:42 +0200186 group.operation(op);
187 group.append(this);
188 group.append();
189 return parent.replaceOperand(this, group).update();
190 },
191
Akron88d237e2020-10-21 08:05:18 +0200192
Akronb19803c2018-08-16 16:39:42 +0200193 /**
194 * Deserialize from json
195 */
196 fromJson : function (json) {
197 if (json === undefined)
198 return this;
199
200 if (json["@type"] === undefined) {
201 KorAP.log(701, "JSON-LD group has no @type attribute");
202 return;
203 };
204
205 if (json["ref"] === undefined ||
206 typeof json["ref"] != 'string') {
207 KorAP.log(821, "Reference is missing");
208 return;
209 };
210
211 this.ref(json["ref"]);
212
213 // Rewrite coming from the server
214 if (json["rewrites"] !== undefined) {
215 this.rewrite(json["rewrites"]);
216 };
217
218 return this;
219 },
220
Akron88d237e2020-10-21 08:05:18 +0200221
Akronb19803c2018-08-16 16:39:42 +0200222 /**
223 * Click on the unspecified object
224 */
225 onclick : function () {
226 console.log("Do not support click on this");
227 },
228
Akron88d237e2020-10-21 08:05:18 +0200229
Akronb19803c2018-08-16 16:39:42 +0200230 // TODO: This is identical to doc.js
231 rewrites : function () {
232 return this._rewrites;
233 },
234
Akron88d237e2020-10-21 08:05:18 +0200235
Akronb19803c2018-08-16 16:39:42 +0200236 // TODO: This is identical to doc.js
237 rewrite : function (value) {
238 if (typeof value === 'string') {
239 value = [{
240 "@type" : "koral:rewrite",
241 "operation" : "operation:" + value,
242 "src" : "Kalamar"
243 }];
244 };
245 this._rewrites = rewriteListClass.create(value);
246 },
247
248
249 // Mark the underlying data as being changed.
250 // This is important for rerendering the dom.
251 // This will also remove rewrite markers, when the data
252 // change happened by the user
253 _changed : function () {
254 this.__changed = true;
255
256 if (this._rewrites === undefined)
257 return;
258
Akron88d237e2020-10-21 08:05:18 +0200259 delete this["_rewrites"];
Akronb19803c2018-08-16 16:39:42 +0200260
Akron24aa0052020-11-10 11:00:34 +0100261 if (this._el === undefined)
Akronb19803c2018-08-16 16:39:42 +0200262 return;
263
Akron24aa0052020-11-10 11:00:34 +0100264 this._el.classList.remove("rewritten");
Akronb19803c2018-08-16 16:39:42 +0200265 },
266
Akron88d237e2020-10-21 08:05:18 +0200267
Akronb19803c2018-08-16 16:39:42 +0200268 toJson : function () {
269 if (!this.ref)
270 return {};
271
272 return {
273 "@type" : "koral:" + this.ldType(),
Akron88d237e2020-10-21 08:05:18 +0200274 "ref" : this.ref()
Akronb19803c2018-08-16 16:39:42 +0200275 };
276 },
277
hebastaa0282be2018-12-05 16:58:00 +0100278
279 incomplete : function () {
280 return this.ref() ? false : true
281 },
Akronb19803c2018-08-16 16:39:42 +0200282
Akron88d237e2020-10-21 08:05:18 +0200283
Akronb19803c2018-08-16 16:39:42 +0200284 toQuery : function () {
hebastaa0282be2018-12-05 16:58:00 +0100285 if (this.incomplete())
Akronb19803c2018-08-16 16:39:42 +0200286 return "";
287
288 // Build doc string based on key
Akron0c4cd222019-07-19 16:33:34 +0200289 return 'referTo ' + this.ref().quote();
Akronb19803c2018-08-16 16:39:42 +0200290 }
291 };
292});