blob: 996c3470db56ad48c81d1a8a24a62105bba414db [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
152 return e;
153 },
154
Nils Diewald4c221252015-04-21 20:19:25 +0000155
156 /**
157 * Get the associated element
158 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000159 element : function () {
160 if (this._element !== undefined)
Akrone4961b12017-05-10 21:04:46 +0200161 return this._element;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000162
163 this._element = document.createElement('div');
164 this._element.setAttribute('class', 'doc');
165
166 this.update();
167 return this._element;
168 },
169
Nils Diewald4c221252015-04-21 20:19:25 +0000170 /**
171 * Wrap a new operation around the doc element
172 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000173 wrap : function (op) {
174 var parent = this.parent();
Nils Diewald7c8ced22015-04-15 19:21:00 +0000175 var group = require('vc/docgroup').create(parent);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000176 group.operation(op);
177 group.append(this);
178 group.append();
179 return parent.replaceOperand(this, group).update();
180 },
181
Akron587e4d92018-08-31 12:44:26 +0200182 replaceWith : function (op) {
183 var p = this.parent();
184
185 if (p.ldType() === 'docGroup') {
186 p.replaceOperand(this,op);
187 }
188 else if (p.ldType() == null) {
189 p.root(op);
190 };
191 p.update();
192
193 this.destroy();
194 },
195
Nils Diewald4c221252015-04-21 20:19:25 +0000196 /**
197 * Deserialize from json
198 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000199 fromJson : function (json) {
200 if (json === undefined)
Akrone4961b12017-05-10 21:04:46 +0200201 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000202
203 if (json["@type"] === undefined) {
Akrone4961b12017-05-10 21:04:46 +0200204 KorAP.log(701, "JSON-LD group has no @type attribute");
205 return;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000206 };
207
208 if (json["value"] === undefined ||
Akrone4961b12017-05-10 21:04:46 +0200209 typeof json["value"] != 'string') {
210 KorAP.log(805, "Value is invalid");
211 return;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000212 };
213
Akronea4e9052017-07-06 16:12:05 +0200214 var rewrite;
215
Nils Diewald0e6992a2015-04-14 20:13:52 +0000216 // There is a defined key
217 if (json["key"] !== undefined &&
Akrone4961b12017-05-10 21:04:46 +0200218 typeof json["key"] === 'string') {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000219
Akrone4961b12017-05-10 21:04:46 +0200220 // Set key
221 this.key(json["key"]);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000222
Akrone4961b12017-05-10 21:04:46 +0200223 // Set match operation
224 if (json["match"] !== undefined) {
225 if (typeof json["match"] === 'string') {
Akron31d89942018-04-06 16:44:51 +0200226
Akrone4961b12017-05-10 21:04:46 +0200227 this.matchop(json["match"]);
228 }
229 else {
Akron31d89942018-04-06 16:44:51 +0200230 KorAP.log(802, errstr802);
Akrone4961b12017-05-10 21:04:46 +0200231 return;
232 };
233 };
Akron31d89942018-04-06 16:44:51 +0200234
Akronddc98a72018-04-06 17:33:52 +0200235 // Type is unspecified - but may be known by the menu
236 if (json["type"] === undefined && KorAP._vcKeyMenu) {
237
238 // Check the VC list if the field is known
239 var type = KorAP._vcKeyMenu.typeOf(this.key());
240 if (type != undefined) {
241 json["type"] = "type:" + type;
242 };
243 };
244
245 // Type is still undefined
Akron31d89942018-04-06 16:44:51 +0200246 if (json["type"] === undefined) {
Akronddc98a72018-04-06 17:33:52 +0200247
Akron31d89942018-04-06 16:44:51 +0200248 // Check match type
249 if (!KorAP._validUnspecMatchRE.test(this.matchop())) {
250 KorAP.log(802, errstr802);
251
252 // Rewrite method
253 this.matchop('eq');
254 rewrite = 'modification';
255 };
256
257 // Set string value
258 this.value(json["value"]);
259 }
260
261 // Field is string type
262 else if (json["type"] == "type:string") {
Akrone4961b12017-05-10 21:04:46 +0200263 this.type("string");
Nils Diewald0e6992a2015-04-14 20:13:52 +0000264
Akrone4961b12017-05-10 21:04:46 +0200265 // Check match type
266 if (!KorAP._validStringMatchRE.test(this.matchop())) {
Akron31d89942018-04-06 16:44:51 +0200267 KorAP.log(802, errstr802);
268
269 // Rewrite method
270 this.matchop('eq');
271 rewrite = 'modification';
272 };
273
274 // Set string value
275 this.value(json["value"]);
276 }
277
278 // Field is specified
279 else if (json["type"] == "type:text") {
280 this.type("text");
281
282 // Check match type
283 if (!KorAP._validTextMatchRE.test(this.matchop())) {
284 KorAP.log(802, errstr802);
Akronea4e9052017-07-06 16:12:05 +0200285
286 // Rewrite method
287 this.matchop('eq');
288 rewrite = 'modification';
Akrone4961b12017-05-10 21:04:46 +0200289 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000290
Akrone4961b12017-05-10 21:04:46 +0200291 // Set string value
292 this.value(json["value"]);
293 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000294
Akrone4961b12017-05-10 21:04:46 +0200295 // Key is a date
296 else if (json["type"] === "type:date") {
297 this.type("date");
Nils Diewald0e6992a2015-04-14 20:13:52 +0000298
Akrone4961b12017-05-10 21:04:46 +0200299 if (json["value"] !== undefined &&
300 KorAP._validDateRE.test(json["value"])) {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000301
Akrone4961b12017-05-10 21:04:46 +0200302 if (!KorAP._validDateMatchRE.test(this.matchop())) {
Akron31d89942018-04-06 16:44:51 +0200303 KorAP.log(802, errstr802);
Akronea4e9052017-07-06 16:12:05 +0200304
305 // Rewrite method
306 this.matchop('eq');
307 rewrite = 'modification';
Akrone4961b12017-05-10 21:04:46 +0200308 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000309
Akrone4961b12017-05-10 21:04:46 +0200310 // Set value
311 this.value(json["value"]);
312 }
313 else {
314 KorAP.log(806, "Value is not a valid date string");
315 return;
316 };
317 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000318
Akrone4961b12017-05-10 21:04:46 +0200319 // Key is a regular expression
320 else if (json["type"] === "type:regex") {
321 this.type("regex");
Nils Diewald0e6992a2015-04-14 20:13:52 +0000322
Akrone4961b12017-05-10 21:04:46 +0200323 try {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000324
Akrone4961b12017-05-10 21:04:46 +0200325 // Try to create a regular expression
326 var check = new RegExp(json["value"]);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000327
Akrone65a88a2018-04-05 19:14:20 +0200328 if (!KorAP._validStringMatchRE.test(this.matchop())) {
Akron31d89942018-04-06 16:44:51 +0200329 KorAP.log(802, errstr802);
Akronea4e9052017-07-06 16:12:05 +0200330
331 // Rewrite method
332 this.matchop('eq');
333 rewrite = 'modification';
Akrone4961b12017-05-10 21:04:46 +0200334 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000335
Akrone4961b12017-05-10 21:04:46 +0200336 this.value(json["value"]);
337 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000338
Akrone4961b12017-05-10 21:04:46 +0200339 catch (e) {
340 KorAP.log(807, "Value is not a valid regular expression");
341 return;
342 };
343 this.type("regex");
344 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000345
Akrone4961b12017-05-10 21:04:46 +0200346 else {
Akron1bdf5272018-07-24 18:51:30 +0200347 KorAP.log(804, errstr804 + ": " + this.type());
348 throw new Error(errstr804 + ": " + this.type());
Akrone4961b12017-05-10 21:04:46 +0200349 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000350 };
351
Akronea4e9052017-07-06 16:12:05 +0200352 // Rewrite coming from the server
Nils Diewald0e6992a2015-04-14 20:13:52 +0000353 if (json["rewrites"] !== undefined) {
Akronea4e9052017-07-06 16:12:05 +0200354 this.rewrite(json["rewrites"]);
355 }
356
357 // Rewrite coming from Kalamar
358 else if (rewrite !== undefined) {
359 this.rewrite(rewrite);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000360 };
Akronea4e9052017-07-06 16:12:05 +0200361
Nils Diewald0e6992a2015-04-14 20:13:52 +0000362 return this;
363 },
364
Nils Diewald4c221252015-04-21 20:19:25 +0000365 /**
366 * Get or set the key
367 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000368 key : function (value) {
369 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200370 this._key = value;
371 this._changed();
372 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000373 };
374 return this._key;
375 },
376
Nils Diewald4c221252015-04-21 20:19:25 +0000377 // Click on the key, show me the menu
378 _changeKey : function (e) {
379 var menu = KorAP._vcKeyMenu;
380
381 // Insert menu
382 this._element.insertBefore(
Akrone4961b12017-05-10 21:04:46 +0200383 menu.element(),
384 this._keyE
Nils Diewald4c221252015-04-21 20:19:25 +0000385 );
386
387 // Release event
388 var that = this;
389 menu.released(function (key, type) {
Nils Diewald4c221252015-04-21 20:19:25 +0000390
Akron587e4d92018-08-31 12:44:26 +0200391 if (type === 'ref') {
392 // KorAP._delete.bind(that);
393 var ref = docGroupRefClass.create(that.parent());
394 that.replaceWith(ref);
395 }
396 else {
397 var doc = that.key(key).type(type);
Nils Diewald4c221252015-04-21 20:19:25 +0000398
Akron587e4d92018-08-31 12:44:26 +0200399 // This may not be compatible - then switch to default
400 doc.matchop(doc.matchop());
401 doc.value(doc.value());
402
403 // Update the doc
404 doc.update();
405 };
Nils Diewald4c221252015-04-21 20:19:25 +0000406
Akrone4961b12017-05-10 21:04:46 +0200407 // hide!
408 this.hide();
Nils Diewald4c221252015-04-21 20:19:25 +0000409 });
410
411 // TODO: Highlight the old value!
412 menu.show();
413 menu.focus();
414 },
415
416 /**
417 * Get or set the match operator
418 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000419 matchop : function (match) {
Akron31d89942018-04-06 16:44:51 +0200420
Nils Diewald0e6992a2015-04-14 20:13:52 +0000421 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200422 var m = match.replace(/^match:/, '');
Akron31d89942018-04-06 16:44:51 +0200423
Akrone4961b12017-05-10 21:04:46 +0200424 if (
Akron31d89942018-04-06 16:44:51 +0200425 (this._type == undefined) // && KorAP._validUnspecMatchRE.test(m))
Akrone4961b12017-05-10 21:04:46 +0200426 ||
427 (
428 (this._type === 'string' || this._type === 'regex') &&
429 KorAP._validStringMatchRE.test(m)
430 )
431 ||
Akron31d89942018-04-06 16:44:51 +0200432 (this._type === 'text' && KorAP._validTextMatchRE.test(m))
433 ||
Akrone4961b12017-05-10 21:04:46 +0200434 (this._type === 'date' && KorAP._validDateMatchRE.test(m))
435 ) {
436 this._matchop = m;
437 }
438 else {
439 this._matchop = "eq";
440 };
Nils Diewald4c221252015-04-21 20:19:25 +0000441
Akrone4961b12017-05-10 21:04:46 +0200442 this._changed();
Akron31d89942018-04-06 16:44:51 +0200443 return this
Nils Diewald0e6992a2015-04-14 20:13:52 +0000444 };
445 return this._matchop || "eq";
446 },
447
Nils Diewald4c221252015-04-21 20:19:25 +0000448
449 // Click on the match operator, show me the menu
450 _changeMatchop : function (e) {
451 var menu = KorAP._vcMatchopMenu[this.type()];
452
453 if (menu === undefined) {
Akrone4961b12017-05-10 21:04:46 +0200454 KorAP.log(0, "Unable to init menu for " + this.type());
455 return;
Nils Diewald4c221252015-04-21 20:19:25 +0000456 };
457
458 // Insert menu
459 this._element.insertBefore(
Akrone4961b12017-05-10 21:04:46 +0200460 menu.element(),
461 this._matchopE
Nils Diewald4c221252015-04-21 20:19:25 +0000462 );
463
464 // Release event
465 var that = this;
466 menu.released(function (mo) {
Akrone4961b12017-05-10 21:04:46 +0200467 that.matchop(mo).update();
468 this.hide();
Nils Diewald4c221252015-04-21 20:19:25 +0000469 });
470
471 menu.show();
472 menu.focus();
473 },
474
475
476 /**
477 * Get or set the type
478 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000479 type : function (type) {
480 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200481 this._type = type;
482 this._changed();
483 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000484 };
485 return this._type || "string";
486 },
487
Nils Diewald4c221252015-04-21 20:19:25 +0000488
489 /**
490 * Get or set the value
491 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000492 value : function (value) {
493 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200494 if (this._type === 'date' && !KorAP._validDateRE.test(value)) {
495 delete this._value;
496 }
497 else {
498 this._value = value;
499 };
500 this._changed();
501 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000502 };
503 return this._value;
504 },
505
Nils Diewald4c221252015-04-21 20:19:25 +0000506
507 // Click on the match operator, show me the menu
508 _changeValue : function (e) {
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000509 var v = this.value();
510 var that = this;
Akron8db5e3a2018-05-28 19:25:26 +0200511
Nils Diewald7148c6f2015-05-04 15:07:53 +0000512 // Show datepicker
Nils Diewald87507832015-05-01 23:36:41 +0000513 if (this.type() === 'date') {
Akrone4961b12017-05-10 21:04:46 +0200514 var dp = KorAP._vcDatePicker;
Akron516b6f92018-01-03 17:46:59 +0100515 dp.fromString(v);
Nils Diewald87507832015-05-01 23:36:41 +0000516
Akrone4961b12017-05-10 21:04:46 +0200517 // Todo: change this
518 dp.onclick(function (selected) {
Nils Diewald87507832015-05-01 23:36:41 +0000519
Akrone4961b12017-05-10 21:04:46 +0200520 // There are values selected
521 if (selected['year']) {
522 that.value(this.toString());
523 that.update();
524 return;
525 };
Nils Diewald87507832015-05-01 23:36:41 +0000526
Akrone4961b12017-05-10 21:04:46 +0200527 // Remove datepicker
528 that._element.removeChild(
529 dp.element()
530 );
531 });
Nils Diewald87507832015-05-01 23:36:41 +0000532
Akrone4961b12017-05-10 21:04:46 +0200533 // Get element of the date picker
534 var dpElem = dp.show();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000535
Akrone4961b12017-05-10 21:04:46 +0200536 this._element.insertBefore(
537 dpElem,
538 this._valueE
539 );
Nils Diewald7148c6f2015-05-04 15:07:53 +0000540
Akron516b6f92018-01-03 17:46:59 +0100541 dp.input().focus();
Nils Diewald87507832015-05-01 23:36:41 +0000542 }
543 else {
Akrone4961b12017-05-10 21:04:46 +0200544 var regex = this.type() === 'regex' ? true : false;
545 var str = stringValClass.create(this.value(), regex);
546 var strElem = str.element();
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000547
Akrone4961b12017-05-10 21:04:46 +0200548 str.store = function (value, regex) {
549 that.value(value);
550 if (regex === true)
551 that.type('regex');
552 else
553 that.type('string');
554
555 that._element.removeChild(
556 this._element
557 );
558 that.update();
559 };
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000560
Akrone4961b12017-05-10 21:04:46 +0200561 // Insert element
562 this._element.insertBefore(
563 strElem,
564 this._valueE
565 );
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000566
Akrone4961b12017-05-10 21:04:46 +0200567 str.focus();
Nils Diewald87507832015-05-01 23:36:41 +0000568 };
Nils Diewald4c221252015-04-21 20:19:25 +0000569 },
570
571
Nils Diewald0e6992a2015-04-14 20:13:52 +0000572 rewrites : function () {
573 return this._rewrites;
574 },
575
Akronea4e9052017-07-06 16:12:05 +0200576 rewrite : function (value) {
577 if (typeof value === 'string') {
578 value = [{
579 "@type" : "koral:rewrite",
580 "operation" : "operation:" + value,
581 "src" : "Kalamar"
582 }];
583 };
584 this._rewrites = rewriteListClass.create(value);
585 },
586
Akron31d89942018-04-06 16:44:51 +0200587 // Mark the underlying data as being changed.
588 // This is important for rerendering the dom.
589 // This will also remove rewrite markers, when the data
590 // change happened by the user
Nils Diewald0e6992a2015-04-14 20:13:52 +0000591 _changed : function () {
592 this.__changed = true;
Nils Diewald4347ee92015-05-04 20:32:48 +0000593
Nils Diewald0e6992a2015-04-14 20:13:52 +0000594 if (this._rewrites === undefined)
Akrone4961b12017-05-10 21:04:46 +0200595 return;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000596
Akronea4e9052017-07-06 16:12:05 +0200597 delete this["_rewrites"];
Nils Diewald0e6992a2015-04-14 20:13:52 +0000598
599 if (this._element === undefined)
Akrone4961b12017-05-10 21:04:46 +0200600 return;
Akronea4e9052017-07-06 16:12:05 +0200601
Nils Diewald0e6992a2015-04-14 20:13:52 +0000602 this._element.classList.remove("rewritten");
603 },
604
Nils Diewald4c221252015-04-21 20:19:25 +0000605
Nils Diewald0e6992a2015-04-14 20:13:52 +0000606 toJson : function () {
607 if (!this.matchop() || !this.key())
Akrone4961b12017-05-10 21:04:46 +0200608 return {};
Nils Diewald0e6992a2015-04-14 20:13:52 +0000609
610 return {
Akrone4961b12017-05-10 21:04:46 +0200611 "@type" : "koral:" + this.ldType(),
612 "key" : this.key(),
613 "match" : "match:" + this.matchop(),
614 "value" : this.value() || '',
615 "type" : "type:" + this.type()
Nils Diewald0e6992a2015-04-14 20:13:52 +0000616 };
617 },
618
Nils Diewald4c221252015-04-21 20:19:25 +0000619
Nils Diewald0e6992a2015-04-14 20:13:52 +0000620 toQuery : function () {
621 if (!this.matchop() || !this.key())
Akrone4961b12017-05-10 21:04:46 +0200622 return "";
Nils Diewald0e6992a2015-04-14 20:13:52 +0000623
624 // Build doc string based on key
625 var string = this.key() + ' ';
626
627 // Add match operator
628 switch (this.matchop()) {
629 case "ne":
Akrone4961b12017-05-10 21:04:46 +0200630 string += '!=';
631 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000632 case "contains":
Akrone4961b12017-05-10 21:04:46 +0200633 string += '~';
634 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000635 case "excludes":
Akrone4961b12017-05-10 21:04:46 +0200636 string += '!~';
637 break;
Akron6a535d42015-08-26 20:16:58 +0200638 case "containsnot":
Akrone4961b12017-05-10 21:04:46 +0200639 string += '!~';
640 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000641 case "geq":
Akrone4961b12017-05-10 21:04:46 +0200642 string += 'since';
643 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000644 case "leq":
Akrone4961b12017-05-10 21:04:46 +0200645 string += 'until';
646 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000647 default:
Akrone4961b12017-05-10 21:04:46 +0200648 string += (this.type() == 'date') ? 'in' : '=';
649 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000650 };
651
652 string += ' ';
653
654 // Add value
655 switch (this.type()) {
656 case "date":
Akrone4961b12017-05-10 21:04:46 +0200657 return string + this.value();
Nils Diewald0e6992a2015-04-14 20:13:52 +0000658 case "regex":
Akron8778f5d2017-06-30 21:25:55 +0200659 return string + '/' + this.value().escapeRegex() + '/';
Nils Diewald0e6992a2015-04-14 20:13:52 +0000660 case "string":
Akron1bdf5272018-07-24 18:51:30 +0200661 case "text":
Akrone4961b12017-05-10 21:04:46 +0200662 return string + '"' + this.value().quote() + '"';
Nils Diewald0e6992a2015-04-14 20:13:52 +0000663 };
664
665 return "";
666 }
667 };
668});