blob: 2d38b5648b5ab5050c62db936561c637cbf2c088 [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);
115 };
116
117 // Change value
118 this._valueE.addEventListener(
119 'click',
120 this._changeValue.bind(this)
121 );
122
123 // Remove all element children
124 _removeChildren(e);
125
126 // Add spans
127 e.appendChild(this._keyE);
128 e.appendChild(this._matchopE);
129 e.appendChild(this._valueE);
130
131 this.__changed = false;
132
Nils Diewald0e6992a2015-04-14 20:13:52 +0000133 if (this._rewrites !== undefined) {
Akrone4961b12017-05-10 21:04:46 +0200134 e.appendChild(this._rewrites.element());
Nils Diewald0e6992a2015-04-14 20:13:52 +0000135 };
136
137 if (this._parent !== undefined) {
Akronb19803c2018-08-16 16:39:42 +0200138
Akrone4961b12017-05-10 21:04:46 +0200139 // Set operators
140 var op = this.operators(
141 true,
142 true,
143 true
144 );
Nils Diewald0e6992a2015-04-14 20:13:52 +0000145
Akrone4961b12017-05-10 21:04:46 +0200146 // Append new operators
147 e.appendChild(op.element());
Nils Diewald0e6992a2015-04-14 20:13:52 +0000148 };
149
150 return e;
151 },
152
Nils Diewald4c221252015-04-21 20:19:25 +0000153
154 /**
155 * Get the associated element
156 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000157 element : function () {
158 if (this._element !== undefined)
Akrone4961b12017-05-10 21:04:46 +0200159 return this._element;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000160
161 this._element = document.createElement('div');
162 this._element.setAttribute('class', 'doc');
163
164 this.update();
165 return this._element;
166 },
167
Nils Diewald4c221252015-04-21 20:19:25 +0000168 /**
169 * Wrap a new operation around the doc element
170 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000171 wrap : function (op) {
172 var parent = this.parent();
Nils Diewald7c8ced22015-04-15 19:21:00 +0000173 var group = require('vc/docgroup').create(parent);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000174 group.operation(op);
175 group.append(this);
176 group.append();
177 return parent.replaceOperand(this, group).update();
178 },
179
Nils Diewald4c221252015-04-21 20:19:25 +0000180 /**
181 * Deserialize from json
182 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000183 fromJson : function (json) {
184 if (json === undefined)
Akrone4961b12017-05-10 21:04:46 +0200185 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000186
187 if (json["@type"] === undefined) {
Akrone4961b12017-05-10 21:04:46 +0200188 KorAP.log(701, "JSON-LD group has no @type attribute");
189 return;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000190 };
191
192 if (json["value"] === undefined ||
Akrone4961b12017-05-10 21:04:46 +0200193 typeof json["value"] != 'string') {
194 KorAP.log(805, "Value is invalid");
195 return;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000196 };
197
Akronea4e9052017-07-06 16:12:05 +0200198 var rewrite;
199
Nils Diewald0e6992a2015-04-14 20:13:52 +0000200 // There is a defined key
201 if (json["key"] !== undefined &&
Akrone4961b12017-05-10 21:04:46 +0200202 typeof json["key"] === 'string') {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000203
Akrone4961b12017-05-10 21:04:46 +0200204 // Set key
205 this.key(json["key"]);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000206
Akrone4961b12017-05-10 21:04:46 +0200207 // Set match operation
208 if (json["match"] !== undefined) {
209 if (typeof json["match"] === 'string') {
Akron31d89942018-04-06 16:44:51 +0200210
Akrone4961b12017-05-10 21:04:46 +0200211 this.matchop(json["match"]);
212 }
213 else {
Akron31d89942018-04-06 16:44:51 +0200214 KorAP.log(802, errstr802);
Akrone4961b12017-05-10 21:04:46 +0200215 return;
216 };
217 };
Akron31d89942018-04-06 16:44:51 +0200218
Akronddc98a72018-04-06 17:33:52 +0200219 // Type is unspecified - but may be known by the menu
220 if (json["type"] === undefined && KorAP._vcKeyMenu) {
221
222 // Check the VC list if the field is known
223 var type = KorAP._vcKeyMenu.typeOf(this.key());
224 if (type != undefined) {
225 json["type"] = "type:" + type;
226 };
227 };
228
229 // Type is still undefined
Akron31d89942018-04-06 16:44:51 +0200230 if (json["type"] === undefined) {
Akronddc98a72018-04-06 17:33:52 +0200231
Akron31d89942018-04-06 16:44:51 +0200232 // Check match type
233 if (!KorAP._validUnspecMatchRE.test(this.matchop())) {
234 KorAP.log(802, errstr802);
235
236 // Rewrite method
237 this.matchop('eq');
238 rewrite = 'modification';
239 };
240
241 // Set string value
242 this.value(json["value"]);
243 }
244
245 // Field is string type
246 else if (json["type"] == "type:string") {
Akrone4961b12017-05-10 21:04:46 +0200247 this.type("string");
Nils Diewald0e6992a2015-04-14 20:13:52 +0000248
Akrone4961b12017-05-10 21:04:46 +0200249 // Check match type
250 if (!KorAP._validStringMatchRE.test(this.matchop())) {
Akron31d89942018-04-06 16:44:51 +0200251 KorAP.log(802, errstr802);
252
253 // Rewrite method
254 this.matchop('eq');
255 rewrite = 'modification';
256 };
257
258 // Set string value
259 this.value(json["value"]);
260 }
261
262 // Field is specified
263 else if (json["type"] == "type:text") {
264 this.type("text");
265
266 // Check match type
267 if (!KorAP._validTextMatchRE.test(this.matchop())) {
268 KorAP.log(802, errstr802);
Akronea4e9052017-07-06 16:12:05 +0200269
270 // Rewrite method
271 this.matchop('eq');
272 rewrite = 'modification';
Akrone4961b12017-05-10 21:04:46 +0200273 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000274
Akrone4961b12017-05-10 21:04:46 +0200275 // Set string value
276 this.value(json["value"]);
277 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000278
Akrone4961b12017-05-10 21:04:46 +0200279 // Key is a date
280 else if (json["type"] === "type:date") {
281 this.type("date");
Nils Diewald0e6992a2015-04-14 20:13:52 +0000282
Akrone4961b12017-05-10 21:04:46 +0200283 if (json["value"] !== undefined &&
284 KorAP._validDateRE.test(json["value"])) {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000285
Akrone4961b12017-05-10 21:04:46 +0200286 if (!KorAP._validDateMatchRE.test(this.matchop())) {
Akron31d89942018-04-06 16:44:51 +0200287 KorAP.log(802, errstr802);
Akronea4e9052017-07-06 16:12:05 +0200288
289 // Rewrite method
290 this.matchop('eq');
291 rewrite = 'modification';
Akrone4961b12017-05-10 21:04:46 +0200292 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000293
Akrone4961b12017-05-10 21:04:46 +0200294 // Set value
295 this.value(json["value"]);
296 }
297 else {
298 KorAP.log(806, "Value is not a valid date string");
299 return;
300 };
301 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000302
Akrone4961b12017-05-10 21:04:46 +0200303 // Key is a regular expression
304 else if (json["type"] === "type:regex") {
305 this.type("regex");
Nils Diewald0e6992a2015-04-14 20:13:52 +0000306
Akrone4961b12017-05-10 21:04:46 +0200307 try {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000308
Akrone4961b12017-05-10 21:04:46 +0200309 // Try to create a regular expression
310 var check = new RegExp(json["value"]);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000311
Akrone65a88a2018-04-05 19:14:20 +0200312 if (!KorAP._validStringMatchRE.test(this.matchop())) {
Akron31d89942018-04-06 16:44:51 +0200313 KorAP.log(802, errstr802);
Akronea4e9052017-07-06 16:12:05 +0200314
315 // Rewrite method
316 this.matchop('eq');
317 rewrite = 'modification';
Akrone4961b12017-05-10 21:04:46 +0200318 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000319
Akrone4961b12017-05-10 21:04:46 +0200320 this.value(json["value"]);
321 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000322
Akrone4961b12017-05-10 21:04:46 +0200323 catch (e) {
324 KorAP.log(807, "Value is not a valid regular expression");
325 return;
326 };
327 this.type("regex");
328 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000329
Akrone4961b12017-05-10 21:04:46 +0200330 else {
Akron1bdf5272018-07-24 18:51:30 +0200331 KorAP.log(804, errstr804 + ": " + this.type());
332 throw new Error(errstr804 + ": " + this.type());
Akrone4961b12017-05-10 21:04:46 +0200333 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000334 };
335
Akronea4e9052017-07-06 16:12:05 +0200336 // Rewrite coming from the server
Nils Diewald0e6992a2015-04-14 20:13:52 +0000337 if (json["rewrites"] !== undefined) {
Akronea4e9052017-07-06 16:12:05 +0200338 this.rewrite(json["rewrites"]);
339 }
340
341 // Rewrite coming from Kalamar
342 else if (rewrite !== undefined) {
343 this.rewrite(rewrite);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000344 };
Akronea4e9052017-07-06 16:12:05 +0200345
Nils Diewald0e6992a2015-04-14 20:13:52 +0000346 return this;
347 },
348
Nils Diewald4c221252015-04-21 20:19:25 +0000349 /**
350 * Get or set the key
351 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000352 key : function (value) {
353 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200354 this._key = value;
355 this._changed();
356 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000357 };
358 return this._key;
359 },
360
Nils Diewald4c221252015-04-21 20:19:25 +0000361 // Click on the key, show me the menu
362 _changeKey : function (e) {
363 var menu = KorAP._vcKeyMenu;
364
365 // Insert menu
366 this._element.insertBefore(
Akrone4961b12017-05-10 21:04:46 +0200367 menu.element(),
368 this._keyE
Nils Diewald4c221252015-04-21 20:19:25 +0000369 );
370
371 // Release event
372 var that = this;
373 menu.released(function (key, type) {
Akrone4961b12017-05-10 21:04:46 +0200374 var doc = that.key(key).type(type);
Nils Diewald4c221252015-04-21 20:19:25 +0000375
Akrone4961b12017-05-10 21:04:46 +0200376 // This may not be compatible - then switch to default
377 doc.matchop(doc.matchop());
378 doc.value(doc.value());
Nils Diewald4c221252015-04-21 20:19:25 +0000379
Akrone4961b12017-05-10 21:04:46 +0200380 // Update the doc
381 doc.update();
Nils Diewald4c221252015-04-21 20:19:25 +0000382
Akrone4961b12017-05-10 21:04:46 +0200383 // hide!
384 this.hide();
Nils Diewald4c221252015-04-21 20:19:25 +0000385 });
386
387 // TODO: Highlight the old value!
388 menu.show();
389 menu.focus();
390 },
391
392 /**
393 * Get or set the match operator
394 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000395 matchop : function (match) {
Akron31d89942018-04-06 16:44:51 +0200396
Nils Diewald0e6992a2015-04-14 20:13:52 +0000397 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200398 var m = match.replace(/^match:/, '');
Akron31d89942018-04-06 16:44:51 +0200399
Akrone4961b12017-05-10 21:04:46 +0200400 if (
Akron31d89942018-04-06 16:44:51 +0200401 (this._type == undefined) // && KorAP._validUnspecMatchRE.test(m))
Akrone4961b12017-05-10 21:04:46 +0200402 ||
403 (
404 (this._type === 'string' || this._type === 'regex') &&
405 KorAP._validStringMatchRE.test(m)
406 )
407 ||
Akron31d89942018-04-06 16:44:51 +0200408 (this._type === 'text' && KorAP._validTextMatchRE.test(m))
409 ||
Akrone4961b12017-05-10 21:04:46 +0200410 (this._type === 'date' && KorAP._validDateMatchRE.test(m))
411 ) {
412 this._matchop = m;
413 }
414 else {
415 this._matchop = "eq";
416 };
Nils Diewald4c221252015-04-21 20:19:25 +0000417
Akrone4961b12017-05-10 21:04:46 +0200418 this._changed();
Akron31d89942018-04-06 16:44:51 +0200419 return this
Nils Diewald0e6992a2015-04-14 20:13:52 +0000420 };
421 return this._matchop || "eq";
422 },
423
Nils Diewald4c221252015-04-21 20:19:25 +0000424
425 // Click on the match operator, show me the menu
426 _changeMatchop : function (e) {
427 var menu = KorAP._vcMatchopMenu[this.type()];
428
429 if (menu === undefined) {
Akrone4961b12017-05-10 21:04:46 +0200430 KorAP.log(0, "Unable to init menu for " + this.type());
431 return;
Nils Diewald4c221252015-04-21 20:19:25 +0000432 };
433
434 // Insert menu
435 this._element.insertBefore(
Akrone4961b12017-05-10 21:04:46 +0200436 menu.element(),
437 this._matchopE
Nils Diewald4c221252015-04-21 20:19:25 +0000438 );
439
440 // Release event
441 var that = this;
442 menu.released(function (mo) {
Akrone4961b12017-05-10 21:04:46 +0200443 that.matchop(mo).update();
444 this.hide();
Nils Diewald4c221252015-04-21 20:19:25 +0000445 });
446
447 menu.show();
448 menu.focus();
449 },
450
451
452 /**
453 * Get or set the type
454 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000455 type : function (type) {
456 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200457 this._type = type;
458 this._changed();
459 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000460 };
461 return this._type || "string";
462 },
463
Nils Diewald4c221252015-04-21 20:19:25 +0000464
465 /**
466 * Get or set the value
467 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000468 value : function (value) {
469 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200470 if (this._type === 'date' && !KorAP._validDateRE.test(value)) {
471 delete this._value;
472 }
473 else {
474 this._value = value;
475 };
476 this._changed();
477 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000478 };
479 return this._value;
480 },
481
Nils Diewald4c221252015-04-21 20:19:25 +0000482
483 // Click on the match operator, show me the menu
484 _changeValue : function (e) {
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000485 var v = this.value();
486 var that = this;
Akron8db5e3a2018-05-28 19:25:26 +0200487
Nils Diewald7148c6f2015-05-04 15:07:53 +0000488 // Show datepicker
Nils Diewald87507832015-05-01 23:36:41 +0000489 if (this.type() === 'date') {
Akrone4961b12017-05-10 21:04:46 +0200490 var dp = KorAP._vcDatePicker;
Akron516b6f92018-01-03 17:46:59 +0100491 dp.fromString(v);
Nils Diewald87507832015-05-01 23:36:41 +0000492
Akrone4961b12017-05-10 21:04:46 +0200493 // Todo: change this
494 dp.onclick(function (selected) {
Nils Diewald87507832015-05-01 23:36:41 +0000495
Akrone4961b12017-05-10 21:04:46 +0200496 // There are values selected
497 if (selected['year']) {
498 that.value(this.toString());
499 that.update();
500 return;
501 };
Nils Diewald87507832015-05-01 23:36:41 +0000502
Akrone4961b12017-05-10 21:04:46 +0200503 // Remove datepicker
504 that._element.removeChild(
505 dp.element()
506 );
507 });
Nils Diewald87507832015-05-01 23:36:41 +0000508
Akrone4961b12017-05-10 21:04:46 +0200509 // Get element of the date picker
510 var dpElem = dp.show();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000511
Akrone4961b12017-05-10 21:04:46 +0200512 this._element.insertBefore(
513 dpElem,
514 this._valueE
515 );
Nils Diewald7148c6f2015-05-04 15:07:53 +0000516
Akron516b6f92018-01-03 17:46:59 +0100517 dp.input().focus();
Nils Diewald87507832015-05-01 23:36:41 +0000518 }
519 else {
Akrone4961b12017-05-10 21:04:46 +0200520 var regex = this.type() === 'regex' ? true : false;
521 var str = stringValClass.create(this.value(), regex);
522 var strElem = str.element();
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000523
Akrone4961b12017-05-10 21:04:46 +0200524 str.store = function (value, regex) {
525 that.value(value);
526 if (regex === true)
527 that.type('regex');
528 else
529 that.type('string');
530
531 that._element.removeChild(
532 this._element
533 );
534 that.update();
535 };
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000536
Akrone4961b12017-05-10 21:04:46 +0200537 // Insert element
538 this._element.insertBefore(
539 strElem,
540 this._valueE
541 );
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000542
Akrone4961b12017-05-10 21:04:46 +0200543 str.focus();
Nils Diewald87507832015-05-01 23:36:41 +0000544 };
Nils Diewald4c221252015-04-21 20:19:25 +0000545 },
546
547
Nils Diewald0e6992a2015-04-14 20:13:52 +0000548 rewrites : function () {
549 return this._rewrites;
550 },
551
Akronea4e9052017-07-06 16:12:05 +0200552 rewrite : function (value) {
553 if (typeof value === 'string') {
554 value = [{
555 "@type" : "koral:rewrite",
556 "operation" : "operation:" + value,
557 "src" : "Kalamar"
558 }];
559 };
560 this._rewrites = rewriteListClass.create(value);
561 },
562
Akron31d89942018-04-06 16:44:51 +0200563 // Mark the underlying data as being changed.
564 // This is important for rerendering the dom.
565 // This will also remove rewrite markers, when the data
566 // change happened by the user
Nils Diewald0e6992a2015-04-14 20:13:52 +0000567 _changed : function () {
568 this.__changed = true;
Nils Diewald4347ee92015-05-04 20:32:48 +0000569
Nils Diewald0e6992a2015-04-14 20:13:52 +0000570 if (this._rewrites === undefined)
Akrone4961b12017-05-10 21:04:46 +0200571 return;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000572
Akronea4e9052017-07-06 16:12:05 +0200573 delete this["_rewrites"];
Nils Diewald0e6992a2015-04-14 20:13:52 +0000574
575 if (this._element === undefined)
Akrone4961b12017-05-10 21:04:46 +0200576 return;
Akronea4e9052017-07-06 16:12:05 +0200577
Nils Diewald0e6992a2015-04-14 20:13:52 +0000578 this._element.classList.remove("rewritten");
579 },
580
Nils Diewald4c221252015-04-21 20:19:25 +0000581
Nils Diewald0e6992a2015-04-14 20:13:52 +0000582 toJson : function () {
583 if (!this.matchop() || !this.key())
Akrone4961b12017-05-10 21:04:46 +0200584 return {};
Nils Diewald0e6992a2015-04-14 20:13:52 +0000585
586 return {
Akrone4961b12017-05-10 21:04:46 +0200587 "@type" : "koral:" + this.ldType(),
588 "key" : this.key(),
589 "match" : "match:" + this.matchop(),
590 "value" : this.value() || '',
591 "type" : "type:" + this.type()
Nils Diewald0e6992a2015-04-14 20:13:52 +0000592 };
593 },
594
Nils Diewald4c221252015-04-21 20:19:25 +0000595
Nils Diewald0e6992a2015-04-14 20:13:52 +0000596 toQuery : function () {
597 if (!this.matchop() || !this.key())
Akrone4961b12017-05-10 21:04:46 +0200598 return "";
Nils Diewald0e6992a2015-04-14 20:13:52 +0000599
600 // Build doc string based on key
601 var string = this.key() + ' ';
602
603 // Add match operator
604 switch (this.matchop()) {
605 case "ne":
Akrone4961b12017-05-10 21:04:46 +0200606 string += '!=';
607 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000608 case "contains":
Akrone4961b12017-05-10 21:04:46 +0200609 string += '~';
610 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000611 case "excludes":
Akrone4961b12017-05-10 21:04:46 +0200612 string += '!~';
613 break;
Akron6a535d42015-08-26 20:16:58 +0200614 case "containsnot":
Akrone4961b12017-05-10 21:04:46 +0200615 string += '!~';
616 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000617 case "geq":
Akrone4961b12017-05-10 21:04:46 +0200618 string += 'since';
619 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000620 case "leq":
Akrone4961b12017-05-10 21:04:46 +0200621 string += 'until';
622 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000623 default:
Akrone4961b12017-05-10 21:04:46 +0200624 string += (this.type() == 'date') ? 'in' : '=';
625 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000626 };
627
628 string += ' ';
629
630 // Add value
631 switch (this.type()) {
632 case "date":
Akrone4961b12017-05-10 21:04:46 +0200633 return string + this.value();
Nils Diewald0e6992a2015-04-14 20:13:52 +0000634 case "regex":
Akron8778f5d2017-06-30 21:25:55 +0200635 return string + '/' + this.value().escapeRegex() + '/';
Nils Diewald0e6992a2015-04-14 20:13:52 +0000636 case "string":
Akron1bdf5272018-07-24 18:51:30 +0200637 case "text":
Akrone4961b12017-05-10 21:04:46 +0200638 return string + '"' + this.value().quote() + '"';
Nils Diewald0e6992a2015-04-14 20:13:52 +0000639 };
640
641 return "";
642 }
643 };
644});