blob: 67bf37aeb2d7275a6cca1499f81577a43a15a6e4 [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 Diewald4c221252015-04-21 20:19:25 +000012 /*
13 var fieldMenu = menuClass.create([
Nils Diewald7148c6f2015-05-04 15:07:53 +000014 ['Titel', 'title', 'string'],
15 ['Untertitel', 'subTitle', 'string'],
16 ['Veröffentlichungsdatum', 'pubDate', 'date'],
17 ['Autor', 'author', 'string']
Nils Diewald4c221252015-04-21 20:19:25 +000018 ]);
19
20 fieldMenu.limit(5);
21 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000022
23 _validRegexMatchRE = new RegExp("^(?:eq|ne)$");
24
Nils Diewald4c221252015-04-21 20:19:25 +000025 var loc = KorAP.Locale;
26 loc.EMPTY = loc.EMPTY || '⋯';
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000027
Nils Diewald0e6992a2015-04-14 20:13:52 +000028 return {
Nils Diewald4c221252015-04-21 20:19:25 +000029
30 // The JSON-LD type
Nils Diewald0e6992a2015-04-14 20:13:52 +000031 _ldType : "doc",
Nils Diewald4c221252015-04-21 20:19:25 +000032
33 // The object ... maybe not important
Nils Diewald0e6992a2015-04-14 20:13:52 +000034 _obj : function () { return '???'; /*KorAP.Doc*/ },
35
Nils Diewald4c221252015-04-21 20:19:25 +000036 /**
37 * Create a new document criterion
38 * by passing the parent object and a json construct.
39 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000040 create : function (parent, json) {
Nils Diewald4c221252015-04-21 20:19:25 +000041
42 // Create the object, inheriting from Json-LD class
Nils Diewald0e6992a2015-04-14 20:13:52 +000043 var obj = Object(jsonldClass).
Akrone4961b12017-05-10 21:04:46 +020044 create().
45 upgradeTo(this).
46 fromJson(json);
Nils Diewald4c221252015-04-21 20:19:25 +000047
48 // Bind the parent
Nils Diewald0e6992a2015-04-14 20:13:52 +000049 if (parent !== undefined)
Akrone4961b12017-05-10 21:04:46 +020050 obj._parent = parent;
Nils Diewald0e6992a2015-04-14 20:13:52 +000051
52 obj.__changed = true;
53 return obj;
54 },
55
Nils Diewald4c221252015-04-21 20:19:25 +000056 /**
57 * Update the elements content.
58 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000059 update : function () {
60 if (this._element === undefined)
Akrone4961b12017-05-10 21:04:46 +020061 return this.element();
Nils Diewald0e6992a2015-04-14 20:13:52 +000062
63 // Get element
64 var e = this._element;
65
66 // Set ref - TODO: Cleanup!
67 e.refTo = this;
68
69 // Check if there is a change
70 if (this.__changed) {
71
Akrone4961b12017-05-10 21:04:46 +020072 // Was rewritten
73 if (this.rewrites() !== undefined) {
74 e.classList.add("rewritten");
75 };
Nils Diewald0e6992a2015-04-14 20:13:52 +000076
Akrone4961b12017-05-10 21:04:46 +020077 // Added key
78 this._keyE = document.createElement('span');
79 this._keyE.setAttribute('class', 'key');
Nils Diewald0e6992a2015-04-14 20:13:52 +000080
Akrone4961b12017-05-10 21:04:46 +020081 // Change key
82 this._keyE.addEventListener('click', this._changeKey.bind(this));
Nils Diewald0e6992a2015-04-14 20:13:52 +000083
Akrone4961b12017-05-10 21:04:46 +020084 if (this.key()) {
85 var k = this.key();
86 if (loc['VC_' + k] !== undefined)
87 k = loc['VC_' + k];
88 this._keyE.appendChild(document.createTextNode(k));
89 };
Nils Diewald4c221252015-04-21 20:19:25 +000090
Akrone4961b12017-05-10 21:04:46 +020091 // Added match operator
92 this._matchopE = document.createElement('span');
93 this._matchopE.setAttribute('data-type', this.type());
94 this._matchopE.setAttribute('class', 'match');
95 this._matchopE.appendChild(
96 document.createTextNode(this.matchop())
97 );
Nils Diewald0e6992a2015-04-14 20:13:52 +000098
Akrone4961b12017-05-10 21:04:46 +020099 // Change matchop
100 this._matchopE.addEventListener(
101 'click',
102 this._changeMatchop.bind(this)
103 );
Nils Diewald4c221252015-04-21 20:19:25 +0000104
Akrone4961b12017-05-10 21:04:46 +0200105 // Added value operator
106 this._valueE = document.createElement('span');
107 this._valueE.setAttribute('data-type', this.type());
108 this._valueE.setAttribute('class', 'value');
109 if (this.value()) {
110 this._valueE.appendChild(document.createTextNode(this.value()));
111 }
112 else {
113 this._valueE.appendChild(document.createTextNode(loc.EMPTY));
114 };
Nils Diewald4c221252015-04-21 20:19:25 +0000115
Akrone4961b12017-05-10 21:04:46 +0200116 // Change value
117 this._valueE.addEventListener(
118 'click',
119 this._changeValue.bind(this)
120 );
Nils Diewald4c221252015-04-21 20:19:25 +0000121
Nils Diewald0e6992a2015-04-14 20:13:52 +0000122
Akrone4961b12017-05-10 21:04:46 +0200123 // Remove all element children
124 _removeChildren(e);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000125
Akrone4961b12017-05-10 21:04:46 +0200126 // Add spans
127 e.appendChild(this._keyE);
128 e.appendChild(this._matchopE);
129 e.appendChild(this._valueE);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000130
Akrone4961b12017-05-10 21:04:46 +0200131 this.__changed = false;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000132 };
133
134 if (this._rewrites !== undefined) {
Akrone4961b12017-05-10 21:04:46 +0200135 e.appendChild(this._rewrites.element());
Nils Diewald0e6992a2015-04-14 20:13:52 +0000136 };
137
138 if (this._parent !== undefined) {
Akrone4961b12017-05-10 21:04:46 +0200139 // Set operators
140 var op = this.operators(
141 true,
142 true,
143 true
144 );
Nils Diewald0e6992a2015-04-14 20:13:52 +0000145
Akrone4961b12017-05-10 21:04:46 +0200146 // Append new operators
147 e.appendChild(op.element());
Nils Diewald0e6992a2015-04-14 20:13:52 +0000148 };
149
150 return e;
151 },
152
Nils Diewald4c221252015-04-21 20:19:25 +0000153
154 /**
155 * Get the associated element
156 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000157 element : function () {
158 if (this._element !== undefined)
Akrone4961b12017-05-10 21:04:46 +0200159 return this._element;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000160
161 this._element = document.createElement('div');
162 this._element.setAttribute('class', 'doc');
163
164 this.update();
165 return this._element;
166 },
167
Nils Diewald4c221252015-04-21 20:19:25 +0000168 /**
169 * Wrap a new operation around the doc element
170 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000171 wrap : function (op) {
172 var parent = this.parent();
Nils Diewald7c8ced22015-04-15 19:21:00 +0000173 var group = require('vc/docgroup').create(parent);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000174 group.operation(op);
175 group.append(this);
176 group.append();
177 return parent.replaceOperand(this, group).update();
178 },
179
Nils Diewald4c221252015-04-21 20:19:25 +0000180 /**
181 * Deserialize from json
182 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000183 fromJson : function (json) {
184 if (json === undefined)
Akrone4961b12017-05-10 21:04:46 +0200185 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000186
187 if (json["@type"] === undefined) {
Akrone4961b12017-05-10 21:04:46 +0200188 KorAP.log(701, "JSON-LD group has no @type attribute");
189 return;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000190 };
191
192 if (json["value"] === undefined ||
Akrone4961b12017-05-10 21:04:46 +0200193 typeof json["value"] != 'string') {
194 KorAP.log(805, "Value is invalid");
195 return;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000196 };
197
Akronea4e9052017-07-06 16:12:05 +0200198 var rewrite;
199
Nils Diewald0e6992a2015-04-14 20:13:52 +0000200 // There is a defined key
201 if (json["key"] !== undefined &&
Akrone4961b12017-05-10 21:04:46 +0200202 typeof json["key"] === 'string') {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000203
Akrone4961b12017-05-10 21:04:46 +0200204 // Set key
205 this.key(json["key"]);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000206
Akrone4961b12017-05-10 21:04:46 +0200207 // Set match operation
208 if (json["match"] !== undefined) {
209 if (typeof json["match"] === 'string') {
210 this.matchop(json["match"]);
211 }
212 else {
213 KorAP.log(802, "Match type is not supported by value type");
214 return;
215 };
216 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000217
Akrone4961b12017-05-10 21:04:46 +0200218 // Key is a string
219 if (json["type"] === undefined ||
220 json["type"] == "type:string") {
221 this.type("string");
Nils Diewald0e6992a2015-04-14 20:13:52 +0000222
Akrone4961b12017-05-10 21:04:46 +0200223 // Check match type
224 if (!KorAP._validStringMatchRE.test(this.matchop())) {
225 KorAP.log(802, "Match type is not supported by value type");
Akronea4e9052017-07-06 16:12:05 +0200226
227 // Rewrite method
228 this.matchop('eq');
229 rewrite = 'modification';
Akrone4961b12017-05-10 21:04:46 +0200230 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000231
Akrone4961b12017-05-10 21:04:46 +0200232 // Set string value
233 this.value(json["value"]);
234 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000235
Akrone4961b12017-05-10 21:04:46 +0200236 // Key is a date
237 else if (json["type"] === "type:date") {
238 this.type("date");
Nils Diewald0e6992a2015-04-14 20:13:52 +0000239
Akrone4961b12017-05-10 21:04:46 +0200240 if (json["value"] !== undefined &&
241 KorAP._validDateRE.test(json["value"])) {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000242
Akrone4961b12017-05-10 21:04:46 +0200243 if (!KorAP._validDateMatchRE.test(this.matchop())) {
244 KorAP.log(802, "Match type is not supported by value type");
Akronea4e9052017-07-06 16:12:05 +0200245
246 // Rewrite method
247 this.matchop('eq');
248 rewrite = 'modification';
Akrone4961b12017-05-10 21:04:46 +0200249 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000250
Akrone4961b12017-05-10 21:04:46 +0200251 // Set value
252 this.value(json["value"]);
253 }
254 else {
255 KorAP.log(806, "Value is not a valid date string");
256 return;
257 };
258 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000259
Akrone4961b12017-05-10 21:04:46 +0200260 // Key is a regular expression
261 else if (json["type"] === "type:regex") {
262 this.type("regex");
Nils Diewald0e6992a2015-04-14 20:13:52 +0000263
Akrone4961b12017-05-10 21:04:46 +0200264 try {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000265
Akrone4961b12017-05-10 21:04:46 +0200266 // Try to create a regular expression
267 var check = new RegExp(json["value"]);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000268
Akrone4961b12017-05-10 21:04:46 +0200269 if (!_validRegexMatchRE.test(this.matchop())) {
270 KorAP.log(802, "Match type is not supported by value type");
Akronea4e9052017-07-06 16:12:05 +0200271
272 // Rewrite method
273 this.matchop('eq');
274 rewrite = 'modification';
Akrone4961b12017-05-10 21:04:46 +0200275 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000276
Akrone4961b12017-05-10 21:04:46 +0200277 this.value(json["value"]);
278 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000279
Akrone4961b12017-05-10 21:04:46 +0200280 catch (e) {
281 KorAP.log(807, "Value is not a valid regular expression");
282 return;
283 };
284 this.type("regex");
285 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000286
Akrone4961b12017-05-10 21:04:46 +0200287 else {
288 KorAP.log(804, "Unknown value type");
289 return;
290 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000291 };
292
Akronea4e9052017-07-06 16:12:05 +0200293 // Rewrite coming from the server
Nils Diewald0e6992a2015-04-14 20:13:52 +0000294 if (json["rewrites"] !== undefined) {
Akronea4e9052017-07-06 16:12:05 +0200295 this.rewrite(json["rewrites"]);
296 }
297
298 // Rewrite coming from Kalamar
299 else if (rewrite !== undefined) {
300 this.rewrite(rewrite);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000301 };
Akronea4e9052017-07-06 16:12:05 +0200302
Nils Diewald0e6992a2015-04-14 20:13:52 +0000303 return this;
304 },
305
Nils Diewald4c221252015-04-21 20:19:25 +0000306 /**
307 * Get or set the key
308 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000309 key : function (value) {
310 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200311 this._key = value;
312 this._changed();
313 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000314 };
315 return this._key;
316 },
317
Nils Diewald4c221252015-04-21 20:19:25 +0000318 // Click on the key, show me the menu
319 _changeKey : function (e) {
320 var menu = KorAP._vcKeyMenu;
321
322 // Insert menu
323 this._element.insertBefore(
Akrone4961b12017-05-10 21:04:46 +0200324 menu.element(),
325 this._keyE
Nils Diewald4c221252015-04-21 20:19:25 +0000326 );
327
328 // Release event
329 var that = this;
330 menu.released(function (key, type) {
Akrone4961b12017-05-10 21:04:46 +0200331 var doc = that.key(key).type(type);
Nils Diewald4c221252015-04-21 20:19:25 +0000332
Akrone4961b12017-05-10 21:04:46 +0200333 // This may not be compatible - then switch to default
334 doc.matchop(doc.matchop());
335 doc.value(doc.value());
Nils Diewald4c221252015-04-21 20:19:25 +0000336
Akrone4961b12017-05-10 21:04:46 +0200337 // Update the doc
338 doc.update();
Nils Diewald4c221252015-04-21 20:19:25 +0000339
Akrone4961b12017-05-10 21:04:46 +0200340 // hide!
341 this.hide();
Nils Diewald4c221252015-04-21 20:19:25 +0000342 });
343
344 // TODO: Highlight the old value!
345 menu.show();
346 menu.focus();
347 },
348
349 /**
350 * Get or set the match operator
351 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000352 matchop : function (match) {
353 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200354 var m = match.replace(/^match:/, '');
355 if (
356 (this._type === undefined)
357 ||
358 (
359 (this._type === 'string' || this._type === 'regex') &&
360 KorAP._validStringMatchRE.test(m)
361 )
362 ||
363 (this._type === 'date' && KorAP._validDateMatchRE.test(m))
364 ) {
365 this._matchop = m;
366 }
367 else {
368 this._matchop = "eq";
369 };
Nils Diewald4c221252015-04-21 20:19:25 +0000370
Akrone4961b12017-05-10 21:04:46 +0200371 this._changed();
372 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000373 };
374 return this._matchop || "eq";
375 },
376
Nils Diewald4c221252015-04-21 20:19:25 +0000377
378 // Click on the match operator, show me the menu
379 _changeMatchop : function (e) {
380 var menu = KorAP._vcMatchopMenu[this.type()];
381
382 if (menu === undefined) {
Akrone4961b12017-05-10 21:04:46 +0200383 KorAP.log(0, "Unable to init menu for " + this.type());
384 return;
Nils Diewald4c221252015-04-21 20:19:25 +0000385 };
386
387 // Insert menu
388 this._element.insertBefore(
Akrone4961b12017-05-10 21:04:46 +0200389 menu.element(),
390 this._matchopE
Nils Diewald4c221252015-04-21 20:19:25 +0000391 );
392
393 // Release event
394 var that = this;
395 menu.released(function (mo) {
Akrone4961b12017-05-10 21:04:46 +0200396 that.matchop(mo).update();
397 this.hide();
Nils Diewald4c221252015-04-21 20:19:25 +0000398 });
399
400 menu.show();
401 menu.focus();
402 },
403
404
405 /**
406 * Get or set the type
407 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000408 type : function (type) {
409 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200410 this._type = type;
411 this._changed();
412 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000413 };
414 return this._type || "string";
415 },
416
Nils Diewald4c221252015-04-21 20:19:25 +0000417
418 /**
419 * Get or set the value
420 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000421 value : function (value) {
422 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200423 if (this._type === 'date' && !KorAP._validDateRE.test(value)) {
424 delete this._value;
425 }
426 else {
427 this._value = value;
428 };
429 this._changed();
430 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000431 };
432 return this._value;
433 },
434
Nils Diewald4c221252015-04-21 20:19:25 +0000435
436 // Click on the match operator, show me the menu
437 _changeValue : function (e) {
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000438 var v = this.value();
439 var that = this;
Nils Diewald87507832015-05-01 23:36:41 +0000440
Nils Diewald7148c6f2015-05-04 15:07:53 +0000441 // Show datepicker
Nils Diewald87507832015-05-01 23:36:41 +0000442 if (this.type() === 'date') {
Akrone4961b12017-05-10 21:04:46 +0200443 var dp = KorAP._vcDatePicker;
444 dp.fromString(v)
Nils Diewald87507832015-05-01 23:36:41 +0000445
Akrone4961b12017-05-10 21:04:46 +0200446 // Todo: change this
447 dp.onclick(function (selected) {
Nils Diewald87507832015-05-01 23:36:41 +0000448
Akrone4961b12017-05-10 21:04:46 +0200449 // There are values selected
450 if (selected['year']) {
451 that.value(this.toString());
452 that.update();
453 return;
454 };
Nils Diewald87507832015-05-01 23:36:41 +0000455
Akrone4961b12017-05-10 21:04:46 +0200456 // Remove datepicker
457 that._element.removeChild(
458 dp.element()
459 );
460 });
Nils Diewald87507832015-05-01 23:36:41 +0000461
Akrone4961b12017-05-10 21:04:46 +0200462 // Get element of the date picker
463 var dpElem = dp.show();
Nils Diewald7148c6f2015-05-04 15:07:53 +0000464
Akrone4961b12017-05-10 21:04:46 +0200465 this._element.insertBefore(
466 dpElem,
467 this._valueE
468 );
Nils Diewald7148c6f2015-05-04 15:07:53 +0000469
Akrone4961b12017-05-10 21:04:46 +0200470 dpElem.focus();
471 /*
472 dpElem.addEventListener('blur', function (e) {
473 e.halt();
474
475 // Remove datepicker
476 // TODO: If focus is not set to string input
477 that._element.removeChild(this);
478 });
479 */
Nils Diewald87507832015-05-01 23:36:41 +0000480 }
481 else {
Akrone4961b12017-05-10 21:04:46 +0200482 var regex = this.type() === 'regex' ? true : false;
483 var str = stringValClass.create(this.value(), regex);
484 var strElem = str.element();
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000485
Akrone4961b12017-05-10 21:04:46 +0200486 str.store = function (value, regex) {
487 that.value(value);
488 if (regex === true)
489 that.type('regex');
490 else
491 that.type('string');
492
493 that._element.removeChild(
494 this._element
495 );
496 that.update();
497 };
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000498
Akrone4961b12017-05-10 21:04:46 +0200499 // Insert element
500 this._element.insertBefore(
501 strElem,
502 this._valueE
503 );
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000504
Akrone4961b12017-05-10 21:04:46 +0200505 str.focus();
Nils Diewald87507832015-05-01 23:36:41 +0000506 };
Nils Diewald4c221252015-04-21 20:19:25 +0000507 },
508
509
Nils Diewald0e6992a2015-04-14 20:13:52 +0000510 rewrites : function () {
511 return this._rewrites;
512 },
513
Akronea4e9052017-07-06 16:12:05 +0200514 rewrite : function (value) {
515 if (typeof value === 'string') {
516 value = [{
517 "@type" : "koral:rewrite",
518 "operation" : "operation:" + value,
519 "src" : "Kalamar"
520 }];
521 };
522 this._rewrites = rewriteListClass.create(value);
523 },
524
525 // Remove rewrite marker when the data changes
Nils Diewald0e6992a2015-04-14 20:13:52 +0000526 _changed : function () {
527 this.__changed = true;
Nils Diewald4347ee92015-05-04 20:32:48 +0000528
Nils Diewald0e6992a2015-04-14 20:13:52 +0000529 if (this._rewrites === undefined)
Akrone4961b12017-05-10 21:04:46 +0200530 return;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000531
Akronea4e9052017-07-06 16:12:05 +0200532 delete this["_rewrites"];
Nils Diewald0e6992a2015-04-14 20:13:52 +0000533
534 if (this._element === undefined)
Akrone4961b12017-05-10 21:04:46 +0200535 return;
Akronea4e9052017-07-06 16:12:05 +0200536
Nils Diewald0e6992a2015-04-14 20:13:52 +0000537 this._element.classList.remove("rewritten");
538 },
539
Nils Diewald4c221252015-04-21 20:19:25 +0000540
Nils Diewald0e6992a2015-04-14 20:13:52 +0000541 toJson : function () {
542 if (!this.matchop() || !this.key())
Akrone4961b12017-05-10 21:04:46 +0200543 return {};
Nils Diewald0e6992a2015-04-14 20:13:52 +0000544
545 return {
Akrone4961b12017-05-10 21:04:46 +0200546 "@type" : "koral:" + this.ldType(),
547 "key" : this.key(),
548 "match" : "match:" + this.matchop(),
549 "value" : this.value() || '',
550 "type" : "type:" + this.type()
Nils Diewald0e6992a2015-04-14 20:13:52 +0000551 };
552 },
553
Nils Diewald4c221252015-04-21 20:19:25 +0000554
Nils Diewald0e6992a2015-04-14 20:13:52 +0000555 toQuery : function () {
556 if (!this.matchop() || !this.key())
Akrone4961b12017-05-10 21:04:46 +0200557 return "";
Nils Diewald0e6992a2015-04-14 20:13:52 +0000558
559 // Build doc string based on key
560 var string = this.key() + ' ';
561
562 // Add match operator
563 switch (this.matchop()) {
564 case "ne":
Akrone4961b12017-05-10 21:04:46 +0200565 string += '!=';
566 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000567 case "contains":
Akrone4961b12017-05-10 21:04:46 +0200568 string += '~';
569 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000570 case "excludes":
Akrone4961b12017-05-10 21:04:46 +0200571 string += '!~';
572 break;
Akron6a535d42015-08-26 20:16:58 +0200573 case "containsnot":
Akrone4961b12017-05-10 21:04:46 +0200574 string += '!~';
575 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000576 case "geq":
Akrone4961b12017-05-10 21:04:46 +0200577 string += 'since';
578 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000579 case "leq":
Akrone4961b12017-05-10 21:04:46 +0200580 string += 'until';
581 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000582 default:
Akrone4961b12017-05-10 21:04:46 +0200583 string += (this.type() == 'date') ? 'in' : '=';
584 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000585 };
586
587 string += ' ';
588
589 // Add value
590 switch (this.type()) {
591 case "date":
Akrone4961b12017-05-10 21:04:46 +0200592 return string + this.value();
593 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000594 case "regex":
Akron8778f5d2017-06-30 21:25:55 +0200595 return string + '/' + this.value().escapeRegex() + '/';
Akrone4961b12017-05-10 21:04:46 +0200596 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000597 case "string":
Akrone4961b12017-05-10 21:04:46 +0200598 return string + '"' + this.value().quote() + '"';
599 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000600 };
601
602 return "";
603 }
604 };
605});