blob: 2a0e8722af851e9839235a4a9ce8cbaf132e9b59 [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',
Akron587e4d92018-08-31 12:44:26 +02009 'vc/docgroupref',
Nils Diewald4c221252015-04-21 20:19:25 +000010 'util'
Akron587e4d92018-08-31 12:44:26 +020011], function (jsonldClass, rewriteListClass, stringValClass, docGroupRefClass) {
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000012
Akron1bdf5272018-07-24 18:51:30 +020013 /*
14 * TODO:
15 * Improve error handling by using window.onerror() to
16 * capture thrown errors and log them.
17 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000018
Akron31d89942018-04-06 16:44:51 +020019 const errstr802 = "Match type is not supported by value type";
Akron1bdf5272018-07-24 18:51:30 +020020 const errstr804 = "Unknown value type";
Akron0b489ad2018-02-02 16:49:32 +010021 const loc = KorAP.Locale;
Nils Diewald4c221252015-04-21 20:19:25 +000022 loc.EMPTY = loc.EMPTY || '⋯';
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000023
Nils Diewald0e6992a2015-04-14 20:13:52 +000024 return {
Nils Diewald4c221252015-04-21 20:19:25 +000025
26 // The JSON-LD type
Nils Diewald0e6992a2015-04-14 20:13:52 +000027 _ldType : "doc",
Nils Diewald4c221252015-04-21 20:19:25 +000028
29 // The object ... maybe not important
Nils Diewald0e6992a2015-04-14 20:13:52 +000030 _obj : function () { return '???'; /*KorAP.Doc*/ },
31
Nils Diewald4c221252015-04-21 20:19:25 +000032 /**
33 * Create a new document criterion
34 * by passing the parent object and a json construct.
35 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000036 create : function (parent, json) {
Nils Diewald4c221252015-04-21 20:19:25 +000037
38 // Create the object, inheriting from Json-LD class
Nils Diewald0e6992a2015-04-14 20:13:52 +000039 var obj = Object(jsonldClass).
Akrone4961b12017-05-10 21:04:46 +020040 create().
41 upgradeTo(this).
42 fromJson(json);
Nils Diewald4c221252015-04-21 20:19:25 +000043
Akron55a343b2018-04-06 19:57:36 +020044 if (obj === undefined) {
45 console.log(json);
Akrond2474aa2018-08-28 12:06:27 +020046 return;
Akron55a343b2018-04-06 19:57:36 +020047 };
48
Nils Diewald4c221252015-04-21 20:19:25 +000049 // Bind the parent
Nils Diewald0e6992a2015-04-14 20:13:52 +000050 if (parent !== undefined)
Akrone4961b12017-05-10 21:04:46 +020051 obj._parent = parent;
Nils Diewald0e6992a2015-04-14 20:13:52 +000052
53 obj.__changed = true;
54 return obj;
55 },
56
Nils Diewald4c221252015-04-21 20:19:25 +000057 /**
58 * Update the elements content.
59 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000060 update : function () {
61 if (this._element === undefined)
Akrone4961b12017-05-10 21:04:46 +020062 return this.element();
Nils Diewald0e6992a2015-04-14 20:13:52 +000063
64 // Get element
65 var e = this._element;
66
Akronb19803c2018-08-16 16:39:42 +020067 // Check if there is a change in the underlying data
68 if (!this.__changed)
69 return e;
70
Nils Diewald0e6992a2015-04-14 20:13:52 +000071 // Set ref - TODO: Cleanup!
72 e.refTo = this;
73
Nils Diewald0e6992a2015-04-14 20:13:52 +000074
Akronb19803c2018-08-16 16:39:42 +020075 // Was rewritten
76 if (this.rewrites() !== undefined) {
77 e.classList.add("rewritten");
Nils Diewald0e6992a2015-04-14 20:13:52 +000078 };
79
Akronb19803c2018-08-16 16:39:42 +020080 // Added key
81 this._keyE = document.createElement('span');
82 this._keyE.setAttribute('class', 'key');
83
84 // Change key
85 this._keyE.addEventListener('click', this._changeKey.bind(this));
86
87 if (this.key()) {
88 var k = this.key();
89 if (loc['VC_' + k] !== undefined)
90 k = loc['VC_' + k];
91 this._keyE.addT(k);
92 };
93
94 // Added match operator
95 this._matchopE = document.createElement('span');
96 this._matchopE.setAttribute('data-type', this.type());
97 this._matchopE.setAttribute('class', 'match');
98 this._matchopE.addT(this.matchop());
99
100 // Change matchop
101 this._matchopE.addEventListener(
102 'click',
103 this._changeMatchop.bind(this)
104 );
105
106 // Added value operator
107 this._valueE = document.createElement('span');
108 this._valueE.setAttribute('data-type', this.type());
109 this._valueE.setAttribute('class', 'value');
110
111 if (this.value()) {
112 this._valueE.addT(this.value());
113 }
114 else {
115 this._valueE.addT(loc.EMPTY);
Akronebc96662018-08-29 17:36:20 +0200116 this._valueE.classList.add('unspecified');
Akronb19803c2018-08-16 16:39:42 +0200117 };
118
119 // Change value
120 this._valueE.addEventListener(
121 'click',
122 this._changeValue.bind(this)
123 );
124
125 // Remove all element children
126 _removeChildren(e);
127
128 // Add spans
129 e.appendChild(this._keyE);
130 e.appendChild(this._matchopE);
131 e.appendChild(this._valueE);
132
133 this.__changed = false;
134
Nils Diewald0e6992a2015-04-14 20:13:52 +0000135 if (this._rewrites !== undefined) {
Akrone4961b12017-05-10 21:04:46 +0200136 e.appendChild(this._rewrites.element());
Nils Diewald0e6992a2015-04-14 20:13:52 +0000137 };
138
139 if (this._parent !== undefined) {
Akronb19803c2018-08-16 16:39:42 +0200140
Akrone4961b12017-05-10 21:04:46 +0200141 // Set operators
142 var op = this.operators(
143 true,
144 true,
145 true
146 );
Nils Diewald0e6992a2015-04-14 20:13:52 +0000147
Akrone4961b12017-05-10 21:04:46 +0200148 // Append new operators
149 e.appendChild(op.element());
Nils Diewald0e6992a2015-04-14 20:13:52 +0000150 };
151
hebastaa0282be2018-12-05 16:58:00 +0100152 if(KorAP.vc){
153 KorAP.vc.checkGrayingStat();
154 }
155
Nils Diewald0e6992a2015-04-14 20:13:52 +0000156 return e;
157 },
158
Nils Diewald4c221252015-04-21 20:19:25 +0000159
160 /**
161 * Get the associated element
162 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000163 element : function () {
164 if (this._element !== undefined)
Akrone4961b12017-05-10 21:04:46 +0200165 return this._element;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000166
167 this._element = document.createElement('div');
168 this._element.setAttribute('class', 'doc');
169
170 this.update();
171 return this._element;
172 },
173
Nils Diewald4c221252015-04-21 20:19:25 +0000174 /**
175 * Wrap a new operation around the doc element
176 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000177 wrap : function (op) {
178 var parent = this.parent();
Nils Diewald7c8ced22015-04-15 19:21:00 +0000179 var group = require('vc/docgroup').create(parent);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000180 group.operation(op);
181 group.append(this);
182 group.append();
183 return parent.replaceOperand(this, group).update();
184 },
185
Akron587e4d92018-08-31 12:44:26 +0200186 replaceWith : function (op) {
187 var p = this.parent();
188
189 if (p.ldType() === 'docGroup') {
190 p.replaceOperand(this,op);
191 }
192 else if (p.ldType() == null) {
193 p.root(op);
194 };
195 p.update();
196
197 this.destroy();
198 },
199
Nils Diewald4c221252015-04-21 20:19:25 +0000200 /**
201 * Deserialize from json
202 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000203 fromJson : function (json) {
204 if (json === undefined)
Akrone4961b12017-05-10 21:04:46 +0200205 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000206
207 if (json["@type"] === undefined) {
Akrone4961b12017-05-10 21:04:46 +0200208 KorAP.log(701, "JSON-LD group has no @type attribute");
209 return;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000210 };
211
212 if (json["value"] === undefined ||
Akrone4961b12017-05-10 21:04:46 +0200213 typeof json["value"] != 'string') {
214 KorAP.log(805, "Value is invalid");
215 return;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000216 };
217
Akronea4e9052017-07-06 16:12:05 +0200218 var rewrite;
219
Nils Diewald0e6992a2015-04-14 20:13:52 +0000220 // There is a defined key
221 if (json["key"] !== undefined &&
Akrone4961b12017-05-10 21:04:46 +0200222 typeof json["key"] === 'string') {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000223
Akrone4961b12017-05-10 21:04:46 +0200224 // Set key
225 this.key(json["key"]);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000226
Akrone4961b12017-05-10 21:04:46 +0200227 // Set match operation
228 if (json["match"] !== undefined) {
229 if (typeof json["match"] === 'string') {
Akron31d89942018-04-06 16:44:51 +0200230
Akrone4961b12017-05-10 21:04:46 +0200231 this.matchop(json["match"]);
232 }
233 else {
Akron31d89942018-04-06 16:44:51 +0200234 KorAP.log(802, errstr802);
Akrone4961b12017-05-10 21:04:46 +0200235 return;
236 };
237 };
Akron31d89942018-04-06 16:44:51 +0200238
Akronddc98a72018-04-06 17:33:52 +0200239 // Type is unspecified - but may be known by the menu
240 if (json["type"] === undefined && KorAP._vcKeyMenu) {
241
242 // Check the VC list if the field is known
243 var type = KorAP._vcKeyMenu.typeOf(this.key());
244 if (type != undefined) {
245 json["type"] = "type:" + type;
246 };
247 };
248
249 // Type is still undefined
Akron31d89942018-04-06 16:44:51 +0200250 if (json["type"] === undefined) {
Akronddc98a72018-04-06 17:33:52 +0200251
Akron31d89942018-04-06 16:44:51 +0200252 // Check match type
253 if (!KorAP._validUnspecMatchRE.test(this.matchop())) {
254 KorAP.log(802, errstr802);
255
256 // Rewrite method
257 this.matchop('eq');
258 rewrite = 'modification';
259 };
260
261 // Set string value
262 this.value(json["value"]);
263 }
264
265 // Field is string type
266 else if (json["type"] == "type:string") {
Akrone4961b12017-05-10 21:04:46 +0200267 this.type("string");
Nils Diewald0e6992a2015-04-14 20:13:52 +0000268
Akrone4961b12017-05-10 21:04:46 +0200269 // Check match type
270 if (!KorAP._validStringMatchRE.test(this.matchop())) {
Akron31d89942018-04-06 16:44:51 +0200271 KorAP.log(802, errstr802);
272
273 // Rewrite method
274 this.matchop('eq');
275 rewrite = 'modification';
276 };
277
278 // Set string value
279 this.value(json["value"]);
280 }
281
282 // Field is specified
283 else if (json["type"] == "type:text") {
284 this.type("text");
285
286 // Check match type
287 if (!KorAP._validTextMatchRE.test(this.matchop())) {
288 KorAP.log(802, errstr802);
Akronea4e9052017-07-06 16:12:05 +0200289
290 // Rewrite method
291 this.matchop('eq');
292 rewrite = 'modification';
Akrone4961b12017-05-10 21:04:46 +0200293 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000294
Akrone4961b12017-05-10 21:04:46 +0200295 // Set string value
296 this.value(json["value"]);
297 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000298
Akrone4961b12017-05-10 21:04:46 +0200299 // Key is a date
300 else if (json["type"] === "type:date") {
301 this.type("date");
Nils Diewald0e6992a2015-04-14 20:13:52 +0000302
Akrone4961b12017-05-10 21:04:46 +0200303 if (json["value"] !== undefined &&
304 KorAP._validDateRE.test(json["value"])) {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000305
Akrone4961b12017-05-10 21:04:46 +0200306 if (!KorAP._validDateMatchRE.test(this.matchop())) {
Akron31d89942018-04-06 16:44:51 +0200307 KorAP.log(802, errstr802);
Akronea4e9052017-07-06 16:12:05 +0200308
309 // Rewrite method
310 this.matchop('eq');
311 rewrite = 'modification';
Akrone4961b12017-05-10 21:04:46 +0200312 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000313
Akrone4961b12017-05-10 21:04:46 +0200314 // Set value
315 this.value(json["value"]);
316 }
317 else {
318 KorAP.log(806, "Value is not a valid date string");
319 return;
320 };
321 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000322
Akrone4961b12017-05-10 21:04:46 +0200323 // Key is a regular expression
324 else if (json["type"] === "type:regex") {
325 this.type("regex");
Nils Diewald0e6992a2015-04-14 20:13:52 +0000326
Akrone4961b12017-05-10 21:04:46 +0200327 try {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000328
Akrone4961b12017-05-10 21:04:46 +0200329 // Try to create a regular expression
330 var check = new RegExp(json["value"]);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000331
Akrone65a88a2018-04-05 19:14:20 +0200332 if (!KorAP._validStringMatchRE.test(this.matchop())) {
Akron31d89942018-04-06 16:44:51 +0200333 KorAP.log(802, errstr802);
Akronea4e9052017-07-06 16:12:05 +0200334
335 // Rewrite method
336 this.matchop('eq');
337 rewrite = 'modification';
Akrone4961b12017-05-10 21:04:46 +0200338 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000339
Akrone4961b12017-05-10 21:04:46 +0200340 this.value(json["value"]);
341 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000342
Akrone4961b12017-05-10 21:04:46 +0200343 catch (e) {
344 KorAP.log(807, "Value is not a valid regular expression");
345 return;
346 };
347 this.type("regex");
348 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000349
Akrone4961b12017-05-10 21:04:46 +0200350 else {
Akron1bdf5272018-07-24 18:51:30 +0200351 KorAP.log(804, errstr804 + ": " + this.type());
352 throw new Error(errstr804 + ": " + this.type());
Akrone4961b12017-05-10 21:04:46 +0200353 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000354 };
355
Akronea4e9052017-07-06 16:12:05 +0200356 // Rewrite coming from the server
Nils Diewald0e6992a2015-04-14 20:13:52 +0000357 if (json["rewrites"] !== undefined) {
Akronea4e9052017-07-06 16:12:05 +0200358 this.rewrite(json["rewrites"]);
359 }
360
361 // Rewrite coming from Kalamar
362 else if (rewrite !== undefined) {
363 this.rewrite(rewrite);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000364 };
Akronea4e9052017-07-06 16:12:05 +0200365
Nils Diewald0e6992a2015-04-14 20:13:52 +0000366 return this;
367 },
368
Nils Diewald4c221252015-04-21 20:19:25 +0000369 /**
370 * Get or set the key
371 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000372 key : function (value) {
373 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200374 this._key = value;
375 this._changed();
376 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000377 };
378 return this._key;
379 },
380
Nils Diewald4c221252015-04-21 20:19:25 +0000381 // Click on the key, show me the menu
382 _changeKey : function (e) {
383 var menu = KorAP._vcKeyMenu;
384
385 // Insert menu
386 this._element.insertBefore(
Akrone4961b12017-05-10 21:04:46 +0200387 menu.element(),
388 this._keyE
Nils Diewald4c221252015-04-21 20:19:25 +0000389 );
390
391 // Release event
392 var that = this;
393 menu.released(function (key, type) {
Nils Diewald4c221252015-04-21 20:19:25 +0000394
Akron587e4d92018-08-31 12:44:26 +0200395 if (type === 'ref') {
396 // KorAP._delete.bind(that);
397 var ref = docGroupRefClass.create(that.parent());
398 that.replaceWith(ref);
399 }
400 else {
401 var doc = that.key(key).type(type);
Nils Diewald4c221252015-04-21 20:19:25 +0000402
Akron587e4d92018-08-31 12:44:26 +0200403 // This may not be compatible - then switch to default
404 doc.matchop(doc.matchop());
405 doc.value(doc.value());
406
407 // Update the doc
408 doc.update();
409 };
Nils Diewald4c221252015-04-21 20:19:25 +0000410
Akrone4961b12017-05-10 21:04:46 +0200411 // hide!
412 this.hide();
Nils Diewald4c221252015-04-21 20:19:25 +0000413 });
414
415 // TODO: Highlight the old value!
416 menu.show();
417 menu.focus();
418 },
419
420 /**
421 * Get or set the match operator
422 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000423 matchop : function (match) {
Akron31d89942018-04-06 16:44:51 +0200424
Nils Diewald0e6992a2015-04-14 20:13:52 +0000425 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200426 var m = match.replace(/^match:/, '');
Akron31d89942018-04-06 16:44:51 +0200427
Akrone4961b12017-05-10 21:04:46 +0200428 if (
Akron31d89942018-04-06 16:44:51 +0200429 (this._type == undefined) // && KorAP._validUnspecMatchRE.test(m))
Akrone4961b12017-05-10 21:04:46 +0200430 ||
431 (
432 (this._type === 'string' || this._type === 'regex') &&
433 KorAP._validStringMatchRE.test(m)
434 )
435 ||
Akron31d89942018-04-06 16:44:51 +0200436 (this._type === 'text' && KorAP._validTextMatchRE.test(m))
437 ||
Akrone4961b12017-05-10 21:04:46 +0200438 (this._type === 'date' && KorAP._validDateMatchRE.test(m))
439 ) {
440 this._matchop = m;
441 }
442 else {
443 this._matchop = "eq";
444 };
Nils Diewald4c221252015-04-21 20:19:25 +0000445
Akrone4961b12017-05-10 21:04:46 +0200446 this._changed();
Akron31d89942018-04-06 16:44:51 +0200447 return this
Nils Diewald0e6992a2015-04-14 20:13:52 +0000448 };
449 return this._matchop || "eq";
450 },
451
Nils Diewald4c221252015-04-21 20:19:25 +0000452
453 // Click on the match operator, show me the menu
454 _changeMatchop : function (e) {
455 var menu = KorAP._vcMatchopMenu[this.type()];
456
457 if (menu === undefined) {
Akrone4961b12017-05-10 21:04:46 +0200458 KorAP.log(0, "Unable to init menu for " + this.type());
459 return;
Nils Diewald4c221252015-04-21 20:19:25 +0000460 };
461
462 // Insert menu
463 this._element.insertBefore(
Akrone4961b12017-05-10 21:04:46 +0200464 menu.element(),
465 this._matchopE
Nils Diewald4c221252015-04-21 20:19:25 +0000466 );
467
468 // Release event
469 var that = this;
470 menu.released(function (mo) {
Akrone4961b12017-05-10 21:04:46 +0200471 that.matchop(mo).update();
472 this.hide();
Nils Diewald4c221252015-04-21 20:19:25 +0000473 });
474
475 menu.show();
476 menu.focus();
477 },
478
479
480 /**
481 * Get or set the type
482 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000483 type : function (type) {
484 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200485 this._type = type;
486 this._changed();
487 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000488 };
489 return this._type || "string";
490 },
491
Nils Diewald4c221252015-04-21 20:19:25 +0000492
493 /**
494 * Get or set the value
495 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000496 value : function (value) {
497 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200498 if (this._type === 'date' && !KorAP._validDateRE.test(value)) {
499 delete this._value;
500 }
501 else {
502 this._value = value;
503 };
504 this._changed();
505 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000506 };
507 return this._value;
508 },
509
Nils Diewald4c221252015-04-21 20:19:25 +0000510
511 // Click on the match operator, show me the menu
512 _changeValue : function (e) {
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000513 var v = this.value();
514 var that = this;
Akron8db5e3a2018-05-28 19:25:26 +0200515
Nils Diewald7148c6f2015-05-04 15:07:53 +0000516 // Show datepicker
Nils Diewald87507832015-05-01 23:36:41 +0000517 if (this.type() === 'date') {
Akrone4961b12017-05-10 21:04:46 +0200518 var dp = KorAP._vcDatePicker;
Akron516b6f92018-01-03 17:46:59 +0100519 dp.fromString(v);
Nils Diewald87507832015-05-01 23:36:41 +0000520
Akrone4961b12017-05-10 21:04:46 +0200521 // Todo: change this
522 dp.onclick(function (selected) {
Nils Diewald87507832015-05-01 23:36:41 +0000523
Akrone4961b12017-05-10 21:04:46 +0200524 // There are values selected
525 if (selected['year']) {
526 that.value(this.toString());
527 that.update();
528 return;
529 };
Nils Diewald87507832015-05-01 23:36:41 +0000530
Akrone4961b12017-05-10 21:04:46 +0200531 // Remove datepicker
532 that._element.removeChild(
533 dp.element()
534 );
535 });
Nils Diewald87507832015-05-01 23:36:41 +0000536
Akrone4961b12017-05-10 21:04:46 +0200537 // Get element of the date picker
538 var dpElem = dp.show();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000539
Akrone4961b12017-05-10 21:04:46 +0200540 this._element.insertBefore(
541 dpElem,
542 this._valueE
543 );
Nils Diewald7148c6f2015-05-04 15:07:53 +0000544
Akron516b6f92018-01-03 17:46:59 +0100545 dp.input().focus();
Nils Diewald87507832015-05-01 23:36:41 +0000546 }
547 else {
Akrone4961b12017-05-10 21:04:46 +0200548 var regex = this.type() === 'regex' ? true : false;
549 var str = stringValClass.create(this.value(), regex);
550 var strElem = str.element();
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000551
Akrone4961b12017-05-10 21:04:46 +0200552 str.store = function (value, regex) {
553 that.value(value);
554 if (regex === true)
555 that.type('regex');
556 else
557 that.type('string');
558
559 that._element.removeChild(
560 this._element
561 );
562 that.update();
563 };
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000564
Akrone4961b12017-05-10 21:04:46 +0200565 // Insert element
566 this._element.insertBefore(
567 strElem,
568 this._valueE
569 );
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000570
Akrone4961b12017-05-10 21:04:46 +0200571 str.focus();
Nils Diewald87507832015-05-01 23:36:41 +0000572 };
Nils Diewald4c221252015-04-21 20:19:25 +0000573 },
574
575
Nils Diewald0e6992a2015-04-14 20:13:52 +0000576 rewrites : function () {
577 return this._rewrites;
578 },
579
Akronea4e9052017-07-06 16:12:05 +0200580 rewrite : function (value) {
581 if (typeof value === 'string') {
582 value = [{
583 "@type" : "koral:rewrite",
584 "operation" : "operation:" + value,
585 "src" : "Kalamar"
586 }];
587 };
588 this._rewrites = rewriteListClass.create(value);
589 },
590
Akron31d89942018-04-06 16:44:51 +0200591 // Mark the underlying data as being changed.
592 // This is important for rerendering the dom.
593 // This will also remove rewrite markers, when the data
594 // change happened by the user
Nils Diewald0e6992a2015-04-14 20:13:52 +0000595 _changed : function () {
596 this.__changed = true;
Nils Diewald4347ee92015-05-04 20:32:48 +0000597
Nils Diewald0e6992a2015-04-14 20:13:52 +0000598 if (this._rewrites === undefined)
Akrone4961b12017-05-10 21:04:46 +0200599 return;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000600
Akronea4e9052017-07-06 16:12:05 +0200601 delete this["_rewrites"];
Nils Diewald0e6992a2015-04-14 20:13:52 +0000602
603 if (this._element === undefined)
Akrone4961b12017-05-10 21:04:46 +0200604 return;
Akronea4e9052017-07-06 16:12:05 +0200605
Nils Diewald0e6992a2015-04-14 20:13:52 +0000606 this._element.classList.remove("rewritten");
607 },
608
Nils Diewald4c221252015-04-21 20:19:25 +0000609
Nils Diewald0e6992a2015-04-14 20:13:52 +0000610 toJson : function () {
611 if (!this.matchop() || !this.key())
Akrone4961b12017-05-10 21:04:46 +0200612 return {};
Nils Diewald0e6992a2015-04-14 20:13:52 +0000613
614 return {
Akrone4961b12017-05-10 21:04:46 +0200615 "@type" : "koral:" + this.ldType(),
616 "key" : this.key(),
617 "match" : "match:" + this.matchop(),
618 "value" : this.value() || '',
619 "type" : "type:" + this.type()
Nils Diewald0e6992a2015-04-14 20:13:52 +0000620 };
621 },
622
hebastaa0282be2018-12-05 16:58:00 +0100623 incomplete : function () {
624 return !(this.matchop() && this.key() && this.value());
625 },
Nils Diewald4c221252015-04-21 20:19:25 +0000626
Nils Diewald0e6992a2015-04-14 20:13:52 +0000627 toQuery : function () {
hebastaa0282be2018-12-05 16:58:00 +0100628 if (this.incomplete())
Akrone4961b12017-05-10 21:04:46 +0200629 return "";
Nils Diewald0e6992a2015-04-14 20:13:52 +0000630
631 // Build doc string based on key
632 var string = this.key() + ' ';
633
634 // Add match operator
635 switch (this.matchop()) {
636 case "ne":
Akrone4961b12017-05-10 21:04:46 +0200637 string += '!=';
638 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000639 case "contains":
Akrone4961b12017-05-10 21:04:46 +0200640 string += '~';
641 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000642 case "excludes":
Akrone4961b12017-05-10 21:04:46 +0200643 string += '!~';
644 break;
Akron6a535d42015-08-26 20:16:58 +0200645 case "containsnot":
Akrone4961b12017-05-10 21:04:46 +0200646 string += '!~';
647 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000648 case "geq":
Akrone4961b12017-05-10 21:04:46 +0200649 string += 'since';
650 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000651 case "leq":
Akrone4961b12017-05-10 21:04:46 +0200652 string += 'until';
653 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000654 default:
Akrone4961b12017-05-10 21:04:46 +0200655 string += (this.type() == 'date') ? 'in' : '=';
656 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000657 };
658
659 string += ' ';
660
661 // Add value
662 switch (this.type()) {
663 case "date":
Akrone4961b12017-05-10 21:04:46 +0200664 return string + this.value();
Nils Diewald0e6992a2015-04-14 20:13:52 +0000665 case "regex":
Akron8778f5d2017-06-30 21:25:55 +0200666 return string + '/' + this.value().escapeRegex() + '/';
Nils Diewald0e6992a2015-04-14 20:13:52 +0000667 case "string":
Akron1bdf5272018-07-24 18:51:30 +0200668 case "text":
Akrone4961b12017-05-10 21:04:46 +0200669 return string + '"' + this.value().quote() + '"';
Nils Diewald0e6992a2015-04-14 20:13:52 +0000670 };
671
672 return "";
673 }
674 };
675});