blob: 905faf8427b1652ac7d7c39b1e8cfe01f796c69c [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);
79 };
80
81 // Change value
82 this._refE.addEventListener(
83 'click',
84 this._changeRef.bind(this)
85 );
86
87 // Remove all element children
88 _removeChildren(e);
89
90 // Add spans
91 e.appendChild(refTitle);
92 e.appendChild(this._refE);
93
94 this.__changed = false;
95
96 if (this._rewrites !== undefined) {
97 e.appendChild(this._rewrites.element());
98 };
99
100 if (this._parent !== undefined) {
101 // Set operators
102 var op = this.operators(
103 true,
104 true,
105 true
106 );
107
108 // Append new operators
109 e.appendChild(op.element());
110 };
111
112 return this.element();
113 },
114
115
116 /**
117 * Get the associated element
118 */
119 element : function () {
120 if (this._element !== undefined)
121 return this._element;
122 this._element = document.createElement('div');
123 this._element.setAttribute('class', 'doc groupref');
124 this.update();
125 return this._element;
126 },
127
128
129 /**
130 * Get or set the value
131 */
132 ref : function (ref) {
133 if (arguments.length === 1) {
134 this._ref = ref;
135 this._changed();
136 return this;
137 };
138 return this._ref;
139 },
140
141
142 // Click on the reference operator, show me the option
143 _changeRef : function (e) {
144 var that = this;
145
146 var str = stringValClass.create(this.ref(), false, false);
147 var strElem = str.element();
148
149 str.store = function (ref, regex) {
150 that.ref(ref);
151
152 that._element.removeChild(
153 this._element
154 );
155 that.update();
156 };
157
158 // Insert element
159 this._element.insertBefore(
160 strElem,
161 this._refE
162 );
163
164 str.focus();
165 },
166
167
168 /**
169 * Wrap a new operation around the doc element.
170 * This is copypasta from doc.js
171 */
172 wrap : function (op) {
173 var parent = this.parent();
174 var group = require('vc/docgroup').create(parent);
175 group.operation(op);
176 group.append(this);
177 group.append();
178 return parent.replaceOperand(this, group).update();
179 },
180
181 /**
182 * Deserialize from json
183 */
184 fromJson : function (json) {
185 if (json === undefined)
186 return this;
187
188 if (json["@type"] === undefined) {
189 KorAP.log(701, "JSON-LD group has no @type attribute");
190 return;
191 };
192
193 if (json["ref"] === undefined ||
194 typeof json["ref"] != 'string') {
195 KorAP.log(821, "Reference is missing");
196 return;
197 };
198
199 this.ref(json["ref"]);
200
201 // Rewrite coming from the server
202 if (json["rewrites"] !== undefined) {
203 this.rewrite(json["rewrites"]);
204 };
205
206 return this;
207 },
208
209
210 /**
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
263
264 toQuery : function () {
265 if (!this.ref())
266 return "";
267
268 // Build doc string based on key
269 return 'referTo "' + this.ref().quote() + '"';
270 }
271 };
272});