blob: 9f1401a7d7d98d683b35961f5156ab84578097df [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
Akron88d237e2020-10-21 08:05:18 +020052 if (t._element === undefined)
53 return t.element();
54
55 const e = t._element;
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;
Akronb19803c2018-08-16 16:39:42 +020063
64 // 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 };
68
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 };
104
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 };
hebastade595b42019-02-05 13:53:10 +0100115
Akron88d237e2020-10-21 08:05:18 +0200116 KorAP.vc.element().dispatchEvent(
117 new CustomEvent('vcChange', { 'detail' : t })
118 );
hebasta48842cf2018-12-11 12:57:38 +0100119
Akron88d237e2020-10-21 08:05:18 +0200120 return t.element();
Akronb19803c2018-08-16 16:39:42 +0200121 },
122
123
124 /**
125 * Get the associated element
126 */
127 element : function () {
Akron88d237e2020-10-21 08:05:18 +0200128
Akronb19803c2018-08-16 16:39:42 +0200129 if (this._element !== undefined)
130 return this._element;
Akron88d237e2020-10-21 08:05:18 +0200131
132 const e = this._element = document.createElement('div');
133 e.setAttribute('class', 'doc groupref');
Akronb19803c2018-08-16 16:39:42 +0200134 this.update();
Akron88d237e2020-10-21 08:05:18 +0200135 return e;
Akronb19803c2018-08-16 16:39:42 +0200136 },
137
138
139 /**
140 * Get or set the value
141 */
142 ref : function (ref) {
143 if (arguments.length === 1) {
144 this._ref = ref;
145 this._changed();
146 return this;
147 };
148 return this._ref;
149 },
150
151
152 // Click on the reference operator, show me the option
153 _changeRef : function (e) {
Akron88d237e2020-10-21 08:05:18 +0200154 const that = this;
155 const str = stringValClass.create(this.ref(), false, false);
156 const strElem = str.element();
Akronb19803c2018-08-16 16:39:42 +0200157
158 str.store = function (ref, regex) {
159 that.ref(ref);
160
161 that._element.removeChild(
162 this._element
163 );
164 that.update();
165 };
166
167 // Insert element
168 this._element.insertBefore(
169 strElem,
170 this._refE
171 );
172
173 str.focus();
174 },
175
176
177 /**
178 * Wrap a new operation around the doc element.
179 * This is copypasta from doc.js
180 */
181 wrap : function (op) {
Akron88d237e2020-10-21 08:05:18 +0200182 const parent = this.parent();
183 const group = require('vc/docgroup').create(parent);
Akronb19803c2018-08-16 16:39:42 +0200184 group.operation(op);
185 group.append(this);
186 group.append();
187 return parent.replaceOperand(this, group).update();
188 },
189
Akron88d237e2020-10-21 08:05:18 +0200190
Akronb19803c2018-08-16 16:39:42 +0200191 /**
192 * Deserialize from json
193 */
194 fromJson : function (json) {
195 if (json === undefined)
196 return this;
197
198 if (json["@type"] === undefined) {
199 KorAP.log(701, "JSON-LD group has no @type attribute");
200 return;
201 };
202
203 if (json["ref"] === undefined ||
204 typeof json["ref"] != 'string') {
205 KorAP.log(821, "Reference is missing");
206 return;
207 };
208
209 this.ref(json["ref"]);
210
211 // Rewrite coming from the server
212 if (json["rewrites"] !== undefined) {
213 this.rewrite(json["rewrites"]);
214 };
215
216 return this;
217 },
218
Akron88d237e2020-10-21 08:05:18 +0200219
Akronb19803c2018-08-16 16:39:42 +0200220 /**
221 * Click on the unspecified object
222 */
223 onclick : function () {
224 console.log("Do not support click on this");
225 },
226
Akron88d237e2020-10-21 08:05:18 +0200227
Akronb19803c2018-08-16 16:39:42 +0200228 // TODO: This is identical to doc.js
229 rewrites : function () {
230 return this._rewrites;
231 },
232
Akron88d237e2020-10-21 08:05:18 +0200233
Akronb19803c2018-08-16 16:39:42 +0200234 // TODO: This is identical to doc.js
235 rewrite : function (value) {
236 if (typeof value === 'string') {
237 value = [{
238 "@type" : "koral:rewrite",
239 "operation" : "operation:" + value,
240 "src" : "Kalamar"
241 }];
242 };
243 this._rewrites = rewriteListClass.create(value);
244 },
245
246
247 // Mark the underlying data as being changed.
248 // This is important for rerendering the dom.
249 // This will also remove rewrite markers, when the data
250 // change happened by the user
251 _changed : function () {
252 this.__changed = true;
253
254 if (this._rewrites === undefined)
255 return;
256
Akron88d237e2020-10-21 08:05:18 +0200257 delete this["_rewrites"];
Akronb19803c2018-08-16 16:39:42 +0200258
259 if (this._element === undefined)
260 return;
261
262 this._element.classList.remove("rewritten");
263 },
264
Akron88d237e2020-10-21 08:05:18 +0200265
Akronb19803c2018-08-16 16:39:42 +0200266 toJson : function () {
267 if (!this.ref)
268 return {};
269
270 return {
271 "@type" : "koral:" + this.ldType(),
Akron88d237e2020-10-21 08:05:18 +0200272 "ref" : this.ref()
Akronb19803c2018-08-16 16:39:42 +0200273 };
274 },
275
hebastaa0282be2018-12-05 16:58:00 +0100276
277 incomplete : function () {
278 return this.ref() ? false : true
279 },
Akronb19803c2018-08-16 16:39:42 +0200280
Akron88d237e2020-10-21 08:05:18 +0200281
Akronb19803c2018-08-16 16:39:42 +0200282 toQuery : function () {
hebastaa0282be2018-12-05 16:58:00 +0100283 if (this.incomplete())
Akronb19803c2018-08-16 16:39:42 +0200284 return "";
285
286 // Build doc string based on key
Akron0c4cd222019-07-19 16:33:34 +0200287 return 'referTo ' + this.ref().quote();
Akronb19803c2018-08-16 16:39:42 +0200288 }
289 };
290});