blob: 41b72add5b211e9538eceafc470d7ceb6bae7929 [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
Akronddc98a72018-04-06 17:33:52 +0200206 // Type is unspecified - but may be known by the menu
207 if (json["type"] === undefined && KorAP._vcKeyMenu) {
208
209 // Check the VC list if the field is known
210 var type = KorAP._vcKeyMenu.typeOf(this.key());
211 if (type != undefined) {
212 json["type"] = "type:" + type;
213 };
214 };
215
216 // Type is still undefined
Akron31d89942018-04-06 16:44:51 +0200217 if (json["type"] === undefined) {
Akronddc98a72018-04-06 17:33:52 +0200218
Akron31d89942018-04-06 16:44:51 +0200219 // Check match type
220 if (!KorAP._validUnspecMatchRE.test(this.matchop())) {
221 KorAP.log(802, errstr802);
222
223 // Rewrite method
224 this.matchop('eq');
225 rewrite = 'modification';
226 };
227
228 // Set string value
229 this.value(json["value"]);
230 }
231
232 // Field is string type
233 else if (json["type"] == "type:string") {
Akrone4961b12017-05-10 21:04:46 +0200234 this.type("string");
Nils Diewald0e6992a2015-04-14 20:13:52 +0000235
Akrone4961b12017-05-10 21:04:46 +0200236 // Check match type
237 if (!KorAP._validStringMatchRE.test(this.matchop())) {
Akron31d89942018-04-06 16:44:51 +0200238 KorAP.log(802, errstr802);
239
240 // Rewrite method
241 this.matchop('eq');
242 rewrite = 'modification';
243 };
244
245 // Set string value
246 this.value(json["value"]);
247 }
248
249 // Field is specified
250 else if (json["type"] == "type:text") {
251 this.type("text");
252
253 // Check match type
254 if (!KorAP._validTextMatchRE.test(this.matchop())) {
255 KorAP.log(802, errstr802);
Akronea4e9052017-07-06 16:12:05 +0200256
257 // Rewrite method
258 this.matchop('eq');
259 rewrite = 'modification';
Akrone4961b12017-05-10 21:04:46 +0200260 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000261
Akrone4961b12017-05-10 21:04:46 +0200262 // Set string value
263 this.value(json["value"]);
264 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000265
Akrone4961b12017-05-10 21:04:46 +0200266 // Key is a date
267 else if (json["type"] === "type:date") {
268 this.type("date");
Nils Diewald0e6992a2015-04-14 20:13:52 +0000269
Akrone4961b12017-05-10 21:04:46 +0200270 if (json["value"] !== undefined &&
271 KorAP._validDateRE.test(json["value"])) {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000272
Akrone4961b12017-05-10 21:04:46 +0200273 if (!KorAP._validDateMatchRE.test(this.matchop())) {
Akron31d89942018-04-06 16:44:51 +0200274 KorAP.log(802, errstr802);
Akronea4e9052017-07-06 16:12:05 +0200275
276 // Rewrite method
277 this.matchop('eq');
278 rewrite = 'modification';
Akrone4961b12017-05-10 21:04:46 +0200279 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000280
Akrone4961b12017-05-10 21:04:46 +0200281 // Set value
282 this.value(json["value"]);
283 }
284 else {
285 KorAP.log(806, "Value is not a valid date string");
286 return;
287 };
288 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000289
Akrone4961b12017-05-10 21:04:46 +0200290 // Key is a regular expression
291 else if (json["type"] === "type:regex") {
292 this.type("regex");
Nils Diewald0e6992a2015-04-14 20:13:52 +0000293
Akrone4961b12017-05-10 21:04:46 +0200294 try {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000295
Akrone4961b12017-05-10 21:04:46 +0200296 // Try to create a regular expression
297 var check = new RegExp(json["value"]);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000298
Akrone65a88a2018-04-05 19:14:20 +0200299 if (!KorAP._validStringMatchRE.test(this.matchop())) {
Akron31d89942018-04-06 16:44:51 +0200300 KorAP.log(802, errstr802);
Akronea4e9052017-07-06 16:12:05 +0200301
302 // Rewrite method
303 this.matchop('eq');
304 rewrite = 'modification';
Akrone4961b12017-05-10 21:04:46 +0200305 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000306
Akrone4961b12017-05-10 21:04:46 +0200307 this.value(json["value"]);
308 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000309
Akrone4961b12017-05-10 21:04:46 +0200310 catch (e) {
311 KorAP.log(807, "Value is not a valid regular expression");
312 return;
313 };
314 this.type("regex");
315 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000316
Akrone4961b12017-05-10 21:04:46 +0200317 else {
318 KorAP.log(804, "Unknown value type");
319 return;
320 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000321 };
322
Akronea4e9052017-07-06 16:12:05 +0200323 // Rewrite coming from the server
Nils Diewald0e6992a2015-04-14 20:13:52 +0000324 if (json["rewrites"] !== undefined) {
Akronea4e9052017-07-06 16:12:05 +0200325 this.rewrite(json["rewrites"]);
326 }
327
328 // Rewrite coming from Kalamar
329 else if (rewrite !== undefined) {
330 this.rewrite(rewrite);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000331 };
Akronea4e9052017-07-06 16:12:05 +0200332
Nils Diewald0e6992a2015-04-14 20:13:52 +0000333 return this;
334 },
335
Nils Diewald4c221252015-04-21 20:19:25 +0000336 /**
337 * Get or set the key
338 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000339 key : function (value) {
340 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200341 this._key = value;
342 this._changed();
343 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000344 };
345 return this._key;
346 },
347
Nils Diewald4c221252015-04-21 20:19:25 +0000348 // Click on the key, show me the menu
349 _changeKey : function (e) {
350 var menu = KorAP._vcKeyMenu;
351
352 // Insert menu
353 this._element.insertBefore(
Akrone4961b12017-05-10 21:04:46 +0200354 menu.element(),
355 this._keyE
Nils Diewald4c221252015-04-21 20:19:25 +0000356 );
357
358 // Release event
359 var that = this;
360 menu.released(function (key, type) {
Akrone4961b12017-05-10 21:04:46 +0200361 var doc = that.key(key).type(type);
Nils Diewald4c221252015-04-21 20:19:25 +0000362
Akrone4961b12017-05-10 21:04:46 +0200363 // This may not be compatible - then switch to default
364 doc.matchop(doc.matchop());
365 doc.value(doc.value());
Nils Diewald4c221252015-04-21 20:19:25 +0000366
Akrone4961b12017-05-10 21:04:46 +0200367 // Update the doc
368 doc.update();
Nils Diewald4c221252015-04-21 20:19:25 +0000369
Akrone4961b12017-05-10 21:04:46 +0200370 // hide!
371 this.hide();
Nils Diewald4c221252015-04-21 20:19:25 +0000372 });
373
374 // TODO: Highlight the old value!
375 menu.show();
376 menu.focus();
377 },
378
379 /**
380 * Get or set the match operator
381 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000382 matchop : function (match) {
Akron31d89942018-04-06 16:44:51 +0200383
Nils Diewald0e6992a2015-04-14 20:13:52 +0000384 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200385 var m = match.replace(/^match:/, '');
Akron31d89942018-04-06 16:44:51 +0200386
Akrone4961b12017-05-10 21:04:46 +0200387 if (
Akron31d89942018-04-06 16:44:51 +0200388 (this._type == undefined) // && KorAP._validUnspecMatchRE.test(m))
Akrone4961b12017-05-10 21:04:46 +0200389 ||
390 (
391 (this._type === 'string' || this._type === 'regex') &&
392 KorAP._validStringMatchRE.test(m)
393 )
394 ||
Akron31d89942018-04-06 16:44:51 +0200395 (this._type === 'text' && KorAP._validTextMatchRE.test(m))
396 ||
Akrone4961b12017-05-10 21:04:46 +0200397 (this._type === 'date' && KorAP._validDateMatchRE.test(m))
398 ) {
399 this._matchop = m;
400 }
401 else {
402 this._matchop = "eq";
403 };
Nils Diewald4c221252015-04-21 20:19:25 +0000404
Akrone4961b12017-05-10 21:04:46 +0200405 this._changed();
Akron31d89942018-04-06 16:44:51 +0200406 return this
Nils Diewald0e6992a2015-04-14 20:13:52 +0000407 };
408 return this._matchop || "eq";
409 },
410
Nils Diewald4c221252015-04-21 20:19:25 +0000411
412 // Click on the match operator, show me the menu
413 _changeMatchop : function (e) {
414 var menu = KorAP._vcMatchopMenu[this.type()];
415
416 if (menu === undefined) {
Akrone4961b12017-05-10 21:04:46 +0200417 KorAP.log(0, "Unable to init menu for " + this.type());
418 return;
Nils Diewald4c221252015-04-21 20:19:25 +0000419 };
420
421 // Insert menu
422 this._element.insertBefore(
Akrone4961b12017-05-10 21:04:46 +0200423 menu.element(),
424 this._matchopE
Nils Diewald4c221252015-04-21 20:19:25 +0000425 );
426
427 // Release event
428 var that = this;
429 menu.released(function (mo) {
Akrone4961b12017-05-10 21:04:46 +0200430 that.matchop(mo).update();
431 this.hide();
Nils Diewald4c221252015-04-21 20:19:25 +0000432 });
433
434 menu.show();
435 menu.focus();
436 },
437
438
439 /**
440 * Get or set the type
441 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000442 type : function (type) {
443 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200444 this._type = type;
445 this._changed();
446 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000447 };
448 return this._type || "string";
449 },
450
Nils Diewald4c221252015-04-21 20:19:25 +0000451
452 /**
453 * Get or set the value
454 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000455 value : function (value) {
456 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200457 if (this._type === 'date' && !KorAP._validDateRE.test(value)) {
458 delete this._value;
459 }
460 else {
461 this._value = value;
462 };
463 this._changed();
464 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000465 };
466 return this._value;
467 },
468
Nils Diewald4c221252015-04-21 20:19:25 +0000469
470 // Click on the match operator, show me the menu
471 _changeValue : function (e) {
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000472 var v = this.value();
473 var that = this;
Nils Diewald87507832015-05-01 23:36:41 +0000474
Nils Diewald7148c6f2015-05-04 15:07:53 +0000475 // Show datepicker
Nils Diewald87507832015-05-01 23:36:41 +0000476 if (this.type() === 'date') {
Akrone4961b12017-05-10 21:04:46 +0200477 var dp = KorAP._vcDatePicker;
Akron516b6f92018-01-03 17:46:59 +0100478 dp.fromString(v);
Nils Diewald87507832015-05-01 23:36:41 +0000479
Akrone4961b12017-05-10 21:04:46 +0200480 // Todo: change this
481 dp.onclick(function (selected) {
Nils Diewald87507832015-05-01 23:36:41 +0000482
Akrone4961b12017-05-10 21:04:46 +0200483 // There are values selected
484 if (selected['year']) {
485 that.value(this.toString());
486 that.update();
487 return;
488 };
Nils Diewald87507832015-05-01 23:36:41 +0000489
Akrone4961b12017-05-10 21:04:46 +0200490 // Remove datepicker
491 that._element.removeChild(
492 dp.element()
493 );
494 });
Nils Diewald87507832015-05-01 23:36:41 +0000495
Akrone4961b12017-05-10 21:04:46 +0200496 // Get element of the date picker
497 var dpElem = dp.show();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000498
Akrone4961b12017-05-10 21:04:46 +0200499 this._element.insertBefore(
500 dpElem,
501 this._valueE
502 );
Nils Diewald7148c6f2015-05-04 15:07:53 +0000503
Akron516b6f92018-01-03 17:46:59 +0100504 dp.input().focus();
Nils Diewald87507832015-05-01 23:36:41 +0000505 }
506 else {
Akrone4961b12017-05-10 21:04:46 +0200507 var regex = this.type() === 'regex' ? true : false;
508 var str = stringValClass.create(this.value(), regex);
509 var strElem = str.element();
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000510
Akrone4961b12017-05-10 21:04:46 +0200511 str.store = function (value, regex) {
512 that.value(value);
513 if (regex === true)
514 that.type('regex');
515 else
516 that.type('string');
517
518 that._element.removeChild(
519 this._element
520 );
521 that.update();
522 };
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000523
Akrone4961b12017-05-10 21:04:46 +0200524 // Insert element
525 this._element.insertBefore(
526 strElem,
527 this._valueE
528 );
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000529
Akrone4961b12017-05-10 21:04:46 +0200530 str.focus();
Nils Diewald87507832015-05-01 23:36:41 +0000531 };
Nils Diewald4c221252015-04-21 20:19:25 +0000532 },
533
534
Nils Diewald0e6992a2015-04-14 20:13:52 +0000535 rewrites : function () {
536 return this._rewrites;
537 },
538
Akronea4e9052017-07-06 16:12:05 +0200539 rewrite : function (value) {
540 if (typeof value === 'string') {
541 value = [{
542 "@type" : "koral:rewrite",
543 "operation" : "operation:" + value,
544 "src" : "Kalamar"
545 }];
546 };
547 this._rewrites = rewriteListClass.create(value);
548 },
549
Akron31d89942018-04-06 16:44:51 +0200550 // Mark the underlying data as being changed.
551 // This is important for rerendering the dom.
552 // This will also remove rewrite markers, when the data
553 // change happened by the user
Nils Diewald0e6992a2015-04-14 20:13:52 +0000554 _changed : function () {
555 this.__changed = true;
Nils Diewald4347ee92015-05-04 20:32:48 +0000556
Nils Diewald0e6992a2015-04-14 20:13:52 +0000557 if (this._rewrites === undefined)
Akrone4961b12017-05-10 21:04:46 +0200558 return;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000559
Akronea4e9052017-07-06 16:12:05 +0200560 delete this["_rewrites"];
Nils Diewald0e6992a2015-04-14 20:13:52 +0000561
562 if (this._element === undefined)
Akrone4961b12017-05-10 21:04:46 +0200563 return;
Akronea4e9052017-07-06 16:12:05 +0200564
Nils Diewald0e6992a2015-04-14 20:13:52 +0000565 this._element.classList.remove("rewritten");
566 },
567
Nils Diewald4c221252015-04-21 20:19:25 +0000568
Nils Diewald0e6992a2015-04-14 20:13:52 +0000569 toJson : function () {
570 if (!this.matchop() || !this.key())
Akrone4961b12017-05-10 21:04:46 +0200571 return {};
Nils Diewald0e6992a2015-04-14 20:13:52 +0000572
573 return {
Akrone4961b12017-05-10 21:04:46 +0200574 "@type" : "koral:" + this.ldType(),
575 "key" : this.key(),
576 "match" : "match:" + this.matchop(),
577 "value" : this.value() || '',
578 "type" : "type:" + this.type()
Nils Diewald0e6992a2015-04-14 20:13:52 +0000579 };
580 },
581
Nils Diewald4c221252015-04-21 20:19:25 +0000582
Nils Diewald0e6992a2015-04-14 20:13:52 +0000583 toQuery : function () {
584 if (!this.matchop() || !this.key())
Akrone4961b12017-05-10 21:04:46 +0200585 return "";
Nils Diewald0e6992a2015-04-14 20:13:52 +0000586
587 // Build doc string based on key
588 var string = this.key() + ' ';
589
590 // Add match operator
591 switch (this.matchop()) {
592 case "ne":
Akrone4961b12017-05-10 21:04:46 +0200593 string += '!=';
594 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000595 case "contains":
Akrone4961b12017-05-10 21:04:46 +0200596 string += '~';
597 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000598 case "excludes":
Akrone4961b12017-05-10 21:04:46 +0200599 string += '!~';
600 break;
Akron6a535d42015-08-26 20:16:58 +0200601 case "containsnot":
Akrone4961b12017-05-10 21:04:46 +0200602 string += '!~';
603 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000604 case "geq":
Akrone4961b12017-05-10 21:04:46 +0200605 string += 'since';
606 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000607 case "leq":
Akrone4961b12017-05-10 21:04:46 +0200608 string += 'until';
609 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000610 default:
Akrone4961b12017-05-10 21:04:46 +0200611 string += (this.type() == 'date') ? 'in' : '=';
612 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000613 };
614
615 string += ' ';
616
617 // Add value
618 switch (this.type()) {
619 case "date":
Akrone4961b12017-05-10 21:04:46 +0200620 return string + this.value();
621 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000622 case "regex":
Akron8778f5d2017-06-30 21:25:55 +0200623 return string + '/' + this.value().escapeRegex() + '/';
Akrone4961b12017-05-10 21:04:46 +0200624 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000625 case "string":
Akrone4961b12017-05-10 21:04:46 +0200626 return string + '"' + this.value().quote() + '"';
627 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000628 };
629
630 return "";
631 }
632 };
633});