blob: bdedb7089b227aa7082a6b46d1f2cac0dcc53236 [file] [log] [blame]
Nils Diewald0e6992a2015-04-14 20:13:52 +00001/**
Nils Diewald4c221252015-04-21 20:19:25 +00002 * A new document criterion
Nils Diewald0e6992a2015-04-14 20:13:52 +00003 */
4
Nils Diewald0e6992a2015-04-14 20:13:52 +00005define([
Nils Diewald4c221252015-04-21 20:19:25 +00006 'vc/jsonld',
7 'vc/rewritelist',
Nils Diewaldf0c4f112015-05-05 12:56:59 +00008 'vc/stringval',
Nils Diewald4c221252015-04-21 20:19:25 +00009 'util'
Nils Diewaldf0c4f112015-05-05 12:56:59 +000010], function (jsonldClass, rewriteListClass, stringValClass) {
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000011
Nils Diewald0e6992a2015-04-14 20:13:52 +000012
Akron31d89942018-04-06 16:44:51 +020013 const errstr802 = "Match type is not supported by value type";
Akron0b489ad2018-02-02 16:49:32 +010014 const loc = KorAP.Locale;
Nils Diewald4c221252015-04-21 20:19:25 +000015 loc.EMPTY = loc.EMPTY || '⋯';
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000016
Nils Diewald0e6992a2015-04-14 20:13:52 +000017 return {
Nils Diewald4c221252015-04-21 20:19:25 +000018
19 // The JSON-LD type
Nils Diewald0e6992a2015-04-14 20:13:52 +000020 _ldType : "doc",
Nils Diewald4c221252015-04-21 20:19:25 +000021
22 // The object ... maybe not important
Nils Diewald0e6992a2015-04-14 20:13:52 +000023 _obj : function () { return '???'; /*KorAP.Doc*/ },
24
Nils Diewald4c221252015-04-21 20:19:25 +000025 /**
26 * Create a new document criterion
27 * by passing the parent object and a json construct.
28 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000029 create : function (parent, json) {
Nils Diewald4c221252015-04-21 20:19:25 +000030
31 // Create the object, inheriting from Json-LD class
Nils Diewald0e6992a2015-04-14 20:13:52 +000032 var obj = Object(jsonldClass).
Akrone4961b12017-05-10 21:04:46 +020033 create().
34 upgradeTo(this).
35 fromJson(json);
Nils Diewald4c221252015-04-21 20:19:25 +000036
Akron55a343b2018-04-06 19:57:36 +020037 if (obj === undefined) {
38 console.log(json);
39 };
40
Nils Diewald4c221252015-04-21 20:19:25 +000041 // Bind the parent
Nils Diewald0e6992a2015-04-14 20:13:52 +000042 if (parent !== undefined)
Akrone4961b12017-05-10 21:04:46 +020043 obj._parent = parent;
Nils Diewald0e6992a2015-04-14 20:13:52 +000044
45 obj.__changed = true;
46 return obj;
47 },
48
Nils Diewald4c221252015-04-21 20:19:25 +000049 /**
50 * Update the elements content.
51 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000052 update : function () {
53 if (this._element === undefined)
Akrone4961b12017-05-10 21:04:46 +020054 return this.element();
Nils Diewald0e6992a2015-04-14 20:13:52 +000055
56 // Get element
57 var e = this._element;
58
59 // Set ref - TODO: Cleanup!
60 e.refTo = this;
61
Akron31d89942018-04-06 16:44:51 +020062 // Check if there is a change in the underlying data
Nils Diewald0e6992a2015-04-14 20:13:52 +000063 if (this.__changed) {
64
Akrone4961b12017-05-10 21:04:46 +020065 // Was rewritten
66 if (this.rewrites() !== undefined) {
67 e.classList.add("rewritten");
68 };
Nils Diewald0e6992a2015-04-14 20:13:52 +000069
Akrone4961b12017-05-10 21:04:46 +020070 // Added key
71 this._keyE = document.createElement('span');
72 this._keyE.setAttribute('class', 'key');
Nils Diewald0e6992a2015-04-14 20:13:52 +000073
Akrone4961b12017-05-10 21:04:46 +020074 // Change key
75 this._keyE.addEventListener('click', this._changeKey.bind(this));
Nils Diewald0e6992a2015-04-14 20:13:52 +000076
Akrone4961b12017-05-10 21:04:46 +020077 if (this.key()) {
78 var k = this.key();
79 if (loc['VC_' + k] !== undefined)
80 k = loc['VC_' + k];
Akron0b489ad2018-02-02 16:49:32 +010081 this._keyE.addT(k);
Akrone4961b12017-05-10 21:04:46 +020082 };
Nils Diewald4c221252015-04-21 20:19:25 +000083
Akrone4961b12017-05-10 21:04:46 +020084 // Added match operator
85 this._matchopE = document.createElement('span');
86 this._matchopE.setAttribute('data-type', this.type());
87 this._matchopE.setAttribute('class', 'match');
Akron0b489ad2018-02-02 16:49:32 +010088 this._matchopE.addT(this.matchop());
Nils Diewald0e6992a2015-04-14 20:13:52 +000089
Akrone4961b12017-05-10 21:04:46 +020090 // Change matchop
91 this._matchopE.addEventListener(
92 'click',
93 this._changeMatchop.bind(this)
94 );
Nils Diewald4c221252015-04-21 20:19:25 +000095
Akrone4961b12017-05-10 21:04:46 +020096 // Added value operator
97 this._valueE = document.createElement('span');
98 this._valueE.setAttribute('data-type', this.type());
99 this._valueE.setAttribute('class', 'value');
100 if (this.value()) {
Akron0b489ad2018-02-02 16:49:32 +0100101 this._valueE.addT(this.value());
Akrone4961b12017-05-10 21:04:46 +0200102 }
103 else {
Akron0b489ad2018-02-02 16:49:32 +0100104 this._valueE.addT(loc.EMPTY);
Akrone4961b12017-05-10 21:04:46 +0200105 };
Nils Diewald4c221252015-04-21 20:19:25 +0000106
Akrone4961b12017-05-10 21:04:46 +0200107 // Change value
108 this._valueE.addEventListener(
109 'click',
110 this._changeValue.bind(this)
111 );
Nils Diewald4c221252015-04-21 20:19:25 +0000112
Nils Diewald0e6992a2015-04-14 20:13:52 +0000113
Akrone4961b12017-05-10 21:04:46 +0200114 // Remove all element children
115 _removeChildren(e);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000116
Akrone4961b12017-05-10 21:04:46 +0200117 // Add spans
118 e.appendChild(this._keyE);
119 e.appendChild(this._matchopE);
120 e.appendChild(this._valueE);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000121
Akrone4961b12017-05-10 21:04:46 +0200122 this.__changed = false;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000123 };
124
125 if (this._rewrites !== undefined) {
Akrone4961b12017-05-10 21:04:46 +0200126 e.appendChild(this._rewrites.element());
Nils Diewald0e6992a2015-04-14 20:13:52 +0000127 };
128
129 if (this._parent !== undefined) {
Akrone4961b12017-05-10 21:04:46 +0200130 // Set operators
131 var op = this.operators(
132 true,
133 true,
134 true
135 );
Nils Diewald0e6992a2015-04-14 20:13:52 +0000136
Akrone4961b12017-05-10 21:04:46 +0200137 // Append new operators
138 e.appendChild(op.element());
Nils Diewald0e6992a2015-04-14 20:13:52 +0000139 };
140
141 return e;
142 },
143
Nils Diewald4c221252015-04-21 20:19:25 +0000144
145 /**
146 * Get the associated element
147 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000148 element : function () {
149 if (this._element !== undefined)
Akrone4961b12017-05-10 21:04:46 +0200150 return this._element;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000151
152 this._element = document.createElement('div');
153 this._element.setAttribute('class', 'doc');
154
155 this.update();
156 return this._element;
157 },
158
Nils Diewald4c221252015-04-21 20:19:25 +0000159 /**
160 * Wrap a new operation around the doc element
161 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000162 wrap : function (op) {
163 var parent = this.parent();
Nils Diewald7c8ced22015-04-15 19:21:00 +0000164 var group = require('vc/docgroup').create(parent);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000165 group.operation(op);
166 group.append(this);
167 group.append();
168 return parent.replaceOperand(this, group).update();
169 },
170
Nils Diewald4c221252015-04-21 20:19:25 +0000171 /**
172 * Deserialize from json
173 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000174 fromJson : function (json) {
175 if (json === undefined)
Akrone4961b12017-05-10 21:04:46 +0200176 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000177
178 if (json["@type"] === undefined) {
Akrone4961b12017-05-10 21:04:46 +0200179 KorAP.log(701, "JSON-LD group has no @type attribute");
180 return;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000181 };
182
183 if (json["value"] === undefined ||
Akrone4961b12017-05-10 21:04:46 +0200184 typeof json["value"] != 'string') {
185 KorAP.log(805, "Value is invalid");
186 return;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000187 };
188
Akronea4e9052017-07-06 16:12:05 +0200189 var rewrite;
190
Nils Diewald0e6992a2015-04-14 20:13:52 +0000191 // There is a defined key
192 if (json["key"] !== undefined &&
Akrone4961b12017-05-10 21:04:46 +0200193 typeof json["key"] === 'string') {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000194
Akrone4961b12017-05-10 21:04:46 +0200195 // Set key
196 this.key(json["key"]);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000197
Akrone4961b12017-05-10 21:04:46 +0200198 // Set match operation
199 if (json["match"] !== undefined) {
200 if (typeof json["match"] === 'string') {
Akron31d89942018-04-06 16:44:51 +0200201
Akrone4961b12017-05-10 21:04:46 +0200202 this.matchop(json["match"]);
203 }
204 else {
Akron31d89942018-04-06 16:44:51 +0200205 KorAP.log(802, errstr802);
Akrone4961b12017-05-10 21:04:46 +0200206 return;
207 };
208 };
Akron31d89942018-04-06 16:44:51 +0200209
Akronddc98a72018-04-06 17:33:52 +0200210 // Type is unspecified - but may be known by the menu
211 if (json["type"] === undefined && KorAP._vcKeyMenu) {
212
213 // Check the VC list if the field is known
214 var type = KorAP._vcKeyMenu.typeOf(this.key());
215 if (type != undefined) {
Akron55a343b2018-04-06 19:57:36 +0200216 console.log("Guessed the type for " + this.key() + " to be " + type);
Akronddc98a72018-04-06 17:33:52 +0200217 json["type"] = "type:" + type;
218 };
219 };
220
221 // Type is still undefined
Akron31d89942018-04-06 16:44:51 +0200222 if (json["type"] === undefined) {
Akronddc98a72018-04-06 17:33:52 +0200223
Akron31d89942018-04-06 16:44:51 +0200224 // Check match type
225 if (!KorAP._validUnspecMatchRE.test(this.matchop())) {
226 KorAP.log(802, errstr802);
227
228 // Rewrite method
229 this.matchop('eq');
230 rewrite = 'modification';
231 };
232
233 // Set string value
234 this.value(json["value"]);
235 }
236
237 // Field is string type
238 else if (json["type"] == "type:string") {
Akrone4961b12017-05-10 21:04:46 +0200239 this.type("string");
Nils Diewald0e6992a2015-04-14 20:13:52 +0000240
Akrone4961b12017-05-10 21:04:46 +0200241 // Check match type
242 if (!KorAP._validStringMatchRE.test(this.matchop())) {
Akron31d89942018-04-06 16:44:51 +0200243 KorAP.log(802, errstr802);
244
245 // Rewrite method
246 this.matchop('eq');
247 rewrite = 'modification';
248 };
249
250 // Set string value
251 this.value(json["value"]);
252 }
253
254 // Field is specified
255 else if (json["type"] == "type:text") {
256 this.type("text");
257
258 // Check match type
259 if (!KorAP._validTextMatchRE.test(this.matchop())) {
260 KorAP.log(802, errstr802);
Akronea4e9052017-07-06 16:12:05 +0200261
262 // Rewrite method
263 this.matchop('eq');
264 rewrite = 'modification';
Akrone4961b12017-05-10 21:04:46 +0200265 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000266
Akrone4961b12017-05-10 21:04:46 +0200267 // Set string value
268 this.value(json["value"]);
269 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000270
Akrone4961b12017-05-10 21:04:46 +0200271 // Key is a date
272 else if (json["type"] === "type:date") {
273 this.type("date");
Nils Diewald0e6992a2015-04-14 20:13:52 +0000274
Akrone4961b12017-05-10 21:04:46 +0200275 if (json["value"] !== undefined &&
276 KorAP._validDateRE.test(json["value"])) {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000277
Akrone4961b12017-05-10 21:04:46 +0200278 if (!KorAP._validDateMatchRE.test(this.matchop())) {
Akron31d89942018-04-06 16:44:51 +0200279 KorAP.log(802, errstr802);
Akronea4e9052017-07-06 16:12:05 +0200280
281 // Rewrite method
282 this.matchop('eq');
283 rewrite = 'modification';
Akrone4961b12017-05-10 21:04:46 +0200284 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000285
Akrone4961b12017-05-10 21:04:46 +0200286 // Set value
287 this.value(json["value"]);
288 }
289 else {
290 KorAP.log(806, "Value is not a valid date string");
291 return;
292 };
293 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000294
Akrone4961b12017-05-10 21:04:46 +0200295 // Key is a regular expression
296 else if (json["type"] === "type:regex") {
297 this.type("regex");
Nils Diewald0e6992a2015-04-14 20:13:52 +0000298
Akrone4961b12017-05-10 21:04:46 +0200299 try {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000300
Akrone4961b12017-05-10 21:04:46 +0200301 // Try to create a regular expression
302 var check = new RegExp(json["value"]);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000303
Akrone65a88a2018-04-05 19:14:20 +0200304 if (!KorAP._validStringMatchRE.test(this.matchop())) {
Akron31d89942018-04-06 16:44:51 +0200305 KorAP.log(802, errstr802);
Akronea4e9052017-07-06 16:12:05 +0200306
307 // Rewrite method
308 this.matchop('eq');
309 rewrite = 'modification';
Akrone4961b12017-05-10 21:04:46 +0200310 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000311
Akrone4961b12017-05-10 21:04:46 +0200312 this.value(json["value"]);
313 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000314
Akrone4961b12017-05-10 21:04:46 +0200315 catch (e) {
316 KorAP.log(807, "Value is not a valid regular expression");
317 return;
318 };
319 this.type("regex");
320 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000321
Akrone4961b12017-05-10 21:04:46 +0200322 else {
323 KorAP.log(804, "Unknown value type");
324 return;
325 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000326 };
327
Akronea4e9052017-07-06 16:12:05 +0200328 // Rewrite coming from the server
Nils Diewald0e6992a2015-04-14 20:13:52 +0000329 if (json["rewrites"] !== undefined) {
Akronea4e9052017-07-06 16:12:05 +0200330 this.rewrite(json["rewrites"]);
331 }
332
333 // Rewrite coming from Kalamar
334 else if (rewrite !== undefined) {
335 this.rewrite(rewrite);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000336 };
Akronea4e9052017-07-06 16:12:05 +0200337
Nils Diewald0e6992a2015-04-14 20:13:52 +0000338 return this;
339 },
340
Nils Diewald4c221252015-04-21 20:19:25 +0000341 /**
342 * Get or set the key
343 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000344 key : function (value) {
345 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200346 this._key = value;
347 this._changed();
348 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000349 };
350 return this._key;
351 },
352
Nils Diewald4c221252015-04-21 20:19:25 +0000353 // Click on the key, show me the menu
354 _changeKey : function (e) {
355 var menu = KorAP._vcKeyMenu;
356
357 // Insert menu
358 this._element.insertBefore(
Akrone4961b12017-05-10 21:04:46 +0200359 menu.element(),
360 this._keyE
Nils Diewald4c221252015-04-21 20:19:25 +0000361 );
362
363 // Release event
364 var that = this;
365 menu.released(function (key, type) {
Akrone4961b12017-05-10 21:04:46 +0200366 var doc = that.key(key).type(type);
Nils Diewald4c221252015-04-21 20:19:25 +0000367
Akrone4961b12017-05-10 21:04:46 +0200368 // This may not be compatible - then switch to default
369 doc.matchop(doc.matchop());
370 doc.value(doc.value());
Nils Diewald4c221252015-04-21 20:19:25 +0000371
Akrone4961b12017-05-10 21:04:46 +0200372 // Update the doc
373 doc.update();
Nils Diewald4c221252015-04-21 20:19:25 +0000374
Akrone4961b12017-05-10 21:04:46 +0200375 // hide!
376 this.hide();
Nils Diewald4c221252015-04-21 20:19:25 +0000377 });
378
379 // TODO: Highlight the old value!
380 menu.show();
381 menu.focus();
382 },
383
384 /**
385 * Get or set the match operator
386 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000387 matchop : function (match) {
Akron31d89942018-04-06 16:44:51 +0200388
Nils Diewald0e6992a2015-04-14 20:13:52 +0000389 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200390 var m = match.replace(/^match:/, '');
Akron31d89942018-04-06 16:44:51 +0200391
Akrone4961b12017-05-10 21:04:46 +0200392 if (
Akron31d89942018-04-06 16:44:51 +0200393 (this._type == undefined) // && KorAP._validUnspecMatchRE.test(m))
Akrone4961b12017-05-10 21:04:46 +0200394 ||
395 (
396 (this._type === 'string' || this._type === 'regex') &&
397 KorAP._validStringMatchRE.test(m)
398 )
399 ||
Akron31d89942018-04-06 16:44:51 +0200400 (this._type === 'text' && KorAP._validTextMatchRE.test(m))
401 ||
Akrone4961b12017-05-10 21:04:46 +0200402 (this._type === 'date' && KorAP._validDateMatchRE.test(m))
403 ) {
404 this._matchop = m;
405 }
406 else {
407 this._matchop = "eq";
408 };
Nils Diewald4c221252015-04-21 20:19:25 +0000409
Akrone4961b12017-05-10 21:04:46 +0200410 this._changed();
Akron31d89942018-04-06 16:44:51 +0200411 return this
Nils Diewald0e6992a2015-04-14 20:13:52 +0000412 };
413 return this._matchop || "eq";
414 },
415
Nils Diewald4c221252015-04-21 20:19:25 +0000416
417 // Click on the match operator, show me the menu
418 _changeMatchop : function (e) {
419 var menu = KorAP._vcMatchopMenu[this.type()];
420
421 if (menu === undefined) {
Akrone4961b12017-05-10 21:04:46 +0200422 KorAP.log(0, "Unable to init menu for " + this.type());
423 return;
Nils Diewald4c221252015-04-21 20:19:25 +0000424 };
425
426 // Insert menu
427 this._element.insertBefore(
Akrone4961b12017-05-10 21:04:46 +0200428 menu.element(),
429 this._matchopE
Nils Diewald4c221252015-04-21 20:19:25 +0000430 );
431
432 // Release event
433 var that = this;
434 menu.released(function (mo) {
Akrone4961b12017-05-10 21:04:46 +0200435 that.matchop(mo).update();
436 this.hide();
Nils Diewald4c221252015-04-21 20:19:25 +0000437 });
438
439 menu.show();
440 menu.focus();
441 },
442
443
444 /**
445 * Get or set the type
446 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000447 type : function (type) {
448 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200449 this._type = type;
450 this._changed();
451 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000452 };
453 return this._type || "string";
454 },
455
Nils Diewald4c221252015-04-21 20:19:25 +0000456
457 /**
458 * Get or set the value
459 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000460 value : function (value) {
461 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200462 if (this._type === 'date' && !KorAP._validDateRE.test(value)) {
463 delete this._value;
464 }
465 else {
466 this._value = value;
467 };
468 this._changed();
469 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000470 };
471 return this._value;
472 },
473
Nils Diewald4c221252015-04-21 20:19:25 +0000474
475 // Click on the match operator, show me the menu
476 _changeValue : function (e) {
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000477 var v = this.value();
478 var that = this;
Nils Diewald87507832015-05-01 23:36:41 +0000479
Nils Diewald7148c6f2015-05-04 15:07:53 +0000480 // Show datepicker
Nils Diewald87507832015-05-01 23:36:41 +0000481 if (this.type() === 'date') {
Akrone4961b12017-05-10 21:04:46 +0200482 var dp = KorAP._vcDatePicker;
Akron516b6f92018-01-03 17:46:59 +0100483 dp.fromString(v);
Nils Diewald87507832015-05-01 23:36:41 +0000484
Akrone4961b12017-05-10 21:04:46 +0200485 // Todo: change this
486 dp.onclick(function (selected) {
Nils Diewald87507832015-05-01 23:36:41 +0000487
Akrone4961b12017-05-10 21:04:46 +0200488 // There are values selected
489 if (selected['year']) {
490 that.value(this.toString());
491 that.update();
492 return;
493 };
Nils Diewald87507832015-05-01 23:36:41 +0000494
Akrone4961b12017-05-10 21:04:46 +0200495 // Remove datepicker
496 that._element.removeChild(
497 dp.element()
498 );
499 });
Nils Diewald87507832015-05-01 23:36:41 +0000500
Akrone4961b12017-05-10 21:04:46 +0200501 // Get element of the date picker
502 var dpElem = dp.show();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000503
Akrone4961b12017-05-10 21:04:46 +0200504 this._element.insertBefore(
505 dpElem,
506 this._valueE
507 );
Nils Diewald7148c6f2015-05-04 15:07:53 +0000508
Akron516b6f92018-01-03 17:46:59 +0100509 dp.input().focus();
Nils Diewald87507832015-05-01 23:36:41 +0000510 }
511 else {
Akrone4961b12017-05-10 21:04:46 +0200512 var regex = this.type() === 'regex' ? true : false;
513 var str = stringValClass.create(this.value(), regex);
514 var strElem = str.element();
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000515
Akrone4961b12017-05-10 21:04:46 +0200516 str.store = function (value, regex) {
517 that.value(value);
518 if (regex === true)
519 that.type('regex');
520 else
521 that.type('string');
522
523 that._element.removeChild(
524 this._element
525 );
526 that.update();
527 };
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000528
Akrone4961b12017-05-10 21:04:46 +0200529 // Insert element
530 this._element.insertBefore(
531 strElem,
532 this._valueE
533 );
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000534
Akrone4961b12017-05-10 21:04:46 +0200535 str.focus();
Nils Diewald87507832015-05-01 23:36:41 +0000536 };
Nils Diewald4c221252015-04-21 20:19:25 +0000537 },
538
539
Nils Diewald0e6992a2015-04-14 20:13:52 +0000540 rewrites : function () {
541 return this._rewrites;
542 },
543
Akronea4e9052017-07-06 16:12:05 +0200544 rewrite : function (value) {
545 if (typeof value === 'string') {
546 value = [{
547 "@type" : "koral:rewrite",
548 "operation" : "operation:" + value,
549 "src" : "Kalamar"
550 }];
551 };
552 this._rewrites = rewriteListClass.create(value);
553 },
554
Akron31d89942018-04-06 16:44:51 +0200555 // Mark the underlying data as being changed.
556 // This is important for rerendering the dom.
557 // This will also remove rewrite markers, when the data
558 // change happened by the user
Nils Diewald0e6992a2015-04-14 20:13:52 +0000559 _changed : function () {
560 this.__changed = true;
Nils Diewald4347ee92015-05-04 20:32:48 +0000561
Nils Diewald0e6992a2015-04-14 20:13:52 +0000562 if (this._rewrites === undefined)
Akrone4961b12017-05-10 21:04:46 +0200563 return;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000564
Akronea4e9052017-07-06 16:12:05 +0200565 delete this["_rewrites"];
Nils Diewald0e6992a2015-04-14 20:13:52 +0000566
567 if (this._element === undefined)
Akrone4961b12017-05-10 21:04:46 +0200568 return;
Akronea4e9052017-07-06 16:12:05 +0200569
Nils Diewald0e6992a2015-04-14 20:13:52 +0000570 this._element.classList.remove("rewritten");
571 },
572
Nils Diewald4c221252015-04-21 20:19:25 +0000573
Nils Diewald0e6992a2015-04-14 20:13:52 +0000574 toJson : function () {
575 if (!this.matchop() || !this.key())
Akrone4961b12017-05-10 21:04:46 +0200576 return {};
Nils Diewald0e6992a2015-04-14 20:13:52 +0000577
578 return {
Akrone4961b12017-05-10 21:04:46 +0200579 "@type" : "koral:" + this.ldType(),
580 "key" : this.key(),
581 "match" : "match:" + this.matchop(),
582 "value" : this.value() || '',
583 "type" : "type:" + this.type()
Nils Diewald0e6992a2015-04-14 20:13:52 +0000584 };
585 },
586
Nils Diewald4c221252015-04-21 20:19:25 +0000587
Nils Diewald0e6992a2015-04-14 20:13:52 +0000588 toQuery : function () {
589 if (!this.matchop() || !this.key())
Akrone4961b12017-05-10 21:04:46 +0200590 return "";
Nils Diewald0e6992a2015-04-14 20:13:52 +0000591
592 // Build doc string based on key
593 var string = this.key() + ' ';
594
595 // Add match operator
596 switch (this.matchop()) {
597 case "ne":
Akrone4961b12017-05-10 21:04:46 +0200598 string += '!=';
599 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000600 case "contains":
Akrone4961b12017-05-10 21:04:46 +0200601 string += '~';
602 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000603 case "excludes":
Akrone4961b12017-05-10 21:04:46 +0200604 string += '!~';
605 break;
Akron6a535d42015-08-26 20:16:58 +0200606 case "containsnot":
Akrone4961b12017-05-10 21:04:46 +0200607 string += '!~';
608 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000609 case "geq":
Akrone4961b12017-05-10 21:04:46 +0200610 string += 'since';
611 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000612 case "leq":
Akrone4961b12017-05-10 21:04:46 +0200613 string += 'until';
614 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000615 default:
Akrone4961b12017-05-10 21:04:46 +0200616 string += (this.type() == 'date') ? 'in' : '=';
617 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000618 };
619
620 string += ' ';
621
622 // Add value
623 switch (this.type()) {
624 case "date":
Akrone4961b12017-05-10 21:04:46 +0200625 return string + this.value();
626 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000627 case "regex":
Akron8778f5d2017-06-30 21:25:55 +0200628 return string + '/' + this.value().escapeRegex() + '/';
Akrone4961b12017-05-10 21:04:46 +0200629 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000630 case "string":
Akrone4961b12017-05-10 21:04:46 +0200631 return string + '"' + this.value().quote() + '"';
632 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000633 };
634
635 return "";
636 }
637 };
638});