blob: 132c4a80aafed051bc43624ec588dc10d9cbcfd6 [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',
Akron587e4d92018-08-31 12:44:26 +02009 'vc/docgroupref',
Nils Diewald4c221252015-04-21 20:19:25 +000010 'util'
Akron587e4d92018-08-31 12:44:26 +020011], function (jsonldClass, rewriteListClass, stringValClass, docGroupRefClass) {
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000012
Akron1bdf5272018-07-24 18:51:30 +020013 /*
14 * TODO:
15 * Improve error handling by using window.onerror() to
16 * capture thrown errors and log them.
17 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000018
Akron31d89942018-04-06 16:44:51 +020019 const errstr802 = "Match type is not supported by value type";
Akron1bdf5272018-07-24 18:51:30 +020020 const errstr804 = "Unknown value type";
Akron0b489ad2018-02-02 16:49:32 +010021 const loc = KorAP.Locale;
Nils Diewald4c221252015-04-21 20:19:25 +000022 loc.EMPTY = loc.EMPTY || '⋯';
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000023
Nils Diewald0e6992a2015-04-14 20:13:52 +000024 return {
Nils Diewald4c221252015-04-21 20:19:25 +000025
26 // The JSON-LD type
Nils Diewald0e6992a2015-04-14 20:13:52 +000027 _ldType : "doc",
Nils Diewald4c221252015-04-21 20:19:25 +000028
29 // The object ... maybe not important
Nils Diewald0e6992a2015-04-14 20:13:52 +000030 _obj : function () { return '???'; /*KorAP.Doc*/ },
31
Nils Diewald4c221252015-04-21 20:19:25 +000032 /**
33 * Create a new document criterion
34 * by passing the parent object and a json construct.
35 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000036 create : function (parent, json) {
Nils Diewald4c221252015-04-21 20:19:25 +000037
38 // Create the object, inheriting from Json-LD class
Nils Diewald0e6992a2015-04-14 20:13:52 +000039 var obj = Object(jsonldClass).
Akrone4961b12017-05-10 21:04:46 +020040 create().
41 upgradeTo(this).
42 fromJson(json);
Nils Diewald4c221252015-04-21 20:19:25 +000043
Akron55a343b2018-04-06 19:57:36 +020044 if (obj === undefined) {
45 console.log(json);
Akrond2474aa2018-08-28 12:06:27 +020046 return;
Akron55a343b2018-04-06 19:57:36 +020047 };
48
Nils Diewald4c221252015-04-21 20:19:25 +000049 // Bind the parent
Nils Diewald0e6992a2015-04-14 20:13:52 +000050 if (parent !== undefined)
Akrone4961b12017-05-10 21:04:46 +020051 obj._parent = parent;
Nils Diewald0e6992a2015-04-14 20:13:52 +000052
53 obj.__changed = true;
54 return obj;
55 },
56
Nils Diewald4c221252015-04-21 20:19:25 +000057 /**
58 * Update the elements content.
59 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000060 update : function () {
61 if (this._element === undefined)
Akrone4961b12017-05-10 21:04:46 +020062 return this.element();
Nils Diewald0e6992a2015-04-14 20:13:52 +000063
64 // Get element
65 var e = this._element;
66
Akronb19803c2018-08-16 16:39:42 +020067 // Check if there is a change in the underlying data
68 if (!this.__changed)
69 return e;
70
Nils Diewald0e6992a2015-04-14 20:13:52 +000071 // Set ref - TODO: Cleanup!
72 e.refTo = this;
73
Nils Diewald0e6992a2015-04-14 20:13:52 +000074
Akronb19803c2018-08-16 16:39:42 +020075 // Was rewritten
76 if (this.rewrites() !== undefined) {
77 e.classList.add("rewritten");
Nils Diewald0e6992a2015-04-14 20:13:52 +000078 };
79
Akronb19803c2018-08-16 16:39:42 +020080 // Added key
81 this._keyE = document.createElement('span');
82 this._keyE.setAttribute('class', 'key');
83
84 // Change key
85 this._keyE.addEventListener('click', this._changeKey.bind(this));
86
87 if (this.key()) {
88 var k = this.key();
89 if (loc['VC_' + k] !== undefined)
90 k = loc['VC_' + k];
91 this._keyE.addT(k);
92 };
93
94 // Added match operator
95 this._matchopE = document.createElement('span');
96 this._matchopE.setAttribute('data-type', this.type());
97 this._matchopE.setAttribute('class', 'match');
98 this._matchopE.addT(this.matchop());
99
100 // Change matchop
101 this._matchopE.addEventListener(
102 'click',
103 this._changeMatchop.bind(this)
104 );
105
106 // Added value operator
107 this._valueE = document.createElement('span');
108 this._valueE.setAttribute('data-type', this.type());
109 this._valueE.setAttribute('class', 'value');
110
111 if (this.value()) {
112 this._valueE.addT(this.value());
113 }
114 else {
115 this._valueE.addT(loc.EMPTY);
Akronebc96662018-08-29 17:36:20 +0200116 this._valueE.classList.add('unspecified');
Akronb19803c2018-08-16 16:39:42 +0200117 };
118
119 // Change value
120 this._valueE.addEventListener(
121 'click',
122 this._changeValue.bind(this)
123 );
124
125 // Remove all element children
126 _removeChildren(e);
127
128 // Add spans
129 e.appendChild(this._keyE);
130 e.appendChild(this._matchopE);
131 e.appendChild(this._valueE);
132
133 this.__changed = false;
134
Nils Diewald0e6992a2015-04-14 20:13:52 +0000135 if (this._rewrites !== undefined) {
Akrone4961b12017-05-10 21:04:46 +0200136 e.appendChild(this._rewrites.element());
Nils Diewald0e6992a2015-04-14 20:13:52 +0000137 };
138
139 if (this._parent !== undefined) {
Akronb19803c2018-08-16 16:39:42 +0200140
Akrone4961b12017-05-10 21:04:46 +0200141 // Set operators
142 var op = this.operators(
143 true,
144 true,
145 true
146 );
Nils Diewald0e6992a2015-04-14 20:13:52 +0000147
Akrone4961b12017-05-10 21:04:46 +0200148 // Append new operators
149 e.appendChild(op.element());
Nils Diewald0e6992a2015-04-14 20:13:52 +0000150 };
151
hebastaa0282be2018-12-05 16:58:00 +0100152 if(KorAP.vc){
hebasta48842cf2018-12-11 12:57:38 +0100153 //Replaced through Event
154 //KorAP.vc.checkGrayingStat(this);
155 var vcchevent = new CustomEvent('vcChange', {'detail':this});
156 document.dispatchEvent(vcchevent);
hebastaa0282be2018-12-05 16:58:00 +0100157 }
158
Nils Diewald0e6992a2015-04-14 20:13:52 +0000159 return e;
160 },
161
Nils Diewald4c221252015-04-21 20:19:25 +0000162
163 /**
164 * Get the associated element
165 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000166 element : function () {
167 if (this._element !== undefined)
Akrone4961b12017-05-10 21:04:46 +0200168 return this._element;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000169
170 this._element = document.createElement('div');
171 this._element.setAttribute('class', 'doc');
172
173 this.update();
174 return this._element;
175 },
176
Nils Diewald4c221252015-04-21 20:19:25 +0000177 /**
178 * Wrap a new operation around the doc element
179 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000180 wrap : function (op) {
181 var parent = this.parent();
Nils Diewald7c8ced22015-04-15 19:21:00 +0000182 var group = require('vc/docgroup').create(parent);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000183 group.operation(op);
184 group.append(this);
185 group.append();
186 return parent.replaceOperand(this, group).update();
187 },
188
Akron587e4d92018-08-31 12:44:26 +0200189 replaceWith : function (op) {
190 var p = this.parent();
191
192 if (p.ldType() === 'docGroup') {
193 p.replaceOperand(this,op);
194 }
195 else if (p.ldType() == null) {
196 p.root(op);
197 };
198 p.update();
199
200 this.destroy();
201 },
202
Nils Diewald4c221252015-04-21 20:19:25 +0000203 /**
204 * Deserialize from json
205 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000206 fromJson : function (json) {
207 if (json === undefined)
Akrone4961b12017-05-10 21:04:46 +0200208 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000209
210 if (json["@type"] === undefined) {
Akrone4961b12017-05-10 21:04:46 +0200211 KorAP.log(701, "JSON-LD group has no @type attribute");
212 return;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000213 };
214
215 if (json["value"] === undefined ||
Akrone4961b12017-05-10 21:04:46 +0200216 typeof json["value"] != 'string') {
217 KorAP.log(805, "Value is invalid");
218 return;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000219 };
220
Akronea4e9052017-07-06 16:12:05 +0200221 var rewrite;
222
Nils Diewald0e6992a2015-04-14 20:13:52 +0000223 // There is a defined key
224 if (json["key"] !== undefined &&
Akrone4961b12017-05-10 21:04:46 +0200225 typeof json["key"] === 'string') {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000226
Akrone4961b12017-05-10 21:04:46 +0200227 // Set key
228 this.key(json["key"]);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000229
Akrone4961b12017-05-10 21:04:46 +0200230 // Set match operation
231 if (json["match"] !== undefined) {
232 if (typeof json["match"] === 'string') {
Akron31d89942018-04-06 16:44:51 +0200233
Akrone4961b12017-05-10 21:04:46 +0200234 this.matchop(json["match"]);
235 }
236 else {
Akron31d89942018-04-06 16:44:51 +0200237 KorAP.log(802, errstr802);
Akrone4961b12017-05-10 21:04:46 +0200238 return;
239 };
240 };
Akron31d89942018-04-06 16:44:51 +0200241
Akronddc98a72018-04-06 17:33:52 +0200242 // Type is unspecified - but may be known by the menu
243 if (json["type"] === undefined && KorAP._vcKeyMenu) {
244
245 // Check the VC list if the field is known
246 var type = KorAP._vcKeyMenu.typeOf(this.key());
247 if (type != undefined) {
248 json["type"] = "type:" + type;
249 };
250 };
251
252 // Type is still undefined
Akron31d89942018-04-06 16:44:51 +0200253 if (json["type"] === undefined) {
Akronddc98a72018-04-06 17:33:52 +0200254
Akron31d89942018-04-06 16:44:51 +0200255 // Check match type
256 if (!KorAP._validUnspecMatchRE.test(this.matchop())) {
257 KorAP.log(802, errstr802);
258
259 // Rewrite method
260 this.matchop('eq');
261 rewrite = 'modification';
262 };
263
264 // Set string value
265 this.value(json["value"]);
266 }
267
268 // Field is string type
269 else if (json["type"] == "type:string") {
Akrone4961b12017-05-10 21:04:46 +0200270 this.type("string");
Nils Diewald0e6992a2015-04-14 20:13:52 +0000271
Akrone4961b12017-05-10 21:04:46 +0200272 // Check match type
273 if (!KorAP._validStringMatchRE.test(this.matchop())) {
Akron31d89942018-04-06 16:44:51 +0200274 KorAP.log(802, errstr802);
275
276 // Rewrite method
277 this.matchop('eq');
278 rewrite = 'modification';
279 };
280
281 // Set string value
282 this.value(json["value"]);
283 }
284
285 // Field is specified
286 else if (json["type"] == "type:text") {
287 this.type("text");
288
289 // Check match type
290 if (!KorAP._validTextMatchRE.test(this.matchop())) {
291 KorAP.log(802, errstr802);
Akronea4e9052017-07-06 16:12:05 +0200292
293 // Rewrite method
294 this.matchop('eq');
295 rewrite = 'modification';
Akrone4961b12017-05-10 21:04:46 +0200296 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000297
Akrone4961b12017-05-10 21:04:46 +0200298 // Set string value
299 this.value(json["value"]);
300 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000301
Akrone4961b12017-05-10 21:04:46 +0200302 // Key is a date
303 else if (json["type"] === "type:date") {
304 this.type("date");
Nils Diewald0e6992a2015-04-14 20:13:52 +0000305
Akrone4961b12017-05-10 21:04:46 +0200306 if (json["value"] !== undefined &&
307 KorAP._validDateRE.test(json["value"])) {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000308
Akrone4961b12017-05-10 21:04:46 +0200309 if (!KorAP._validDateMatchRE.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 // Set value
318 this.value(json["value"]);
319 }
320 else {
321 KorAP.log(806, "Value is not a valid date string");
322 return;
323 };
324 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000325
Akrone4961b12017-05-10 21:04:46 +0200326 // Key is a regular expression
327 else if (json["type"] === "type:regex") {
328 this.type("regex");
Nils Diewald0e6992a2015-04-14 20:13:52 +0000329
Akrone4961b12017-05-10 21:04:46 +0200330 try {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000331
Akrone4961b12017-05-10 21:04:46 +0200332 // Try to create a regular expression
333 var check = new RegExp(json["value"]);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000334
Akrone65a88a2018-04-05 19:14:20 +0200335 if (!KorAP._validStringMatchRE.test(this.matchop())) {
Akron31d89942018-04-06 16:44:51 +0200336 KorAP.log(802, errstr802);
Akronea4e9052017-07-06 16:12:05 +0200337
338 // Rewrite method
339 this.matchop('eq');
340 rewrite = 'modification';
Akrone4961b12017-05-10 21:04:46 +0200341 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000342
Akrone4961b12017-05-10 21:04:46 +0200343 this.value(json["value"]);
344 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000345
Akrone4961b12017-05-10 21:04:46 +0200346 catch (e) {
347 KorAP.log(807, "Value is not a valid regular expression");
348 return;
349 };
350 this.type("regex");
351 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000352
Akrone4961b12017-05-10 21:04:46 +0200353 else {
Akron1bdf5272018-07-24 18:51:30 +0200354 KorAP.log(804, errstr804 + ": " + this.type());
355 throw new Error(errstr804 + ": " + this.type());
Akrone4961b12017-05-10 21:04:46 +0200356 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000357 };
358
Akronea4e9052017-07-06 16:12:05 +0200359 // Rewrite coming from the server
Nils Diewald0e6992a2015-04-14 20:13:52 +0000360 if (json["rewrites"] !== undefined) {
Akronea4e9052017-07-06 16:12:05 +0200361 this.rewrite(json["rewrites"]);
362 }
363
364 // Rewrite coming from Kalamar
365 else if (rewrite !== undefined) {
366 this.rewrite(rewrite);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000367 };
Akronea4e9052017-07-06 16:12:05 +0200368
Nils Diewald0e6992a2015-04-14 20:13:52 +0000369 return this;
370 },
371
Nils Diewald4c221252015-04-21 20:19:25 +0000372 /**
373 * Get or set the key
374 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000375 key : function (value) {
376 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200377 this._key = value;
378 this._changed();
379 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000380 };
381 return this._key;
382 },
383
Nils Diewald4c221252015-04-21 20:19:25 +0000384 // Click on the key, show me the menu
385 _changeKey : function (e) {
386 var menu = KorAP._vcKeyMenu;
387
388 // Insert menu
389 this._element.insertBefore(
Akrone4961b12017-05-10 21:04:46 +0200390 menu.element(),
391 this._keyE
Nils Diewald4c221252015-04-21 20:19:25 +0000392 );
393
394 // Release event
395 var that = this;
396 menu.released(function (key, type) {
Nils Diewald4c221252015-04-21 20:19:25 +0000397
Akron587e4d92018-08-31 12:44:26 +0200398 if (type === 'ref') {
399 // KorAP._delete.bind(that);
400 var ref = docGroupRefClass.create(that.parent());
401 that.replaceWith(ref);
402 }
403 else {
404 var doc = that.key(key).type(type);
Nils Diewald4c221252015-04-21 20:19:25 +0000405
Akron587e4d92018-08-31 12:44:26 +0200406 // This may not be compatible - then switch to default
407 doc.matchop(doc.matchop());
408 doc.value(doc.value());
409
410 // Update the doc
411 doc.update();
412 };
Nils Diewald4c221252015-04-21 20:19:25 +0000413
Akrone4961b12017-05-10 21:04:46 +0200414 // hide!
415 this.hide();
Nils Diewald4c221252015-04-21 20:19:25 +0000416 });
417
418 // TODO: Highlight the old value!
419 menu.show();
420 menu.focus();
421 },
422
423 /**
424 * Get or set the match operator
425 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000426 matchop : function (match) {
Akron31d89942018-04-06 16:44:51 +0200427
Nils Diewald0e6992a2015-04-14 20:13:52 +0000428 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200429 var m = match.replace(/^match:/, '');
Akron31d89942018-04-06 16:44:51 +0200430
Akrone4961b12017-05-10 21:04:46 +0200431 if (
Akron31d89942018-04-06 16:44:51 +0200432 (this._type == undefined) // && KorAP._validUnspecMatchRE.test(m))
Akrone4961b12017-05-10 21:04:46 +0200433 ||
434 (
435 (this._type === 'string' || this._type === 'regex') &&
436 KorAP._validStringMatchRE.test(m)
437 )
438 ||
Akron31d89942018-04-06 16:44:51 +0200439 (this._type === 'text' && KorAP._validTextMatchRE.test(m))
440 ||
Akrone4961b12017-05-10 21:04:46 +0200441 (this._type === 'date' && KorAP._validDateMatchRE.test(m))
442 ) {
443 this._matchop = m;
444 }
445 else {
446 this._matchop = "eq";
447 };
Nils Diewald4c221252015-04-21 20:19:25 +0000448
Akrone4961b12017-05-10 21:04:46 +0200449 this._changed();
Akron31d89942018-04-06 16:44:51 +0200450 return this
Nils Diewald0e6992a2015-04-14 20:13:52 +0000451 };
452 return this._matchop || "eq";
453 },
454
Nils Diewald4c221252015-04-21 20:19:25 +0000455
456 // Click on the match operator, show me the menu
457 _changeMatchop : function (e) {
458 var menu = KorAP._vcMatchopMenu[this.type()];
459
460 if (menu === undefined) {
Akrone4961b12017-05-10 21:04:46 +0200461 KorAP.log(0, "Unable to init menu for " + this.type());
462 return;
Nils Diewald4c221252015-04-21 20:19:25 +0000463 };
464
465 // Insert menu
466 this._element.insertBefore(
Akrone4961b12017-05-10 21:04:46 +0200467 menu.element(),
468 this._matchopE
Nils Diewald4c221252015-04-21 20:19:25 +0000469 );
470
471 // Release event
472 var that = this;
473 menu.released(function (mo) {
Akrone4961b12017-05-10 21:04:46 +0200474 that.matchop(mo).update();
475 this.hide();
Nils Diewald4c221252015-04-21 20:19:25 +0000476 });
477
478 menu.show();
479 menu.focus();
480 },
481
482
483 /**
484 * Get or set the type
485 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000486 type : function (type) {
487 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200488 this._type = type;
489 this._changed();
490 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000491 };
492 return this._type || "string";
493 },
494
Nils Diewald4c221252015-04-21 20:19:25 +0000495
496 /**
497 * Get or set the value
498 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000499 value : function (value) {
500 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200501 if (this._type === 'date' && !KorAP._validDateRE.test(value)) {
502 delete this._value;
503 }
504 else {
505 this._value = value;
506 };
507 this._changed();
508 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000509 };
510 return this._value;
511 },
512
Nils Diewald4c221252015-04-21 20:19:25 +0000513
514 // Click on the match operator, show me the menu
515 _changeValue : function (e) {
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000516 var v = this.value();
517 var that = this;
Akron8db5e3a2018-05-28 19:25:26 +0200518
Nils Diewald7148c6f2015-05-04 15:07:53 +0000519 // Show datepicker
Nils Diewald87507832015-05-01 23:36:41 +0000520 if (this.type() === 'date') {
Akrone4961b12017-05-10 21:04:46 +0200521 var dp = KorAP._vcDatePicker;
Akron516b6f92018-01-03 17:46:59 +0100522 dp.fromString(v);
Nils Diewald87507832015-05-01 23:36:41 +0000523
Akrone4961b12017-05-10 21:04:46 +0200524 // Todo: change this
525 dp.onclick(function (selected) {
Nils Diewald87507832015-05-01 23:36:41 +0000526
Akrone4961b12017-05-10 21:04:46 +0200527 // There are values selected
528 if (selected['year']) {
529 that.value(this.toString());
530 that.update();
531 return;
532 };
Nils Diewald87507832015-05-01 23:36:41 +0000533
Akrone4961b12017-05-10 21:04:46 +0200534 // Remove datepicker
535 that._element.removeChild(
536 dp.element()
537 );
538 });
Nils Diewald87507832015-05-01 23:36:41 +0000539
Akrone4961b12017-05-10 21:04:46 +0200540 // Get element of the date picker
541 var dpElem = dp.show();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000542
Akrone4961b12017-05-10 21:04:46 +0200543 this._element.insertBefore(
544 dpElem,
545 this._valueE
546 );
Nils Diewald7148c6f2015-05-04 15:07:53 +0000547
Akron516b6f92018-01-03 17:46:59 +0100548 dp.input().focus();
Nils Diewald87507832015-05-01 23:36:41 +0000549 }
550 else {
Akrone4961b12017-05-10 21:04:46 +0200551 var regex = this.type() === 'regex' ? true : false;
552 var str = stringValClass.create(this.value(), regex);
553 var strElem = str.element();
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000554
Akrone4961b12017-05-10 21:04:46 +0200555 str.store = function (value, regex) {
556 that.value(value);
557 if (regex === true)
558 that.type('regex');
559 else
560 that.type('string');
561
562 that._element.removeChild(
563 this._element
564 );
565 that.update();
566 };
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000567
Akrone4961b12017-05-10 21:04:46 +0200568 // Insert element
569 this._element.insertBefore(
570 strElem,
571 this._valueE
572 );
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000573
Akrone4961b12017-05-10 21:04:46 +0200574 str.focus();
Nils Diewald87507832015-05-01 23:36:41 +0000575 };
Nils Diewald4c221252015-04-21 20:19:25 +0000576 },
577
578
Nils Diewald0e6992a2015-04-14 20:13:52 +0000579 rewrites : function () {
580 return this._rewrites;
581 },
582
Akronea4e9052017-07-06 16:12:05 +0200583 rewrite : function (value) {
584 if (typeof value === 'string') {
585 value = [{
586 "@type" : "koral:rewrite",
587 "operation" : "operation:" + value,
588 "src" : "Kalamar"
589 }];
590 };
591 this._rewrites = rewriteListClass.create(value);
592 },
593
Akron31d89942018-04-06 16:44:51 +0200594 // Mark the underlying data as being changed.
595 // This is important for rerendering the dom.
596 // This will also remove rewrite markers, when the data
597 // change happened by the user
Nils Diewald0e6992a2015-04-14 20:13:52 +0000598 _changed : function () {
599 this.__changed = true;
Nils Diewald4347ee92015-05-04 20:32:48 +0000600
Nils Diewald0e6992a2015-04-14 20:13:52 +0000601 if (this._rewrites === undefined)
Akrone4961b12017-05-10 21:04:46 +0200602 return;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000603
Akronea4e9052017-07-06 16:12:05 +0200604 delete this["_rewrites"];
Nils Diewald0e6992a2015-04-14 20:13:52 +0000605
606 if (this._element === undefined)
Akrone4961b12017-05-10 21:04:46 +0200607 return;
Akronea4e9052017-07-06 16:12:05 +0200608
Nils Diewald0e6992a2015-04-14 20:13:52 +0000609 this._element.classList.remove("rewritten");
610 },
611
Nils Diewald4c221252015-04-21 20:19:25 +0000612
Nils Diewald0e6992a2015-04-14 20:13:52 +0000613 toJson : function () {
614 if (!this.matchop() || !this.key())
Akrone4961b12017-05-10 21:04:46 +0200615 return {};
Nils Diewald0e6992a2015-04-14 20:13:52 +0000616
617 return {
Akrone4961b12017-05-10 21:04:46 +0200618 "@type" : "koral:" + this.ldType(),
619 "key" : this.key(),
620 "match" : "match:" + this.matchop(),
621 "value" : this.value() || '',
622 "type" : "type:" + this.type()
Nils Diewald0e6992a2015-04-14 20:13:52 +0000623 };
624 },
625
hebastaa0282be2018-12-05 16:58:00 +0100626 incomplete : function () {
627 return !(this.matchop() && this.key() && this.value());
628 },
Nils Diewald4c221252015-04-21 20:19:25 +0000629
Nils Diewald0e6992a2015-04-14 20:13:52 +0000630 toQuery : function () {
hebastaa0282be2018-12-05 16:58:00 +0100631 if (this.incomplete())
Akrone4961b12017-05-10 21:04:46 +0200632 return "";
Nils Diewald0e6992a2015-04-14 20:13:52 +0000633
634 // Build doc string based on key
635 var string = this.key() + ' ';
636
637 // Add match operator
638 switch (this.matchop()) {
639 case "ne":
Akrone4961b12017-05-10 21:04:46 +0200640 string += '!=';
641 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000642 case "contains":
Akrone4961b12017-05-10 21:04:46 +0200643 string += '~';
644 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000645 case "excludes":
Akrone4961b12017-05-10 21:04:46 +0200646 string += '!~';
647 break;
Akron6a535d42015-08-26 20:16:58 +0200648 case "containsnot":
Akrone4961b12017-05-10 21:04:46 +0200649 string += '!~';
650 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000651 case "geq":
Akrone4961b12017-05-10 21:04:46 +0200652 string += 'since';
653 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000654 case "leq":
Akrone4961b12017-05-10 21:04:46 +0200655 string += 'until';
656 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000657 default:
Akrone4961b12017-05-10 21:04:46 +0200658 string += (this.type() == 'date') ? 'in' : '=';
659 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000660 };
661
662 string += ' ';
663
664 // Add value
665 switch (this.type()) {
666 case "date":
Akrone4961b12017-05-10 21:04:46 +0200667 return string + this.value();
Nils Diewald0e6992a2015-04-14 20:13:52 +0000668 case "regex":
Akron8778f5d2017-06-30 21:25:55 +0200669 return string + '/' + this.value().escapeRegex() + '/';
Nils Diewald0e6992a2015-04-14 20:13:52 +0000670 case "string":
Akron1bdf5272018-07-24 18:51:30 +0200671 case "text":
Akrone4961b12017-05-10 21:04:46 +0200672 return string + '"' + this.value().quote() + '"';
Nils Diewald0e6992a2015-04-14 20:13:52 +0000673 };
674
675 return "";
676 }
677 };
678});