blob: 3df0f700019376b69b5b96525b52bc65bf7bc4c1 [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
37 // Bind the parent
Nils Diewald0e6992a2015-04-14 20:13:52 +000038 if (parent !== undefined)
Akrone4961b12017-05-10 21:04:46 +020039 obj._parent = parent;
Nils Diewald0e6992a2015-04-14 20:13:52 +000040
41 obj.__changed = true;
42 return obj;
43 },
44
Nils Diewald4c221252015-04-21 20:19:25 +000045 /**
46 * Update the elements content.
47 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000048 update : function () {
49 if (this._element === undefined)
Akrone4961b12017-05-10 21:04:46 +020050 return this.element();
Nils Diewald0e6992a2015-04-14 20:13:52 +000051
52 // Get element
53 var e = this._element;
54
55 // Set ref - TODO: Cleanup!
56 e.refTo = this;
57
Akron31d89942018-04-06 16:44:51 +020058 // Check if there is a change in the underlying data
Nils Diewald0e6992a2015-04-14 20:13:52 +000059 if (this.__changed) {
60
Akrone4961b12017-05-10 21:04:46 +020061 // Was rewritten
62 if (this.rewrites() !== undefined) {
63 e.classList.add("rewritten");
64 };
Nils Diewald0e6992a2015-04-14 20:13:52 +000065
Akrone4961b12017-05-10 21:04:46 +020066 // Added key
67 this._keyE = document.createElement('span');
68 this._keyE.setAttribute('class', 'key');
Nils Diewald0e6992a2015-04-14 20:13:52 +000069
Akrone4961b12017-05-10 21:04:46 +020070 // Change key
71 this._keyE.addEventListener('click', this._changeKey.bind(this));
Nils Diewald0e6992a2015-04-14 20:13:52 +000072
Akrone4961b12017-05-10 21:04:46 +020073 if (this.key()) {
74 var k = this.key();
75 if (loc['VC_' + k] !== undefined)
76 k = loc['VC_' + k];
Akron0b489ad2018-02-02 16:49:32 +010077 this._keyE.addT(k);
Akrone4961b12017-05-10 21:04:46 +020078 };
Nils Diewald4c221252015-04-21 20:19:25 +000079
Akrone4961b12017-05-10 21:04:46 +020080 // Added match operator
81 this._matchopE = document.createElement('span');
82 this._matchopE.setAttribute('data-type', this.type());
83 this._matchopE.setAttribute('class', 'match');
Akron0b489ad2018-02-02 16:49:32 +010084 this._matchopE.addT(this.matchop());
Nils Diewald0e6992a2015-04-14 20:13:52 +000085
Akrone4961b12017-05-10 21:04:46 +020086 // Change matchop
87 this._matchopE.addEventListener(
88 'click',
89 this._changeMatchop.bind(this)
90 );
Nils Diewald4c221252015-04-21 20:19:25 +000091
Akrone4961b12017-05-10 21:04:46 +020092 // Added value operator
93 this._valueE = document.createElement('span');
94 this._valueE.setAttribute('data-type', this.type());
95 this._valueE.setAttribute('class', 'value');
96 if (this.value()) {
Akron0b489ad2018-02-02 16:49:32 +010097 this._valueE.addT(this.value());
Akrone4961b12017-05-10 21:04:46 +020098 }
99 else {
Akron0b489ad2018-02-02 16:49:32 +0100100 this._valueE.addT(loc.EMPTY);
Akrone4961b12017-05-10 21:04:46 +0200101 };
Nils Diewald4c221252015-04-21 20:19:25 +0000102
Akrone4961b12017-05-10 21:04:46 +0200103 // Change value
104 this._valueE.addEventListener(
105 'click',
106 this._changeValue.bind(this)
107 );
Nils Diewald4c221252015-04-21 20:19:25 +0000108
Nils Diewald0e6992a2015-04-14 20:13:52 +0000109
Akrone4961b12017-05-10 21:04:46 +0200110 // Remove all element children
111 _removeChildren(e);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000112
Akrone4961b12017-05-10 21:04:46 +0200113 // Add spans
114 e.appendChild(this._keyE);
115 e.appendChild(this._matchopE);
116 e.appendChild(this._valueE);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000117
Akrone4961b12017-05-10 21:04:46 +0200118 this.__changed = false;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000119 };
120
121 if (this._rewrites !== undefined) {
Akrone4961b12017-05-10 21:04:46 +0200122 e.appendChild(this._rewrites.element());
Nils Diewald0e6992a2015-04-14 20:13:52 +0000123 };
124
125 if (this._parent !== undefined) {
Akrone4961b12017-05-10 21:04:46 +0200126 // Set operators
127 var op = this.operators(
128 true,
129 true,
130 true
131 );
Nils Diewald0e6992a2015-04-14 20:13:52 +0000132
Akrone4961b12017-05-10 21:04:46 +0200133 // Append new operators
134 e.appendChild(op.element());
Nils Diewald0e6992a2015-04-14 20:13:52 +0000135 };
136
137 return e;
138 },
139
Nils Diewald4c221252015-04-21 20:19:25 +0000140
141 /**
142 * Get the associated element
143 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000144 element : function () {
145 if (this._element !== undefined)
Akrone4961b12017-05-10 21:04:46 +0200146 return this._element;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000147
148 this._element = document.createElement('div');
149 this._element.setAttribute('class', 'doc');
150
151 this.update();
152 return this._element;
153 },
154
Nils Diewald4c221252015-04-21 20:19:25 +0000155 /**
156 * Wrap a new operation around the doc element
157 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000158 wrap : function (op) {
159 var parent = this.parent();
Nils Diewald7c8ced22015-04-15 19:21:00 +0000160 var group = require('vc/docgroup').create(parent);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000161 group.operation(op);
162 group.append(this);
163 group.append();
164 return parent.replaceOperand(this, group).update();
165 },
166
Nils Diewald4c221252015-04-21 20:19:25 +0000167 /**
168 * Deserialize from json
169 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000170 fromJson : function (json) {
171 if (json === undefined)
Akrone4961b12017-05-10 21:04:46 +0200172 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000173
174 if (json["@type"] === undefined) {
Akrone4961b12017-05-10 21:04:46 +0200175 KorAP.log(701, "JSON-LD group has no @type attribute");
176 return;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000177 };
178
179 if (json["value"] === undefined ||
Akrone4961b12017-05-10 21:04:46 +0200180 typeof json["value"] != 'string') {
181 KorAP.log(805, "Value is invalid");
182 return;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000183 };
184
Akronea4e9052017-07-06 16:12:05 +0200185 var rewrite;
186
Nils Diewald0e6992a2015-04-14 20:13:52 +0000187 // There is a defined key
188 if (json["key"] !== undefined &&
Akrone4961b12017-05-10 21:04:46 +0200189 typeof json["key"] === 'string') {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000190
Akrone4961b12017-05-10 21:04:46 +0200191 // Set key
192 this.key(json["key"]);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000193
Akrone4961b12017-05-10 21:04:46 +0200194 // Set match operation
195 if (json["match"] !== undefined) {
196 if (typeof json["match"] === 'string') {
Akron31d89942018-04-06 16:44:51 +0200197
Akrone4961b12017-05-10 21:04:46 +0200198 this.matchop(json["match"]);
199 }
200 else {
Akron31d89942018-04-06 16:44:51 +0200201 KorAP.log(802, errstr802);
Akrone4961b12017-05-10 21:04:46 +0200202 return;
203 };
204 };
Akron31d89942018-04-06 16:44:51 +0200205
206 // Type is unspecified
207 if (json["type"] === undefined) {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000208
Akron31d89942018-04-06 16:44:51 +0200209 // TODO:
210 // First check the VC list if the field is known
211
212 // Check match type
213 if (!KorAP._validUnspecMatchRE.test(this.matchop())) {
214 KorAP.log(802, errstr802);
215
216 // Rewrite method
217 this.matchop('eq');
218 rewrite = 'modification';
219 };
220
221 // Set string value
222 this.value(json["value"]);
223 }
224
225 // Field is string type
226 else if (json["type"] == "type:string") {
Akrone4961b12017-05-10 21:04:46 +0200227 this.type("string");
Nils Diewald0e6992a2015-04-14 20:13:52 +0000228
Akrone4961b12017-05-10 21:04:46 +0200229 // Check match type
230 if (!KorAP._validStringMatchRE.test(this.matchop())) {
Akron31d89942018-04-06 16:44:51 +0200231 KorAP.log(802, errstr802);
232
233 // Rewrite method
234 this.matchop('eq');
235 rewrite = 'modification';
236 };
237
238 // Set string value
239 this.value(json["value"]);
240 }
241
242 // Field is specified
243 else if (json["type"] == "type:text") {
244 this.type("text");
245
246 // Check match type
247 if (!KorAP._validTextMatchRE.test(this.matchop())) {
248 KorAP.log(802, errstr802);
Akronea4e9052017-07-06 16:12:05 +0200249
250 // Rewrite method
251 this.matchop('eq');
252 rewrite = 'modification';
Akrone4961b12017-05-10 21:04:46 +0200253 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000254
Akrone4961b12017-05-10 21:04:46 +0200255 // Set string value
256 this.value(json["value"]);
257 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000258
Akrone4961b12017-05-10 21:04:46 +0200259 // Key is a date
260 else if (json["type"] === "type:date") {
261 this.type("date");
Nils Diewald0e6992a2015-04-14 20:13:52 +0000262
Akrone4961b12017-05-10 21:04:46 +0200263 if (json["value"] !== undefined &&
264 KorAP._validDateRE.test(json["value"])) {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000265
Akrone4961b12017-05-10 21:04:46 +0200266 if (!KorAP._validDateMatchRE.test(this.matchop())) {
Akron31d89942018-04-06 16:44:51 +0200267 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 value
275 this.value(json["value"]);
276 }
277 else {
278 KorAP.log(806, "Value is not a valid date string");
279 return;
280 };
281 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000282
Akrone4961b12017-05-10 21:04:46 +0200283 // Key is a regular expression
284 else if (json["type"] === "type:regex") {
285 this.type("regex");
Nils Diewald0e6992a2015-04-14 20:13:52 +0000286
Akrone4961b12017-05-10 21:04:46 +0200287 try {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000288
Akrone4961b12017-05-10 21:04:46 +0200289 // Try to create a regular expression
290 var check = new RegExp(json["value"]);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000291
Akrone65a88a2018-04-05 19:14:20 +0200292 if (!KorAP._validStringMatchRE.test(this.matchop())) {
Akron31d89942018-04-06 16:44:51 +0200293 KorAP.log(802, errstr802);
Akronea4e9052017-07-06 16:12:05 +0200294
295 // Rewrite method
296 this.matchop('eq');
297 rewrite = 'modification';
Akrone4961b12017-05-10 21:04:46 +0200298 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000299
Akrone4961b12017-05-10 21:04:46 +0200300 this.value(json["value"]);
301 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000302
Akrone4961b12017-05-10 21:04:46 +0200303 catch (e) {
304 KorAP.log(807, "Value is not a valid regular expression");
305 return;
306 };
307 this.type("regex");
308 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000309
Akrone4961b12017-05-10 21:04:46 +0200310 else {
311 KorAP.log(804, "Unknown value type");
312 return;
313 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000314 };
315
Akronea4e9052017-07-06 16:12:05 +0200316 // Rewrite coming from the server
Nils Diewald0e6992a2015-04-14 20:13:52 +0000317 if (json["rewrites"] !== undefined) {
Akronea4e9052017-07-06 16:12:05 +0200318 this.rewrite(json["rewrites"]);
319 }
320
321 // Rewrite coming from Kalamar
322 else if (rewrite !== undefined) {
323 this.rewrite(rewrite);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000324 };
Akronea4e9052017-07-06 16:12:05 +0200325
Nils Diewald0e6992a2015-04-14 20:13:52 +0000326 return this;
327 },
328
Nils Diewald4c221252015-04-21 20:19:25 +0000329 /**
330 * Get or set the key
331 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000332 key : function (value) {
333 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200334 this._key = value;
335 this._changed();
336 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000337 };
338 return this._key;
339 },
340
Nils Diewald4c221252015-04-21 20:19:25 +0000341 // Click on the key, show me the menu
342 _changeKey : function (e) {
343 var menu = KorAP._vcKeyMenu;
344
345 // Insert menu
346 this._element.insertBefore(
Akrone4961b12017-05-10 21:04:46 +0200347 menu.element(),
348 this._keyE
Nils Diewald4c221252015-04-21 20:19:25 +0000349 );
350
351 // Release event
352 var that = this;
353 menu.released(function (key, type) {
Akrone4961b12017-05-10 21:04:46 +0200354 var doc = that.key(key).type(type);
Nils Diewald4c221252015-04-21 20:19:25 +0000355
Akrone4961b12017-05-10 21:04:46 +0200356 // This may not be compatible - then switch to default
357 doc.matchop(doc.matchop());
358 doc.value(doc.value());
Nils Diewald4c221252015-04-21 20:19:25 +0000359
Akrone4961b12017-05-10 21:04:46 +0200360 // Update the doc
361 doc.update();
Nils Diewald4c221252015-04-21 20:19:25 +0000362
Akrone4961b12017-05-10 21:04:46 +0200363 // hide!
364 this.hide();
Nils Diewald4c221252015-04-21 20:19:25 +0000365 });
366
367 // TODO: Highlight the old value!
368 menu.show();
369 menu.focus();
370 },
371
372 /**
373 * Get or set the match operator
374 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000375 matchop : function (match) {
Akron31d89942018-04-06 16:44:51 +0200376
Nils Diewald0e6992a2015-04-14 20:13:52 +0000377 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200378 var m = match.replace(/^match:/, '');
Akron31d89942018-04-06 16:44:51 +0200379
Akrone4961b12017-05-10 21:04:46 +0200380 if (
Akron31d89942018-04-06 16:44:51 +0200381 (this._type == undefined) // && KorAP._validUnspecMatchRE.test(m))
Akrone4961b12017-05-10 21:04:46 +0200382 ||
383 (
384 (this._type === 'string' || this._type === 'regex') &&
385 KorAP._validStringMatchRE.test(m)
386 )
387 ||
Akron31d89942018-04-06 16:44:51 +0200388 (this._type === 'text' && KorAP._validTextMatchRE.test(m))
389 ||
Akrone4961b12017-05-10 21:04:46 +0200390 (this._type === 'date' && KorAP._validDateMatchRE.test(m))
391 ) {
392 this._matchop = m;
393 }
394 else {
395 this._matchop = "eq";
396 };
Nils Diewald4c221252015-04-21 20:19:25 +0000397
Akrone4961b12017-05-10 21:04:46 +0200398 this._changed();
Akron31d89942018-04-06 16:44:51 +0200399 return this
Nils Diewald0e6992a2015-04-14 20:13:52 +0000400 };
401 return this._matchop || "eq";
402 },
403
Nils Diewald4c221252015-04-21 20:19:25 +0000404
405 // Click on the match operator, show me the menu
406 _changeMatchop : function (e) {
407 var menu = KorAP._vcMatchopMenu[this.type()];
408
409 if (menu === undefined) {
Akrone4961b12017-05-10 21:04:46 +0200410 KorAP.log(0, "Unable to init menu for " + this.type());
411 return;
Nils Diewald4c221252015-04-21 20:19:25 +0000412 };
413
414 // Insert menu
415 this._element.insertBefore(
Akrone4961b12017-05-10 21:04:46 +0200416 menu.element(),
417 this._matchopE
Nils Diewald4c221252015-04-21 20:19:25 +0000418 );
419
420 // Release event
421 var that = this;
422 menu.released(function (mo) {
Akrone4961b12017-05-10 21:04:46 +0200423 that.matchop(mo).update();
424 this.hide();
Nils Diewald4c221252015-04-21 20:19:25 +0000425 });
426
427 menu.show();
428 menu.focus();
429 },
430
431
432 /**
433 * Get or set the type
434 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000435 type : function (type) {
436 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200437 this._type = type;
438 this._changed();
439 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000440 };
441 return this._type || "string";
442 },
443
Nils Diewald4c221252015-04-21 20:19:25 +0000444
445 /**
446 * Get or set the value
447 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000448 value : function (value) {
449 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200450 if (this._type === 'date' && !KorAP._validDateRE.test(value)) {
451 delete this._value;
452 }
453 else {
454 this._value = value;
455 };
456 this._changed();
457 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000458 };
459 return this._value;
460 },
461
Nils Diewald4c221252015-04-21 20:19:25 +0000462
463 // Click on the match operator, show me the menu
464 _changeValue : function (e) {
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000465 var v = this.value();
466 var that = this;
Nils Diewald87507832015-05-01 23:36:41 +0000467
Nils Diewald7148c6f2015-05-04 15:07:53 +0000468 // Show datepicker
Nils Diewald87507832015-05-01 23:36:41 +0000469 if (this.type() === 'date') {
Akrone4961b12017-05-10 21:04:46 +0200470 var dp = KorAP._vcDatePicker;
Akron516b6f92018-01-03 17:46:59 +0100471 dp.fromString(v);
Nils Diewald87507832015-05-01 23:36:41 +0000472
Akrone4961b12017-05-10 21:04:46 +0200473 // Todo: change this
474 dp.onclick(function (selected) {
Nils Diewald87507832015-05-01 23:36:41 +0000475
Akrone4961b12017-05-10 21:04:46 +0200476 // There are values selected
477 if (selected['year']) {
478 that.value(this.toString());
479 that.update();
480 return;
481 };
Nils Diewald87507832015-05-01 23:36:41 +0000482
Akrone4961b12017-05-10 21:04:46 +0200483 // Remove datepicker
484 that._element.removeChild(
485 dp.element()
486 );
487 });
Nils Diewald87507832015-05-01 23:36:41 +0000488
Akrone4961b12017-05-10 21:04:46 +0200489 // Get element of the date picker
490 var dpElem = dp.show();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000491
Akrone4961b12017-05-10 21:04:46 +0200492 this._element.insertBefore(
493 dpElem,
494 this._valueE
495 );
Nils Diewald7148c6f2015-05-04 15:07:53 +0000496
Akron516b6f92018-01-03 17:46:59 +0100497 dp.input().focus();
Nils Diewald87507832015-05-01 23:36:41 +0000498 }
499 else {
Akrone4961b12017-05-10 21:04:46 +0200500 var regex = this.type() === 'regex' ? true : false;
501 var str = stringValClass.create(this.value(), regex);
502 var strElem = str.element();
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000503
Akrone4961b12017-05-10 21:04:46 +0200504 str.store = function (value, regex) {
505 that.value(value);
506 if (regex === true)
507 that.type('regex');
508 else
509 that.type('string');
510
511 that._element.removeChild(
512 this._element
513 );
514 that.update();
515 };
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000516
Akrone4961b12017-05-10 21:04:46 +0200517 // Insert element
518 this._element.insertBefore(
519 strElem,
520 this._valueE
521 );
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000522
Akrone4961b12017-05-10 21:04:46 +0200523 str.focus();
Nils Diewald87507832015-05-01 23:36:41 +0000524 };
Nils Diewald4c221252015-04-21 20:19:25 +0000525 },
526
527
Nils Diewald0e6992a2015-04-14 20:13:52 +0000528 rewrites : function () {
529 return this._rewrites;
530 },
531
Akronea4e9052017-07-06 16:12:05 +0200532 rewrite : function (value) {
533 if (typeof value === 'string') {
534 value = [{
535 "@type" : "koral:rewrite",
536 "operation" : "operation:" + value,
537 "src" : "Kalamar"
538 }];
539 };
540 this._rewrites = rewriteListClass.create(value);
541 },
542
Akron31d89942018-04-06 16:44:51 +0200543 // Mark the underlying data as being changed.
544 // This is important for rerendering the dom.
545 // This will also remove rewrite markers, when the data
546 // change happened by the user
Nils Diewald0e6992a2015-04-14 20:13:52 +0000547 _changed : function () {
548 this.__changed = true;
Nils Diewald4347ee92015-05-04 20:32:48 +0000549
Nils Diewald0e6992a2015-04-14 20:13:52 +0000550 if (this._rewrites === undefined)
Akrone4961b12017-05-10 21:04:46 +0200551 return;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000552
Akronea4e9052017-07-06 16:12:05 +0200553 delete this["_rewrites"];
Nils Diewald0e6992a2015-04-14 20:13:52 +0000554
555 if (this._element === undefined)
Akrone4961b12017-05-10 21:04:46 +0200556 return;
Akronea4e9052017-07-06 16:12:05 +0200557
Nils Diewald0e6992a2015-04-14 20:13:52 +0000558 this._element.classList.remove("rewritten");
559 },
560
Nils Diewald4c221252015-04-21 20:19:25 +0000561
Nils Diewald0e6992a2015-04-14 20:13:52 +0000562 toJson : function () {
563 if (!this.matchop() || !this.key())
Akrone4961b12017-05-10 21:04:46 +0200564 return {};
Nils Diewald0e6992a2015-04-14 20:13:52 +0000565
566 return {
Akrone4961b12017-05-10 21:04:46 +0200567 "@type" : "koral:" + this.ldType(),
568 "key" : this.key(),
569 "match" : "match:" + this.matchop(),
570 "value" : this.value() || '',
571 "type" : "type:" + this.type()
Nils Diewald0e6992a2015-04-14 20:13:52 +0000572 };
573 },
574
Nils Diewald4c221252015-04-21 20:19:25 +0000575
Nils Diewald0e6992a2015-04-14 20:13:52 +0000576 toQuery : function () {
577 if (!this.matchop() || !this.key())
Akrone4961b12017-05-10 21:04:46 +0200578 return "";
Nils Diewald0e6992a2015-04-14 20:13:52 +0000579
580 // Build doc string based on key
581 var string = this.key() + ' ';
582
583 // Add match operator
584 switch (this.matchop()) {
585 case "ne":
Akrone4961b12017-05-10 21:04:46 +0200586 string += '!=';
587 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000588 case "contains":
Akrone4961b12017-05-10 21:04:46 +0200589 string += '~';
590 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000591 case "excludes":
Akrone4961b12017-05-10 21:04:46 +0200592 string += '!~';
593 break;
Akron6a535d42015-08-26 20:16:58 +0200594 case "containsnot":
Akrone4961b12017-05-10 21:04:46 +0200595 string += '!~';
596 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000597 case "geq":
Akrone4961b12017-05-10 21:04:46 +0200598 string += 'since';
599 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000600 case "leq":
Akrone4961b12017-05-10 21:04:46 +0200601 string += 'until';
602 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000603 default:
Akrone4961b12017-05-10 21:04:46 +0200604 string += (this.type() == 'date') ? 'in' : '=';
605 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000606 };
607
608 string += ' ';
609
610 // Add value
611 switch (this.type()) {
612 case "date":
Akrone4961b12017-05-10 21:04:46 +0200613 return string + this.value();
614 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000615 case "regex":
Akron8778f5d2017-06-30 21:25:55 +0200616 return string + '/' + this.value().escapeRegex() + '/';
Akrone4961b12017-05-10 21:04:46 +0200617 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000618 case "string":
Akrone4961b12017-05-10 21:04:46 +0200619 return string + '"' + this.value().quote() + '"';
620 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000621 };
622
623 return "";
624 }
625 };
626});