blob: 7c4f98a87f52adf039c51df033b968cdef258a40 [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);
45 };
46
Nils Diewald4c221252015-04-21 20:19:25 +000047 // Bind the parent
Nils Diewald0e6992a2015-04-14 20:13:52 +000048 if (parent !== undefined)
Akrone4961b12017-05-10 21:04:46 +020049 obj._parent = parent;
Nils Diewald0e6992a2015-04-14 20:13:52 +000050
51 obj.__changed = true;
52 return obj;
53 },
54
Nils Diewald4c221252015-04-21 20:19:25 +000055 /**
56 * Update the elements content.
57 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000058 update : function () {
59 if (this._element === undefined)
Akrone4961b12017-05-10 21:04:46 +020060 return this.element();
Nils Diewald0e6992a2015-04-14 20:13:52 +000061
62 // Get element
63 var e = this._element;
64
Akronb19803c2018-08-16 16:39:42 +020065 // Check if there is a change in the underlying data
66 if (!this.__changed)
67 return e;
68
Nils Diewald0e6992a2015-04-14 20:13:52 +000069 // Set ref - TODO: Cleanup!
70 e.refTo = this;
71
Nils Diewald0e6992a2015-04-14 20:13:52 +000072
Akronb19803c2018-08-16 16:39:42 +020073 // Was rewritten
74 if (this.rewrites() !== undefined) {
75 e.classList.add("rewritten");
Nils Diewald0e6992a2015-04-14 20:13:52 +000076 };
77
Akronb19803c2018-08-16 16:39:42 +020078 // Added key
79 this._keyE = document.createElement('span');
80 this._keyE.setAttribute('class', 'key');
81
82 // Change key
83 this._keyE.addEventListener('click', this._changeKey.bind(this));
84
85 if (this.key()) {
86 var k = this.key();
87 if (loc['VC_' + k] !== undefined)
88 k = loc['VC_' + k];
89 this._keyE.addT(k);
90 };
91
92 // Added match operator
93 this._matchopE = document.createElement('span');
94 this._matchopE.setAttribute('data-type', this.type());
95 this._matchopE.setAttribute('class', 'match');
96 this._matchopE.addT(this.matchop());
97
98 // Change matchop
99 this._matchopE.addEventListener(
100 'click',
101 this._changeMatchop.bind(this)
102 );
103
104 // Added value operator
105 this._valueE = document.createElement('span');
106 this._valueE.setAttribute('data-type', this.type());
107 this._valueE.setAttribute('class', 'value');
108
109 if (this.value()) {
110 this._valueE.addT(this.value());
111 }
112 else {
113 this._valueE.addT(loc.EMPTY);
114 };
115
116 // Change value
117 this._valueE.addEventListener(
118 'click',
119 this._changeValue.bind(this)
120 );
121
122 // Remove all element children
123 _removeChildren(e);
124
125 // Add spans
126 e.appendChild(this._keyE);
127 e.appendChild(this._matchopE);
128 e.appendChild(this._valueE);
129
130 this.__changed = false;
131
Nils Diewald0e6992a2015-04-14 20:13:52 +0000132 if (this._rewrites !== undefined) {
Akrone4961b12017-05-10 21:04:46 +0200133 e.appendChild(this._rewrites.element());
Nils Diewald0e6992a2015-04-14 20:13:52 +0000134 };
135
136 if (this._parent !== undefined) {
Akronb19803c2018-08-16 16:39:42 +0200137
Akrone4961b12017-05-10 21:04:46 +0200138 // Set operators
139 var op = this.operators(
140 true,
141 true,
142 true
143 );
Nils Diewald0e6992a2015-04-14 20:13:52 +0000144
Akrone4961b12017-05-10 21:04:46 +0200145 // Append new operators
146 e.appendChild(op.element());
Nils Diewald0e6992a2015-04-14 20:13:52 +0000147 };
148
149 return e;
150 },
151
Nils Diewald4c221252015-04-21 20:19:25 +0000152
153 /**
154 * Get the associated element
155 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000156 element : function () {
157 if (this._element !== undefined)
Akrone4961b12017-05-10 21:04:46 +0200158 return this._element;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000159
160 this._element = document.createElement('div');
161 this._element.setAttribute('class', 'doc');
162
163 this.update();
164 return this._element;
165 },
166
Nils Diewald4c221252015-04-21 20:19:25 +0000167 /**
168 * Wrap a new operation around the doc element
169 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000170 wrap : function (op) {
171 var parent = this.parent();
Nils Diewald7c8ced22015-04-15 19:21:00 +0000172 var group = require('vc/docgroup').create(parent);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000173 group.operation(op);
174 group.append(this);
175 group.append();
176 return parent.replaceOperand(this, group).update();
177 },
178
Nils Diewald4c221252015-04-21 20:19:25 +0000179 /**
180 * Deserialize from json
181 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000182 fromJson : function (json) {
183 if (json === undefined)
Akrone4961b12017-05-10 21:04:46 +0200184 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000185
186 if (json["@type"] === undefined) {
Akrone4961b12017-05-10 21:04:46 +0200187 KorAP.log(701, "JSON-LD group has no @type attribute");
188 return;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000189 };
190
191 if (json["value"] === undefined ||
Akrone4961b12017-05-10 21:04:46 +0200192 typeof json["value"] != 'string') {
193 KorAP.log(805, "Value is invalid");
194 return;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000195 };
196
Akronea4e9052017-07-06 16:12:05 +0200197 var rewrite;
198
Nils Diewald0e6992a2015-04-14 20:13:52 +0000199 // There is a defined key
200 if (json["key"] !== undefined &&
Akrone4961b12017-05-10 21:04:46 +0200201 typeof json["key"] === 'string') {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000202
Akrone4961b12017-05-10 21:04:46 +0200203 // Set key
204 this.key(json["key"]);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000205
Akrone4961b12017-05-10 21:04:46 +0200206 // Set match operation
207 if (json["match"] !== undefined) {
208 if (typeof json["match"] === 'string') {
Akron31d89942018-04-06 16:44:51 +0200209
Akrone4961b12017-05-10 21:04:46 +0200210 this.matchop(json["match"]);
211 }
212 else {
Akron31d89942018-04-06 16:44:51 +0200213 KorAP.log(802, errstr802);
Akrone4961b12017-05-10 21:04:46 +0200214 return;
215 };
216 };
Akron31d89942018-04-06 16:44:51 +0200217
Akronddc98a72018-04-06 17:33:52 +0200218 // Type is unspecified - but may be known by the menu
219 if (json["type"] === undefined && KorAP._vcKeyMenu) {
220
221 // Check the VC list if the field is known
222 var type = KorAP._vcKeyMenu.typeOf(this.key());
223 if (type != undefined) {
224 json["type"] = "type:" + type;
225 };
226 };
227
228 // Type is still undefined
Akron31d89942018-04-06 16:44:51 +0200229 if (json["type"] === undefined) {
Akronddc98a72018-04-06 17:33:52 +0200230
Akron31d89942018-04-06 16:44:51 +0200231 // Check match type
232 if (!KorAP._validUnspecMatchRE.test(this.matchop())) {
233 KorAP.log(802, errstr802);
234
235 // Rewrite method
236 this.matchop('eq');
237 rewrite = 'modification';
238 };
239
240 // Set string value
241 this.value(json["value"]);
242 }
243
244 // Field is string type
245 else if (json["type"] == "type:string") {
Akrone4961b12017-05-10 21:04:46 +0200246 this.type("string");
Nils Diewald0e6992a2015-04-14 20:13:52 +0000247
Akrone4961b12017-05-10 21:04:46 +0200248 // Check match type
249 if (!KorAP._validStringMatchRE.test(this.matchop())) {
Akron31d89942018-04-06 16:44:51 +0200250 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 specified
262 else if (json["type"] == "type:text") {
263 this.type("text");
264
265 // Check match type
266 if (!KorAP._validTextMatchRE.test(this.matchop())) {
267 KorAP.log(802, errstr802);
Akronea4e9052017-07-06 16:12:05 +0200268
269 // Rewrite method
270 this.matchop('eq');
271 rewrite = 'modification';
Akrone4961b12017-05-10 21:04:46 +0200272 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000273
Akrone4961b12017-05-10 21:04:46 +0200274 // Set string value
275 this.value(json["value"]);
276 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000277
Akrone4961b12017-05-10 21:04:46 +0200278 // Key is a date
279 else if (json["type"] === "type:date") {
280 this.type("date");
Nils Diewald0e6992a2015-04-14 20:13:52 +0000281
Akrone4961b12017-05-10 21:04:46 +0200282 if (json["value"] !== undefined &&
283 KorAP._validDateRE.test(json["value"])) {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000284
Akrone4961b12017-05-10 21:04:46 +0200285 if (!KorAP._validDateMatchRE.test(this.matchop())) {
Akron31d89942018-04-06 16:44:51 +0200286 KorAP.log(802, errstr802);
Akronea4e9052017-07-06 16:12:05 +0200287
288 // Rewrite method
289 this.matchop('eq');
290 rewrite = 'modification';
Akrone4961b12017-05-10 21:04:46 +0200291 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000292
Akrone4961b12017-05-10 21:04:46 +0200293 // Set value
294 this.value(json["value"]);
295 }
296 else {
297 KorAP.log(806, "Value is not a valid date string");
298 return;
299 };
300 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000301
Akrone4961b12017-05-10 21:04:46 +0200302 // Key is a regular expression
303 else if (json["type"] === "type:regex") {
304 this.type("regex");
Nils Diewald0e6992a2015-04-14 20:13:52 +0000305
Akrone4961b12017-05-10 21:04:46 +0200306 try {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000307
Akrone4961b12017-05-10 21:04:46 +0200308 // Try to create a regular expression
309 var check = new RegExp(json["value"]);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000310
Akrone65a88a2018-04-05 19:14:20 +0200311 if (!KorAP._validStringMatchRE.test(this.matchop())) {
Akron31d89942018-04-06 16:44:51 +0200312 KorAP.log(802, errstr802);
Akronea4e9052017-07-06 16:12:05 +0200313
314 // Rewrite method
315 this.matchop('eq');
316 rewrite = 'modification';
Akrone4961b12017-05-10 21:04:46 +0200317 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000318
Akrone4961b12017-05-10 21:04:46 +0200319 this.value(json["value"]);
320 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000321
Akrone4961b12017-05-10 21:04:46 +0200322 catch (e) {
323 KorAP.log(807, "Value is not a valid regular expression");
324 return;
325 };
326 this.type("regex");
327 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000328
Akrone4961b12017-05-10 21:04:46 +0200329 else {
Akron1bdf5272018-07-24 18:51:30 +0200330 KorAP.log(804, errstr804 + ": " + this.type());
331 throw new Error(errstr804 + ": " + this.type());
Akrone4961b12017-05-10 21:04:46 +0200332 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000333 };
334
Akronea4e9052017-07-06 16:12:05 +0200335 // Rewrite coming from the server
Nils Diewald0e6992a2015-04-14 20:13:52 +0000336 if (json["rewrites"] !== undefined) {
Akronea4e9052017-07-06 16:12:05 +0200337 this.rewrite(json["rewrites"]);
338 }
339
340 // Rewrite coming from Kalamar
341 else if (rewrite !== undefined) {
342 this.rewrite(rewrite);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000343 };
Akronea4e9052017-07-06 16:12:05 +0200344
Nils Diewald0e6992a2015-04-14 20:13:52 +0000345 return this;
346 },
347
Nils Diewald4c221252015-04-21 20:19:25 +0000348 /**
349 * Get or set the key
350 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000351 key : function (value) {
352 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200353 this._key = value;
354 this._changed();
355 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000356 };
357 return this._key;
358 },
359
Nils Diewald4c221252015-04-21 20:19:25 +0000360 // Click on the key, show me the menu
361 _changeKey : function (e) {
362 var menu = KorAP._vcKeyMenu;
363
364 // Insert menu
365 this._element.insertBefore(
Akrone4961b12017-05-10 21:04:46 +0200366 menu.element(),
367 this._keyE
Nils Diewald4c221252015-04-21 20:19:25 +0000368 );
369
370 // Release event
371 var that = this;
372 menu.released(function (key, type) {
Akrone4961b12017-05-10 21:04:46 +0200373 var doc = that.key(key).type(type);
Nils Diewald4c221252015-04-21 20:19:25 +0000374
Akrone4961b12017-05-10 21:04:46 +0200375 // This may not be compatible - then switch to default
376 doc.matchop(doc.matchop());
377 doc.value(doc.value());
Nils Diewald4c221252015-04-21 20:19:25 +0000378
Akrone4961b12017-05-10 21:04:46 +0200379 // Update the doc
380 doc.update();
Nils Diewald4c221252015-04-21 20:19:25 +0000381
Akrone4961b12017-05-10 21:04:46 +0200382 // hide!
383 this.hide();
Nils Diewald4c221252015-04-21 20:19:25 +0000384 });
385
386 // TODO: Highlight the old value!
387 menu.show();
388 menu.focus();
389 },
390
391 /**
392 * Get or set the match operator
393 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000394 matchop : function (match) {
Akron31d89942018-04-06 16:44:51 +0200395
Nils Diewald0e6992a2015-04-14 20:13:52 +0000396 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200397 var m = match.replace(/^match:/, '');
Akron31d89942018-04-06 16:44:51 +0200398
Akrone4961b12017-05-10 21:04:46 +0200399 if (
Akron31d89942018-04-06 16:44:51 +0200400 (this._type == undefined) // && KorAP._validUnspecMatchRE.test(m))
Akrone4961b12017-05-10 21:04:46 +0200401 ||
402 (
403 (this._type === 'string' || this._type === 'regex') &&
404 KorAP._validStringMatchRE.test(m)
405 )
406 ||
Akron31d89942018-04-06 16:44:51 +0200407 (this._type === 'text' && KorAP._validTextMatchRE.test(m))
408 ||
Akrone4961b12017-05-10 21:04:46 +0200409 (this._type === 'date' && KorAP._validDateMatchRE.test(m))
410 ) {
411 this._matchop = m;
412 }
413 else {
414 this._matchop = "eq";
415 };
Nils Diewald4c221252015-04-21 20:19:25 +0000416
Akrone4961b12017-05-10 21:04:46 +0200417 this._changed();
Akron31d89942018-04-06 16:44:51 +0200418 return this
Nils Diewald0e6992a2015-04-14 20:13:52 +0000419 };
420 return this._matchop || "eq";
421 },
422
Nils Diewald4c221252015-04-21 20:19:25 +0000423
424 // Click on the match operator, show me the menu
425 _changeMatchop : function (e) {
426 var menu = KorAP._vcMatchopMenu[this.type()];
427
428 if (menu === undefined) {
Akrone4961b12017-05-10 21:04:46 +0200429 KorAP.log(0, "Unable to init menu for " + this.type());
430 return;
Nils Diewald4c221252015-04-21 20:19:25 +0000431 };
432
433 // Insert menu
434 this._element.insertBefore(
Akrone4961b12017-05-10 21:04:46 +0200435 menu.element(),
436 this._matchopE
Nils Diewald4c221252015-04-21 20:19:25 +0000437 );
438
439 // Release event
440 var that = this;
441 menu.released(function (mo) {
Akrone4961b12017-05-10 21:04:46 +0200442 that.matchop(mo).update();
443 this.hide();
Nils Diewald4c221252015-04-21 20:19:25 +0000444 });
445
446 menu.show();
447 menu.focus();
448 },
449
450
451 /**
452 * Get or set the type
453 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000454 type : function (type) {
455 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200456 this._type = type;
457 this._changed();
458 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000459 };
460 return this._type || "string";
461 },
462
Nils Diewald4c221252015-04-21 20:19:25 +0000463
464 /**
465 * Get or set the value
466 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000467 value : function (value) {
468 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200469 if (this._type === 'date' && !KorAP._validDateRE.test(value)) {
470 delete this._value;
471 }
472 else {
473 this._value = value;
474 };
475 this._changed();
476 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000477 };
478 return this._value;
479 },
480
Nils Diewald4c221252015-04-21 20:19:25 +0000481
482 // Click on the match operator, show me the menu
483 _changeValue : function (e) {
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000484 var v = this.value();
485 var that = this;
Akron8db5e3a2018-05-28 19:25:26 +0200486
Nils Diewald7148c6f2015-05-04 15:07:53 +0000487 // Show datepicker
Nils Diewald87507832015-05-01 23:36:41 +0000488 if (this.type() === 'date') {
Akrone4961b12017-05-10 21:04:46 +0200489 var dp = KorAP._vcDatePicker;
Akron516b6f92018-01-03 17:46:59 +0100490 dp.fromString(v);
Nils Diewald87507832015-05-01 23:36:41 +0000491
Akrone4961b12017-05-10 21:04:46 +0200492 // Todo: change this
493 dp.onclick(function (selected) {
Nils Diewald87507832015-05-01 23:36:41 +0000494
Akrone4961b12017-05-10 21:04:46 +0200495 // There are values selected
496 if (selected['year']) {
497 that.value(this.toString());
498 that.update();
499 return;
500 };
Nils Diewald87507832015-05-01 23:36:41 +0000501
Akrone4961b12017-05-10 21:04:46 +0200502 // Remove datepicker
503 that._element.removeChild(
504 dp.element()
505 );
506 });
Nils Diewald87507832015-05-01 23:36:41 +0000507
Akrone4961b12017-05-10 21:04:46 +0200508 // Get element of the date picker
509 var dpElem = dp.show();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000510
Akrone4961b12017-05-10 21:04:46 +0200511 this._element.insertBefore(
512 dpElem,
513 this._valueE
514 );
Nils Diewald7148c6f2015-05-04 15:07:53 +0000515
Akron516b6f92018-01-03 17:46:59 +0100516 dp.input().focus();
Nils Diewald87507832015-05-01 23:36:41 +0000517 }
518 else {
Akrone4961b12017-05-10 21:04:46 +0200519 var regex = this.type() === 'regex' ? true : false;
520 var str = stringValClass.create(this.value(), regex);
521 var strElem = str.element();
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000522
Akrone4961b12017-05-10 21:04:46 +0200523 str.store = function (value, regex) {
524 that.value(value);
525 if (regex === true)
526 that.type('regex');
527 else
528 that.type('string');
529
530 that._element.removeChild(
531 this._element
532 );
533 that.update();
534 };
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000535
Akrone4961b12017-05-10 21:04:46 +0200536 // Insert element
537 this._element.insertBefore(
538 strElem,
539 this._valueE
540 );
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000541
Akrone4961b12017-05-10 21:04:46 +0200542 str.focus();
Nils Diewald87507832015-05-01 23:36:41 +0000543 };
Nils Diewald4c221252015-04-21 20:19:25 +0000544 },
545
546
Nils Diewald0e6992a2015-04-14 20:13:52 +0000547 rewrites : function () {
548 return this._rewrites;
549 },
550
Akronea4e9052017-07-06 16:12:05 +0200551 rewrite : function (value) {
552 if (typeof value === 'string') {
553 value = [{
554 "@type" : "koral:rewrite",
555 "operation" : "operation:" + value,
556 "src" : "Kalamar"
557 }];
558 };
559 this._rewrites = rewriteListClass.create(value);
560 },
561
Akron31d89942018-04-06 16:44:51 +0200562 // Mark the underlying data as being changed.
563 // This is important for rerendering the dom.
564 // This will also remove rewrite markers, when the data
565 // change happened by the user
Nils Diewald0e6992a2015-04-14 20:13:52 +0000566 _changed : function () {
567 this.__changed = true;
Nils Diewald4347ee92015-05-04 20:32:48 +0000568
Nils Diewald0e6992a2015-04-14 20:13:52 +0000569 if (this._rewrites === undefined)
Akrone4961b12017-05-10 21:04:46 +0200570 return;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000571
Akronea4e9052017-07-06 16:12:05 +0200572 delete this["_rewrites"];
Nils Diewald0e6992a2015-04-14 20:13:52 +0000573
574 if (this._element === undefined)
Akrone4961b12017-05-10 21:04:46 +0200575 return;
Akronea4e9052017-07-06 16:12:05 +0200576
Nils Diewald0e6992a2015-04-14 20:13:52 +0000577 this._element.classList.remove("rewritten");
578 },
579
Nils Diewald4c221252015-04-21 20:19:25 +0000580
Nils Diewald0e6992a2015-04-14 20:13:52 +0000581 toJson : function () {
582 if (!this.matchop() || !this.key())
Akrone4961b12017-05-10 21:04:46 +0200583 return {};
Nils Diewald0e6992a2015-04-14 20:13:52 +0000584
585 return {
Akrone4961b12017-05-10 21:04:46 +0200586 "@type" : "koral:" + this.ldType(),
587 "key" : this.key(),
588 "match" : "match:" + this.matchop(),
589 "value" : this.value() || '',
590 "type" : "type:" + this.type()
Nils Diewald0e6992a2015-04-14 20:13:52 +0000591 };
592 },
593
Nils Diewald4c221252015-04-21 20:19:25 +0000594
Nils Diewald0e6992a2015-04-14 20:13:52 +0000595 toQuery : function () {
596 if (!this.matchop() || !this.key())
Akrone4961b12017-05-10 21:04:46 +0200597 return "";
Nils Diewald0e6992a2015-04-14 20:13:52 +0000598
599 // Build doc string based on key
600 var string = this.key() + ' ';
601
602 // Add match operator
603 switch (this.matchop()) {
604 case "ne":
Akrone4961b12017-05-10 21:04:46 +0200605 string += '!=';
606 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000607 case "contains":
Akrone4961b12017-05-10 21:04:46 +0200608 string += '~';
609 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000610 case "excludes":
Akrone4961b12017-05-10 21:04:46 +0200611 string += '!~';
612 break;
Akron6a535d42015-08-26 20:16:58 +0200613 case "containsnot":
Akrone4961b12017-05-10 21:04:46 +0200614 string += '!~';
615 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000616 case "geq":
Akrone4961b12017-05-10 21:04:46 +0200617 string += 'since';
618 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000619 case "leq":
Akrone4961b12017-05-10 21:04:46 +0200620 string += 'until';
621 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000622 default:
Akrone4961b12017-05-10 21:04:46 +0200623 string += (this.type() == 'date') ? 'in' : '=';
624 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000625 };
626
627 string += ' ';
628
629 // Add value
630 switch (this.type()) {
631 case "date":
Akrone4961b12017-05-10 21:04:46 +0200632 return string + this.value();
Nils Diewald0e6992a2015-04-14 20:13:52 +0000633 case "regex":
Akron8778f5d2017-06-30 21:25:55 +0200634 return string + '/' + this.value().escapeRegex() + '/';
Nils Diewald0e6992a2015-04-14 20:13:52 +0000635 case "string":
Akron1bdf5272018-07-24 18:51:30 +0200636 case "text":
Akrone4961b12017-05-10 21:04:46 +0200637 return string + '"' + this.value().quote() + '"';
Nils Diewald0e6992a2015-04-14 20:13:52 +0000638 };
639
640 return "";
641 }
642 };
643});