blob: 15c72873220e715fcf5ee5c05f1f5d2537f6f787 [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
65 // Set ref - TODO: Cleanup!
66 e.refTo = this;
67
Akron31d89942018-04-06 16:44:51 +020068 // Check if there is a change in the underlying data
Nils Diewald0e6992a2015-04-14 20:13:52 +000069 if (this.__changed) {
70
Akrone4961b12017-05-10 21:04:46 +020071 // Was rewritten
72 if (this.rewrites() !== undefined) {
73 e.classList.add("rewritten");
74 };
Nils Diewald0e6992a2015-04-14 20:13:52 +000075
Akrone4961b12017-05-10 21:04:46 +020076 // Added key
77 this._keyE = document.createElement('span');
78 this._keyE.setAttribute('class', 'key');
Nils Diewald0e6992a2015-04-14 20:13:52 +000079
Akrone4961b12017-05-10 21:04:46 +020080 // Change key
81 this._keyE.addEventListener('click', this._changeKey.bind(this));
Nils Diewald0e6992a2015-04-14 20:13:52 +000082
Akrone4961b12017-05-10 21:04:46 +020083 if (this.key()) {
84 var k = this.key();
85 if (loc['VC_' + k] !== undefined)
86 k = loc['VC_' + k];
Akron0b489ad2018-02-02 16:49:32 +010087 this._keyE.addT(k);
Akrone4961b12017-05-10 21:04:46 +020088 };
Nils Diewald4c221252015-04-21 20:19:25 +000089
Akrone4961b12017-05-10 21:04:46 +020090 // Added match operator
91 this._matchopE = document.createElement('span');
92 this._matchopE.setAttribute('data-type', this.type());
93 this._matchopE.setAttribute('class', 'match');
Akron0b489ad2018-02-02 16:49:32 +010094 this._matchopE.addT(this.matchop());
Nils Diewald0e6992a2015-04-14 20:13:52 +000095
Akrone4961b12017-05-10 21:04:46 +020096 // Change matchop
97 this._matchopE.addEventListener(
98 'click',
99 this._changeMatchop.bind(this)
100 );
Nils Diewald4c221252015-04-21 20:19:25 +0000101
Akrone4961b12017-05-10 21:04:46 +0200102 // Added value operator
103 this._valueE = document.createElement('span');
104 this._valueE.setAttribute('data-type', this.type());
105 this._valueE.setAttribute('class', 'value');
106 if (this.value()) {
Akron0b489ad2018-02-02 16:49:32 +0100107 this._valueE.addT(this.value());
Akrone4961b12017-05-10 21:04:46 +0200108 }
109 else {
Akron0b489ad2018-02-02 16:49:32 +0100110 this._valueE.addT(loc.EMPTY);
Akrone4961b12017-05-10 21:04:46 +0200111 };
Nils Diewald4c221252015-04-21 20:19:25 +0000112
Akrone4961b12017-05-10 21:04:46 +0200113 // Change value
114 this._valueE.addEventListener(
115 'click',
116 this._changeValue.bind(this)
117 );
Nils Diewald4c221252015-04-21 20:19:25 +0000118
Nils Diewald0e6992a2015-04-14 20:13:52 +0000119
Akrone4961b12017-05-10 21:04:46 +0200120 // Remove all element children
121 _removeChildren(e);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000122
Akrone4961b12017-05-10 21:04:46 +0200123 // Add spans
124 e.appendChild(this._keyE);
125 e.appendChild(this._matchopE);
126 e.appendChild(this._valueE);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000127
Akrone4961b12017-05-10 21:04:46 +0200128 this.__changed = false;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000129 };
130
131 if (this._rewrites !== undefined) {
Akrone4961b12017-05-10 21:04:46 +0200132 e.appendChild(this._rewrites.element());
Nils Diewald0e6992a2015-04-14 20:13:52 +0000133 };
134
135 if (this._parent !== undefined) {
Akrone4961b12017-05-10 21:04:46 +0200136 // Set operators
137 var op = this.operators(
138 true,
139 true,
140 true
141 );
Nils Diewald0e6992a2015-04-14 20:13:52 +0000142
Akrone4961b12017-05-10 21:04:46 +0200143 // Append new operators
144 e.appendChild(op.element());
Nils Diewald0e6992a2015-04-14 20:13:52 +0000145 };
146
147 return e;
148 },
149
Nils Diewald4c221252015-04-21 20:19:25 +0000150
151 /**
152 * Get the associated element
153 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000154 element : function () {
155 if (this._element !== undefined)
Akrone4961b12017-05-10 21:04:46 +0200156 return this._element;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000157
158 this._element = document.createElement('div');
159 this._element.setAttribute('class', 'doc');
160
161 this.update();
162 return this._element;
163 },
164
Nils Diewald4c221252015-04-21 20:19:25 +0000165 /**
166 * Wrap a new operation around the doc element
167 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000168 wrap : function (op) {
169 var parent = this.parent();
Nils Diewald7c8ced22015-04-15 19:21:00 +0000170 var group = require('vc/docgroup').create(parent);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000171 group.operation(op);
172 group.append(this);
173 group.append();
174 return parent.replaceOperand(this, group).update();
175 },
176
Nils Diewald4c221252015-04-21 20:19:25 +0000177 /**
178 * Deserialize from json
179 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000180 fromJson : function (json) {
181 if (json === undefined)
Akrone4961b12017-05-10 21:04:46 +0200182 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000183
184 if (json["@type"] === undefined) {
Akrone4961b12017-05-10 21:04:46 +0200185 KorAP.log(701, "JSON-LD group has no @type attribute");
186 return;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000187 };
188
189 if (json["value"] === undefined ||
Akrone4961b12017-05-10 21:04:46 +0200190 typeof json["value"] != 'string') {
191 KorAP.log(805, "Value is invalid");
192 return;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000193 };
194
Akronea4e9052017-07-06 16:12:05 +0200195 var rewrite;
196
Nils Diewald0e6992a2015-04-14 20:13:52 +0000197 // There is a defined key
198 if (json["key"] !== undefined &&
Akrone4961b12017-05-10 21:04:46 +0200199 typeof json["key"] === 'string') {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000200
Akrone4961b12017-05-10 21:04:46 +0200201 // Set key
202 this.key(json["key"]);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000203
Akrone4961b12017-05-10 21:04:46 +0200204 // Set match operation
205 if (json["match"] !== undefined) {
206 if (typeof json["match"] === 'string') {
Akron31d89942018-04-06 16:44:51 +0200207
Akrone4961b12017-05-10 21:04:46 +0200208 this.matchop(json["match"]);
209 }
210 else {
Akron31d89942018-04-06 16:44:51 +0200211 KorAP.log(802, errstr802);
Akrone4961b12017-05-10 21:04:46 +0200212 return;
213 };
214 };
Akron31d89942018-04-06 16:44:51 +0200215
Akronddc98a72018-04-06 17:33:52 +0200216 // Type is unspecified - but may be known by the menu
217 if (json["type"] === undefined && KorAP._vcKeyMenu) {
218
219 // Check the VC list if the field is known
220 var type = KorAP._vcKeyMenu.typeOf(this.key());
221 if (type != undefined) {
222 json["type"] = "type:" + type;
223 };
224 };
225
226 // Type is still undefined
Akron31d89942018-04-06 16:44:51 +0200227 if (json["type"] === undefined) {
Akronddc98a72018-04-06 17:33:52 +0200228
Akron31d89942018-04-06 16:44:51 +0200229 // Check match type
230 if (!KorAP._validUnspecMatchRE.test(this.matchop())) {
231 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 string type
243 else if (json["type"] == "type:string") {
Akrone4961b12017-05-10 21:04:46 +0200244 this.type("string");
Nils Diewald0e6992a2015-04-14 20:13:52 +0000245
Akrone4961b12017-05-10 21:04:46 +0200246 // Check match type
247 if (!KorAP._validStringMatchRE.test(this.matchop())) {
Akron31d89942018-04-06 16:44:51 +0200248 KorAP.log(802, errstr802);
249
250 // Rewrite method
251 this.matchop('eq');
252 rewrite = 'modification';
253 };
254
255 // Set string value
256 this.value(json["value"]);
257 }
258
259 // Field is specified
260 else if (json["type"] == "type:text") {
261 this.type("text");
262
263 // Check match type
264 if (!KorAP._validTextMatchRE.test(this.matchop())) {
265 KorAP.log(802, errstr802);
Akronea4e9052017-07-06 16:12:05 +0200266
267 // Rewrite method
268 this.matchop('eq');
269 rewrite = 'modification';
Akrone4961b12017-05-10 21:04:46 +0200270 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000271
Akrone4961b12017-05-10 21:04:46 +0200272 // Set string value
273 this.value(json["value"]);
274 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000275
Akrone4961b12017-05-10 21:04:46 +0200276 // Key is a date
277 else if (json["type"] === "type:date") {
278 this.type("date");
Nils Diewald0e6992a2015-04-14 20:13:52 +0000279
Akrone4961b12017-05-10 21:04:46 +0200280 if (json["value"] !== undefined &&
281 KorAP._validDateRE.test(json["value"])) {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000282
Akrone4961b12017-05-10 21:04:46 +0200283 if (!KorAP._validDateMatchRE.test(this.matchop())) {
Akron31d89942018-04-06 16:44:51 +0200284 KorAP.log(802, errstr802);
Akronea4e9052017-07-06 16:12:05 +0200285
286 // Rewrite method
287 this.matchop('eq');
288 rewrite = 'modification';
Akrone4961b12017-05-10 21:04:46 +0200289 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000290
Akrone4961b12017-05-10 21:04:46 +0200291 // Set value
292 this.value(json["value"]);
293 }
294 else {
295 KorAP.log(806, "Value is not a valid date string");
296 return;
297 };
298 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000299
Akrone4961b12017-05-10 21:04:46 +0200300 // Key is a regular expression
301 else if (json["type"] === "type:regex") {
302 this.type("regex");
Nils Diewald0e6992a2015-04-14 20:13:52 +0000303
Akrone4961b12017-05-10 21:04:46 +0200304 try {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000305
Akrone4961b12017-05-10 21:04:46 +0200306 // Try to create a regular expression
307 var check = new RegExp(json["value"]);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000308
Akrone65a88a2018-04-05 19:14:20 +0200309 if (!KorAP._validStringMatchRE.test(this.matchop())) {
Akron31d89942018-04-06 16:44:51 +0200310 KorAP.log(802, errstr802);
Akronea4e9052017-07-06 16:12:05 +0200311
312 // Rewrite method
313 this.matchop('eq');
314 rewrite = 'modification';
Akrone4961b12017-05-10 21:04:46 +0200315 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000316
Akrone4961b12017-05-10 21:04:46 +0200317 this.value(json["value"]);
318 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000319
Akrone4961b12017-05-10 21:04:46 +0200320 catch (e) {
321 KorAP.log(807, "Value is not a valid regular expression");
322 return;
323 };
324 this.type("regex");
325 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000326
Akrone4961b12017-05-10 21:04:46 +0200327 else {
Akron1bdf5272018-07-24 18:51:30 +0200328 KorAP.log(804, errstr804 + ": " + this.type());
329 throw new Error(errstr804 + ": " + this.type());
Akrone4961b12017-05-10 21:04:46 +0200330 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000331 };
332
Akronea4e9052017-07-06 16:12:05 +0200333 // Rewrite coming from the server
Nils Diewald0e6992a2015-04-14 20:13:52 +0000334 if (json["rewrites"] !== undefined) {
Akronea4e9052017-07-06 16:12:05 +0200335 this.rewrite(json["rewrites"]);
336 }
337
338 // Rewrite coming from Kalamar
339 else if (rewrite !== undefined) {
340 this.rewrite(rewrite);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000341 };
Akronea4e9052017-07-06 16:12:05 +0200342
Nils Diewald0e6992a2015-04-14 20:13:52 +0000343 return this;
344 },
345
Nils Diewald4c221252015-04-21 20:19:25 +0000346 /**
347 * Get or set the key
348 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000349 key : function (value) {
350 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200351 this._key = value;
352 this._changed();
353 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000354 };
355 return this._key;
356 },
357
Nils Diewald4c221252015-04-21 20:19:25 +0000358 // Click on the key, show me the menu
359 _changeKey : function (e) {
360 var menu = KorAP._vcKeyMenu;
361
362 // Insert menu
363 this._element.insertBefore(
Akrone4961b12017-05-10 21:04:46 +0200364 menu.element(),
365 this._keyE
Nils Diewald4c221252015-04-21 20:19:25 +0000366 );
367
368 // Release event
369 var that = this;
370 menu.released(function (key, type) {
Akrone4961b12017-05-10 21:04:46 +0200371 var doc = that.key(key).type(type);
Nils Diewald4c221252015-04-21 20:19:25 +0000372
Akrone4961b12017-05-10 21:04:46 +0200373 // This may not be compatible - then switch to default
374 doc.matchop(doc.matchop());
375 doc.value(doc.value());
Nils Diewald4c221252015-04-21 20:19:25 +0000376
Akrone4961b12017-05-10 21:04:46 +0200377 // Update the doc
378 doc.update();
Nils Diewald4c221252015-04-21 20:19:25 +0000379
Akrone4961b12017-05-10 21:04:46 +0200380 // hide!
381 this.hide();
Nils Diewald4c221252015-04-21 20:19:25 +0000382 });
383
384 // TODO: Highlight the old value!
385 menu.show();
386 menu.focus();
387 },
388
389 /**
390 * Get or set the match operator
391 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000392 matchop : function (match) {
Akron31d89942018-04-06 16:44:51 +0200393
Nils Diewald0e6992a2015-04-14 20:13:52 +0000394 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200395 var m = match.replace(/^match:/, '');
Akron31d89942018-04-06 16:44:51 +0200396
Akrone4961b12017-05-10 21:04:46 +0200397 if (
Akron31d89942018-04-06 16:44:51 +0200398 (this._type == undefined) // && KorAP._validUnspecMatchRE.test(m))
Akrone4961b12017-05-10 21:04:46 +0200399 ||
400 (
401 (this._type === 'string' || this._type === 'regex') &&
402 KorAP._validStringMatchRE.test(m)
403 )
404 ||
Akron31d89942018-04-06 16:44:51 +0200405 (this._type === 'text' && KorAP._validTextMatchRE.test(m))
406 ||
Akrone4961b12017-05-10 21:04:46 +0200407 (this._type === 'date' && KorAP._validDateMatchRE.test(m))
408 ) {
409 this._matchop = m;
410 }
411 else {
412 this._matchop = "eq";
413 };
Nils Diewald4c221252015-04-21 20:19:25 +0000414
Akrone4961b12017-05-10 21:04:46 +0200415 this._changed();
Akron31d89942018-04-06 16:44:51 +0200416 return this
Nils Diewald0e6992a2015-04-14 20:13:52 +0000417 };
418 return this._matchop || "eq";
419 },
420
Nils Diewald4c221252015-04-21 20:19:25 +0000421
422 // Click on the match operator, show me the menu
423 _changeMatchop : function (e) {
424 var menu = KorAP._vcMatchopMenu[this.type()];
425
426 if (menu === undefined) {
Akrone4961b12017-05-10 21:04:46 +0200427 KorAP.log(0, "Unable to init menu for " + this.type());
428 return;
Nils Diewald4c221252015-04-21 20:19:25 +0000429 };
430
431 // Insert menu
432 this._element.insertBefore(
Akrone4961b12017-05-10 21:04:46 +0200433 menu.element(),
434 this._matchopE
Nils Diewald4c221252015-04-21 20:19:25 +0000435 );
436
437 // Release event
438 var that = this;
439 menu.released(function (mo) {
Akrone4961b12017-05-10 21:04:46 +0200440 that.matchop(mo).update();
441 this.hide();
Nils Diewald4c221252015-04-21 20:19:25 +0000442 });
443
444 menu.show();
445 menu.focus();
446 },
447
448
449 /**
450 * Get or set the type
451 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000452 type : function (type) {
453 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200454 this._type = type;
455 this._changed();
456 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000457 };
458 return this._type || "string";
459 },
460
Nils Diewald4c221252015-04-21 20:19:25 +0000461
462 /**
463 * Get or set the value
464 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000465 value : function (value) {
466 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200467 if (this._type === 'date' && !KorAP._validDateRE.test(value)) {
468 delete this._value;
469 }
470 else {
471 this._value = value;
472 };
473 this._changed();
474 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000475 };
476 return this._value;
477 },
478
Nils Diewald4c221252015-04-21 20:19:25 +0000479
480 // Click on the match operator, show me the menu
481 _changeValue : function (e) {
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000482 var v = this.value();
483 var that = this;
Akron8db5e3a2018-05-28 19:25:26 +0200484
Nils Diewald7148c6f2015-05-04 15:07:53 +0000485 // Show datepicker
Nils Diewald87507832015-05-01 23:36:41 +0000486 if (this.type() === 'date') {
Akrone4961b12017-05-10 21:04:46 +0200487 var dp = KorAP._vcDatePicker;
Akron516b6f92018-01-03 17:46:59 +0100488 dp.fromString(v);
Nils Diewald87507832015-05-01 23:36:41 +0000489
Akrone4961b12017-05-10 21:04:46 +0200490 // Todo: change this
491 dp.onclick(function (selected) {
Nils Diewald87507832015-05-01 23:36:41 +0000492
Akrone4961b12017-05-10 21:04:46 +0200493 // There are values selected
494 if (selected['year']) {
495 that.value(this.toString());
496 that.update();
497 return;
498 };
Nils Diewald87507832015-05-01 23:36:41 +0000499
Akrone4961b12017-05-10 21:04:46 +0200500 // Remove datepicker
501 that._element.removeChild(
502 dp.element()
503 );
504 });
Nils Diewald87507832015-05-01 23:36:41 +0000505
Akrone4961b12017-05-10 21:04:46 +0200506 // Get element of the date picker
507 var dpElem = dp.show();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000508
Akrone4961b12017-05-10 21:04:46 +0200509 this._element.insertBefore(
510 dpElem,
511 this._valueE
512 );
Nils Diewald7148c6f2015-05-04 15:07:53 +0000513
Akron516b6f92018-01-03 17:46:59 +0100514 dp.input().focus();
Nils Diewald87507832015-05-01 23:36:41 +0000515 }
516 else {
Akrone4961b12017-05-10 21:04:46 +0200517 var regex = this.type() === 'regex' ? true : false;
518 var str = stringValClass.create(this.value(), regex);
519 var strElem = str.element();
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000520
Akrone4961b12017-05-10 21:04:46 +0200521 str.store = function (value, regex) {
522 that.value(value);
523 if (regex === true)
524 that.type('regex');
525 else
526 that.type('string');
527
528 that._element.removeChild(
529 this._element
530 );
531 that.update();
532 };
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000533
Akrone4961b12017-05-10 21:04:46 +0200534 // Insert element
535 this._element.insertBefore(
536 strElem,
537 this._valueE
538 );
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000539
Akrone4961b12017-05-10 21:04:46 +0200540 str.focus();
Nils Diewald87507832015-05-01 23:36:41 +0000541 };
Nils Diewald4c221252015-04-21 20:19:25 +0000542 },
543
544
Nils Diewald0e6992a2015-04-14 20:13:52 +0000545 rewrites : function () {
546 return this._rewrites;
547 },
548
Akronea4e9052017-07-06 16:12:05 +0200549 rewrite : function (value) {
550 if (typeof value === 'string') {
551 value = [{
552 "@type" : "koral:rewrite",
553 "operation" : "operation:" + value,
554 "src" : "Kalamar"
555 }];
556 };
557 this._rewrites = rewriteListClass.create(value);
558 },
559
Akron31d89942018-04-06 16:44:51 +0200560 // Mark the underlying data as being changed.
561 // This is important for rerendering the dom.
562 // This will also remove rewrite markers, when the data
563 // change happened by the user
Nils Diewald0e6992a2015-04-14 20:13:52 +0000564 _changed : function () {
565 this.__changed = true;
Nils Diewald4347ee92015-05-04 20:32:48 +0000566
Nils Diewald0e6992a2015-04-14 20:13:52 +0000567 if (this._rewrites === undefined)
Akrone4961b12017-05-10 21:04:46 +0200568 return;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000569
Akronea4e9052017-07-06 16:12:05 +0200570 delete this["_rewrites"];
Nils Diewald0e6992a2015-04-14 20:13:52 +0000571
572 if (this._element === undefined)
Akrone4961b12017-05-10 21:04:46 +0200573 return;
Akronea4e9052017-07-06 16:12:05 +0200574
Nils Diewald0e6992a2015-04-14 20:13:52 +0000575 this._element.classList.remove("rewritten");
576 },
577
Nils Diewald4c221252015-04-21 20:19:25 +0000578
Nils Diewald0e6992a2015-04-14 20:13:52 +0000579 toJson : function () {
580 if (!this.matchop() || !this.key())
Akrone4961b12017-05-10 21:04:46 +0200581 return {};
Nils Diewald0e6992a2015-04-14 20:13:52 +0000582
583 return {
Akrone4961b12017-05-10 21:04:46 +0200584 "@type" : "koral:" + this.ldType(),
585 "key" : this.key(),
586 "match" : "match:" + this.matchop(),
587 "value" : this.value() || '',
588 "type" : "type:" + this.type()
Nils Diewald0e6992a2015-04-14 20:13:52 +0000589 };
590 },
591
Nils Diewald4c221252015-04-21 20:19:25 +0000592
Nils Diewald0e6992a2015-04-14 20:13:52 +0000593 toQuery : function () {
594 if (!this.matchop() || !this.key())
Akrone4961b12017-05-10 21:04:46 +0200595 return "";
Nils Diewald0e6992a2015-04-14 20:13:52 +0000596
597 // Build doc string based on key
598 var string = this.key() + ' ';
599
600 // Add match operator
601 switch (this.matchop()) {
602 case "ne":
Akrone4961b12017-05-10 21:04:46 +0200603 string += '!=';
604 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000605 case "contains":
Akrone4961b12017-05-10 21:04:46 +0200606 string += '~';
607 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000608 case "excludes":
Akrone4961b12017-05-10 21:04:46 +0200609 string += '!~';
610 break;
Akron6a535d42015-08-26 20:16:58 +0200611 case "containsnot":
Akrone4961b12017-05-10 21:04:46 +0200612 string += '!~';
613 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000614 case "geq":
Akrone4961b12017-05-10 21:04:46 +0200615 string += 'since';
616 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000617 case "leq":
Akrone4961b12017-05-10 21:04:46 +0200618 string += 'until';
619 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000620 default:
Akrone4961b12017-05-10 21:04:46 +0200621 string += (this.type() == 'date') ? 'in' : '=';
622 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000623 };
624
625 string += ' ';
626
627 // Add value
628 switch (this.type()) {
629 case "date":
Akrone4961b12017-05-10 21:04:46 +0200630 return string + this.value();
Nils Diewald0e6992a2015-04-14 20:13:52 +0000631 case "regex":
Akron8778f5d2017-06-30 21:25:55 +0200632 return string + '/' + this.value().escapeRegex() + '/';
Nils Diewald0e6992a2015-04-14 20:13:52 +0000633 case "string":
Akron1bdf5272018-07-24 18:51:30 +0200634 case "text":
Akrone4961b12017-05-10 21:04:46 +0200635 return string + '"' + this.value().quote() + '"';
Nils Diewald0e6992a2015-04-14 20:13:52 +0000636 };
637
638 return "";
639 }
640 };
641});