blob: f3e5559c30e2bd415cb66e24d5f54389d0c655c9 [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 _validRegexMatchRE = new RegExp("^(?:eq|ne)$");
13
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
58 // Check if there is a change
59 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') {
197 this.matchop(json["match"]);
198 }
199 else {
200 KorAP.log(802, "Match type is not supported by value type");
201 return;
202 };
203 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000204
Akrone4961b12017-05-10 21:04:46 +0200205 // Key is a string
206 if (json["type"] === undefined ||
207 json["type"] == "type:string") {
208 this.type("string");
Nils Diewald0e6992a2015-04-14 20:13:52 +0000209
Akrone4961b12017-05-10 21:04:46 +0200210 // Check match type
211 if (!KorAP._validStringMatchRE.test(this.matchop())) {
212 KorAP.log(802, "Match type is not supported by value type");
Akronea4e9052017-07-06 16:12:05 +0200213
214 // Rewrite method
215 this.matchop('eq');
216 rewrite = 'modification';
Akrone4961b12017-05-10 21:04:46 +0200217 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000218
Akrone4961b12017-05-10 21:04:46 +0200219 // Set string value
220 this.value(json["value"]);
221 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000222
Akrone4961b12017-05-10 21:04:46 +0200223 // Key is a date
224 else if (json["type"] === "type:date") {
225 this.type("date");
Nils Diewald0e6992a2015-04-14 20:13:52 +0000226
Akrone4961b12017-05-10 21:04:46 +0200227 if (json["value"] !== undefined &&
228 KorAP._validDateRE.test(json["value"])) {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000229
Akrone4961b12017-05-10 21:04:46 +0200230 if (!KorAP._validDateMatchRE.test(this.matchop())) {
231 KorAP.log(802, "Match type is not supported by value type");
Akronea4e9052017-07-06 16:12:05 +0200232
233 // Rewrite method
234 this.matchop('eq');
235 rewrite = 'modification';
Akrone4961b12017-05-10 21:04:46 +0200236 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000237
Akrone4961b12017-05-10 21:04:46 +0200238 // Set value
239 this.value(json["value"]);
240 }
241 else {
242 KorAP.log(806, "Value is not a valid date string");
243 return;
244 };
245 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000246
Akrone4961b12017-05-10 21:04:46 +0200247 // Key is a regular expression
248 else if (json["type"] === "type:regex") {
249 this.type("regex");
Nils Diewald0e6992a2015-04-14 20:13:52 +0000250
Akrone4961b12017-05-10 21:04:46 +0200251 try {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000252
Akrone4961b12017-05-10 21:04:46 +0200253 // Try to create a regular expression
254 var check = new RegExp(json["value"]);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000255
Akrone4961b12017-05-10 21:04:46 +0200256 if (!_validRegexMatchRE.test(this.matchop())) {
257 KorAP.log(802, "Match type is not supported by value type");
Akronea4e9052017-07-06 16:12:05 +0200258
259 // Rewrite method
260 this.matchop('eq');
261 rewrite = 'modification';
Akrone4961b12017-05-10 21:04:46 +0200262 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000263
Akrone4961b12017-05-10 21:04:46 +0200264 this.value(json["value"]);
265 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000266
Akrone4961b12017-05-10 21:04:46 +0200267 catch (e) {
268 KorAP.log(807, "Value is not a valid regular expression");
269 return;
270 };
271 this.type("regex");
272 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000273
Akrone4961b12017-05-10 21:04:46 +0200274 else {
275 KorAP.log(804, "Unknown value type");
276 return;
277 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000278 };
279
Akronea4e9052017-07-06 16:12:05 +0200280 // Rewrite coming from the server
Nils Diewald0e6992a2015-04-14 20:13:52 +0000281 if (json["rewrites"] !== undefined) {
Akronea4e9052017-07-06 16:12:05 +0200282 this.rewrite(json["rewrites"]);
283 }
284
285 // Rewrite coming from Kalamar
286 else if (rewrite !== undefined) {
287 this.rewrite(rewrite);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000288 };
Akronea4e9052017-07-06 16:12:05 +0200289
Nils Diewald0e6992a2015-04-14 20:13:52 +0000290 return this;
291 },
292
Nils Diewald4c221252015-04-21 20:19:25 +0000293 /**
294 * Get or set the key
295 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000296 key : function (value) {
297 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200298 this._key = value;
299 this._changed();
300 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000301 };
302 return this._key;
303 },
304
Nils Diewald4c221252015-04-21 20:19:25 +0000305 // Click on the key, show me the menu
306 _changeKey : function (e) {
307 var menu = KorAP._vcKeyMenu;
308
309 // Insert menu
310 this._element.insertBefore(
Akrone4961b12017-05-10 21:04:46 +0200311 menu.element(),
312 this._keyE
Nils Diewald4c221252015-04-21 20:19:25 +0000313 );
314
315 // Release event
316 var that = this;
317 menu.released(function (key, type) {
Akrone4961b12017-05-10 21:04:46 +0200318 var doc = that.key(key).type(type);
Nils Diewald4c221252015-04-21 20:19:25 +0000319
Akrone4961b12017-05-10 21:04:46 +0200320 // This may not be compatible - then switch to default
321 doc.matchop(doc.matchop());
322 doc.value(doc.value());
Nils Diewald4c221252015-04-21 20:19:25 +0000323
Akrone4961b12017-05-10 21:04:46 +0200324 // Update the doc
325 doc.update();
Nils Diewald4c221252015-04-21 20:19:25 +0000326
Akrone4961b12017-05-10 21:04:46 +0200327 // hide!
328 this.hide();
Nils Diewald4c221252015-04-21 20:19:25 +0000329 });
330
331 // TODO: Highlight the old value!
332 menu.show();
333 menu.focus();
334 },
335
336 /**
337 * Get or set the match operator
338 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000339 matchop : function (match) {
340 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200341 var m = match.replace(/^match:/, '');
342 if (
343 (this._type === undefined)
344 ||
345 (
346 (this._type === 'string' || this._type === 'regex') &&
347 KorAP._validStringMatchRE.test(m)
348 )
349 ||
350 (this._type === 'date' && KorAP._validDateMatchRE.test(m))
351 ) {
352 this._matchop = m;
353 }
354 else {
355 this._matchop = "eq";
356 };
Nils Diewald4c221252015-04-21 20:19:25 +0000357
Akrone4961b12017-05-10 21:04:46 +0200358 this._changed();
359 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000360 };
361 return this._matchop || "eq";
362 },
363
Nils Diewald4c221252015-04-21 20:19:25 +0000364
365 // Click on the match operator, show me the menu
366 _changeMatchop : function (e) {
367 var menu = KorAP._vcMatchopMenu[this.type()];
368
369 if (menu === undefined) {
Akrone4961b12017-05-10 21:04:46 +0200370 KorAP.log(0, "Unable to init menu for " + this.type());
371 return;
Nils Diewald4c221252015-04-21 20:19:25 +0000372 };
373
374 // Insert menu
375 this._element.insertBefore(
Akrone4961b12017-05-10 21:04:46 +0200376 menu.element(),
377 this._matchopE
Nils Diewald4c221252015-04-21 20:19:25 +0000378 );
379
380 // Release event
381 var that = this;
382 menu.released(function (mo) {
Akrone4961b12017-05-10 21:04:46 +0200383 that.matchop(mo).update();
384 this.hide();
Nils Diewald4c221252015-04-21 20:19:25 +0000385 });
386
387 menu.show();
388 menu.focus();
389 },
390
391
392 /**
393 * Get or set the type
394 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000395 type : function (type) {
396 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200397 this._type = type;
398 this._changed();
399 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000400 };
401 return this._type || "string";
402 },
403
Nils Diewald4c221252015-04-21 20:19:25 +0000404
405 /**
406 * Get or set the value
407 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000408 value : function (value) {
409 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200410 if (this._type === 'date' && !KorAP._validDateRE.test(value)) {
411 delete this._value;
412 }
413 else {
414 this._value = value;
415 };
416 this._changed();
417 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000418 };
419 return this._value;
420 },
421
Nils Diewald4c221252015-04-21 20:19:25 +0000422
423 // Click on the match operator, show me the menu
424 _changeValue : function (e) {
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000425 var v = this.value();
426 var that = this;
Nils Diewald87507832015-05-01 23:36:41 +0000427
Nils Diewald7148c6f2015-05-04 15:07:53 +0000428 // Show datepicker
Nils Diewald87507832015-05-01 23:36:41 +0000429 if (this.type() === 'date') {
Akrone4961b12017-05-10 21:04:46 +0200430 var dp = KorAP._vcDatePicker;
Akron516b6f92018-01-03 17:46:59 +0100431 dp.fromString(v);
Nils Diewald87507832015-05-01 23:36:41 +0000432
Akrone4961b12017-05-10 21:04:46 +0200433 // Todo: change this
434 dp.onclick(function (selected) {
Nils Diewald87507832015-05-01 23:36:41 +0000435
Akrone4961b12017-05-10 21:04:46 +0200436 // There are values selected
437 if (selected['year']) {
438 that.value(this.toString());
439 that.update();
440 return;
441 };
Nils Diewald87507832015-05-01 23:36:41 +0000442
Akrone4961b12017-05-10 21:04:46 +0200443 // Remove datepicker
444 that._element.removeChild(
445 dp.element()
446 );
447 });
Nils Diewald87507832015-05-01 23:36:41 +0000448
Akrone4961b12017-05-10 21:04:46 +0200449 // Get element of the date picker
450 var dpElem = dp.show();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000451
Akrone4961b12017-05-10 21:04:46 +0200452 this._element.insertBefore(
453 dpElem,
454 this._valueE
455 );
Nils Diewald7148c6f2015-05-04 15:07:53 +0000456
Akron516b6f92018-01-03 17:46:59 +0100457 dp.input().focus();
Nils Diewald87507832015-05-01 23:36:41 +0000458 }
459 else {
Akrone4961b12017-05-10 21:04:46 +0200460 var regex = this.type() === 'regex' ? true : false;
461 var str = stringValClass.create(this.value(), regex);
462 var strElem = str.element();
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000463
Akrone4961b12017-05-10 21:04:46 +0200464 str.store = function (value, regex) {
465 that.value(value);
466 if (regex === true)
467 that.type('regex');
468 else
469 that.type('string');
470
471 that._element.removeChild(
472 this._element
473 );
474 that.update();
475 };
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000476
Akrone4961b12017-05-10 21:04:46 +0200477 // Insert element
478 this._element.insertBefore(
479 strElem,
480 this._valueE
481 );
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000482
Akrone4961b12017-05-10 21:04:46 +0200483 str.focus();
Nils Diewald87507832015-05-01 23:36:41 +0000484 };
Nils Diewald4c221252015-04-21 20:19:25 +0000485 },
486
487
Nils Diewald0e6992a2015-04-14 20:13:52 +0000488 rewrites : function () {
489 return this._rewrites;
490 },
491
Akronea4e9052017-07-06 16:12:05 +0200492 rewrite : function (value) {
493 if (typeof value === 'string') {
494 value = [{
495 "@type" : "koral:rewrite",
496 "operation" : "operation:" + value,
497 "src" : "Kalamar"
498 }];
499 };
500 this._rewrites = rewriteListClass.create(value);
501 },
502
503 // Remove rewrite marker when the data changes
Nils Diewald0e6992a2015-04-14 20:13:52 +0000504 _changed : function () {
505 this.__changed = true;
Nils Diewald4347ee92015-05-04 20:32:48 +0000506
Nils Diewald0e6992a2015-04-14 20:13:52 +0000507 if (this._rewrites === undefined)
Akrone4961b12017-05-10 21:04:46 +0200508 return;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000509
Akronea4e9052017-07-06 16:12:05 +0200510 delete this["_rewrites"];
Nils Diewald0e6992a2015-04-14 20:13:52 +0000511
512 if (this._element === undefined)
Akrone4961b12017-05-10 21:04:46 +0200513 return;
Akronea4e9052017-07-06 16:12:05 +0200514
Nils Diewald0e6992a2015-04-14 20:13:52 +0000515 this._element.classList.remove("rewritten");
516 },
517
Nils Diewald4c221252015-04-21 20:19:25 +0000518
Nils Diewald0e6992a2015-04-14 20:13:52 +0000519 toJson : function () {
520 if (!this.matchop() || !this.key())
Akrone4961b12017-05-10 21:04:46 +0200521 return {};
Nils Diewald0e6992a2015-04-14 20:13:52 +0000522
523 return {
Akrone4961b12017-05-10 21:04:46 +0200524 "@type" : "koral:" + this.ldType(),
525 "key" : this.key(),
526 "match" : "match:" + this.matchop(),
527 "value" : this.value() || '',
528 "type" : "type:" + this.type()
Nils Diewald0e6992a2015-04-14 20:13:52 +0000529 };
530 },
531
Nils Diewald4c221252015-04-21 20:19:25 +0000532
Nils Diewald0e6992a2015-04-14 20:13:52 +0000533 toQuery : function () {
534 if (!this.matchop() || !this.key())
Akrone4961b12017-05-10 21:04:46 +0200535 return "";
Nils Diewald0e6992a2015-04-14 20:13:52 +0000536
537 // Build doc string based on key
538 var string = this.key() + ' ';
539
540 // Add match operator
541 switch (this.matchop()) {
542 case "ne":
Akrone4961b12017-05-10 21:04:46 +0200543 string += '!=';
544 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000545 case "contains":
Akrone4961b12017-05-10 21:04:46 +0200546 string += '~';
547 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000548 case "excludes":
Akrone4961b12017-05-10 21:04:46 +0200549 string += '!~';
550 break;
Akron6a535d42015-08-26 20:16:58 +0200551 case "containsnot":
Akrone4961b12017-05-10 21:04:46 +0200552 string += '!~';
553 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000554 case "geq":
Akrone4961b12017-05-10 21:04:46 +0200555 string += 'since';
556 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000557 case "leq":
Akrone4961b12017-05-10 21:04:46 +0200558 string += 'until';
559 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000560 default:
Akrone4961b12017-05-10 21:04:46 +0200561 string += (this.type() == 'date') ? 'in' : '=';
562 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000563 };
564
565 string += ' ';
566
567 // Add value
568 switch (this.type()) {
569 case "date":
Akrone4961b12017-05-10 21:04:46 +0200570 return string + this.value();
571 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000572 case "regex":
Akron8778f5d2017-06-30 21:25:55 +0200573 return string + '/' + this.value().escapeRegex() + '/';
Akrone4961b12017-05-10 21:04:46 +0200574 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000575 case "string":
Akrone4961b12017-05-10 21:04:46 +0200576 return string + '"' + this.value().quote() + '"';
577 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000578 };
579
580 return "";
581 }
582 };
583});