blob: 83ac244a6141812043051a81a293f1e732e31081 [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) {
Akron88d237e2020-10-21 08:05:18 +020028 const obj = Object(jsonldClass).
29 create().
30 upgradeTo(this).
31 fromJson(json);
32
33 if (obj === undefined)
Akronb19803c2018-08-16 16:39:42 +020034 console.log(json);
Akronb19803c2018-08-16 16:39:42 +020035
36 if (parent !== undefined)
37 obj._parent = parent;
Akron88d237e2020-10-21 08:05:18 +020038
Akronb19803c2018-08-16 16:39:42 +020039 obj.__changed = true;
40 return obj;
41 },
42
43
44 /**
45 * Update the element
46 */
47 update : function () {
Akron88d237e2020-10-21 08:05:18 +020048 const t = this;
Akronb19803c2018-08-16 16:39:42 +020049
Akron88d237e2020-10-21 08:05:18 +020050 if (t._element === undefined)
51 return t.element();
52
53 const e = t._element;
Akronb19803c2018-08-16 16:39:42 +020054
55 // Check if there is a change in the underlying data
Akron88d237e2020-10-21 08:05:18 +020056 if (!t.__changed)
Akronb19803c2018-08-16 16:39:42 +020057 return e;
58
59 // Set ref - TODO: Cleanup!
Akron88d237e2020-10-21 08:05:18 +020060 e.refTo = t;
Akronb19803c2018-08-16 16:39:42 +020061
62 // Was rewritten
Akron88d237e2020-10-21 08:05:18 +020063 if (t.rewrites() !== undefined) {
Akronb19803c2018-08-16 16:39:42 +020064 e.classList.add("rewritten");
65 };
66
Akron88d237e2020-10-21 08:05:18 +020067 const refTitle = document.createElement('span');
Akron3ad46942018-08-22 16:47:14 +020068 refTitle.classList.add('key','fixed', 'ref');
69 refTitle.addT('referTo');
Akronb19803c2018-08-16 16:39:42 +020070
71 // Added value operator
Akron88d237e2020-10-21 08:05:18 +020072 const refE = t._refE = document.createElement('span');
73 refE.setAttribute('data-type', "string");
74 refE.setAttribute('class', 'value');
75
Akronb19803c2018-08-16 16:39:42 +020076 if (this.ref()) {
Akron88d237e2020-10-21 08:05:18 +020077 refE.addT(t.ref());
Akronb19803c2018-08-16 16:39:42 +020078 }
79 else {
Akron88d237e2020-10-21 08:05:18 +020080 refE.addT(loc.EMPTY);
81 refE.classList.add('unspecified');
Akronb19803c2018-08-16 16:39:42 +020082 };
83
84 // Change value
Akron88d237e2020-10-21 08:05:18 +020085 refE.addEventListener(
Akronb19803c2018-08-16 16:39:42 +020086 'click',
Akron88d237e2020-10-21 08:05:18 +020087 t._changeRef.bind(t)
Akronb19803c2018-08-16 16:39:42 +020088 );
89
90 // Remove all element children
91 _removeChildren(e);
92
93 // Add spans
94 e.appendChild(refTitle);
Akron88d237e2020-10-21 08:05:18 +020095 e.appendChild(refE);
Akronb19803c2018-08-16 16:39:42 +020096
Akron88d237e2020-10-21 08:05:18 +020097 t.__changed = false;
Akronb19803c2018-08-16 16:39:42 +020098
Akron88d237e2020-10-21 08:05:18 +020099 if (t._rewrites !== undefined) {
100 e.appendChild(t._rewrites.element());
Akronb19803c2018-08-16 16:39:42 +0200101 };
102
Akron88d237e2020-10-21 08:05:18 +0200103 if (t._parent !== undefined) {
104
Akronb19803c2018-08-16 16:39:42 +0200105 // Set operators
Akron88d237e2020-10-21 08:05:18 +0200106 // Append new operators
107 e.appendChild(t.operators(
Akronb19803c2018-08-16 16:39:42 +0200108 true,
109 true,
110 true
Akron88d237e2020-10-21 08:05:18 +0200111 ).element());
Akronb19803c2018-08-16 16:39:42 +0200112 };
hebastade595b42019-02-05 13:53:10 +0100113
Akron88d237e2020-10-21 08:05:18 +0200114 KorAP.vc.element().dispatchEvent(
115 new CustomEvent('vcChange', { 'detail' : t })
116 );
hebasta48842cf2018-12-11 12:57:38 +0100117
Akron88d237e2020-10-21 08:05:18 +0200118 return t.element();
Akronb19803c2018-08-16 16:39:42 +0200119 },
120
121
122 /**
123 * Get the associated element
124 */
125 element : function () {
Akron88d237e2020-10-21 08:05:18 +0200126
Akronb19803c2018-08-16 16:39:42 +0200127 if (this._element !== undefined)
128 return this._element;
Akron88d237e2020-10-21 08:05:18 +0200129
130 const e = this._element = document.createElement('div');
131 e.setAttribute('class', 'doc groupref');
Akronb19803c2018-08-16 16:39:42 +0200132 this.update();
Akron88d237e2020-10-21 08:05:18 +0200133 return e;
Akronb19803c2018-08-16 16:39:42 +0200134 },
135
136
137 /**
138 * Get or set the value
139 */
140 ref : function (ref) {
141 if (arguments.length === 1) {
142 this._ref = ref;
143 this._changed();
144 return this;
145 };
146 return this._ref;
147 },
148
149
150 // Click on the reference operator, show me the option
151 _changeRef : function (e) {
Akron88d237e2020-10-21 08:05:18 +0200152 const that = this;
153 const str = stringValClass.create(this.ref(), false, false);
154 const strElem = str.element();
Akronb19803c2018-08-16 16:39:42 +0200155
156 str.store = function (ref, regex) {
157 that.ref(ref);
158
159 that._element.removeChild(
160 this._element
161 );
162 that.update();
163 };
164
165 // Insert element
166 this._element.insertBefore(
167 strElem,
168 this._refE
169 );
170
171 str.focus();
172 },
173
174
175 /**
176 * Wrap a new operation around the doc element.
177 * This is copypasta from doc.js
178 */
179 wrap : function (op) {
Akron88d237e2020-10-21 08:05:18 +0200180 const parent = this.parent();
181 const group = require('vc/docgroup').create(parent);
Akronb19803c2018-08-16 16:39:42 +0200182 group.operation(op);
183 group.append(this);
184 group.append();
185 return parent.replaceOperand(this, group).update();
186 },
187
Akron88d237e2020-10-21 08:05:18 +0200188
Akronb19803c2018-08-16 16:39:42 +0200189 /**
190 * Deserialize from json
191 */
192 fromJson : function (json) {
193 if (json === undefined)
194 return this;
195
196 if (json["@type"] === undefined) {
197 KorAP.log(701, "JSON-LD group has no @type attribute");
198 return;
199 };
200
201 if (json["ref"] === undefined ||
202 typeof json["ref"] != 'string') {
203 KorAP.log(821, "Reference is missing");
204 return;
205 };
206
207 this.ref(json["ref"]);
208
209 // Rewrite coming from the server
210 if (json["rewrites"] !== undefined) {
211 this.rewrite(json["rewrites"]);
212 };
213
214 return this;
215 },
216
Akron88d237e2020-10-21 08:05:18 +0200217
Akronb19803c2018-08-16 16:39:42 +0200218 /**
219 * Click on the unspecified object
220 */
221 onclick : function () {
222 console.log("Do not support click on this");
223 },
224
Akron88d237e2020-10-21 08:05:18 +0200225
Akronb19803c2018-08-16 16:39:42 +0200226 // TODO: This is identical to doc.js
227 rewrites : function () {
228 return this._rewrites;
229 },
230
Akron88d237e2020-10-21 08:05:18 +0200231
Akronb19803c2018-08-16 16:39:42 +0200232 // TODO: This is identical to doc.js
233 rewrite : function (value) {
234 if (typeof value === 'string') {
235 value = [{
236 "@type" : "koral:rewrite",
237 "operation" : "operation:" + value,
238 "src" : "Kalamar"
239 }];
240 };
241 this._rewrites = rewriteListClass.create(value);
242 },
243
244
245 // Mark the underlying data as being changed.
246 // This is important for rerendering the dom.
247 // This will also remove rewrite markers, when the data
248 // change happened by the user
249 _changed : function () {
250 this.__changed = true;
251
252 if (this._rewrites === undefined)
253 return;
254
Akron88d237e2020-10-21 08:05:18 +0200255 delete this["_rewrites"];
Akronb19803c2018-08-16 16:39:42 +0200256
257 if (this._element === undefined)
258 return;
259
260 this._element.classList.remove("rewritten");
261 },
262
Akron88d237e2020-10-21 08:05:18 +0200263
Akronb19803c2018-08-16 16:39:42 +0200264 toJson : function () {
265 if (!this.ref)
266 return {};
267
268 return {
269 "@type" : "koral:" + this.ldType(),
Akron88d237e2020-10-21 08:05:18 +0200270 "ref" : this.ref()
Akronb19803c2018-08-16 16:39:42 +0200271 };
272 },
273
hebastaa0282be2018-12-05 16:58:00 +0100274
275 incomplete : function () {
276 return this.ref() ? false : true
277 },
Akronb19803c2018-08-16 16:39:42 +0200278
Akron88d237e2020-10-21 08:05:18 +0200279
Akronb19803c2018-08-16 16:39:42 +0200280 toQuery : function () {
hebastaa0282be2018-12-05 16:58:00 +0100281 if (this.incomplete())
Akronb19803c2018-08-16 16:39:42 +0200282 return "";
283
284 // Build doc string based on key
Akron0c4cd222019-07-19 16:33:34 +0200285 return 'referTo ' + this.ref().quote();
Akronb19803c2018-08-16 16:39:42 +0200286 }
287 };
288});