blob: fd4c3185232f590a59c50db48bd1ff15fa4cf8a8 [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 */
Akron88d237e2020-10-21 08:05:18 +02004"use strict";
Nils Diewald0e6992a2015-04-14 20:13:52 +00005
Nils Diewald0e6992a2015-04-14 20:13:52 +00006define([
Nils Diewald4c221252015-04-21 20:19:25 +00007 'vc/jsonld',
8 'vc/rewritelist',
Nils Diewaldf0c4f112015-05-05 12:56:59 +00009 'vc/stringval',
Akron587e4d92018-08-31 12:44:26 +020010 'vc/docgroupref',
Nils Diewald4c221252015-04-21 20:19:25 +000011 'util'
Akron587e4d92018-08-31 12:44:26 +020012], function (jsonldClass, rewriteListClass, stringValClass, docGroupRefClass) {
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000013
Akron1bdf5272018-07-24 18:51:30 +020014 /*
15 * TODO:
16 * Improve error handling by using window.onerror() to
17 * capture thrown errors and log them.
18 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000019
Akron31d89942018-04-06 16:44:51 +020020 const errstr802 = "Match type is not supported by value type";
Akron1bdf5272018-07-24 18:51:30 +020021 const errstr804 = "Unknown value type";
Akron0b489ad2018-02-02 16:49:32 +010022 const loc = KorAP.Locale;
Nils Diewald4c221252015-04-21 20:19:25 +000023 loc.EMPTY = loc.EMPTY || '⋯';
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000024
Nils Diewald0e6992a2015-04-14 20:13:52 +000025 return {
Nils Diewald4c221252015-04-21 20:19:25 +000026
27 // The JSON-LD type
Nils Diewald0e6992a2015-04-14 20:13:52 +000028 _ldType : "doc",
Nils Diewald4c221252015-04-21 20:19:25 +000029
30 // The object ... maybe not important
Nils Diewald0e6992a2015-04-14 20:13:52 +000031 _obj : function () { return '???'; /*KorAP.Doc*/ },
32
Akron88d237e2020-10-21 08:05:18 +020033
Nils Diewald4c221252015-04-21 20:19:25 +000034 /**
35 * Create a new document criterion
36 * by passing the parent object and a json construct.
37 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000038 create : function (parent, json) {
Nils Diewald4c221252015-04-21 20:19:25 +000039
40 // Create the object, inheriting from Json-LD class
Akron88d237e2020-10-21 08:05:18 +020041 const obj = Object(jsonldClass).
42 create().
43 upgradeTo(this).
44 fromJson(json);
Nils Diewald4c221252015-04-21 20:19:25 +000045
Akron55a343b2018-04-06 19:57:36 +020046 if (obj === undefined) {
47 console.log(json);
Akrond2474aa2018-08-28 12:06:27 +020048 return;
Akron55a343b2018-04-06 19:57:36 +020049 };
50
Nils Diewald4c221252015-04-21 20:19:25 +000051 // Bind the parent
Nils Diewald0e6992a2015-04-14 20:13:52 +000052 if (parent !== undefined)
Akrone4961b12017-05-10 21:04:46 +020053 obj._parent = parent;
Nils Diewald0e6992a2015-04-14 20:13:52 +000054
55 obj.__changed = true;
56 return obj;
57 },
58
Akron88d237e2020-10-21 08:05:18 +020059
Nils Diewald4c221252015-04-21 20:19:25 +000060 /**
61 * Update the elements content.
62 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000063 update : function () {
Akron88d237e2020-10-21 08:05:18 +020064 const t = this;
65
66 if (t._element === undefined)
67 return t.element();
Nils Diewald0e6992a2015-04-14 20:13:52 +000068
69 // Get element
Akron88d237e2020-10-21 08:05:18 +020070 const e = t._element;
Nils Diewald0e6992a2015-04-14 20:13:52 +000071
Akronb19803c2018-08-16 16:39:42 +020072 // Check if there is a change in the underlying data
Akron88d237e2020-10-21 08:05:18 +020073 if (!t.__changed)
Akronb19803c2018-08-16 16:39:42 +020074 return e;
75
Nils Diewald0e6992a2015-04-14 20:13:52 +000076 // Set ref - TODO: Cleanup!
Akron88d237e2020-10-21 08:05:18 +020077 e.refTo = t;
Nils Diewald0e6992a2015-04-14 20:13:52 +000078
Nils Diewald0e6992a2015-04-14 20:13:52 +000079
Akronb19803c2018-08-16 16:39:42 +020080 // Was rewritten
Akron88d237e2020-10-21 08:05:18 +020081 if (t.rewrites() !== undefined) {
Akronb19803c2018-08-16 16:39:42 +020082 e.classList.add("rewritten");
Nils Diewald0e6992a2015-04-14 20:13:52 +000083 };
84
Akronb19803c2018-08-16 16:39:42 +020085 // Added key
Akron88d237e2020-10-21 08:05:18 +020086 const keyE = t._keyE = document.createElement('span');
87 keyE.setAttribute('class', 'key');
Akronb19803c2018-08-16 16:39:42 +020088
89 // Change key
Akron88d237e2020-10-21 08:05:18 +020090 keyE.addEventListener('click', t._changeKey.bind(t));
Akronb19803c2018-08-16 16:39:42 +020091
Akron88d237e2020-10-21 08:05:18 +020092 if (t.key()) {
93 const k = t.key();
Akronb19803c2018-08-16 16:39:42 +020094 if (loc['VC_' + k] !== undefined)
95 k = loc['VC_' + k];
Akron88d237e2020-10-21 08:05:18 +020096 keyE.addT(k);
Akronb19803c2018-08-16 16:39:42 +020097 };
98
99 // Added match operator
Akron88d237e2020-10-21 08:05:18 +0200100 const matchopE = t._matchopE = document.createElement('span');
101 matchopE.setAttribute('data-type', t.type());
102 matchopE.setAttribute('class', 'match');
103 matchopE.addT(t.matchop());
Akronb19803c2018-08-16 16:39:42 +0200104
105 // Change matchop
Akron88d237e2020-10-21 08:05:18 +0200106 t._matchopE.addEventListener(
Akronb19803c2018-08-16 16:39:42 +0200107 'click',
Akron88d237e2020-10-21 08:05:18 +0200108 t._changeMatchop.bind(t)
Akronb19803c2018-08-16 16:39:42 +0200109 );
110
111 // Added value operator
Akron88d237e2020-10-21 08:05:18 +0200112 const valueE = t._valueE = document.createElement('span');
113 valueE.setAttribute('data-type', t.type());
114 valueE.setAttribute('class', 'value');
Akronb19803c2018-08-16 16:39:42 +0200115
Akron88d237e2020-10-21 08:05:18 +0200116 if (t.value()) {
117 valueE.addT(t.value());
Akronb19803c2018-08-16 16:39:42 +0200118 }
119 else {
Akron88d237e2020-10-21 08:05:18 +0200120 valueE.addT(loc.EMPTY);
121 valueE.classList.add('unspecified');
Akronb19803c2018-08-16 16:39:42 +0200122 };
123
124 // Change value
Akron88d237e2020-10-21 08:05:18 +0200125 valueE.addEventListener(
Akronb19803c2018-08-16 16:39:42 +0200126 'click',
Akron88d237e2020-10-21 08:05:18 +0200127 t._changeValue.bind(t)
Akronb19803c2018-08-16 16:39:42 +0200128 );
129
130 // Remove all element children
131 _removeChildren(e);
132
133 // Add spans
Akron88d237e2020-10-21 08:05:18 +0200134 e.appendChild(keyE);
135 e.appendChild(matchopE);
136 e.appendChild(valueE);
Akronb19803c2018-08-16 16:39:42 +0200137
Akron88d237e2020-10-21 08:05:18 +0200138 t.__changed = false;
Akronb19803c2018-08-16 16:39:42 +0200139
Akron88d237e2020-10-21 08:05:18 +0200140 if (t._rewrites !== undefined) {
141 e.appendChild(t._rewrites.element());
Nils Diewald0e6992a2015-04-14 20:13:52 +0000142 };
143
Akron88d237e2020-10-21 08:05:18 +0200144 if (t._parent !== undefined) {
Akronb19803c2018-08-16 16:39:42 +0200145
Akrone4961b12017-05-10 21:04:46 +0200146 // Set operators
Akron88d237e2020-10-21 08:05:18 +0200147 // Append new operators
148 e.appendChild(t.operators(
Akrone4961b12017-05-10 21:04:46 +0200149 true,
150 true,
151 true
Akron88d237e2020-10-21 08:05:18 +0200152 ).element());
Nils Diewald0e6992a2015-04-14 20:13:52 +0000153 };
154
Akron88d237e2020-10-21 08:05:18 +0200155 if (KorAP.vc){
156 KorAP.vc.element().dispatchEvent(
157 new CustomEvent('vcChange', {'detail' : t})
158 );
hebastaa0282be2018-12-05 16:58:00 +0100159 }
160
Nils Diewald0e6992a2015-04-14 20:13:52 +0000161 return e;
162 },
163
Nils Diewald4c221252015-04-21 20:19:25 +0000164
165 /**
166 * Get the associated element
167 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000168 element : function () {
Akron88d237e2020-10-21 08:05:18 +0200169 const t = this;
170 if (t._element !== undefined)
171 return t._element;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000172
Akron88d237e2020-10-21 08:05:18 +0200173 t._element = document.createElement('div');
174 t._element.setAttribute('class', 'doc');
175 t.update();
176 return t._element;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000177 },
178
Akron88d237e2020-10-21 08:05:18 +0200179
Nils Diewald4c221252015-04-21 20:19:25 +0000180 /**
181 * Wrap a new operation around the doc element
182 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000183 wrap : function (op) {
Akron88d237e2020-10-21 08:05:18 +0200184 const parent = this.parent();
185 const group = require('vc/docgroup').create(parent);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000186 group.operation(op);
187 group.append(this);
188 group.append();
189 return parent.replaceOperand(this, group).update();
190 },
191
Akron88d237e2020-10-21 08:05:18 +0200192
Akron587e4d92018-08-31 12:44:26 +0200193 replaceWith : function (op) {
Akron88d237e2020-10-21 08:05:18 +0200194 const p = this.parent();
Akron587e4d92018-08-31 12:44:26 +0200195
196 if (p.ldType() === 'docGroup') {
Akron88d237e2020-10-21 08:05:18 +0200197 p.replaceOperand(this, op);
Akron587e4d92018-08-31 12:44:26 +0200198 }
199 else if (p.ldType() == null) {
200 p.root(op);
201 };
Akron88d237e2020-10-21 08:05:18 +0200202
Akron587e4d92018-08-31 12:44:26 +0200203 p.update();
204
205 this.destroy();
206 },
207
Akron88d237e2020-10-21 08:05:18 +0200208
Nils Diewald4c221252015-04-21 20:19:25 +0000209 /**
210 * Deserialize from json
211 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000212 fromJson : function (json) {
Akron88d237e2020-10-21 08:05:18 +0200213 const t = this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000214 if (json === undefined)
Akron88d237e2020-10-21 08:05:18 +0200215 return t;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000216
217 if (json["@type"] === undefined) {
Akrone4961b12017-05-10 21:04:46 +0200218 KorAP.log(701, "JSON-LD group has no @type attribute");
219 return;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000220 };
221
222 if (json["value"] === undefined ||
Akrone4961b12017-05-10 21:04:46 +0200223 typeof json["value"] != 'string') {
224 KorAP.log(805, "Value is invalid");
225 return;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000226 };
227
Akron88d237e2020-10-21 08:05:18 +0200228 let rewrite;
Akronea4e9052017-07-06 16:12:05 +0200229
Nils Diewald0e6992a2015-04-14 20:13:52 +0000230 // There is a defined key
231 if (json["key"] !== undefined &&
Akrone4961b12017-05-10 21:04:46 +0200232 typeof json["key"] === 'string') {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000233
Akrone4961b12017-05-10 21:04:46 +0200234 // Set key
Akron88d237e2020-10-21 08:05:18 +0200235 t.key(json["key"]);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000236
Akrone4961b12017-05-10 21:04:46 +0200237 // Set match operation
238 if (json["match"] !== undefined) {
239 if (typeof json["match"] === 'string') {
Akron88d237e2020-10-21 08:05:18 +0200240 t.matchop(json["match"]);
Akrone4961b12017-05-10 21:04:46 +0200241 }
242 else {
Akron31d89942018-04-06 16:44:51 +0200243 KorAP.log(802, errstr802);
Akrone4961b12017-05-10 21:04:46 +0200244 return;
245 };
246 };
Akron31d89942018-04-06 16:44:51 +0200247
Akronddc98a72018-04-06 17:33:52 +0200248 // Type is unspecified - but may be known by the menu
249 if (json["type"] === undefined && KorAP._vcKeyMenu) {
250
251 // Check the VC list if the field is known
Akron88d237e2020-10-21 08:05:18 +0200252 const type = KorAP._vcKeyMenu.typeOf(t.key());
Akronddc98a72018-04-06 17:33:52 +0200253 if (type != undefined) {
254 json["type"] = "type:" + type;
255 };
256 };
257
258 // Type is still undefined
Akron31d89942018-04-06 16:44:51 +0200259 if (json["type"] === undefined) {
Akronddc98a72018-04-06 17:33:52 +0200260
Akron31d89942018-04-06 16:44:51 +0200261 // Check match type
Akron88d237e2020-10-21 08:05:18 +0200262 if (!KorAP._validUnspecMatchRE.test(t.matchop())) {
Akron31d89942018-04-06 16:44:51 +0200263 KorAP.log(802, errstr802);
264
265 // Rewrite method
Akron88d237e2020-10-21 08:05:18 +0200266 t.matchop('eq');
Akron31d89942018-04-06 16:44:51 +0200267 rewrite = 'modification';
268 };
269
270 // Set string value
Akron88d237e2020-10-21 08:05:18 +0200271 t.value(json["value"]);
Akron31d89942018-04-06 16:44:51 +0200272 }
273
274 // Field is string type
275 else if (json["type"] == "type:string") {
Akron88d237e2020-10-21 08:05:18 +0200276 t.type("string");
Nils Diewald0e6992a2015-04-14 20:13:52 +0000277
Akrone4961b12017-05-10 21:04:46 +0200278 // Check match type
Akron88d237e2020-10-21 08:05:18 +0200279 if (!KorAP._validStringMatchRE.test(t.matchop())) {
Akron31d89942018-04-06 16:44:51 +0200280 KorAP.log(802, errstr802);
281
282 // Rewrite method
Akron88d237e2020-10-21 08:05:18 +0200283 t.matchop('eq');
Akron31d89942018-04-06 16:44:51 +0200284 rewrite = 'modification';
285 };
Akron88d237e2020-10-21 08:05:18 +0200286
Akron31d89942018-04-06 16:44:51 +0200287 // Set string value
Akron88d237e2020-10-21 08:05:18 +0200288 t.value(json["value"]);
Akron31d89942018-04-06 16:44:51 +0200289 }
290
291 // Field is specified
292 else if (json["type"] == "type:text") {
Akron88d237e2020-10-21 08:05:18 +0200293 t.type("text");
Akron31d89942018-04-06 16:44:51 +0200294
295 // Check match type
Akron88d237e2020-10-21 08:05:18 +0200296 if (!KorAP._validTextMatchRE.test(t.matchop())) {
Akron31d89942018-04-06 16:44:51 +0200297 KorAP.log(802, errstr802);
Akronea4e9052017-07-06 16:12:05 +0200298
299 // Rewrite method
Akron88d237e2020-10-21 08:05:18 +0200300 t.matchop('eq');
Akronea4e9052017-07-06 16:12:05 +0200301 rewrite = 'modification';
Akrone4961b12017-05-10 21:04:46 +0200302 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000303
Akrone4961b12017-05-10 21:04:46 +0200304 // Set string value
Akron88d237e2020-10-21 08:05:18 +0200305 t.value(json["value"]);
Akrone4961b12017-05-10 21:04:46 +0200306 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000307
Akrone4961b12017-05-10 21:04:46 +0200308 // Key is a date
309 else if (json["type"] === "type:date") {
Akron88d237e2020-10-21 08:05:18 +0200310 t.type("date");
Nils Diewald0e6992a2015-04-14 20:13:52 +0000311
Akrone4961b12017-05-10 21:04:46 +0200312 if (json["value"] !== undefined &&
313 KorAP._validDateRE.test(json["value"])) {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000314
Akron88d237e2020-10-21 08:05:18 +0200315 if (!KorAP._validDateMatchRE.test(t.matchop())) {
Akron31d89942018-04-06 16:44:51 +0200316 KorAP.log(802, errstr802);
Akronea4e9052017-07-06 16:12:05 +0200317
318 // Rewrite method
Akron88d237e2020-10-21 08:05:18 +0200319 t.matchop('eq');
Akronea4e9052017-07-06 16:12:05 +0200320 rewrite = 'modification';
Akrone4961b12017-05-10 21:04:46 +0200321 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000322
Akrone4961b12017-05-10 21:04:46 +0200323 // Set value
Akron88d237e2020-10-21 08:05:18 +0200324 t.value(json["value"]);
Akrone4961b12017-05-10 21:04:46 +0200325 }
326 else {
327 KorAP.log(806, "Value is not a valid date string");
328 return;
329 };
330 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000331
Akrone4961b12017-05-10 21:04:46 +0200332 // Key is a regular expression
333 else if (json["type"] === "type:regex") {
Akron88d237e2020-10-21 08:05:18 +0200334 t.type("regex");
Nils Diewald0e6992a2015-04-14 20:13:52 +0000335
Akrone4961b12017-05-10 21:04:46 +0200336 try {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000337
Akrone4961b12017-05-10 21:04:46 +0200338 // Try to create a regular expression
Akron88d237e2020-10-21 08:05:18 +0200339 let check = new RegExp(json["value"]);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000340
Akron88d237e2020-10-21 08:05:18 +0200341 if (!KorAP._validStringMatchRE.test(t.matchop())) {
Akron31d89942018-04-06 16:44:51 +0200342 KorAP.log(802, errstr802);
Akronea4e9052017-07-06 16:12:05 +0200343
344 // Rewrite method
Akron88d237e2020-10-21 08:05:18 +0200345 t.matchop('eq');
Akronea4e9052017-07-06 16:12:05 +0200346 rewrite = 'modification';
Akrone4961b12017-05-10 21:04:46 +0200347 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000348
Akron88d237e2020-10-21 08:05:18 +0200349 t.value(json["value"]);
Akrone4961b12017-05-10 21:04:46 +0200350 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000351
Akrone4961b12017-05-10 21:04:46 +0200352 catch (e) {
353 KorAP.log(807, "Value is not a valid regular expression");
354 return;
355 };
Akron88d237e2020-10-21 08:05:18 +0200356 t.type("regex");
Akrone4961b12017-05-10 21:04:46 +0200357 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000358
Akrone4961b12017-05-10 21:04:46 +0200359 else {
Akron88d237e2020-10-21 08:05:18 +0200360 KorAP.log(804, errstr804 + ": " + t.type());
361 throw new Error(errstr804 + ": " + t.type());
Akrone4961b12017-05-10 21:04:46 +0200362 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000363 };
364
Akronea4e9052017-07-06 16:12:05 +0200365 // Rewrite coming from the server
Nils Diewald0e6992a2015-04-14 20:13:52 +0000366 if (json["rewrites"] !== undefined) {
Akron88d237e2020-10-21 08:05:18 +0200367 t.rewrite(json["rewrites"]);
Akronea4e9052017-07-06 16:12:05 +0200368 }
369
370 // Rewrite coming from Kalamar
371 else if (rewrite !== undefined) {
Akron88d237e2020-10-21 08:05:18 +0200372 t.rewrite(rewrite);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000373 };
Akronea4e9052017-07-06 16:12:05 +0200374
Akron88d237e2020-10-21 08:05:18 +0200375 return t;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000376 },
377
Akron88d237e2020-10-21 08:05:18 +0200378
Nils Diewald4c221252015-04-21 20:19:25 +0000379 /**
380 * Get or set the key
381 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000382 key : function (value) {
383 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200384 this._key = value;
385 this._changed();
386 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000387 };
388 return this._key;
389 },
390
Akron88d237e2020-10-21 08:05:18 +0200391
Nils Diewald4c221252015-04-21 20:19:25 +0000392 // Click on the key, show me the menu
393 _changeKey : function (e) {
Akron88d237e2020-10-21 08:05:18 +0200394 const menu = KorAP._vcKeyMenu;
Nils Diewald4c221252015-04-21 20:19:25 +0000395
396 // Insert menu
397 this._element.insertBefore(
Akrone4961b12017-05-10 21:04:46 +0200398 menu.element(),
399 this._keyE
Nils Diewald4c221252015-04-21 20:19:25 +0000400 );
401
402 // Release event
Akron88d237e2020-10-21 08:05:18 +0200403 const that = this;
Nils Diewald4c221252015-04-21 20:19:25 +0000404 menu.released(function (key, type) {
Nils Diewald4c221252015-04-21 20:19:25 +0000405
Akron587e4d92018-08-31 12:44:26 +0200406 if (type === 'ref') {
407 // KorAP._delete.bind(that);
Akron88d237e2020-10-21 08:05:18 +0200408 that.replaceWith(
409 docGroupRefClass.create(that.parent())
410 );
Akron587e4d92018-08-31 12:44:26 +0200411 }
Akron88d237e2020-10-21 08:05:18 +0200412
Akron587e4d92018-08-31 12:44:26 +0200413 else {
Akron88d237e2020-10-21 08:05:18 +0200414 const doc = that.key(key).type(type);
Nils Diewald4c221252015-04-21 20:19:25 +0000415
Akron587e4d92018-08-31 12:44:26 +0200416 // This may not be compatible - then switch to default
417 doc.matchop(doc.matchop());
418 doc.value(doc.value());
419
420 // Update the doc
421 doc.update();
422 };
Nils Diewald4c221252015-04-21 20:19:25 +0000423
Akrone4961b12017-05-10 21:04:46 +0200424 // hide!
425 this.hide();
Nils Diewald4c221252015-04-21 20:19:25 +0000426 });
427
428 // TODO: Highlight the old value!
429 menu.show();
430 menu.focus();
431 },
432
Akron88d237e2020-10-21 08:05:18 +0200433
Nils Diewald4c221252015-04-21 20:19:25 +0000434 /**
435 * Get or set the match operator
436 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000437 matchop : function (match) {
Akron88d237e2020-10-21 08:05:18 +0200438 const t = this;
Akron31d89942018-04-06 16:44:51 +0200439
Nils Diewald0e6992a2015-04-14 20:13:52 +0000440 if (arguments.length === 1) {
Akron88d237e2020-10-21 08:05:18 +0200441 const m = match.replace(/^match:/, '');
Akron31d89942018-04-06 16:44:51 +0200442
Akrone4961b12017-05-10 21:04:46 +0200443 if (
Akron88d237e2020-10-21 08:05:18 +0200444 (t._type == undefined) // && KorAP._validUnspecMatchRE.test(m))
Akrone4961b12017-05-10 21:04:46 +0200445 ||
446 (
Akron88d237e2020-10-21 08:05:18 +0200447 (t._type === 'string' || t._type === 'regex') &&
Akrone4961b12017-05-10 21:04:46 +0200448 KorAP._validStringMatchRE.test(m)
449 )
450 ||
Akron88d237e2020-10-21 08:05:18 +0200451 (t._type === 'text' && KorAP._validTextMatchRE.test(m))
Akron31d89942018-04-06 16:44:51 +0200452 ||
Akron88d237e2020-10-21 08:05:18 +0200453 (t._type === 'date' && KorAP._validDateMatchRE.test(m))
Akrone4961b12017-05-10 21:04:46 +0200454 ) {
Akron88d237e2020-10-21 08:05:18 +0200455 t._matchop = m;
Akrone4961b12017-05-10 21:04:46 +0200456 }
457 else {
Akron88d237e2020-10-21 08:05:18 +0200458 t._matchop = "eq";
Akrone4961b12017-05-10 21:04:46 +0200459 };
Nils Diewald4c221252015-04-21 20:19:25 +0000460
Akron88d237e2020-10-21 08:05:18 +0200461 t._changed();
462 return t;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000463 };
Akron88d237e2020-10-21 08:05:18 +0200464
465 return t._matchop || "eq";
Nils Diewald0e6992a2015-04-14 20:13:52 +0000466 },
467
Nils Diewald4c221252015-04-21 20:19:25 +0000468
469 // Click on the match operator, show me the menu
470 _changeMatchop : function (e) {
Akron88d237e2020-10-21 08:05:18 +0200471 const menu = KorAP._vcMatchopMenu[this.type()];
Nils Diewald4c221252015-04-21 20:19:25 +0000472
473 if (menu === undefined) {
Akrone4961b12017-05-10 21:04:46 +0200474 KorAP.log(0, "Unable to init menu for " + this.type());
475 return;
Nils Diewald4c221252015-04-21 20:19:25 +0000476 };
477
478 // Insert menu
479 this._element.insertBefore(
Akrone4961b12017-05-10 21:04:46 +0200480 menu.element(),
481 this._matchopE
Nils Diewald4c221252015-04-21 20:19:25 +0000482 );
483
484 // Release event
Akron88d237e2020-10-21 08:05:18 +0200485 const that = this;
Nils Diewald4c221252015-04-21 20:19:25 +0000486 menu.released(function (mo) {
Akrone4961b12017-05-10 21:04:46 +0200487 that.matchop(mo).update();
488 this.hide();
Nils Diewald4c221252015-04-21 20:19:25 +0000489 });
490
491 menu.show();
492 menu.focus();
493 },
494
495
496 /**
497 * Get or set the type
498 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000499 type : function (type) {
500 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200501 this._type = type;
502 this._changed();
503 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000504 };
505 return this._type || "string";
506 },
507
Nils Diewald4c221252015-04-21 20:19:25 +0000508
509 /**
510 * Get or set the value
511 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000512 value : function (value) {
Akron88d237e2020-10-21 08:05:18 +0200513 const t = this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000514 if (arguments.length === 1) {
Akron88d237e2020-10-21 08:05:18 +0200515 if (t._type === 'date' && !KorAP._validDateRE.test(value)) {
516 delete t._value;
Akrone4961b12017-05-10 21:04:46 +0200517 }
518 else {
Akron88d237e2020-10-21 08:05:18 +0200519 t._value = value;
Akrone4961b12017-05-10 21:04:46 +0200520 };
Akron88d237e2020-10-21 08:05:18 +0200521 t._changed();
522 return t;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000523 };
Akron88d237e2020-10-21 08:05:18 +0200524 return t._value;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000525 },
526
Nils Diewald4c221252015-04-21 20:19:25 +0000527
528 // Click on the match operator, show me the menu
529 _changeValue : function (e) {
Akron88d237e2020-10-21 08:05:18 +0200530 const that = this;
Akron8db5e3a2018-05-28 19:25:26 +0200531
Nils Diewald7148c6f2015-05-04 15:07:53 +0000532 // Show datepicker
Nils Diewald87507832015-05-01 23:36:41 +0000533 if (this.type() === 'date') {
Akron88d237e2020-10-21 08:05:18 +0200534 const dp = KorAP._vcDatePicker;
535 dp.fromString(this.value());
Nils Diewald87507832015-05-01 23:36:41 +0000536
Akrone4961b12017-05-10 21:04:46 +0200537 // Todo: change this
538 dp.onclick(function (selected) {
Nils Diewald87507832015-05-01 23:36:41 +0000539
Akrone4961b12017-05-10 21:04:46 +0200540 // There are values selected
541 if (selected['year']) {
542 that.value(this.toString());
543 that.update();
544 return;
545 };
Nils Diewald87507832015-05-01 23:36:41 +0000546
Akrone4961b12017-05-10 21:04:46 +0200547 // Remove datepicker
548 that._element.removeChild(
549 dp.element()
550 );
551 });
Nils Diewald87507832015-05-01 23:36:41 +0000552
Akrone4961b12017-05-10 21:04:46 +0200553 this._element.insertBefore(
Akron88d237e2020-10-21 08:05:18 +0200554 dp.show(), // Get element of the date picker
Akrone4961b12017-05-10 21:04:46 +0200555 this._valueE
556 );
Nils Diewald7148c6f2015-05-04 15:07:53 +0000557
Akron516b6f92018-01-03 17:46:59 +0100558 dp.input().focus();
Nils Diewald87507832015-05-01 23:36:41 +0000559 }
Akron88d237e2020-10-21 08:05:18 +0200560
Nils Diewald87507832015-05-01 23:36:41 +0000561 else {
Akron88d237e2020-10-21 08:05:18 +0200562 const regex = this.type() === 'regex' ? true : false;
563 const str = stringValClass.create(this.value(), regex);
564 const strElem = str.element();
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000565
Akrone4961b12017-05-10 21:04:46 +0200566 str.store = function (value, regex) {
567 that.value(value);
568 if (regex === true)
569 that.type('regex');
570 else
571 that.type('string');
572
573 that._element.removeChild(
574 this._element
575 );
576 that.update();
577 };
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000578
Akrone4961b12017-05-10 21:04:46 +0200579 // Insert element
580 this._element.insertBefore(
581 strElem,
582 this._valueE
583 );
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000584
Akrone4961b12017-05-10 21:04:46 +0200585 str.focus();
Nils Diewald87507832015-05-01 23:36:41 +0000586 };
Nils Diewald4c221252015-04-21 20:19:25 +0000587 },
588
589
Nils Diewald0e6992a2015-04-14 20:13:52 +0000590 rewrites : function () {
591 return this._rewrites;
592 },
593
Akron88d237e2020-10-21 08:05:18 +0200594
Akronea4e9052017-07-06 16:12:05 +0200595 rewrite : function (value) {
596 if (typeof value === 'string') {
597 value = [{
598 "@type" : "koral:rewrite",
599 "operation" : "operation:" + value,
600 "src" : "Kalamar"
601 }];
602 };
603 this._rewrites = rewriteListClass.create(value);
604 },
605
Akron88d237e2020-10-21 08:05:18 +0200606
Akron31d89942018-04-06 16:44:51 +0200607 // Mark the underlying data as being changed.
608 // This is important for rerendering the dom.
609 // This will also remove rewrite markers, when the data
610 // change happened by the user
Nils Diewald0e6992a2015-04-14 20:13:52 +0000611 _changed : function () {
612 this.__changed = true;
Nils Diewald4347ee92015-05-04 20:32:48 +0000613
Nils Diewald0e6992a2015-04-14 20:13:52 +0000614 if (this._rewrites === undefined)
Akrone4961b12017-05-10 21:04:46 +0200615 return;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000616
Akron88d237e2020-10-21 08:05:18 +0200617 delete this["_rewrites"];
618
Nils Diewald0e6992a2015-04-14 20:13:52 +0000619 if (this._element === undefined)
Akrone4961b12017-05-10 21:04:46 +0200620 return;
Akronea4e9052017-07-06 16:12:05 +0200621
Nils Diewald0e6992a2015-04-14 20:13:52 +0000622 this._element.classList.remove("rewritten");
623 },
624
Nils Diewald4c221252015-04-21 20:19:25 +0000625
Nils Diewald0e6992a2015-04-14 20:13:52 +0000626 toJson : function () {
Akron88d237e2020-10-21 08:05:18 +0200627 const t = this;
628
629 if (!t.matchop() || !t.key())
Akrone4961b12017-05-10 21:04:46 +0200630 return {};
Nils Diewald0e6992a2015-04-14 20:13:52 +0000631
632 return {
Akron88d237e2020-10-21 08:05:18 +0200633 "@type" : "koral:" + t.ldType(),
634 "key" : t.key(),
635 "match" : "match:" + t.matchop(),
636 "value" : t.value() || '',
637 "type" : "type:" + t.type()
Nils Diewald0e6992a2015-04-14 20:13:52 +0000638 };
639 },
640
Akron88d237e2020-10-21 08:05:18 +0200641
hebastaa0282be2018-12-05 16:58:00 +0100642 incomplete : function () {
643 return !(this.matchop() && this.key() && this.value());
644 },
Nils Diewald4c221252015-04-21 20:19:25 +0000645
Akron88d237e2020-10-21 08:05:18 +0200646
Nils Diewald0e6992a2015-04-14 20:13:52 +0000647 toQuery : function () {
hebastaa0282be2018-12-05 16:58:00 +0100648 if (this.incomplete())
Akrone4961b12017-05-10 21:04:46 +0200649 return "";
Nils Diewald0e6992a2015-04-14 20:13:52 +0000650
651 // Build doc string based on key
Akron88d237e2020-10-21 08:05:18 +0200652 let string = this.key() + ' ';
Nils Diewald0e6992a2015-04-14 20:13:52 +0000653
654 // Add match operator
655 switch (this.matchop()) {
656 case "ne":
Akrone4961b12017-05-10 21:04:46 +0200657 string += '!=';
658 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000659 case "contains":
Akrone4961b12017-05-10 21:04:46 +0200660 string += '~';
661 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000662 case "excludes":
Akrone4961b12017-05-10 21:04:46 +0200663 string += '!~';
664 break;
Akron6a535d42015-08-26 20:16:58 +0200665 case "containsnot":
Akrone4961b12017-05-10 21:04:46 +0200666 string += '!~';
667 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000668 case "geq":
Akrone4961b12017-05-10 21:04:46 +0200669 string += 'since';
670 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000671 case "leq":
Akrone4961b12017-05-10 21:04:46 +0200672 string += 'until';
673 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000674 default:
Akrone4961b12017-05-10 21:04:46 +0200675 string += (this.type() == 'date') ? 'in' : '=';
676 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000677 };
678
679 string += ' ';
680
681 // Add value
682 switch (this.type()) {
683 case "date":
Akrone4961b12017-05-10 21:04:46 +0200684 return string + this.value();
Nils Diewald0e6992a2015-04-14 20:13:52 +0000685 case "regex":
Akron8778f5d2017-06-30 21:25:55 +0200686 return string + '/' + this.value().escapeRegex() + '/';
Nils Diewald0e6992a2015-04-14 20:13:52 +0000687 case "string":
Akron1bdf5272018-07-24 18:51:30 +0200688 case "text":
Akron0c4cd222019-07-19 16:33:34 +0200689 return string + this.value().quote();
Nils Diewald0e6992a2015-04-14 20:13:52 +0000690 };
691
692 return "";
693 }
694 };
695});