blob: 8202fb5b3d5ecf8598df580371f536e949a6cf11 [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',
Akron5d4f2e42024-12-16 09:10:27 +010010 'vc/intval',
Akron587e4d92018-08-31 12:44:26 +020011 'vc/docgroupref',
Nils Diewald4c221252015-04-21 20:19:25 +000012 'util'
Akron5d4f2e42024-12-16 09:10:27 +010013], function (jsonldClass, rewriteListClass, stringValClass, intValClass, docGroupRefClass) {
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000014
Akron1bdf5272018-07-24 18:51:30 +020015 /*
16 * TODO:
17 * Improve error handling by using window.onerror() to
18 * capture thrown errors and log them.
19 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000020
Akron31d89942018-04-06 16:44:51 +020021 const errstr802 = "Match type is not supported by value type";
Akron1bdf5272018-07-24 18:51:30 +020022 const errstr804 = "Unknown value type";
Akron0b489ad2018-02-02 16:49:32 +010023 const loc = KorAP.Locale;
Nils Diewald4c221252015-04-21 20:19:25 +000024 loc.EMPTY = loc.EMPTY || '⋯';
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000025
Nils Diewald0e6992a2015-04-14 20:13:52 +000026 return {
Nils Diewald4c221252015-04-21 20:19:25 +000027
28 // The JSON-LD type
Nils Diewald0e6992a2015-04-14 20:13:52 +000029 _ldType : "doc",
Nils Diewald4c221252015-04-21 20:19:25 +000030
31 // The object ... maybe not important
Nils Diewald0e6992a2015-04-14 20:13:52 +000032 _obj : function () { return '???'; /*KorAP.Doc*/ },
33
Akron88d237e2020-10-21 08:05:18 +020034
Nils Diewald4c221252015-04-21 20:19:25 +000035 /**
36 * Create a new document criterion
37 * by passing the parent object and a json construct.
38 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000039 create : function (parent, json) {
Nils Diewald4c221252015-04-21 20:19:25 +000040
41 // Create the object, inheriting from Json-LD class
Akron88d237e2020-10-21 08:05:18 +020042 const obj = Object(jsonldClass).
43 create().
44 upgradeTo(this).
45 fromJson(json);
Nils Diewald4c221252015-04-21 20:19:25 +000046
Akron55a343b2018-04-06 19:57:36 +020047 if (obj === undefined) {
48 console.log(json);
Akrond2474aa2018-08-28 12:06:27 +020049 return;
Akron55a343b2018-04-06 19:57:36 +020050 };
51
Nils Diewald4c221252015-04-21 20:19:25 +000052 // Bind the parent
Nils Diewald0e6992a2015-04-14 20:13:52 +000053 if (parent !== undefined)
Akrone4961b12017-05-10 21:04:46 +020054 obj._parent = parent;
Nils Diewald0e6992a2015-04-14 20:13:52 +000055
56 obj.__changed = true;
57 return obj;
58 },
59
Akron88d237e2020-10-21 08:05:18 +020060
Nils Diewald4c221252015-04-21 20:19:25 +000061 /**
62 * Update the elements content.
63 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000064 update : function () {
Akron88d237e2020-10-21 08:05:18 +020065 const t = this;
66
Akron24aa0052020-11-10 11:00:34 +010067 if (t._el === undefined)
Akron88d237e2020-10-21 08:05:18 +020068 return t.element();
Nils Diewald0e6992a2015-04-14 20:13:52 +000069
70 // Get element
Akron24aa0052020-11-10 11:00:34 +010071 const e = t._el;
Nils Diewald0e6992a2015-04-14 20:13:52 +000072
Akronb19803c2018-08-16 16:39:42 +020073 // Check if there is a change in the underlying data
Akron88d237e2020-10-21 08:05:18 +020074 if (!t.__changed)
Akronb19803c2018-08-16 16:39:42 +020075 return e;
76
Nils Diewald0e6992a2015-04-14 20:13:52 +000077 // Set ref - TODO: Cleanup!
Akron88d237e2020-10-21 08:05:18 +020078 e.refTo = t;
Nils Diewald0e6992a2015-04-14 20:13:52 +000079
Nils Diewald0e6992a2015-04-14 20:13:52 +000080
Akronb19803c2018-08-16 16:39:42 +020081 // Was rewritten
Akron88d237e2020-10-21 08:05:18 +020082 if (t.rewrites() !== undefined) {
Akronb19803c2018-08-16 16:39:42 +020083 e.classList.add("rewritten");
Nils Diewald0e6992a2015-04-14 20:13:52 +000084 };
85
Akronb19803c2018-08-16 16:39:42 +020086 // Added key
Akron88d237e2020-10-21 08:05:18 +020087 const keyE = t._keyE = document.createElement('span');
88 keyE.setAttribute('class', 'key');
Akronb19803c2018-08-16 16:39:42 +020089
90 // Change key
Akron88d237e2020-10-21 08:05:18 +020091 keyE.addEventListener('click', t._changeKey.bind(t));
Akronb19803c2018-08-16 16:39:42 +020092
Akron88d237e2020-10-21 08:05:18 +020093 if (t.key()) {
94 const k = t.key();
Akronb19803c2018-08-16 16:39:42 +020095 if (loc['VC_' + k] !== undefined)
96 k = loc['VC_' + k];
Akron88d237e2020-10-21 08:05:18 +020097 keyE.addT(k);
Akronb19803c2018-08-16 16:39:42 +020098 };
99
100 // Added match operator
Akron88d237e2020-10-21 08:05:18 +0200101 const matchopE = t._matchopE = document.createElement('span');
102 matchopE.setAttribute('data-type', t.type());
103 matchopE.setAttribute('class', 'match');
104 matchopE.addT(t.matchop());
Akronb19803c2018-08-16 16:39:42 +0200105
106 // Change matchop
Akron88d237e2020-10-21 08:05:18 +0200107 t._matchopE.addEventListener(
Akronb19803c2018-08-16 16:39:42 +0200108 'click',
Akron88d237e2020-10-21 08:05:18 +0200109 t._changeMatchop.bind(t)
Akronb19803c2018-08-16 16:39:42 +0200110 );
111
112 // Added value operator
Akron88d237e2020-10-21 08:05:18 +0200113 const valueE = t._valueE = document.createElement('span');
114 valueE.setAttribute('data-type', t.type());
115 valueE.setAttribute('class', 'value');
Akronb19803c2018-08-16 16:39:42 +0200116
Akron88d237e2020-10-21 08:05:18 +0200117 if (t.value()) {
118 valueE.addT(t.value());
Akronb19803c2018-08-16 16:39:42 +0200119 }
120 else {
Akron88d237e2020-10-21 08:05:18 +0200121 valueE.addT(loc.EMPTY);
122 valueE.classList.add('unspecified');
Akronb19803c2018-08-16 16:39:42 +0200123 };
124
125 // Change value
Akron88d237e2020-10-21 08:05:18 +0200126 valueE.addEventListener(
Akronb19803c2018-08-16 16:39:42 +0200127 'click',
Akron88d237e2020-10-21 08:05:18 +0200128 t._changeValue.bind(t)
Akronb19803c2018-08-16 16:39:42 +0200129 );
130
131 // Remove all element children
132 _removeChildren(e);
133
134 // Add spans
Akron88d237e2020-10-21 08:05:18 +0200135 e.appendChild(keyE);
136 e.appendChild(matchopE);
137 e.appendChild(valueE);
Akronb19803c2018-08-16 16:39:42 +0200138
Akron88d237e2020-10-21 08:05:18 +0200139 t.__changed = false;
Akronb19803c2018-08-16 16:39:42 +0200140
Akron88d237e2020-10-21 08:05:18 +0200141 if (t._rewrites !== undefined) {
142 e.appendChild(t._rewrites.element());
Nils Diewald0e6992a2015-04-14 20:13:52 +0000143 };
144
Akron88d237e2020-10-21 08:05:18 +0200145 if (t._parent !== undefined) {
Akronb19803c2018-08-16 16:39:42 +0200146
Akrone4961b12017-05-10 21:04:46 +0200147 // Set operators
Akron88d237e2020-10-21 08:05:18 +0200148 // Append new operators
149 e.appendChild(t.operators(
Akrone4961b12017-05-10 21:04:46 +0200150 true,
151 true,
152 true
Akron88d237e2020-10-21 08:05:18 +0200153 ).element());
Nils Diewald0e6992a2015-04-14 20:13:52 +0000154 };
155
Akron88d237e2020-10-21 08:05:18 +0200156 if (KorAP.vc){
157 KorAP.vc.element().dispatchEvent(
158 new CustomEvent('vcChange', {'detail' : t})
159 );
hebastaa0282be2018-12-05 16:58:00 +0100160 }
161
Nils Diewald0e6992a2015-04-14 20:13:52 +0000162 return e;
163 },
164
Nils Diewald4c221252015-04-21 20:19:25 +0000165
166 /**
167 * Get the associated element
168 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000169 element : function () {
Akron88d237e2020-10-21 08:05:18 +0200170 const t = this;
Akron24aa0052020-11-10 11:00:34 +0100171 if (t._el !== undefined)
172 return t._el;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000173
Akron24aa0052020-11-10 11:00:34 +0100174 t._el = document.createElement('div');
175 t._el.setAttribute('class', 'doc');
Akron88d237e2020-10-21 08:05:18 +0200176 t.update();
Akron24aa0052020-11-10 11:00:34 +0100177 return t._el;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000178 },
179
Akron88d237e2020-10-21 08:05:18 +0200180
Nils Diewald4c221252015-04-21 20:19:25 +0000181 /**
182 * Wrap a new operation around the doc element
183 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000184 wrap : function (op) {
Akron88d237e2020-10-21 08:05:18 +0200185 const parent = this.parent();
186 const group = require('vc/docgroup').create(parent);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000187 group.operation(op);
188 group.append(this);
189 group.append();
190 return parent.replaceOperand(this, group).update();
191 },
192
Akron88d237e2020-10-21 08:05:18 +0200193
Akron587e4d92018-08-31 12:44:26 +0200194 replaceWith : function (op) {
Akron88d237e2020-10-21 08:05:18 +0200195 const p = this.parent();
Akron587e4d92018-08-31 12:44:26 +0200196
197 if (p.ldType() === 'docGroup') {
Akron88d237e2020-10-21 08:05:18 +0200198 p.replaceOperand(this, op);
Akron587e4d92018-08-31 12:44:26 +0200199 }
200 else if (p.ldType() == null) {
201 p.root(op);
202 };
Akron88d237e2020-10-21 08:05:18 +0200203
Akron587e4d92018-08-31 12:44:26 +0200204 p.update();
205
206 this.destroy();
207 },
208
Akron88d237e2020-10-21 08:05:18 +0200209
Nils Diewald4c221252015-04-21 20:19:25 +0000210 /**
211 * Deserialize from json
212 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000213 fromJson : function (json) {
Akron88d237e2020-10-21 08:05:18 +0200214 const t = this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000215 if (json === undefined)
Akron88d237e2020-10-21 08:05:18 +0200216 return t;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000217
218 if (json["@type"] === undefined) {
Akrone4961b12017-05-10 21:04:46 +0200219 KorAP.log(701, "JSON-LD group has no @type attribute");
220 return;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000221 };
222
Akron5d4f2e42024-12-16 09:10:27 +0100223 if (json["value"] === undefined
224 ||
225 (typeof json["value"] != 'string'
226
227 && !(json["type"] != undefined &&
228 json["type"] == "type:integer" &&
229 typeof json["value"] == 'number')
230 )) {
Akrone4961b12017-05-10 21:04:46 +0200231 KorAP.log(805, "Value is invalid");
232 return;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000233 };
234
Akron88d237e2020-10-21 08:05:18 +0200235 let rewrite;
Akronea4e9052017-07-06 16:12:05 +0200236
Nils Diewald0e6992a2015-04-14 20:13:52 +0000237 // There is a defined key
238 if (json["key"] !== undefined &&
Akrone4961b12017-05-10 21:04:46 +0200239 typeof json["key"] === 'string') {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000240
Akrone4961b12017-05-10 21:04:46 +0200241 // Set key
Akron88d237e2020-10-21 08:05:18 +0200242 t.key(json["key"]);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000243
Akrone4961b12017-05-10 21:04:46 +0200244 // Set match operation
245 if (json["match"] !== undefined) {
246 if (typeof json["match"] === 'string') {
Akron88d237e2020-10-21 08:05:18 +0200247 t.matchop(json["match"]);
Akrone4961b12017-05-10 21:04:46 +0200248 }
249 else {
Akron31d89942018-04-06 16:44:51 +0200250 KorAP.log(802, errstr802);
Akrone4961b12017-05-10 21:04:46 +0200251 return;
252 };
253 };
Akron31d89942018-04-06 16:44:51 +0200254
Akronddc98a72018-04-06 17:33:52 +0200255 // Type is unspecified - but may be known by the menu
256 if (json["type"] === undefined && KorAP._vcKeyMenu) {
257
258 // Check the VC list if the field is known
Akron88d237e2020-10-21 08:05:18 +0200259 const type = KorAP._vcKeyMenu.typeOf(t.key());
Akronddc98a72018-04-06 17:33:52 +0200260 if (type != undefined) {
261 json["type"] = "type:" + type;
262 };
263 };
264
265 // Type is still undefined
Akron31d89942018-04-06 16:44:51 +0200266 if (json["type"] === undefined) {
Akronddc98a72018-04-06 17:33:52 +0200267
Akron31d89942018-04-06 16:44:51 +0200268 // Check match type
Akron88d237e2020-10-21 08:05:18 +0200269 if (!KorAP._validUnspecMatchRE.test(t.matchop())) {
Akron31d89942018-04-06 16:44:51 +0200270 KorAP.log(802, errstr802);
271
272 // Rewrite method
Akron88d237e2020-10-21 08:05:18 +0200273 t.matchop('eq');
Akron31d89942018-04-06 16:44:51 +0200274 rewrite = 'modification';
275 };
276
277 // Set string value
Akron88d237e2020-10-21 08:05:18 +0200278 t.value(json["value"]);
Akron31d89942018-04-06 16:44:51 +0200279 }
280
281 // Field is string type
282 else if (json["type"] == "type:string") {
Akron88d237e2020-10-21 08:05:18 +0200283 t.type("string");
Nils Diewald0e6992a2015-04-14 20:13:52 +0000284
Akrone4961b12017-05-10 21:04:46 +0200285 // Check match type
Akron88d237e2020-10-21 08:05:18 +0200286 if (!KorAP._validStringMatchRE.test(t.matchop())) {
Akron31d89942018-04-06 16:44:51 +0200287 KorAP.log(802, errstr802);
288
289 // Rewrite method
Akron88d237e2020-10-21 08:05:18 +0200290 t.matchop('eq');
Akron31d89942018-04-06 16:44:51 +0200291 rewrite = 'modification';
292 };
Akron88d237e2020-10-21 08:05:18 +0200293
Akron31d89942018-04-06 16:44:51 +0200294 // Set string value
Akron88d237e2020-10-21 08:05:18 +0200295 t.value(json["value"]);
Akron31d89942018-04-06 16:44:51 +0200296 }
297
298 // Field is specified
299 else if (json["type"] == "type:text") {
Akron88d237e2020-10-21 08:05:18 +0200300 t.type("text");
Akron31d89942018-04-06 16:44:51 +0200301
302 // Check match type
Akron88d237e2020-10-21 08:05:18 +0200303 if (!KorAP._validTextMatchRE.test(t.matchop())) {
Akron31d89942018-04-06 16:44:51 +0200304 KorAP.log(802, errstr802);
Akronea4e9052017-07-06 16:12:05 +0200305
306 // Rewrite method
Akron88d237e2020-10-21 08:05:18 +0200307 t.matchop('eq');
Akronea4e9052017-07-06 16:12:05 +0200308 rewrite = 'modification';
Akrone4961b12017-05-10 21:04:46 +0200309 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000310
Akrone4961b12017-05-10 21:04:46 +0200311 // Set string value
Akron88d237e2020-10-21 08:05:18 +0200312 t.value(json["value"]);
Akrone4961b12017-05-10 21:04:46 +0200313 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000314
Akron5d4f2e42024-12-16 09:10:27 +0100315 // Key is integer
316 else if (json["type"] == "type:integer") {
317 t.type("integer");
318
319 // Check match type
320 if (!KorAP._validIntegerMatchRE.test(t.matchop())) {
321 KorAP.log(802, errstr802);
322
323 // Rewrite method
324 t.matchop('eq');
325 rewrite = 'modification';
326 };
327
328 // Set string value
329 t.value(json["value"]);
330 }
331
332
Akrone4961b12017-05-10 21:04:46 +0200333 // Key is a date
334 else if (json["type"] === "type:date") {
Akron88d237e2020-10-21 08:05:18 +0200335 t.type("date");
Nils Diewald0e6992a2015-04-14 20:13:52 +0000336
Akrone4961b12017-05-10 21:04:46 +0200337 if (json["value"] !== undefined &&
338 KorAP._validDateRE.test(json["value"])) {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000339
Akron88d237e2020-10-21 08:05:18 +0200340 if (!KorAP._validDateMatchRE.test(t.matchop())) {
Akron31d89942018-04-06 16:44:51 +0200341 KorAP.log(802, errstr802);
Akronea4e9052017-07-06 16:12:05 +0200342
343 // Rewrite method
Akron88d237e2020-10-21 08:05:18 +0200344 t.matchop('eq');
Akronea4e9052017-07-06 16:12:05 +0200345 rewrite = 'modification';
Akrone4961b12017-05-10 21:04:46 +0200346 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000347
Akrone4961b12017-05-10 21:04:46 +0200348 // Set value
Akron88d237e2020-10-21 08:05:18 +0200349 t.value(json["value"]);
Akrone4961b12017-05-10 21:04:46 +0200350 }
351 else {
352 KorAP.log(806, "Value is not a valid date string");
353 return;
354 };
355 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000356
Akrone4961b12017-05-10 21:04:46 +0200357 // Key is a regular expression
358 else if (json["type"] === "type:regex") {
Akron88d237e2020-10-21 08:05:18 +0200359 t.type("regex");
Nils Diewald0e6992a2015-04-14 20:13:52 +0000360
Akrone4961b12017-05-10 21:04:46 +0200361 try {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000362
Akrone4961b12017-05-10 21:04:46 +0200363 // Try to create a regular expression
Akron88d237e2020-10-21 08:05:18 +0200364 let check = new RegExp(json["value"]);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000365
Akron88d237e2020-10-21 08:05:18 +0200366 if (!KorAP._validStringMatchRE.test(t.matchop())) {
Akron31d89942018-04-06 16:44:51 +0200367 KorAP.log(802, errstr802);
Akronea4e9052017-07-06 16:12:05 +0200368
369 // Rewrite method
Akron88d237e2020-10-21 08:05:18 +0200370 t.matchop('eq');
Akronea4e9052017-07-06 16:12:05 +0200371 rewrite = 'modification';
Akrone4961b12017-05-10 21:04:46 +0200372 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000373
Akron88d237e2020-10-21 08:05:18 +0200374 t.value(json["value"]);
Akrone4961b12017-05-10 21:04:46 +0200375 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000376
Akrone4961b12017-05-10 21:04:46 +0200377 catch (e) {
378 KorAP.log(807, "Value is not a valid regular expression");
379 return;
380 };
Akron88d237e2020-10-21 08:05:18 +0200381 t.type("regex");
Akrone4961b12017-05-10 21:04:46 +0200382 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000383
Akrone4961b12017-05-10 21:04:46 +0200384 else {
Akron88d237e2020-10-21 08:05:18 +0200385 KorAP.log(804, errstr804 + ": " + t.type());
386 throw new Error(errstr804 + ": " + t.type());
Akrone4961b12017-05-10 21:04:46 +0200387 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000388 };
389
Akronea4e9052017-07-06 16:12:05 +0200390 // Rewrite coming from the server
Nils Diewald0e6992a2015-04-14 20:13:52 +0000391 if (json["rewrites"] !== undefined) {
Akron88d237e2020-10-21 08:05:18 +0200392 t.rewrite(json["rewrites"]);
Akronea4e9052017-07-06 16:12:05 +0200393 }
394
395 // Rewrite coming from Kalamar
396 else if (rewrite !== undefined) {
Akron88d237e2020-10-21 08:05:18 +0200397 t.rewrite(rewrite);
Nils Diewald0e6992a2015-04-14 20:13:52 +0000398 };
Akronea4e9052017-07-06 16:12:05 +0200399
Akron88d237e2020-10-21 08:05:18 +0200400 return t;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000401 },
402
Akron88d237e2020-10-21 08:05:18 +0200403
Nils Diewald4c221252015-04-21 20:19:25 +0000404 /**
405 * Get or set the key
406 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000407 key : function (value) {
408 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200409 this._key = value;
410 this._changed();
411 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000412 };
413 return this._key;
414 },
415
Akron88d237e2020-10-21 08:05:18 +0200416
Nils Diewald4c221252015-04-21 20:19:25 +0000417 // Click on the key, show me the menu
418 _changeKey : function (e) {
Akron88d237e2020-10-21 08:05:18 +0200419 const menu = KorAP._vcKeyMenu;
Nils Diewald4c221252015-04-21 20:19:25 +0000420
421 // Insert menu
Akron24aa0052020-11-10 11:00:34 +0100422 this._el.insertBefore(
Akrone4961b12017-05-10 21:04:46 +0200423 menu.element(),
424 this._keyE
Nils Diewald4c221252015-04-21 20:19:25 +0000425 );
426
427 // Release event
Akron88d237e2020-10-21 08:05:18 +0200428 const that = this;
Nils Diewald4c221252015-04-21 20:19:25 +0000429 menu.released(function (key, type) {
Nils Diewald4c221252015-04-21 20:19:25 +0000430
Akron587e4d92018-08-31 12:44:26 +0200431 if (type === 'ref') {
432 // KorAP._delete.bind(that);
Akron88d237e2020-10-21 08:05:18 +0200433 that.replaceWith(
434 docGroupRefClass.create(that.parent())
435 );
Akron587e4d92018-08-31 12:44:26 +0200436 }
Akron88d237e2020-10-21 08:05:18 +0200437
Akron587e4d92018-08-31 12:44:26 +0200438 else {
Akron88d237e2020-10-21 08:05:18 +0200439 const doc = that.key(key).type(type);
Nils Diewald4c221252015-04-21 20:19:25 +0000440
Akron587e4d92018-08-31 12:44:26 +0200441 // This may not be compatible - then switch to default
442 doc.matchop(doc.matchop());
443 doc.value(doc.value());
444
445 // Update the doc
446 doc.update();
447 };
Nils Diewald4c221252015-04-21 20:19:25 +0000448
Akrone4961b12017-05-10 21:04:46 +0200449 // hide!
450 this.hide();
Nils Diewald4c221252015-04-21 20:19:25 +0000451 });
452
453 // TODO: Highlight the old value!
454 menu.show();
455 menu.focus();
456 },
457
Akron88d237e2020-10-21 08:05:18 +0200458
Nils Diewald4c221252015-04-21 20:19:25 +0000459 /**
460 * Get or set the match operator
461 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000462 matchop : function (match) {
Akron88d237e2020-10-21 08:05:18 +0200463 const t = this;
Akron31d89942018-04-06 16:44:51 +0200464
Nils Diewald0e6992a2015-04-14 20:13:52 +0000465 if (arguments.length === 1) {
Akron88d237e2020-10-21 08:05:18 +0200466 const m = match.replace(/^match:/, '');
Akron31d89942018-04-06 16:44:51 +0200467
Akrone4961b12017-05-10 21:04:46 +0200468 if (
Akron88d237e2020-10-21 08:05:18 +0200469 (t._type == undefined) // && KorAP._validUnspecMatchRE.test(m))
Akrone4961b12017-05-10 21:04:46 +0200470 ||
471 (
Akron88d237e2020-10-21 08:05:18 +0200472 (t._type === 'string' || t._type === 'regex') &&
Akrone4961b12017-05-10 21:04:46 +0200473 KorAP._validStringMatchRE.test(m)
474 )
475 ||
Akron88d237e2020-10-21 08:05:18 +0200476 (t._type === 'text' && KorAP._validTextMatchRE.test(m))
Akron31d89942018-04-06 16:44:51 +0200477 ||
Akron5d4f2e42024-12-16 09:10:27 +0100478 (t._type === 'integer' && KorAP._validIntegerMatchRE.test(m))
479 ||
Akron88d237e2020-10-21 08:05:18 +0200480 (t._type === 'date' && KorAP._validDateMatchRE.test(m))
Akrone4961b12017-05-10 21:04:46 +0200481 ) {
Akron88d237e2020-10-21 08:05:18 +0200482 t._matchop = m;
Akrone4961b12017-05-10 21:04:46 +0200483 }
484 else {
Akron88d237e2020-10-21 08:05:18 +0200485 t._matchop = "eq";
Akrone4961b12017-05-10 21:04:46 +0200486 };
Nils Diewald4c221252015-04-21 20:19:25 +0000487
Akron88d237e2020-10-21 08:05:18 +0200488 t._changed();
489 return t;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000490 };
Akron88d237e2020-10-21 08:05:18 +0200491
492 return t._matchop || "eq";
Nils Diewald0e6992a2015-04-14 20:13:52 +0000493 },
494
Nils Diewald4c221252015-04-21 20:19:25 +0000495
496 // Click on the match operator, show me the menu
497 _changeMatchop : function (e) {
Akron88d237e2020-10-21 08:05:18 +0200498 const menu = KorAP._vcMatchopMenu[this.type()];
Nils Diewald4c221252015-04-21 20:19:25 +0000499
500 if (menu === undefined) {
Akrone4961b12017-05-10 21:04:46 +0200501 KorAP.log(0, "Unable to init menu for " + this.type());
502 return;
Nils Diewald4c221252015-04-21 20:19:25 +0000503 };
504
505 // Insert menu
Akron24aa0052020-11-10 11:00:34 +0100506 this._el.insertBefore(
Akrone4961b12017-05-10 21:04:46 +0200507 menu.element(),
508 this._matchopE
Nils Diewald4c221252015-04-21 20:19:25 +0000509 );
510
511 // Release event
Akron88d237e2020-10-21 08:05:18 +0200512 const that = this;
Nils Diewald4c221252015-04-21 20:19:25 +0000513 menu.released(function (mo) {
Akrone4961b12017-05-10 21:04:46 +0200514 that.matchop(mo).update();
515 this.hide();
Nils Diewald4c221252015-04-21 20:19:25 +0000516 });
517
518 menu.show();
519 menu.focus();
520 },
521
522
523 /**
524 * Get or set the type
525 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000526 type : function (type) {
527 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200528 this._type = type;
529 this._changed();
530 return this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000531 };
532 return this._type || "string";
533 },
534
Nils Diewald4c221252015-04-21 20:19:25 +0000535
536 /**
537 * Get or set the value
538 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000539 value : function (value) {
Akron88d237e2020-10-21 08:05:18 +0200540 const t = this;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000541 if (arguments.length === 1) {
Akron88d237e2020-10-21 08:05:18 +0200542 if (t._type === 'date' && !KorAP._validDateRE.test(value)) {
543 delete t._value;
Akrone4961b12017-05-10 21:04:46 +0200544 }
545 else {
Akron88d237e2020-10-21 08:05:18 +0200546 t._value = value;
Akrone4961b12017-05-10 21:04:46 +0200547 };
Akron88d237e2020-10-21 08:05:18 +0200548 t._changed();
549 return t;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000550 };
Akron88d237e2020-10-21 08:05:18 +0200551 return t._value;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000552 },
553
Nils Diewald4c221252015-04-21 20:19:25 +0000554
555 // Click on the match operator, show me the menu
556 _changeValue : function (e) {
Akron88d237e2020-10-21 08:05:18 +0200557 const that = this;
Akron8db5e3a2018-05-28 19:25:26 +0200558
Nils Diewald7148c6f2015-05-04 15:07:53 +0000559 // Show datepicker
Nils Diewald87507832015-05-01 23:36:41 +0000560 if (this.type() === 'date') {
Akron88d237e2020-10-21 08:05:18 +0200561 const dp = KorAP._vcDatePicker;
562 dp.fromString(this.value());
Nils Diewald87507832015-05-01 23:36:41 +0000563
Akrone4961b12017-05-10 21:04:46 +0200564 // Todo: change this
565 dp.onclick(function (selected) {
Nils Diewald87507832015-05-01 23:36:41 +0000566
Akrone4961b12017-05-10 21:04:46 +0200567 // There are values selected
568 if (selected['year']) {
569 that.value(this.toString());
570 that.update();
571 return;
572 };
Nils Diewald87507832015-05-01 23:36:41 +0000573
Akrone4961b12017-05-10 21:04:46 +0200574 // Remove datepicker
Akron24aa0052020-11-10 11:00:34 +0100575 that._el.removeChild(
Akrone4961b12017-05-10 21:04:46 +0200576 dp.element()
577 );
578 });
Nils Diewald87507832015-05-01 23:36:41 +0000579
Akron24aa0052020-11-10 11:00:34 +0100580 this._el.insertBefore(
Akron88d237e2020-10-21 08:05:18 +0200581 dp.show(), // Get element of the date picker
Akrone4961b12017-05-10 21:04:46 +0200582 this._valueE
583 );
Nils Diewald7148c6f2015-05-04 15:07:53 +0000584
Akron516b6f92018-01-03 17:46:59 +0100585 dp.input().focus();
Nils Diewald87507832015-05-01 23:36:41 +0000586 }
Akron5d4f2e42024-12-16 09:10:27 +0100587
Nils Diewald87507832015-05-01 23:36:41 +0000588 else {
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000589
Akron5d4f2e42024-12-16 09:10:27 +0100590 let vcVal;
591
592 if (this.type() === 'integer') {
593 vcVal = intValClass.create(this.value());
594
595 vcVal.store = function (value) {
596 that.value(value);
597 that.type('integer');
598 that._el.removeChild(
599 this._el
600 );
601 that.update();
602 };
603 }
604
605 else {
606 const regex = this.type() === 'regex' ? true : false;
607 vcVal = stringValClass.create(this.value(), regex);
608
609 vcVal.store = function (value, regex) {
610 that.value(value);
611 if (regex === true)
612 that.type('regex');
613 else
614 that.type('string');
Akrone4961b12017-05-10 21:04:46 +0200615
Akron5d4f2e42024-12-16 09:10:27 +0100616 that._el.removeChild(
617 this._el
618 );
619 that.update();
620 };
Akrone4961b12017-05-10 21:04:46 +0200621 };
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000622
Akrone4961b12017-05-10 21:04:46 +0200623 // Insert element
Akron24aa0052020-11-10 11:00:34 +0100624 this._el.insertBefore(
Akron5d4f2e42024-12-16 09:10:27 +0100625 vcVal.element(),
Akrone4961b12017-05-10 21:04:46 +0200626 this._valueE
627 );
Nils Diewaldc4c4b832015-05-05 16:00:08 +0000628
Akron5d4f2e42024-12-16 09:10:27 +0100629 vcVal.focus();
Nils Diewald87507832015-05-01 23:36:41 +0000630 };
Nils Diewald4c221252015-04-21 20:19:25 +0000631 },
632
633
Nils Diewald0e6992a2015-04-14 20:13:52 +0000634 rewrites : function () {
635 return this._rewrites;
636 },
637
Akron88d237e2020-10-21 08:05:18 +0200638
Akronea4e9052017-07-06 16:12:05 +0200639 rewrite : function (value) {
640 if (typeof value === 'string') {
641 value = [{
642 "@type" : "koral:rewrite",
643 "operation" : "operation:" + value,
644 "src" : "Kalamar"
645 }];
646 };
647 this._rewrites = rewriteListClass.create(value);
648 },
649
Akron88d237e2020-10-21 08:05:18 +0200650
Akron31d89942018-04-06 16:44:51 +0200651 // Mark the underlying data as being changed.
652 // This is important for rerendering the dom.
653 // This will also remove rewrite markers, when the data
654 // change happened by the user
Nils Diewald0e6992a2015-04-14 20:13:52 +0000655 _changed : function () {
656 this.__changed = true;
Nils Diewald4347ee92015-05-04 20:32:48 +0000657
Nils Diewald0e6992a2015-04-14 20:13:52 +0000658 if (this._rewrites === undefined)
Akrone4961b12017-05-10 21:04:46 +0200659 return;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000660
Akron88d237e2020-10-21 08:05:18 +0200661 delete this["_rewrites"];
662
Akron24aa0052020-11-10 11:00:34 +0100663 if (this._el === undefined)
Akrone4961b12017-05-10 21:04:46 +0200664 return;
Akronea4e9052017-07-06 16:12:05 +0200665
Akron24aa0052020-11-10 11:00:34 +0100666 this._el.classList.remove("rewritten");
Nils Diewald0e6992a2015-04-14 20:13:52 +0000667 },
668
Nils Diewald4c221252015-04-21 20:19:25 +0000669
Nils Diewald0e6992a2015-04-14 20:13:52 +0000670 toJson : function () {
Akron88d237e2020-10-21 08:05:18 +0200671 const t = this;
672
673 if (!t.matchop() || !t.key())
Akrone4961b12017-05-10 21:04:46 +0200674 return {};
Nils Diewald0e6992a2015-04-14 20:13:52 +0000675
676 return {
Akron88d237e2020-10-21 08:05:18 +0200677 "@type" : "koral:" + t.ldType(),
678 "key" : t.key(),
679 "match" : "match:" + t.matchop(),
680 "value" : t.value() || '',
681 "type" : "type:" + t.type()
Nils Diewald0e6992a2015-04-14 20:13:52 +0000682 };
683 },
684
Akron88d237e2020-10-21 08:05:18 +0200685
hebastaa0282be2018-12-05 16:58:00 +0100686 incomplete : function () {
687 return !(this.matchop() && this.key() && this.value());
688 },
Nils Diewald4c221252015-04-21 20:19:25 +0000689
Akron88d237e2020-10-21 08:05:18 +0200690
Nils Diewald0e6992a2015-04-14 20:13:52 +0000691 toQuery : function () {
hebastaa0282be2018-12-05 16:58:00 +0100692 if (this.incomplete())
Akrone4961b12017-05-10 21:04:46 +0200693 return "";
Nils Diewald0e6992a2015-04-14 20:13:52 +0000694
695 // Build doc string based on key
Akron88d237e2020-10-21 08:05:18 +0200696 let string = this.key() + ' ';
Nils Diewald0e6992a2015-04-14 20:13:52 +0000697
698 // Add match operator
699 switch (this.matchop()) {
700 case "ne":
Akrone4961b12017-05-10 21:04:46 +0200701 string += '!=';
702 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000703 case "contains":
Akrone4961b12017-05-10 21:04:46 +0200704 string += '~';
705 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000706 case "excludes":
Akrone4961b12017-05-10 21:04:46 +0200707 string += '!~';
708 break;
Akron6a535d42015-08-26 20:16:58 +0200709 case "containsnot":
Akrone4961b12017-05-10 21:04:46 +0200710 string += '!~';
711 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000712 case "geq":
Akron5d4f2e42024-12-16 09:10:27 +0100713 string += (this.type() == 'date') ? 'since' : '>=';
Akrone4961b12017-05-10 21:04:46 +0200714 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000715 case "leq":
Akron5d4f2e42024-12-16 09:10:27 +0100716 string += (this.type() == 'date') ? 'until' : '<=';
Akrone4961b12017-05-10 21:04:46 +0200717 break;
Marc Kupietza5f79e62024-12-17 07:15:46 +0100718 case "lt":
719 string += '<';
720 break;
721 case "gt":
722 string += '>';
723 break;
724 default:
Akrone4961b12017-05-10 21:04:46 +0200725 string += (this.type() == 'date') ? 'in' : '=';
726 break;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000727 };
728
729 string += ' ';
730
731 // Add value
732 switch (this.type()) {
733 case "date":
Akron5d4f2e42024-12-16 09:10:27 +0100734 case "integer":
Akrone4961b12017-05-10 21:04:46 +0200735 return string + this.value();
Nils Diewald0e6992a2015-04-14 20:13:52 +0000736 case "regex":
Akron8778f5d2017-06-30 21:25:55 +0200737 return string + '/' + this.value().escapeRegex() + '/';
Nils Diewald0e6992a2015-04-14 20:13:52 +0000738 case "string":
Akron1bdf5272018-07-24 18:51:30 +0200739 case "text":
Akron0c4cd222019-07-19 16:33:34 +0200740 return string + this.value().quote();
Nils Diewald0e6992a2015-04-14 20:13:52 +0000741 };
742
743 return "";
744 }
745 };
746});