blob: f173de808f3a845c771faba4cd715ea454d03604 [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
Akron0b489ad2018-02-02 16:49:32 +010013 const loc = KorAP.Locale;
Nils Diewald4c221252015-04-21 20:19:25 +000014 loc.EMPTY = loc.EMPTY || '⋯';
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000015
Nils Diewald0e6992a2015-04-14 20:13:52 +000016 return {
Nils Diewald4c221252015-04-21 20:19:25 +000017
18 // The JSON-LD type
Nils Diewald0e6992a2015-04-14 20:13:52 +000019 _ldType : "doc",
Nils Diewald4c221252015-04-21 20:19:25 +000020
21 // The object ... maybe not important
Nils Diewald0e6992a2015-04-14 20:13:52 +000022 _obj : function () { return '???'; /*KorAP.Doc*/ },
23
Nils Diewald4c221252015-04-21 20:19:25 +000024 /**
25 * Create a new document criterion
26 * by passing the parent object and a json construct.
27 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000028 create : function (parent, json) {
Nils Diewald4c221252015-04-21 20:19:25 +000029
30 // Create the object, inheriting from Json-LD class
Nils Diewald0e6992a2015-04-14 20:13:52 +000031 var obj = Object(jsonldClass).
Akrone4961b12017-05-10 21:04:46 +020032 create().
33 upgradeTo(this).
34 fromJson(json);
Nils Diewald4c221252015-04-21 20:19:25 +000035
36 // Bind the parent
Nils Diewald0e6992a2015-04-14 20:13:52 +000037 if (parent !== undefined)
Akrone4961b12017-05-10 21:04:46 +020038 obj._parent = parent;
Nils Diewald0e6992a2015-04-14 20:13:52 +000039
40 obj.__changed = true;
41 return obj;
42 },
43
Nils Diewald4c221252015-04-21 20:19:25 +000044 /**
45 * Update the elements content.
46 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000047 update : function () {
48 if (this._element === undefined)
Akrone4961b12017-05-10 21:04:46 +020049 return this.element();
Nils Diewald0e6992a2015-04-14 20:13:52 +000050
51 // Get element
52 var e = this._element;
53
54 // Set ref - TODO: Cleanup!
55 e.refTo = this;
56
57 // Check if there is a change
58 if (this.__changed) {
59
Akrone4961b12017-05-10 21:04:46 +020060 // Was rewritten
61 if (this.rewrites() !== undefined) {
62 e.classList.add("rewritten");
63 };
Nils Diewald0e6992a2015-04-14 20:13:52 +000064
Akrone4961b12017-05-10 21:04:46 +020065 // Added key
66 this._keyE = document.createElement('span');
67 this._keyE.setAttribute('class', 'key');
Nils Diewald0e6992a2015-04-14 20:13:52 +000068
Akrone4961b12017-05-10 21:04:46 +020069 // Change key
70 this._keyE.addEventListener('click', this._changeKey.bind(this));
Nils Diewald0e6992a2015-04-14 20:13:52 +000071
Akrone4961b12017-05-10 21:04:46 +020072 if (this.key()) {
73 var k = this.key();
74 if (loc['VC_' + k] !== undefined)
75 k = loc['VC_' + k];
Akron0b489ad2018-02-02 16:49:32 +010076 this._keyE.addT(k);
Akrone4961b12017-05-10 21:04:46 +020077 };
Nils Diewald4c221252015-04-21 20:19:25 +000078
Akrone4961b12017-05-10 21:04:46 +020079 // Added match operator
80 this._matchopE = document.createElement('span');
81 this._matchopE.setAttribute('data-type', this.type());
82 this._matchopE.setAttribute('class', 'match');
Akron0b489ad2018-02-02 16:49:32 +010083 this._matchopE.addT(this.matchop());
Nils Diewald0e6992a2015-04-14 20:13:52 +000084
Akrone4961b12017-05-10 21:04:46 +020085 // Change matchop
86 this._matchopE.addEventListener(
87 'click',
88 this._changeMatchop.bind(this)
89 );
Nils Diewald4c221252015-04-21 20:19:25 +000090
Akrone4961b12017-05-10 21:04:46 +020091 // Added value operator
92 this._valueE = document.createElement('span');
93 this._valueE.setAttribute('data-type', this.type());
94 this._valueE.setAttribute('class', 'value');
95 if (this.value()) {
Akron0b489ad2018-02-02 16:49:32 +010096 this._valueE.addT(this.value());
Akrone4961b12017-05-10 21:04:46 +020097 }
98 else {
Akron0b489ad2018-02-02 16:49:32 +010099 this._valueE.addT(loc.EMPTY);
Akrone4961b12017-05-10 21:04:46 +0200100 };
Nils Diewald4c221252015-04-21 20:19:25 +0000101
Akrone4961b12017-05-10 21:04:46 +0200102 // Change value
103 this._valueE.addEventListener(
104 'click',
105 this._changeValue.bind(this)
106 );
Nils Diewald4c221252015-04-21 20:19:25 +0000107
Nils Diewald0e6992a2015-04-14 20:13:52 +0000108
Akrone4961b12017-05-10 21:04:46 +0200109 // Remove all element children
110 _removeChildren(e);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000111
Akrone4961b12017-05-10 21:04:46 +0200112 // Add spans
113 e.appendChild(this._keyE);
114 e.appendChild(this._matchopE);
115 e.appendChild(this._valueE);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000116
Akrone4961b12017-05-10 21:04:46 +0200117 this.__changed = false;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000118 };
119
120 if (this._rewrites !== undefined) {
Akrone4961b12017-05-10 21:04:46 +0200121 e.appendChild(this._rewrites.element());
Nils Diewald0e6992a2015-04-14 20:13:52 +0000122 };
123
124 if (this._parent !== undefined) {
Akrone4961b12017-05-10 21:04:46 +0200125 // Set operators
126 var op = this.operators(
127 true,
128 true,
129 true
130 );
Nils Diewald0e6992a2015-04-14 20:13:52 +0000131
Akrone4961b12017-05-10 21:04:46 +0200132 // Append new operators
133 e.appendChild(op.element());
Nils Diewald0e6992a2015-04-14 20:13:52 +0000134 };
135
136 return e;
137 },
138
Nils Diewald4c221252015-04-21 20:19:25 +0000139
140 /**
141 * Get the associated element
142 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000143 element : function () {
144 if (this._element !== undefined)
Akrone4961b12017-05-10 21:04:46 +0200145 return this._element;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000146
147 this._element = document.createElement('div');
148 this._element.setAttribute('class', 'doc');
149
150 this.update();
151 return this._element;
152 },
153
Nils Diewald4c221252015-04-21 20:19:25 +0000154 /**
155 * Wrap a new operation around the doc element
156 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000157 wrap : function (op) {
158 var parent = this.parent();
Nils Diewald7c8ced22015-04-15 19:21:00 +0000159 var group = require('vc/docgroup').create(parent);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000160 group.operation(op);
161 group.append(this);
162 group.append();
163 return parent.replaceOperand(this, group).update();
164 },
165
Nils Diewald4c221252015-04-21 20:19:25 +0000166 /**
167 * Deserialize from json
168 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000169 fromJson : function (json) {
170 if (json === undefined)
Akrone4961b12017-05-10 21:04:46 +0200171 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000172
173 if (json["@type"] === undefined) {
Akrone4961b12017-05-10 21:04:46 +0200174 KorAP.log(701, "JSON-LD group has no @type attribute");
175 return;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000176 };
177
178 if (json["value"] === undefined ||
Akrone4961b12017-05-10 21:04:46 +0200179 typeof json["value"] != 'string') {
180 KorAP.log(805, "Value is invalid");
181 return;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000182 };
183
Akronea4e9052017-07-06 16:12:05 +0200184 var rewrite;
185
Nils Diewald0e6992a2015-04-14 20:13:52 +0000186 // There is a defined key
187 if (json["key"] !== undefined &&
Akrone4961b12017-05-10 21:04:46 +0200188 typeof json["key"] === 'string') {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000189
Akrone4961b12017-05-10 21:04:46 +0200190 // Set key
191 this.key(json["key"]);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000192
Akrone4961b12017-05-10 21:04:46 +0200193 // Set match operation
194 if (json["match"] !== undefined) {
195 if (typeof json["match"] === 'string') {
196 this.matchop(json["match"]);
197 }
198 else {
199 KorAP.log(802, "Match type is not supported by value type");
200 return;
201 };
202 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000203
Akrone4961b12017-05-10 21:04:46 +0200204 // Key is a string
205 if (json["type"] === undefined ||
206 json["type"] == "type:string") {
207 this.type("string");
Nils Diewald0e6992a2015-04-14 20:13:52 +0000208
Akrone4961b12017-05-10 21:04:46 +0200209 // Check match type
210 if (!KorAP._validStringMatchRE.test(this.matchop())) {
211 KorAP.log(802, "Match type is not supported by value type");
Akronea4e9052017-07-06 16:12:05 +0200212
213 // Rewrite method
214 this.matchop('eq');
215 rewrite = 'modification';
Akrone4961b12017-05-10 21:04:46 +0200216 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000217
Akrone4961b12017-05-10 21:04:46 +0200218 // Set string value
219 this.value(json["value"]);
220 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000221
Akrone4961b12017-05-10 21:04:46 +0200222 // Key is a date
223 else if (json["type"] === "type:date") {
224 this.type("date");
Nils Diewald0e6992a2015-04-14 20:13:52 +0000225
Akrone4961b12017-05-10 21:04:46 +0200226 if (json["value"] !== undefined &&
227 KorAP._validDateRE.test(json["value"])) {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000228
Akrone4961b12017-05-10 21:04:46 +0200229 if (!KorAP._validDateMatchRE.test(this.matchop())) {
230 KorAP.log(802, "Match type is not supported by value type");
Akronea4e9052017-07-06 16:12:05 +0200231
232 // Rewrite method
233 this.matchop('eq');
234 rewrite = 'modification';
Akrone4961b12017-05-10 21:04:46 +0200235 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000236
Akrone4961b12017-05-10 21:04:46 +0200237 // Set value
238 this.value(json["value"]);
239 }
240 else {
241 KorAP.log(806, "Value is not a valid date string");
242 return;
243 };
244 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000245
Akrone4961b12017-05-10 21:04:46 +0200246 // Key is a regular expression
247 else if (json["type"] === "type:regex") {
248 this.type("regex");
Nils Diewald0e6992a2015-04-14 20:13:52 +0000249
Akrone4961b12017-05-10 21:04:46 +0200250 try {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000251
Akrone4961b12017-05-10 21:04:46 +0200252 // Try to create a regular expression
253 var check = new RegExp(json["value"]);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000254
Akrone65a88a2018-04-05 19:14:20 +0200255 if (!KorAP._validStringMatchRE.test(this.matchop())) {
Akrone4961b12017-05-10 21:04:46 +0200256 KorAP.log(802, "Match type is not supported by value type");
Akronea4e9052017-07-06 16:12:05 +0200257
258 // Rewrite method
259 this.matchop('eq');
260 rewrite = 'modification';
Akrone4961b12017-05-10 21:04:46 +0200261 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000262
Akrone4961b12017-05-10 21:04:46 +0200263 this.value(json["value"]);
264 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000265
Akrone4961b12017-05-10 21:04:46 +0200266 catch (e) {
267 KorAP.log(807, "Value is not a valid regular expression");
268 return;
269 };
270 this.type("regex");
271 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000272
Akrone4961b12017-05-10 21:04:46 +0200273 else {
274 KorAP.log(804, "Unknown value type");
275 return;
276 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000277 };
278
Akronea4e9052017-07-06 16:12:05 +0200279 // Rewrite coming from the server
Nils Diewald0e6992a2015-04-14 20:13:52 +0000280 if (json["rewrites"] !== undefined) {
Akronea4e9052017-07-06 16:12:05 +0200281 this.rewrite(json["rewrites"]);
282 }
283
284 // Rewrite coming from Kalamar
285 else if (rewrite !== undefined) {
286 this.rewrite(rewrite);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000287 };
Akronea4e9052017-07-06 16:12:05 +0200288
Nils Diewald0e6992a2015-04-14 20:13:52 +0000289 return this;
290 },
291
Nils Diewald4c221252015-04-21 20:19:25 +0000292 /**
293 * Get or set the key
294 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000295 key : function (value) {
296 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200297 this._key = value;
298 this._changed();
299 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000300 };
301 return this._key;
302 },
303
Nils Diewald4c221252015-04-21 20:19:25 +0000304 // Click on the key, show me the menu
305 _changeKey : function (e) {
306 var menu = KorAP._vcKeyMenu;
307
308 // Insert menu
309 this._element.insertBefore(
Akrone4961b12017-05-10 21:04:46 +0200310 menu.element(),
311 this._keyE
Nils Diewald4c221252015-04-21 20:19:25 +0000312 );
313
314 // Release event
315 var that = this;
316 menu.released(function (key, type) {
Akrone4961b12017-05-10 21:04:46 +0200317 var doc = that.key(key).type(type);
Nils Diewald4c221252015-04-21 20:19:25 +0000318
Akrone4961b12017-05-10 21:04:46 +0200319 // This may not be compatible - then switch to default
320 doc.matchop(doc.matchop());
321 doc.value(doc.value());
Nils Diewald4c221252015-04-21 20:19:25 +0000322
Akrone4961b12017-05-10 21:04:46 +0200323 // Update the doc
324 doc.update();
Nils Diewald4c221252015-04-21 20:19:25 +0000325
Akrone4961b12017-05-10 21:04:46 +0200326 // hide!
327 this.hide();
Nils Diewald4c221252015-04-21 20:19:25 +0000328 });
329
330 // TODO: Highlight the old value!
331 menu.show();
332 menu.focus();
333 },
334
335 /**
336 * Get or set the match operator
337 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000338 matchop : function (match) {
339 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200340 var m = match.replace(/^match:/, '');
341 if (
342 (this._type === undefined)
343 ||
344 (
345 (this._type === 'string' || this._type === 'regex') &&
346 KorAP._validStringMatchRE.test(m)
347 )
348 ||
349 (this._type === 'date' && KorAP._validDateMatchRE.test(m))
350 ) {
351 this._matchop = m;
352 }
353 else {
354 this._matchop = "eq";
355 };
Nils Diewald4c221252015-04-21 20:19:25 +0000356
Akrone4961b12017-05-10 21:04:46 +0200357 this._changed();
358 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000359 };
360 return this._matchop || "eq";
361 },
362
Nils Diewald4c221252015-04-21 20:19:25 +0000363
364 // Click on the match operator, show me the menu
365 _changeMatchop : function (e) {
366 var menu = KorAP._vcMatchopMenu[this.type()];
367
368 if (menu === undefined) {
Akrone4961b12017-05-10 21:04:46 +0200369 KorAP.log(0, "Unable to init menu for " + this.type());
370 return;
Nils Diewald4c221252015-04-21 20:19:25 +0000371 };
372
373 // Insert menu
374 this._element.insertBefore(
Akrone4961b12017-05-10 21:04:46 +0200375 menu.element(),
376 this._matchopE
Nils Diewald4c221252015-04-21 20:19:25 +0000377 );
378
379 // Release event
380 var that = this;
381 menu.released(function (mo) {
Akrone4961b12017-05-10 21:04:46 +0200382 that.matchop(mo).update();
383 this.hide();
Nils Diewald4c221252015-04-21 20:19:25 +0000384 });
385
386 menu.show();
387 menu.focus();
388 },
389
390
391 /**
392 * Get or set the type
393 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000394 type : function (type) {
395 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200396 this._type = type;
397 this._changed();
398 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000399 };
400 return this._type || "string";
401 },
402
Nils Diewald4c221252015-04-21 20:19:25 +0000403
404 /**
405 * Get or set the value
406 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000407 value : function (value) {
408 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200409 if (this._type === 'date' && !KorAP._validDateRE.test(value)) {
410 delete this._value;
411 }
412 else {
413 this._value = value;
414 };
415 this._changed();
416 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000417 };
418 return this._value;
419 },
420
Nils Diewald4c221252015-04-21 20:19:25 +0000421
422 // Click on the match operator, show me the menu
423 _changeValue : function (e) {
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000424 var v = this.value();
425 var that = this;
Nils Diewald87507832015-05-01 23:36:41 +0000426
Nils Diewald7148c6f2015-05-04 15:07:53 +0000427 // Show datepicker
Nils Diewald87507832015-05-01 23:36:41 +0000428 if (this.type() === 'date') {
Akrone4961b12017-05-10 21:04:46 +0200429 var dp = KorAP._vcDatePicker;
Akron516b6f92018-01-03 17:46:59 +0100430 dp.fromString(v);
Nils Diewald87507832015-05-01 23:36:41 +0000431
Akrone4961b12017-05-10 21:04:46 +0200432 // Todo: change this
433 dp.onclick(function (selected) {
Nils Diewald87507832015-05-01 23:36:41 +0000434
Akrone4961b12017-05-10 21:04:46 +0200435 // There are values selected
436 if (selected['year']) {
437 that.value(this.toString());
438 that.update();
439 return;
440 };
Nils Diewald87507832015-05-01 23:36:41 +0000441
Akrone4961b12017-05-10 21:04:46 +0200442 // Remove datepicker
443 that._element.removeChild(
444 dp.element()
445 );
446 });
Nils Diewald87507832015-05-01 23:36:41 +0000447
Akrone4961b12017-05-10 21:04:46 +0200448 // Get element of the date picker
449 var dpElem = dp.show();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000450
Akrone4961b12017-05-10 21:04:46 +0200451 this._element.insertBefore(
452 dpElem,
453 this._valueE
454 );
Nils Diewald7148c6f2015-05-04 15:07:53 +0000455
Akron516b6f92018-01-03 17:46:59 +0100456 dp.input().focus();
Nils Diewald87507832015-05-01 23:36:41 +0000457 }
458 else {
Akrone4961b12017-05-10 21:04:46 +0200459 var regex = this.type() === 'regex' ? true : false;
460 var str = stringValClass.create(this.value(), regex);
461 var strElem = str.element();
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000462
Akrone4961b12017-05-10 21:04:46 +0200463 str.store = function (value, regex) {
464 that.value(value);
465 if (regex === true)
466 that.type('regex');
467 else
468 that.type('string');
469
470 that._element.removeChild(
471 this._element
472 );
473 that.update();
474 };
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000475
Akrone4961b12017-05-10 21:04:46 +0200476 // Insert element
477 this._element.insertBefore(
478 strElem,
479 this._valueE
480 );
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000481
Akrone4961b12017-05-10 21:04:46 +0200482 str.focus();
Nils Diewald87507832015-05-01 23:36:41 +0000483 };
Nils Diewald4c221252015-04-21 20:19:25 +0000484 },
485
486
Nils Diewald0e6992a2015-04-14 20:13:52 +0000487 rewrites : function () {
488 return this._rewrites;
489 },
490
Akronea4e9052017-07-06 16:12:05 +0200491 rewrite : function (value) {
492 if (typeof value === 'string') {
493 value = [{
494 "@type" : "koral:rewrite",
495 "operation" : "operation:" + value,
496 "src" : "Kalamar"
497 }];
498 };
499 this._rewrites = rewriteListClass.create(value);
500 },
501
502 // Remove rewrite marker when the data changes
Nils Diewald0e6992a2015-04-14 20:13:52 +0000503 _changed : function () {
504 this.__changed = true;
Nils Diewald4347ee92015-05-04 20:32:48 +0000505
Nils Diewald0e6992a2015-04-14 20:13:52 +0000506 if (this._rewrites === undefined)
Akrone4961b12017-05-10 21:04:46 +0200507 return;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000508
Akronea4e9052017-07-06 16:12:05 +0200509 delete this["_rewrites"];
Nils Diewald0e6992a2015-04-14 20:13:52 +0000510
511 if (this._element === undefined)
Akrone4961b12017-05-10 21:04:46 +0200512 return;
Akronea4e9052017-07-06 16:12:05 +0200513
Nils Diewald0e6992a2015-04-14 20:13:52 +0000514 this._element.classList.remove("rewritten");
515 },
516
Nils Diewald4c221252015-04-21 20:19:25 +0000517
Nils Diewald0e6992a2015-04-14 20:13:52 +0000518 toJson : function () {
519 if (!this.matchop() || !this.key())
Akrone4961b12017-05-10 21:04:46 +0200520 return {};
Nils Diewald0e6992a2015-04-14 20:13:52 +0000521
522 return {
Akrone4961b12017-05-10 21:04:46 +0200523 "@type" : "koral:" + this.ldType(),
524 "key" : this.key(),
525 "match" : "match:" + this.matchop(),
526 "value" : this.value() || '',
527 "type" : "type:" + this.type()
Nils Diewald0e6992a2015-04-14 20:13:52 +0000528 };
529 },
530
Nils Diewald4c221252015-04-21 20:19:25 +0000531
Nils Diewald0e6992a2015-04-14 20:13:52 +0000532 toQuery : function () {
533 if (!this.matchop() || !this.key())
Akrone4961b12017-05-10 21:04:46 +0200534 return "";
Nils Diewald0e6992a2015-04-14 20:13:52 +0000535
536 // Build doc string based on key
537 var string = this.key() + ' ';
538
539 // Add match operator
540 switch (this.matchop()) {
541 case "ne":
Akrone4961b12017-05-10 21:04:46 +0200542 string += '!=';
543 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000544 case "contains":
Akrone4961b12017-05-10 21:04:46 +0200545 string += '~';
546 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000547 case "excludes":
Akrone4961b12017-05-10 21:04:46 +0200548 string += '!~';
549 break;
Akron6a535d42015-08-26 20:16:58 +0200550 case "containsnot":
Akrone4961b12017-05-10 21:04:46 +0200551 string += '!~';
552 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000553 case "geq":
Akrone4961b12017-05-10 21:04:46 +0200554 string += 'since';
555 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000556 case "leq":
Akrone4961b12017-05-10 21:04:46 +0200557 string += 'until';
558 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000559 default:
Akrone4961b12017-05-10 21:04:46 +0200560 string += (this.type() == 'date') ? 'in' : '=';
561 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000562 };
563
564 string += ' ';
565
566 // Add value
567 switch (this.type()) {
568 case "date":
Akrone4961b12017-05-10 21:04:46 +0200569 return string + this.value();
570 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000571 case "regex":
Akron8778f5d2017-06-30 21:25:55 +0200572 return string + '/' + this.value().escapeRegex() + '/';
Akrone4961b12017-05-10 21:04:46 +0200573 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000574 case "string":
Akrone4961b12017-05-10 21:04:46 +0200575 return string + '"' + this.value().quote() + '"';
576 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000577 };
578
579 return "";
580 }
581 };
582});