blob: 821aa64bb910063bf3866aace6d7a48460418ecb [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
Akron1bdf5272018-07-24 18:51:30 +020012 /*
13 * TODO:
14 * Improve error handling by using window.onerror() to
15 * capture thrown errors and log them.
16 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000017
Akron31d89942018-04-06 16:44:51 +020018 const errstr802 = "Match type is not supported by value type";
Akron1bdf5272018-07-24 18:51:30 +020019 const errstr804 = "Unknown value type";
Akron0b489ad2018-02-02 16:49:32 +010020 const loc = KorAP.Locale;
Nils Diewald4c221252015-04-21 20:19:25 +000021 loc.EMPTY = loc.EMPTY || '⋯';
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000022
Nils Diewald0e6992a2015-04-14 20:13:52 +000023 return {
Nils Diewald4c221252015-04-21 20:19:25 +000024
25 // The JSON-LD type
Nils Diewald0e6992a2015-04-14 20:13:52 +000026 _ldType : "doc",
Nils Diewald4c221252015-04-21 20:19:25 +000027
28 // The object ... maybe not important
Nils Diewald0e6992a2015-04-14 20:13:52 +000029 _obj : function () { return '???'; /*KorAP.Doc*/ },
30
Nils Diewald4c221252015-04-21 20:19:25 +000031 /**
32 * Create a new document criterion
33 * by passing the parent object and a json construct.
34 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000035 create : function (parent, json) {
Nils Diewald4c221252015-04-21 20:19:25 +000036
37 // Create the object, inheriting from Json-LD class
Nils Diewald0e6992a2015-04-14 20:13:52 +000038 var obj = Object(jsonldClass).
Akrone4961b12017-05-10 21:04:46 +020039 create().
40 upgradeTo(this).
41 fromJson(json);
Nils Diewald4c221252015-04-21 20:19:25 +000042
Akron55a343b2018-04-06 19:57:36 +020043 if (obj === undefined) {
44 console.log(json);
Akrond2474aa2018-08-28 12:06:27 +020045 return;
Akron55a343b2018-04-06 19:57:36 +020046 };
47
Nils Diewald4c221252015-04-21 20:19:25 +000048 // Bind the parent
Nils Diewald0e6992a2015-04-14 20:13:52 +000049 if (parent !== undefined)
Akrone4961b12017-05-10 21:04:46 +020050 obj._parent = parent;
Nils Diewald0e6992a2015-04-14 20:13:52 +000051
52 obj.__changed = true;
53 return obj;
54 },
55
Nils Diewald4c221252015-04-21 20:19:25 +000056 /**
57 * Update the elements content.
58 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000059 update : function () {
60 if (this._element === undefined)
Akrone4961b12017-05-10 21:04:46 +020061 return this.element();
Nils Diewald0e6992a2015-04-14 20:13:52 +000062
63 // Get element
64 var e = this._element;
65
Akronb19803c2018-08-16 16:39:42 +020066 // Check if there is a change in the underlying data
67 if (!this.__changed)
68 return e;
69
Nils Diewald0e6992a2015-04-14 20:13:52 +000070 // Set ref - TODO: Cleanup!
71 e.refTo = this;
72
Nils Diewald0e6992a2015-04-14 20:13:52 +000073
Akronb19803c2018-08-16 16:39:42 +020074 // Was rewritten
75 if (this.rewrites() !== undefined) {
76 e.classList.add("rewritten");
Nils Diewald0e6992a2015-04-14 20:13:52 +000077 };
78
Akronb19803c2018-08-16 16:39:42 +020079 // Added key
80 this._keyE = document.createElement('span');
81 this._keyE.setAttribute('class', 'key');
82
83 // Change key
84 this._keyE.addEventListener('click', this._changeKey.bind(this));
85
86 if (this.key()) {
87 var k = this.key();
88 if (loc['VC_' + k] !== undefined)
89 k = loc['VC_' + k];
90 this._keyE.addT(k);
91 };
92
93 // Added match operator
94 this._matchopE = document.createElement('span');
95 this._matchopE.setAttribute('data-type', this.type());
96 this._matchopE.setAttribute('class', 'match');
97 this._matchopE.addT(this.matchop());
98
99 // Change matchop
100 this._matchopE.addEventListener(
101 'click',
102 this._changeMatchop.bind(this)
103 );
104
105 // Added value operator
106 this._valueE = document.createElement('span');
107 this._valueE.setAttribute('data-type', this.type());
108 this._valueE.setAttribute('class', 'value');
109
110 if (this.value()) {
111 this._valueE.addT(this.value());
112 }
113 else {
114 this._valueE.addT(loc.EMPTY);
Akronebc96662018-08-29 17:36:20 +0200115 this._valueE.classList.add('unspecified');
Akronb19803c2018-08-16 16:39:42 +0200116 };
117
118 // Change value
119 this._valueE.addEventListener(
120 'click',
121 this._changeValue.bind(this)
122 );
123
124 // Remove all element children
125 _removeChildren(e);
126
127 // Add spans
128 e.appendChild(this._keyE);
129 e.appendChild(this._matchopE);
130 e.appendChild(this._valueE);
131
132 this.__changed = false;
133
Nils Diewald0e6992a2015-04-14 20:13:52 +0000134 if (this._rewrites !== undefined) {
Akrone4961b12017-05-10 21:04:46 +0200135 e.appendChild(this._rewrites.element());
Nils Diewald0e6992a2015-04-14 20:13:52 +0000136 };
137
138 if (this._parent !== undefined) {
Akronb19803c2018-08-16 16:39:42 +0200139
Akrone4961b12017-05-10 21:04:46 +0200140 // Set operators
141 var op = this.operators(
142 true,
143 true,
144 true
145 );
Nils Diewald0e6992a2015-04-14 20:13:52 +0000146
Akrone4961b12017-05-10 21:04:46 +0200147 // Append new operators
148 e.appendChild(op.element());
Nils Diewald0e6992a2015-04-14 20:13:52 +0000149 };
150
151 return e;
152 },
153
Nils Diewald4c221252015-04-21 20:19:25 +0000154
155 /**
156 * Get the associated element
157 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000158 element : function () {
159 if (this._element !== undefined)
Akrone4961b12017-05-10 21:04:46 +0200160 return this._element;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000161
162 this._element = document.createElement('div');
163 this._element.setAttribute('class', 'doc');
164
165 this.update();
166 return this._element;
167 },
168
Nils Diewald4c221252015-04-21 20:19:25 +0000169 /**
170 * Wrap a new operation around the doc element
171 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000172 wrap : function (op) {
173 var parent = this.parent();
Nils Diewald7c8ced22015-04-15 19:21:00 +0000174 var group = require('vc/docgroup').create(parent);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000175 group.operation(op);
176 group.append(this);
177 group.append();
178 return parent.replaceOperand(this, group).update();
179 },
180
Nils Diewald4c221252015-04-21 20:19:25 +0000181 /**
182 * Deserialize from json
183 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000184 fromJson : function (json) {
185 if (json === undefined)
Akrone4961b12017-05-10 21:04:46 +0200186 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000187
188 if (json["@type"] === undefined) {
Akrone4961b12017-05-10 21:04:46 +0200189 KorAP.log(701, "JSON-LD group has no @type attribute");
190 return;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000191 };
192
193 if (json["value"] === undefined ||
Akrone4961b12017-05-10 21:04:46 +0200194 typeof json["value"] != 'string') {
195 KorAP.log(805, "Value is invalid");
196 return;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000197 };
198
Akronea4e9052017-07-06 16:12:05 +0200199 var rewrite;
200
Nils Diewald0e6992a2015-04-14 20:13:52 +0000201 // There is a defined key
202 if (json["key"] !== undefined &&
Akrone4961b12017-05-10 21:04:46 +0200203 typeof json["key"] === 'string') {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000204
Akrone4961b12017-05-10 21:04:46 +0200205 // Set key
206 this.key(json["key"]);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000207
Akrone4961b12017-05-10 21:04:46 +0200208 // Set match operation
209 if (json["match"] !== undefined) {
210 if (typeof json["match"] === 'string') {
Akron31d89942018-04-06 16:44:51 +0200211
Akrone4961b12017-05-10 21:04:46 +0200212 this.matchop(json["match"]);
213 }
214 else {
Akron31d89942018-04-06 16:44:51 +0200215 KorAP.log(802, errstr802);
Akrone4961b12017-05-10 21:04:46 +0200216 return;
217 };
218 };
Akron31d89942018-04-06 16:44:51 +0200219
Akronddc98a72018-04-06 17:33:52 +0200220 // Type is unspecified - but may be known by the menu
221 if (json["type"] === undefined && KorAP._vcKeyMenu) {
222
223 // Check the VC list if the field is known
224 var type = KorAP._vcKeyMenu.typeOf(this.key());
225 if (type != undefined) {
226 json["type"] = "type:" + type;
227 };
228 };
229
230 // Type is still undefined
Akron31d89942018-04-06 16:44:51 +0200231 if (json["type"] === undefined) {
Akronddc98a72018-04-06 17:33:52 +0200232
Akron31d89942018-04-06 16:44:51 +0200233 // Check match type
234 if (!KorAP._validUnspecMatchRE.test(this.matchop())) {
235 KorAP.log(802, errstr802);
236
237 // Rewrite method
238 this.matchop('eq');
239 rewrite = 'modification';
240 };
241
242 // Set string value
243 this.value(json["value"]);
244 }
245
246 // Field is string type
247 else if (json["type"] == "type:string") {
Akrone4961b12017-05-10 21:04:46 +0200248 this.type("string");
Nils Diewald0e6992a2015-04-14 20:13:52 +0000249
Akrone4961b12017-05-10 21:04:46 +0200250 // Check match type
251 if (!KorAP._validStringMatchRE.test(this.matchop())) {
Akron31d89942018-04-06 16:44:51 +0200252 KorAP.log(802, errstr802);
253
254 // Rewrite method
255 this.matchop('eq');
256 rewrite = 'modification';
257 };
258
259 // Set string value
260 this.value(json["value"]);
261 }
262
263 // Field is specified
264 else if (json["type"] == "type:text") {
265 this.type("text");
266
267 // Check match type
268 if (!KorAP._validTextMatchRE.test(this.matchop())) {
269 KorAP.log(802, errstr802);
Akronea4e9052017-07-06 16:12:05 +0200270
271 // Rewrite method
272 this.matchop('eq');
273 rewrite = 'modification';
Akrone4961b12017-05-10 21:04:46 +0200274 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000275
Akrone4961b12017-05-10 21:04:46 +0200276 // Set string value
277 this.value(json["value"]);
278 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000279
Akrone4961b12017-05-10 21:04:46 +0200280 // Key is a date
281 else if (json["type"] === "type:date") {
282 this.type("date");
Nils Diewald0e6992a2015-04-14 20:13:52 +0000283
Akrone4961b12017-05-10 21:04:46 +0200284 if (json["value"] !== undefined &&
285 KorAP._validDateRE.test(json["value"])) {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000286
Akrone4961b12017-05-10 21:04:46 +0200287 if (!KorAP._validDateMatchRE.test(this.matchop())) {
Akron31d89942018-04-06 16:44:51 +0200288 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 value
296 this.value(json["value"]);
297 }
298 else {
299 KorAP.log(806, "Value is not a valid date string");
300 return;
301 };
302 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000303
Akrone4961b12017-05-10 21:04:46 +0200304 // Key is a regular expression
305 else if (json["type"] === "type:regex") {
306 this.type("regex");
Nils Diewald0e6992a2015-04-14 20:13:52 +0000307
Akrone4961b12017-05-10 21:04:46 +0200308 try {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000309
Akrone4961b12017-05-10 21:04:46 +0200310 // Try to create a regular expression
311 var check = new RegExp(json["value"]);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000312
Akrone65a88a2018-04-05 19:14:20 +0200313 if (!KorAP._validStringMatchRE.test(this.matchop())) {
Akron31d89942018-04-06 16:44:51 +0200314 KorAP.log(802, errstr802);
Akronea4e9052017-07-06 16:12:05 +0200315
316 // Rewrite method
317 this.matchop('eq');
318 rewrite = 'modification';
Akrone4961b12017-05-10 21:04:46 +0200319 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000320
Akrone4961b12017-05-10 21:04:46 +0200321 this.value(json["value"]);
322 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000323
Akrone4961b12017-05-10 21:04:46 +0200324 catch (e) {
325 KorAP.log(807, "Value is not a valid regular expression");
326 return;
327 };
328 this.type("regex");
329 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000330
Akrone4961b12017-05-10 21:04:46 +0200331 else {
Akron1bdf5272018-07-24 18:51:30 +0200332 KorAP.log(804, errstr804 + ": " + this.type());
333 throw new Error(errstr804 + ": " + this.type());
Akrone4961b12017-05-10 21:04:46 +0200334 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000335 };
336
Akronea4e9052017-07-06 16:12:05 +0200337 // Rewrite coming from the server
Nils Diewald0e6992a2015-04-14 20:13:52 +0000338 if (json["rewrites"] !== undefined) {
Akronea4e9052017-07-06 16:12:05 +0200339 this.rewrite(json["rewrites"]);
340 }
341
342 // Rewrite coming from Kalamar
343 else if (rewrite !== undefined) {
344 this.rewrite(rewrite);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000345 };
Akronea4e9052017-07-06 16:12:05 +0200346
Nils Diewald0e6992a2015-04-14 20:13:52 +0000347 return this;
348 },
349
Nils Diewald4c221252015-04-21 20:19:25 +0000350 /**
351 * Get or set the key
352 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000353 key : function (value) {
354 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200355 this._key = value;
356 this._changed();
357 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000358 };
359 return this._key;
360 },
361
Nils Diewald4c221252015-04-21 20:19:25 +0000362 // Click on the key, show me the menu
363 _changeKey : function (e) {
364 var menu = KorAP._vcKeyMenu;
365
366 // Insert menu
367 this._element.insertBefore(
Akrone4961b12017-05-10 21:04:46 +0200368 menu.element(),
369 this._keyE
Nils Diewald4c221252015-04-21 20:19:25 +0000370 );
371
372 // Release event
373 var that = this;
374 menu.released(function (key, type) {
Akrone4961b12017-05-10 21:04:46 +0200375 var doc = that.key(key).type(type);
Nils Diewald4c221252015-04-21 20:19:25 +0000376
Akrone4961b12017-05-10 21:04:46 +0200377 // This may not be compatible - then switch to default
378 doc.matchop(doc.matchop());
379 doc.value(doc.value());
Nils Diewald4c221252015-04-21 20:19:25 +0000380
Akrone4961b12017-05-10 21:04:46 +0200381 // Update the doc
382 doc.update();
Nils Diewald4c221252015-04-21 20:19:25 +0000383
Akrone4961b12017-05-10 21:04:46 +0200384 // hide!
385 this.hide();
Nils Diewald4c221252015-04-21 20:19:25 +0000386 });
387
388 // TODO: Highlight the old value!
389 menu.show();
390 menu.focus();
391 },
392
393 /**
394 * Get or set the match operator
395 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000396 matchop : function (match) {
Akron31d89942018-04-06 16:44:51 +0200397
Nils Diewald0e6992a2015-04-14 20:13:52 +0000398 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200399 var m = match.replace(/^match:/, '');
Akron31d89942018-04-06 16:44:51 +0200400
Akrone4961b12017-05-10 21:04:46 +0200401 if (
Akron31d89942018-04-06 16:44:51 +0200402 (this._type == undefined) // && KorAP._validUnspecMatchRE.test(m))
Akrone4961b12017-05-10 21:04:46 +0200403 ||
404 (
405 (this._type === 'string' || this._type === 'regex') &&
406 KorAP._validStringMatchRE.test(m)
407 )
408 ||
Akron31d89942018-04-06 16:44:51 +0200409 (this._type === 'text' && KorAP._validTextMatchRE.test(m))
410 ||
Akrone4961b12017-05-10 21:04:46 +0200411 (this._type === 'date' && KorAP._validDateMatchRE.test(m))
412 ) {
413 this._matchop = m;
414 }
415 else {
416 this._matchop = "eq";
417 };
Nils Diewald4c221252015-04-21 20:19:25 +0000418
Akrone4961b12017-05-10 21:04:46 +0200419 this._changed();
Akron31d89942018-04-06 16:44:51 +0200420 return this
Nils Diewald0e6992a2015-04-14 20:13:52 +0000421 };
422 return this._matchop || "eq";
423 },
424
Nils Diewald4c221252015-04-21 20:19:25 +0000425
426 // Click on the match operator, show me the menu
427 _changeMatchop : function (e) {
428 var menu = KorAP._vcMatchopMenu[this.type()];
429
430 if (menu === undefined) {
Akrone4961b12017-05-10 21:04:46 +0200431 KorAP.log(0, "Unable to init menu for " + this.type());
432 return;
Nils Diewald4c221252015-04-21 20:19:25 +0000433 };
434
435 // Insert menu
436 this._element.insertBefore(
Akrone4961b12017-05-10 21:04:46 +0200437 menu.element(),
438 this._matchopE
Nils Diewald4c221252015-04-21 20:19:25 +0000439 );
440
441 // Release event
442 var that = this;
443 menu.released(function (mo) {
Akrone4961b12017-05-10 21:04:46 +0200444 that.matchop(mo).update();
445 this.hide();
Nils Diewald4c221252015-04-21 20:19:25 +0000446 });
447
448 menu.show();
449 menu.focus();
450 },
451
452
453 /**
454 * Get or set the type
455 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000456 type : function (type) {
457 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200458 this._type = type;
459 this._changed();
460 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000461 };
462 return this._type || "string";
463 },
464
Nils Diewald4c221252015-04-21 20:19:25 +0000465
466 /**
467 * Get or set the value
468 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000469 value : function (value) {
470 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200471 if (this._type === 'date' && !KorAP._validDateRE.test(value)) {
472 delete this._value;
473 }
474 else {
475 this._value = value;
476 };
477 this._changed();
478 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000479 };
480 return this._value;
481 },
482
Nils Diewald4c221252015-04-21 20:19:25 +0000483
484 // Click on the match operator, show me the menu
485 _changeValue : function (e) {
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000486 var v = this.value();
487 var that = this;
Akron8db5e3a2018-05-28 19:25:26 +0200488
Nils Diewald7148c6f2015-05-04 15:07:53 +0000489 // Show datepicker
Nils Diewald87507832015-05-01 23:36:41 +0000490 if (this.type() === 'date') {
Akrone4961b12017-05-10 21:04:46 +0200491 var dp = KorAP._vcDatePicker;
Akron516b6f92018-01-03 17:46:59 +0100492 dp.fromString(v);
Nils Diewald87507832015-05-01 23:36:41 +0000493
Akrone4961b12017-05-10 21:04:46 +0200494 // Todo: change this
495 dp.onclick(function (selected) {
Nils Diewald87507832015-05-01 23:36:41 +0000496
Akrone4961b12017-05-10 21:04:46 +0200497 // There are values selected
498 if (selected['year']) {
499 that.value(this.toString());
500 that.update();
501 return;
502 };
Nils Diewald87507832015-05-01 23:36:41 +0000503
Akrone4961b12017-05-10 21:04:46 +0200504 // Remove datepicker
505 that._element.removeChild(
506 dp.element()
507 );
508 });
Nils Diewald87507832015-05-01 23:36:41 +0000509
Akrone4961b12017-05-10 21:04:46 +0200510 // Get element of the date picker
511 var dpElem = dp.show();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000512
Akrone4961b12017-05-10 21:04:46 +0200513 this._element.insertBefore(
514 dpElem,
515 this._valueE
516 );
Nils Diewald7148c6f2015-05-04 15:07:53 +0000517
Akron516b6f92018-01-03 17:46:59 +0100518 dp.input().focus();
Nils Diewald87507832015-05-01 23:36:41 +0000519 }
520 else {
Akrone4961b12017-05-10 21:04:46 +0200521 var regex = this.type() === 'regex' ? true : false;
522 var str = stringValClass.create(this.value(), regex);
523 var strElem = str.element();
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000524
Akrone4961b12017-05-10 21:04:46 +0200525 str.store = function (value, regex) {
526 that.value(value);
527 if (regex === true)
528 that.type('regex');
529 else
530 that.type('string');
531
532 that._element.removeChild(
533 this._element
534 );
535 that.update();
536 };
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000537
Akrone4961b12017-05-10 21:04:46 +0200538 // Insert element
539 this._element.insertBefore(
540 strElem,
541 this._valueE
542 );
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000543
Akrone4961b12017-05-10 21:04:46 +0200544 str.focus();
Nils Diewald87507832015-05-01 23:36:41 +0000545 };
Nils Diewald4c221252015-04-21 20:19:25 +0000546 },
547
548
Nils Diewald0e6992a2015-04-14 20:13:52 +0000549 rewrites : function () {
550 return this._rewrites;
551 },
552
Akronea4e9052017-07-06 16:12:05 +0200553 rewrite : function (value) {
554 if (typeof value === 'string') {
555 value = [{
556 "@type" : "koral:rewrite",
557 "operation" : "operation:" + value,
558 "src" : "Kalamar"
559 }];
560 };
561 this._rewrites = rewriteListClass.create(value);
562 },
563
Akron31d89942018-04-06 16:44:51 +0200564 // Mark the underlying data as being changed.
565 // This is important for rerendering the dom.
566 // This will also remove rewrite markers, when the data
567 // change happened by the user
Nils Diewald0e6992a2015-04-14 20:13:52 +0000568 _changed : function () {
569 this.__changed = true;
Nils Diewald4347ee92015-05-04 20:32:48 +0000570
Nils Diewald0e6992a2015-04-14 20:13:52 +0000571 if (this._rewrites === undefined)
Akrone4961b12017-05-10 21:04:46 +0200572 return;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000573
Akronea4e9052017-07-06 16:12:05 +0200574 delete this["_rewrites"];
Nils Diewald0e6992a2015-04-14 20:13:52 +0000575
576 if (this._element === undefined)
Akrone4961b12017-05-10 21:04:46 +0200577 return;
Akronea4e9052017-07-06 16:12:05 +0200578
Nils Diewald0e6992a2015-04-14 20:13:52 +0000579 this._element.classList.remove("rewritten");
580 },
581
Nils Diewald4c221252015-04-21 20:19:25 +0000582
Nils Diewald0e6992a2015-04-14 20:13:52 +0000583 toJson : function () {
584 if (!this.matchop() || !this.key())
Akrone4961b12017-05-10 21:04:46 +0200585 return {};
Nils Diewald0e6992a2015-04-14 20:13:52 +0000586
587 return {
Akrone4961b12017-05-10 21:04:46 +0200588 "@type" : "koral:" + this.ldType(),
589 "key" : this.key(),
590 "match" : "match:" + this.matchop(),
591 "value" : this.value() || '',
592 "type" : "type:" + this.type()
Nils Diewald0e6992a2015-04-14 20:13:52 +0000593 };
594 },
595
Nils Diewald4c221252015-04-21 20:19:25 +0000596
Nils Diewald0e6992a2015-04-14 20:13:52 +0000597 toQuery : function () {
598 if (!this.matchop() || !this.key())
Akrone4961b12017-05-10 21:04:46 +0200599 return "";
Nils Diewald0e6992a2015-04-14 20:13:52 +0000600
601 // Build doc string based on key
602 var string = this.key() + ' ';
603
604 // Add match operator
605 switch (this.matchop()) {
606 case "ne":
Akrone4961b12017-05-10 21:04:46 +0200607 string += '!=';
608 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000609 case "contains":
Akrone4961b12017-05-10 21:04:46 +0200610 string += '~';
611 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000612 case "excludes":
Akrone4961b12017-05-10 21:04:46 +0200613 string += '!~';
614 break;
Akron6a535d42015-08-26 20:16:58 +0200615 case "containsnot":
Akrone4961b12017-05-10 21:04:46 +0200616 string += '!~';
617 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000618 case "geq":
Akrone4961b12017-05-10 21:04:46 +0200619 string += 'since';
620 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000621 case "leq":
Akrone4961b12017-05-10 21:04:46 +0200622 string += 'until';
623 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000624 default:
Akrone4961b12017-05-10 21:04:46 +0200625 string += (this.type() == 'date') ? 'in' : '=';
626 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000627 };
628
629 string += ' ';
630
631 // Add value
632 switch (this.type()) {
633 case "date":
Akrone4961b12017-05-10 21:04:46 +0200634 return string + this.value();
Nils Diewald0e6992a2015-04-14 20:13:52 +0000635 case "regex":
Akron8778f5d2017-06-30 21:25:55 +0200636 return string + '/' + this.value().escapeRegex() + '/';
Nils Diewald0e6992a2015-04-14 20:13:52 +0000637 case "string":
Akron1bdf5272018-07-24 18:51:30 +0200638 case "text":
Akrone4961b12017-05-10 21:04:46 +0200639 return string + '"' + this.value().quote() + '"';
Nils Diewald0e6992a2015-04-14 20:13:52 +0000640 };
641
642 return "";
643 }
644 };
645});