blob: 8ea8364d62475694a7411bfaa93aafef176467cd [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){
hebasta48842cf2018-12-11 12:57:38 +0100153 var vcchevent = new CustomEvent('vcChange', {'detail':this});
hebasta4dd77bc2019-02-07 12:57:57 +0100154 KorAP.vc.element().dispatchEvent(vcchevent);
hebastaa0282be2018-12-05 16:58:00 +0100155 }
156
Nils Diewald0e6992a2015-04-14 20:13:52 +0000157 return e;
158 },
159
Nils Diewald4c221252015-04-21 20:19:25 +0000160
161 /**
162 * Get the associated element
163 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000164 element : function () {
165 if (this._element !== undefined)
Akrone4961b12017-05-10 21:04:46 +0200166 return this._element;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000167
168 this._element = document.createElement('div');
169 this._element.setAttribute('class', 'doc');
170
171 this.update();
172 return this._element;
173 },
174
Nils Diewald4c221252015-04-21 20:19:25 +0000175 /**
176 * Wrap a new operation around the doc element
177 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000178 wrap : function (op) {
179 var parent = this.parent();
Nils Diewald7c8ced22015-04-15 19:21:00 +0000180 var group = require('vc/docgroup').create(parent);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000181 group.operation(op);
182 group.append(this);
183 group.append();
184 return parent.replaceOperand(this, group).update();
185 },
186
Akron587e4d92018-08-31 12:44:26 +0200187 replaceWith : function (op) {
188 var p = this.parent();
189
190 if (p.ldType() === 'docGroup') {
191 p.replaceOperand(this,op);
192 }
193 else if (p.ldType() == null) {
194 p.root(op);
195 };
196 p.update();
197
198 this.destroy();
199 },
200
Nils Diewald4c221252015-04-21 20:19:25 +0000201 /**
202 * Deserialize from json
203 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000204 fromJson : function (json) {
205 if (json === undefined)
Akrone4961b12017-05-10 21:04:46 +0200206 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000207
208 if (json["@type"] === undefined) {
Akrone4961b12017-05-10 21:04:46 +0200209 KorAP.log(701, "JSON-LD group has no @type attribute");
210 return;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000211 };
212
213 if (json["value"] === undefined ||
Akrone4961b12017-05-10 21:04:46 +0200214 typeof json["value"] != 'string') {
215 KorAP.log(805, "Value is invalid");
216 return;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000217 };
218
Akronea4e9052017-07-06 16:12:05 +0200219 var rewrite;
220
Nils Diewald0e6992a2015-04-14 20:13:52 +0000221 // There is a defined key
222 if (json["key"] !== undefined &&
Akrone4961b12017-05-10 21:04:46 +0200223 typeof json["key"] === 'string') {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000224
Akrone4961b12017-05-10 21:04:46 +0200225 // Set key
226 this.key(json["key"]);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000227
Akrone4961b12017-05-10 21:04:46 +0200228 // Set match operation
229 if (json["match"] !== undefined) {
230 if (typeof json["match"] === 'string') {
Akron31d89942018-04-06 16:44:51 +0200231
Akrone4961b12017-05-10 21:04:46 +0200232 this.matchop(json["match"]);
233 }
234 else {
Akron31d89942018-04-06 16:44:51 +0200235 KorAP.log(802, errstr802);
Akrone4961b12017-05-10 21:04:46 +0200236 return;
237 };
238 };
Akron31d89942018-04-06 16:44:51 +0200239
Akronddc98a72018-04-06 17:33:52 +0200240 // Type is unspecified - but may be known by the menu
241 if (json["type"] === undefined && KorAP._vcKeyMenu) {
242
243 // Check the VC list if the field is known
244 var type = KorAP._vcKeyMenu.typeOf(this.key());
245 if (type != undefined) {
246 json["type"] = "type:" + type;
247 };
248 };
249
250 // Type is still undefined
Akron31d89942018-04-06 16:44:51 +0200251 if (json["type"] === undefined) {
Akronddc98a72018-04-06 17:33:52 +0200252
Akron31d89942018-04-06 16:44:51 +0200253 // Check match type
254 if (!KorAP._validUnspecMatchRE.test(this.matchop())) {
255 KorAP.log(802, errstr802);
256
257 // Rewrite method
258 this.matchop('eq');
259 rewrite = 'modification';
260 };
261
262 // Set string value
263 this.value(json["value"]);
264 }
265
266 // Field is string type
267 else if (json["type"] == "type:string") {
Akrone4961b12017-05-10 21:04:46 +0200268 this.type("string");
Nils Diewald0e6992a2015-04-14 20:13:52 +0000269
Akrone4961b12017-05-10 21:04:46 +0200270 // Check match type
271 if (!KorAP._validStringMatchRE.test(this.matchop())) {
Akron31d89942018-04-06 16:44:51 +0200272 KorAP.log(802, errstr802);
273
274 // Rewrite method
275 this.matchop('eq');
276 rewrite = 'modification';
277 };
278
279 // Set string value
280 this.value(json["value"]);
281 }
282
283 // Field is specified
284 else if (json["type"] == "type:text") {
285 this.type("text");
286
287 // Check match type
288 if (!KorAP._validTextMatchRE.test(this.matchop())) {
289 KorAP.log(802, errstr802);
Akronea4e9052017-07-06 16:12:05 +0200290
291 // Rewrite method
292 this.matchop('eq');
293 rewrite = 'modification';
Akrone4961b12017-05-10 21:04:46 +0200294 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000295
Akrone4961b12017-05-10 21:04:46 +0200296 // Set string value
297 this.value(json["value"]);
298 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000299
Akrone4961b12017-05-10 21:04:46 +0200300 // Key is a date
301 else if (json["type"] === "type:date") {
302 this.type("date");
Nils Diewald0e6992a2015-04-14 20:13:52 +0000303
Akrone4961b12017-05-10 21:04:46 +0200304 if (json["value"] !== undefined &&
305 KorAP._validDateRE.test(json["value"])) {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000306
Akrone4961b12017-05-10 21:04:46 +0200307 if (!KorAP._validDateMatchRE.test(this.matchop())) {
Akron31d89942018-04-06 16:44:51 +0200308 KorAP.log(802, errstr802);
Akronea4e9052017-07-06 16:12:05 +0200309
310 // Rewrite method
311 this.matchop('eq');
312 rewrite = 'modification';
Akrone4961b12017-05-10 21:04:46 +0200313 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000314
Akrone4961b12017-05-10 21:04:46 +0200315 // Set value
316 this.value(json["value"]);
317 }
318 else {
319 KorAP.log(806, "Value is not a valid date string");
320 return;
321 };
322 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000323
Akrone4961b12017-05-10 21:04:46 +0200324 // Key is a regular expression
325 else if (json["type"] === "type:regex") {
326 this.type("regex");
Nils Diewald0e6992a2015-04-14 20:13:52 +0000327
Akrone4961b12017-05-10 21:04:46 +0200328 try {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000329
Akrone4961b12017-05-10 21:04:46 +0200330 // Try to create a regular expression
331 var check = new RegExp(json["value"]);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000332
Akrone65a88a2018-04-05 19:14:20 +0200333 if (!KorAP._validStringMatchRE.test(this.matchop())) {
Akron31d89942018-04-06 16:44:51 +0200334 KorAP.log(802, errstr802);
Akronea4e9052017-07-06 16:12:05 +0200335
336 // Rewrite method
337 this.matchop('eq');
338 rewrite = 'modification';
Akrone4961b12017-05-10 21:04:46 +0200339 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000340
Akrone4961b12017-05-10 21:04:46 +0200341 this.value(json["value"]);
342 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000343
Akrone4961b12017-05-10 21:04:46 +0200344 catch (e) {
345 KorAP.log(807, "Value is not a valid regular expression");
346 return;
347 };
348 this.type("regex");
349 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000350
Akrone4961b12017-05-10 21:04:46 +0200351 else {
Akron1bdf5272018-07-24 18:51:30 +0200352 KorAP.log(804, errstr804 + ": " + this.type());
353 throw new Error(errstr804 + ": " + this.type());
Akrone4961b12017-05-10 21:04:46 +0200354 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000355 };
356
Akronea4e9052017-07-06 16:12:05 +0200357 // Rewrite coming from the server
Nils Diewald0e6992a2015-04-14 20:13:52 +0000358 if (json["rewrites"] !== undefined) {
Akronea4e9052017-07-06 16:12:05 +0200359 this.rewrite(json["rewrites"]);
360 }
361
362 // Rewrite coming from Kalamar
363 else if (rewrite !== undefined) {
364 this.rewrite(rewrite);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000365 };
Akronea4e9052017-07-06 16:12:05 +0200366
Nils Diewald0e6992a2015-04-14 20:13:52 +0000367 return this;
368 },
369
Nils Diewald4c221252015-04-21 20:19:25 +0000370 /**
371 * Get or set the key
372 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000373 key : function (value) {
374 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200375 this._key = value;
376 this._changed();
377 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000378 };
379 return this._key;
380 },
381
Nils Diewald4c221252015-04-21 20:19:25 +0000382 // Click on the key, show me the menu
383 _changeKey : function (e) {
384 var menu = KorAP._vcKeyMenu;
385
386 // Insert menu
387 this._element.insertBefore(
Akrone4961b12017-05-10 21:04:46 +0200388 menu.element(),
389 this._keyE
Nils Diewald4c221252015-04-21 20:19:25 +0000390 );
391
392 // Release event
393 var that = this;
394 menu.released(function (key, type) {
Nils Diewald4c221252015-04-21 20:19:25 +0000395
Akron587e4d92018-08-31 12:44:26 +0200396 if (type === 'ref') {
397 // KorAP._delete.bind(that);
398 var ref = docGroupRefClass.create(that.parent());
399 that.replaceWith(ref);
400 }
401 else {
402 var doc = that.key(key).type(type);
Nils Diewald4c221252015-04-21 20:19:25 +0000403
Akron587e4d92018-08-31 12:44:26 +0200404 // This may not be compatible - then switch to default
405 doc.matchop(doc.matchop());
406 doc.value(doc.value());
407
408 // Update the doc
409 doc.update();
410 };
Nils Diewald4c221252015-04-21 20:19:25 +0000411
Akrone4961b12017-05-10 21:04:46 +0200412 // hide!
413 this.hide();
Nils Diewald4c221252015-04-21 20:19:25 +0000414 });
415
416 // TODO: Highlight the old value!
417 menu.show();
418 menu.focus();
419 },
420
421 /**
422 * Get or set the match operator
423 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000424 matchop : function (match) {
Akron31d89942018-04-06 16:44:51 +0200425
Nils Diewald0e6992a2015-04-14 20:13:52 +0000426 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200427 var m = match.replace(/^match:/, '');
Akron31d89942018-04-06 16:44:51 +0200428
Akrone4961b12017-05-10 21:04:46 +0200429 if (
Akron31d89942018-04-06 16:44:51 +0200430 (this._type == undefined) // && KorAP._validUnspecMatchRE.test(m))
Akrone4961b12017-05-10 21:04:46 +0200431 ||
432 (
433 (this._type === 'string' || this._type === 'regex') &&
434 KorAP._validStringMatchRE.test(m)
435 )
436 ||
Akron31d89942018-04-06 16:44:51 +0200437 (this._type === 'text' && KorAP._validTextMatchRE.test(m))
438 ||
Akrone4961b12017-05-10 21:04:46 +0200439 (this._type === 'date' && KorAP._validDateMatchRE.test(m))
440 ) {
441 this._matchop = m;
442 }
443 else {
444 this._matchop = "eq";
445 };
Nils Diewald4c221252015-04-21 20:19:25 +0000446
Akrone4961b12017-05-10 21:04:46 +0200447 this._changed();
Akron31d89942018-04-06 16:44:51 +0200448 return this
Nils Diewald0e6992a2015-04-14 20:13:52 +0000449 };
450 return this._matchop || "eq";
451 },
452
Nils Diewald4c221252015-04-21 20:19:25 +0000453
454 // Click on the match operator, show me the menu
455 _changeMatchop : function (e) {
456 var menu = KorAP._vcMatchopMenu[this.type()];
457
458 if (menu === undefined) {
Akrone4961b12017-05-10 21:04:46 +0200459 KorAP.log(0, "Unable to init menu for " + this.type());
460 return;
Nils Diewald4c221252015-04-21 20:19:25 +0000461 };
462
463 // Insert menu
464 this._element.insertBefore(
Akrone4961b12017-05-10 21:04:46 +0200465 menu.element(),
466 this._matchopE
Nils Diewald4c221252015-04-21 20:19:25 +0000467 );
468
469 // Release event
470 var that = this;
471 menu.released(function (mo) {
Akrone4961b12017-05-10 21:04:46 +0200472 that.matchop(mo).update();
473 this.hide();
Nils Diewald4c221252015-04-21 20:19:25 +0000474 });
475
476 menu.show();
477 menu.focus();
478 },
479
480
481 /**
482 * Get or set the type
483 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000484 type : function (type) {
485 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200486 this._type = type;
487 this._changed();
488 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000489 };
490 return this._type || "string";
491 },
492
Nils Diewald4c221252015-04-21 20:19:25 +0000493
494 /**
495 * Get or set the value
496 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000497 value : function (value) {
498 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200499 if (this._type === 'date' && !KorAP._validDateRE.test(value)) {
500 delete this._value;
501 }
502 else {
503 this._value = value;
504 };
505 this._changed();
506 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000507 };
508 return this._value;
509 },
510
Nils Diewald4c221252015-04-21 20:19:25 +0000511
512 // Click on the match operator, show me the menu
513 _changeValue : function (e) {
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000514 var v = this.value();
515 var that = this;
Akron8db5e3a2018-05-28 19:25:26 +0200516
Nils Diewald7148c6f2015-05-04 15:07:53 +0000517 // Show datepicker
Nils Diewald87507832015-05-01 23:36:41 +0000518 if (this.type() === 'date') {
Akrone4961b12017-05-10 21:04:46 +0200519 var dp = KorAP._vcDatePicker;
Akron516b6f92018-01-03 17:46:59 +0100520 dp.fromString(v);
Nils Diewald87507832015-05-01 23:36:41 +0000521
Akrone4961b12017-05-10 21:04:46 +0200522 // Todo: change this
523 dp.onclick(function (selected) {
Nils Diewald87507832015-05-01 23:36:41 +0000524
Akrone4961b12017-05-10 21:04:46 +0200525 // There are values selected
526 if (selected['year']) {
527 that.value(this.toString());
528 that.update();
529 return;
530 };
Nils Diewald87507832015-05-01 23:36:41 +0000531
Akrone4961b12017-05-10 21:04:46 +0200532 // Remove datepicker
533 that._element.removeChild(
534 dp.element()
535 );
536 });
Nils Diewald87507832015-05-01 23:36:41 +0000537
Akrone4961b12017-05-10 21:04:46 +0200538 // Get element of the date picker
539 var dpElem = dp.show();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000540
Akrone4961b12017-05-10 21:04:46 +0200541 this._element.insertBefore(
542 dpElem,
543 this._valueE
544 );
Nils Diewald7148c6f2015-05-04 15:07:53 +0000545
Akron516b6f92018-01-03 17:46:59 +0100546 dp.input().focus();
Nils Diewald87507832015-05-01 23:36:41 +0000547 }
548 else {
Akrone4961b12017-05-10 21:04:46 +0200549 var regex = this.type() === 'regex' ? true : false;
550 var str = stringValClass.create(this.value(), regex);
551 var strElem = str.element();
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000552
Akrone4961b12017-05-10 21:04:46 +0200553 str.store = function (value, regex) {
554 that.value(value);
555 if (regex === true)
556 that.type('regex');
557 else
558 that.type('string');
559
560 that._element.removeChild(
561 this._element
562 );
563 that.update();
564 };
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000565
Akrone4961b12017-05-10 21:04:46 +0200566 // Insert element
567 this._element.insertBefore(
568 strElem,
569 this._valueE
570 );
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000571
Akrone4961b12017-05-10 21:04:46 +0200572 str.focus();
Nils Diewald87507832015-05-01 23:36:41 +0000573 };
Nils Diewald4c221252015-04-21 20:19:25 +0000574 },
575
576
Nils Diewald0e6992a2015-04-14 20:13:52 +0000577 rewrites : function () {
578 return this._rewrites;
579 },
580
Akronea4e9052017-07-06 16:12:05 +0200581 rewrite : function (value) {
582 if (typeof value === 'string') {
583 value = [{
584 "@type" : "koral:rewrite",
585 "operation" : "operation:" + value,
586 "src" : "Kalamar"
587 }];
588 };
589 this._rewrites = rewriteListClass.create(value);
590 },
591
Akron31d89942018-04-06 16:44:51 +0200592 // Mark the underlying data as being changed.
593 // This is important for rerendering the dom.
594 // This will also remove rewrite markers, when the data
595 // change happened by the user
Nils Diewald0e6992a2015-04-14 20:13:52 +0000596 _changed : function () {
597 this.__changed = true;
Nils Diewald4347ee92015-05-04 20:32:48 +0000598
Nils Diewald0e6992a2015-04-14 20:13:52 +0000599 if (this._rewrites === undefined)
Akrone4961b12017-05-10 21:04:46 +0200600 return;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000601
Akronea4e9052017-07-06 16:12:05 +0200602 delete this["_rewrites"];
Nils Diewald0e6992a2015-04-14 20:13:52 +0000603
604 if (this._element === undefined)
Akrone4961b12017-05-10 21:04:46 +0200605 return;
Akronea4e9052017-07-06 16:12:05 +0200606
Nils Diewald0e6992a2015-04-14 20:13:52 +0000607 this._element.classList.remove("rewritten");
608 },
609
Nils Diewald4c221252015-04-21 20:19:25 +0000610
Nils Diewald0e6992a2015-04-14 20:13:52 +0000611 toJson : function () {
612 if (!this.matchop() || !this.key())
Akrone4961b12017-05-10 21:04:46 +0200613 return {};
Nils Diewald0e6992a2015-04-14 20:13:52 +0000614
615 return {
Akrone4961b12017-05-10 21:04:46 +0200616 "@type" : "koral:" + this.ldType(),
617 "key" : this.key(),
618 "match" : "match:" + this.matchop(),
619 "value" : this.value() || '',
620 "type" : "type:" + this.type()
Nils Diewald0e6992a2015-04-14 20:13:52 +0000621 };
622 },
623
hebastaa0282be2018-12-05 16:58:00 +0100624 incomplete : function () {
625 return !(this.matchop() && this.key() && this.value());
626 },
Nils Diewald4c221252015-04-21 20:19:25 +0000627
Nils Diewald0e6992a2015-04-14 20:13:52 +0000628 toQuery : function () {
hebastaa0282be2018-12-05 16:58:00 +0100629 if (this.incomplete())
Akrone4961b12017-05-10 21:04:46 +0200630 return "";
Nils Diewald0e6992a2015-04-14 20:13:52 +0000631
632 // Build doc string based on key
633 var string = this.key() + ' ';
634
635 // Add match operator
636 switch (this.matchop()) {
637 case "ne":
Akrone4961b12017-05-10 21:04:46 +0200638 string += '!=';
639 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000640 case "contains":
Akrone4961b12017-05-10 21:04:46 +0200641 string += '~';
642 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000643 case "excludes":
Akrone4961b12017-05-10 21:04:46 +0200644 string += '!~';
645 break;
Akron6a535d42015-08-26 20:16:58 +0200646 case "containsnot":
Akrone4961b12017-05-10 21:04:46 +0200647 string += '!~';
648 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000649 case "geq":
Akrone4961b12017-05-10 21:04:46 +0200650 string += 'since';
651 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000652 case "leq":
Akrone4961b12017-05-10 21:04:46 +0200653 string += 'until';
654 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000655 default:
Akrone4961b12017-05-10 21:04:46 +0200656 string += (this.type() == 'date') ? 'in' : '=';
657 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000658 };
659
660 string += ' ';
661
662 // Add value
663 switch (this.type()) {
664 case "date":
Akrone4961b12017-05-10 21:04:46 +0200665 return string + this.value();
Nils Diewald0e6992a2015-04-14 20:13:52 +0000666 case "regex":
Akron8778f5d2017-06-30 21:25:55 +0200667 return string + '/' + this.value().escapeRegex() + '/';
Nils Diewald0e6992a2015-04-14 20:13:52 +0000668 case "string":
Akron1bdf5272018-07-24 18:51:30 +0200669 case "text":
Akron0c4cd222019-07-19 16:33:34 +0200670 return string + this.value().quote();
Nils Diewald0e6992a2015-04-14 20:13:52 +0000671 };
672
673 return "";
674 }
675 };
676});