blob: d2a413e5232284e0cc4c4e419b8d53562cf87f04 [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) {
216 json["type"] = "type:" + type;
217 };
218 };
219
220 // Type is still undefined
Akron31d89942018-04-06 16:44:51 +0200221 if (json["type"] === undefined) {
Akronddc98a72018-04-06 17:33:52 +0200222
Akron31d89942018-04-06 16:44:51 +0200223 // Check match type
224 if (!KorAP._validUnspecMatchRE.test(this.matchop())) {
225 KorAP.log(802, errstr802);
226
227 // Rewrite method
228 this.matchop('eq');
229 rewrite = 'modification';
230 };
231
232 // Set string value
233 this.value(json["value"]);
234 }
235
236 // Field is string type
237 else if (json["type"] == "type:string") {
Akrone4961b12017-05-10 21:04:46 +0200238 this.type("string");
Nils Diewald0e6992a2015-04-14 20:13:52 +0000239
Akrone4961b12017-05-10 21:04:46 +0200240 // Check match type
241 if (!KorAP._validStringMatchRE.test(this.matchop())) {
Akron31d89942018-04-06 16:44:51 +0200242 KorAP.log(802, errstr802);
243
244 // Rewrite method
245 this.matchop('eq');
246 rewrite = 'modification';
247 };
248
249 // Set string value
250 this.value(json["value"]);
251 }
252
253 // Field is specified
254 else if (json["type"] == "type:text") {
255 this.type("text");
256
257 // Check match type
258 if (!KorAP._validTextMatchRE.test(this.matchop())) {
259 KorAP.log(802, errstr802);
Akronea4e9052017-07-06 16:12:05 +0200260
261 // Rewrite method
262 this.matchop('eq');
263 rewrite = 'modification';
Akrone4961b12017-05-10 21:04:46 +0200264 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000265
Akrone4961b12017-05-10 21:04:46 +0200266 // Set string value
267 this.value(json["value"]);
268 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000269
Akrone4961b12017-05-10 21:04:46 +0200270 // Key is a date
271 else if (json["type"] === "type:date") {
272 this.type("date");
Nils Diewald0e6992a2015-04-14 20:13:52 +0000273
Akrone4961b12017-05-10 21:04:46 +0200274 if (json["value"] !== undefined &&
275 KorAP._validDateRE.test(json["value"])) {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000276
Akrone4961b12017-05-10 21:04:46 +0200277 if (!KorAP._validDateMatchRE.test(this.matchop())) {
Akron31d89942018-04-06 16:44:51 +0200278 KorAP.log(802, errstr802);
Akronea4e9052017-07-06 16:12:05 +0200279
280 // Rewrite method
281 this.matchop('eq');
282 rewrite = 'modification';
Akrone4961b12017-05-10 21:04:46 +0200283 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000284
Akrone4961b12017-05-10 21:04:46 +0200285 // Set value
286 this.value(json["value"]);
287 }
288 else {
289 KorAP.log(806, "Value is not a valid date string");
290 return;
291 };
292 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000293
Akrone4961b12017-05-10 21:04:46 +0200294 // Key is a regular expression
295 else if (json["type"] === "type:regex") {
296 this.type("regex");
Nils Diewald0e6992a2015-04-14 20:13:52 +0000297
Akrone4961b12017-05-10 21:04:46 +0200298 try {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000299
Akrone4961b12017-05-10 21:04:46 +0200300 // Try to create a regular expression
301 var check = new RegExp(json["value"]);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000302
Akrone65a88a2018-04-05 19:14:20 +0200303 if (!KorAP._validStringMatchRE.test(this.matchop())) {
Akron31d89942018-04-06 16:44:51 +0200304 KorAP.log(802, errstr802);
Akronea4e9052017-07-06 16:12:05 +0200305
306 // Rewrite method
307 this.matchop('eq');
308 rewrite = 'modification';
Akrone4961b12017-05-10 21:04:46 +0200309 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000310
Akrone4961b12017-05-10 21:04:46 +0200311 this.value(json["value"]);
312 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000313
Akrone4961b12017-05-10 21:04:46 +0200314 catch (e) {
315 KorAP.log(807, "Value is not a valid regular expression");
316 return;
317 };
318 this.type("regex");
319 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000320
Akrone4961b12017-05-10 21:04:46 +0200321 else {
322 KorAP.log(804, "Unknown value type");
323 return;
324 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000325 };
326
Akronea4e9052017-07-06 16:12:05 +0200327 // Rewrite coming from the server
Nils Diewald0e6992a2015-04-14 20:13:52 +0000328 if (json["rewrites"] !== undefined) {
Akronea4e9052017-07-06 16:12:05 +0200329 this.rewrite(json["rewrites"]);
330 }
331
332 // Rewrite coming from Kalamar
333 else if (rewrite !== undefined) {
334 this.rewrite(rewrite);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000335 };
Akronea4e9052017-07-06 16:12:05 +0200336
Nils Diewald0e6992a2015-04-14 20:13:52 +0000337 return this;
338 },
339
Nils Diewald4c221252015-04-21 20:19:25 +0000340 /**
341 * Get or set the key
342 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000343 key : function (value) {
344 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200345 this._key = value;
346 this._changed();
347 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000348 };
349 return this._key;
350 },
351
Nils Diewald4c221252015-04-21 20:19:25 +0000352 // Click on the key, show me the menu
353 _changeKey : function (e) {
354 var menu = KorAP._vcKeyMenu;
355
356 // Insert menu
357 this._element.insertBefore(
Akrone4961b12017-05-10 21:04:46 +0200358 menu.element(),
359 this._keyE
Nils Diewald4c221252015-04-21 20:19:25 +0000360 );
361
362 // Release event
363 var that = this;
364 menu.released(function (key, type) {
Akrone4961b12017-05-10 21:04:46 +0200365 var doc = that.key(key).type(type);
Nils Diewald4c221252015-04-21 20:19:25 +0000366
Akrone4961b12017-05-10 21:04:46 +0200367 // This may not be compatible - then switch to default
368 doc.matchop(doc.matchop());
369 doc.value(doc.value());
Nils Diewald4c221252015-04-21 20:19:25 +0000370
Akrone4961b12017-05-10 21:04:46 +0200371 // Update the doc
372 doc.update();
Nils Diewald4c221252015-04-21 20:19:25 +0000373
Akrone4961b12017-05-10 21:04:46 +0200374 // hide!
375 this.hide();
Nils Diewald4c221252015-04-21 20:19:25 +0000376 });
377
378 // TODO: Highlight the old value!
379 menu.show();
380 menu.focus();
381 },
382
383 /**
384 * Get or set the match operator
385 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000386 matchop : function (match) {
Akron31d89942018-04-06 16:44:51 +0200387
Nils Diewald0e6992a2015-04-14 20:13:52 +0000388 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200389 var m = match.replace(/^match:/, '');
Akron31d89942018-04-06 16:44:51 +0200390
Akrone4961b12017-05-10 21:04:46 +0200391 if (
Akron31d89942018-04-06 16:44:51 +0200392 (this._type == undefined) // && KorAP._validUnspecMatchRE.test(m))
Akrone4961b12017-05-10 21:04:46 +0200393 ||
394 (
395 (this._type === 'string' || this._type === 'regex') &&
396 KorAP._validStringMatchRE.test(m)
397 )
398 ||
Akron31d89942018-04-06 16:44:51 +0200399 (this._type === 'text' && KorAP._validTextMatchRE.test(m))
400 ||
Akrone4961b12017-05-10 21:04:46 +0200401 (this._type === 'date' && KorAP._validDateMatchRE.test(m))
402 ) {
403 this._matchop = m;
404 }
405 else {
406 this._matchop = "eq";
407 };
Nils Diewald4c221252015-04-21 20:19:25 +0000408
Akrone4961b12017-05-10 21:04:46 +0200409 this._changed();
Akron31d89942018-04-06 16:44:51 +0200410 return this
Nils Diewald0e6992a2015-04-14 20:13:52 +0000411 };
412 return this._matchop || "eq";
413 },
414
Nils Diewald4c221252015-04-21 20:19:25 +0000415
416 // Click on the match operator, show me the menu
417 _changeMatchop : function (e) {
418 var menu = KorAP._vcMatchopMenu[this.type()];
419
420 if (menu === undefined) {
Akrone4961b12017-05-10 21:04:46 +0200421 KorAP.log(0, "Unable to init menu for " + this.type());
422 return;
Nils Diewald4c221252015-04-21 20:19:25 +0000423 };
424
425 // Insert menu
426 this._element.insertBefore(
Akrone4961b12017-05-10 21:04:46 +0200427 menu.element(),
428 this._matchopE
Nils Diewald4c221252015-04-21 20:19:25 +0000429 );
430
431 // Release event
432 var that = this;
433 menu.released(function (mo) {
Akrone4961b12017-05-10 21:04:46 +0200434 that.matchop(mo).update();
435 this.hide();
Nils Diewald4c221252015-04-21 20:19:25 +0000436 });
437
438 menu.show();
439 menu.focus();
440 },
441
442
443 /**
444 * Get or set the type
445 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000446 type : function (type) {
447 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200448 this._type = type;
449 this._changed();
450 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000451 };
452 return this._type || "string";
453 },
454
Nils Diewald4c221252015-04-21 20:19:25 +0000455
456 /**
457 * Get or set the value
458 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000459 value : function (value) {
460 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200461 if (this._type === 'date' && !KorAP._validDateRE.test(value)) {
462 delete this._value;
463 }
464 else {
465 this._value = value;
466 };
467 this._changed();
468 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000469 };
470 return this._value;
471 },
472
Nils Diewald4c221252015-04-21 20:19:25 +0000473
474 // Click on the match operator, show me the menu
475 _changeValue : function (e) {
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000476 var v = this.value();
477 var that = this;
Akron8db5e3a2018-05-28 19:25:26 +0200478
Nils Diewald7148c6f2015-05-04 15:07:53 +0000479 // Show datepicker
Nils Diewald87507832015-05-01 23:36:41 +0000480 if (this.type() === 'date') {
Akrone4961b12017-05-10 21:04:46 +0200481 var dp = KorAP._vcDatePicker;
Akron516b6f92018-01-03 17:46:59 +0100482 dp.fromString(v);
Nils Diewald87507832015-05-01 23:36:41 +0000483
Akrone4961b12017-05-10 21:04:46 +0200484 // Todo: change this
485 dp.onclick(function (selected) {
Nils Diewald87507832015-05-01 23:36:41 +0000486
Akrone4961b12017-05-10 21:04:46 +0200487 // There are values selected
488 if (selected['year']) {
489 that.value(this.toString());
490 that.update();
491 return;
492 };
Nils Diewald87507832015-05-01 23:36:41 +0000493
Akrone4961b12017-05-10 21:04:46 +0200494 // Remove datepicker
495 that._element.removeChild(
496 dp.element()
497 );
498 });
Nils Diewald87507832015-05-01 23:36:41 +0000499
Akrone4961b12017-05-10 21:04:46 +0200500 // Get element of the date picker
501 var dpElem = dp.show();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000502
Akrone4961b12017-05-10 21:04:46 +0200503 this._element.insertBefore(
504 dpElem,
505 this._valueE
506 );
Nils Diewald7148c6f2015-05-04 15:07:53 +0000507
Akron516b6f92018-01-03 17:46:59 +0100508 dp.input().focus();
Nils Diewald87507832015-05-01 23:36:41 +0000509 }
510 else {
Akrone4961b12017-05-10 21:04:46 +0200511 var regex = this.type() === 'regex' ? true : false;
512 var str = stringValClass.create(this.value(), regex);
513 var strElem = str.element();
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000514
Akrone4961b12017-05-10 21:04:46 +0200515 str.store = function (value, regex) {
516 that.value(value);
517 if (regex === true)
518 that.type('regex');
519 else
520 that.type('string');
521
522 that._element.removeChild(
523 this._element
524 );
525 that.update();
526 };
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000527
Akrone4961b12017-05-10 21:04:46 +0200528 // Insert element
529 this._element.insertBefore(
530 strElem,
531 this._valueE
532 );
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000533
Akrone4961b12017-05-10 21:04:46 +0200534 str.focus();
Nils Diewald87507832015-05-01 23:36:41 +0000535 };
Nils Diewald4c221252015-04-21 20:19:25 +0000536 },
537
538
Nils Diewald0e6992a2015-04-14 20:13:52 +0000539 rewrites : function () {
540 return this._rewrites;
541 },
542
Akronea4e9052017-07-06 16:12:05 +0200543 rewrite : function (value) {
544 if (typeof value === 'string') {
545 value = [{
546 "@type" : "koral:rewrite",
547 "operation" : "operation:" + value,
548 "src" : "Kalamar"
549 }];
550 };
551 this._rewrites = rewriteListClass.create(value);
552 },
553
Akron31d89942018-04-06 16:44:51 +0200554 // Mark the underlying data as being changed.
555 // This is important for rerendering the dom.
556 // This will also remove rewrite markers, when the data
557 // change happened by the user
Nils Diewald0e6992a2015-04-14 20:13:52 +0000558 _changed : function () {
559 this.__changed = true;
Nils Diewald4347ee92015-05-04 20:32:48 +0000560
Nils Diewald0e6992a2015-04-14 20:13:52 +0000561 if (this._rewrites === undefined)
Akrone4961b12017-05-10 21:04:46 +0200562 return;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000563
Akronea4e9052017-07-06 16:12:05 +0200564 delete this["_rewrites"];
Nils Diewald0e6992a2015-04-14 20:13:52 +0000565
566 if (this._element === undefined)
Akrone4961b12017-05-10 21:04:46 +0200567 return;
Akronea4e9052017-07-06 16:12:05 +0200568
Nils Diewald0e6992a2015-04-14 20:13:52 +0000569 this._element.classList.remove("rewritten");
570 },
571
Nils Diewald4c221252015-04-21 20:19:25 +0000572
Nils Diewald0e6992a2015-04-14 20:13:52 +0000573 toJson : function () {
574 if (!this.matchop() || !this.key())
Akrone4961b12017-05-10 21:04:46 +0200575 return {};
Nils Diewald0e6992a2015-04-14 20:13:52 +0000576
577 return {
Akrone4961b12017-05-10 21:04:46 +0200578 "@type" : "koral:" + this.ldType(),
579 "key" : this.key(),
580 "match" : "match:" + this.matchop(),
581 "value" : this.value() || '',
582 "type" : "type:" + this.type()
Nils Diewald0e6992a2015-04-14 20:13:52 +0000583 };
584 },
585
Nils Diewald4c221252015-04-21 20:19:25 +0000586
Nils Diewald0e6992a2015-04-14 20:13:52 +0000587 toQuery : function () {
588 if (!this.matchop() || !this.key())
Akrone4961b12017-05-10 21:04:46 +0200589 return "";
Nils Diewald0e6992a2015-04-14 20:13:52 +0000590
591 // Build doc string based on key
592 var string = this.key() + ' ';
593
594 // Add match operator
595 switch (this.matchop()) {
596 case "ne":
Akrone4961b12017-05-10 21:04:46 +0200597 string += '!=';
598 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000599 case "contains":
Akrone4961b12017-05-10 21:04:46 +0200600 string += '~';
601 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000602 case "excludes":
Akrone4961b12017-05-10 21:04:46 +0200603 string += '!~';
604 break;
Akron6a535d42015-08-26 20:16:58 +0200605 case "containsnot":
Akrone4961b12017-05-10 21:04:46 +0200606 string += '!~';
607 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000608 case "geq":
Akrone4961b12017-05-10 21:04:46 +0200609 string += 'since';
610 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000611 case "leq":
Akrone4961b12017-05-10 21:04:46 +0200612 string += 'until';
613 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000614 default:
Akrone4961b12017-05-10 21:04:46 +0200615 string += (this.type() == 'date') ? 'in' : '=';
616 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000617 };
618
619 string += ' ';
620
621 // Add value
622 switch (this.type()) {
623 case "date":
Akrone4961b12017-05-10 21:04:46 +0200624 return string + this.value();
625 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000626 case "regex":
Akron8778f5d2017-06-30 21:25:55 +0200627 return string + '/' + this.value().escapeRegex() + '/';
Akrone4961b12017-05-10 21:04:46 +0200628 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000629 case "string":
Akrone4961b12017-05-10 21:04:46 +0200630 return string + '"' + this.value().quote() + '"';
631 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000632 };
633
634 return "";
635 }
636 };
637});