blob: 4bc5e13188aa5bd58b2549eab4605169d7b87ae0 [file] [log] [blame]
Nils Diewald86dad5b2015-01-28 15:09:07 +00001/**
hebastaa79d69d2018-07-24 12:13:02 +02002 * Create virtual collections with a visual user interface. This resembles the
3 * collection type objects of a KoralQuery "collection" object.
4 *
Nils Diewald4c221252015-04-21 20:19:25 +00005 * KoralQuery v0.3 is expected.
hebastaa79d69d2018-07-24 12:13:02 +02006 *
Nils Diewald86dad5b2015-01-28 15:09:07 +00007 * @author Nils Diewald
8 */
Nils Diewald2fe12e12015-03-06 16:47:06 +00009/*
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000010 * This replaces a previous version written by Mengfei Zhou
Nils Diewald2fe12e12015-03-06 16:47:06 +000011 */
Nils Diewald2fe12e12015-03-06 16:47:06 +000012
Nils Diewaldd0770492014-12-19 03:55:00 +000013/*
hebasta86759392018-07-25 15:44:37 +020014 TODO: Disable "and" or "or" in case it's followed
15 by an unspecified document
16 TODO: Add "and"-method to root to add further constraints
17 based on match-input (like clicking on a pubDate timestamp in a match)
18 TODO: Implement "persistence"-Option, injecting the current creation
19 date stamp
20 TODO: Implement vec-Type for document-id vectors like docID in [1,2,3,4 ...]
21
22 Error codes:
23 701: "JSON-LD group has no @type attribute"
24 704: "Operation needs operand list"
25 802: "Match type is not supported by value type"
26 804: "Unknown value type"
27 805: "Value is invalid"
28 806: "Value is not a valid date string"
29 807: "Value is not a valid regular expression"
30 810: "Unknown document group operation" (like 711)
31 811: "Document group expects operation" (like 703)
32 812: "Operand not supported in document group" (like 744)
33 813: "Collection type is not supported" (like 713)
34 814: "Unknown rewrite operation"
35 815: "Rewrite expects source"
36
37 Localization strings:
38 KorAP.Locale = {
39 EMPTY : '...',
40 AND : 'and',
41 OR : 'or',
42 DELETE : 'x' }
43
44 and various field names with the prefix 'VC_'
hebastaa79d69d2018-07-24 12:13:02 +020045 */
Nils Diewald86dad5b2015-01-28 15:09:07 +000046
Akronb19803c2018-08-16 16:39:42 +020047define([
48 'vc/unspecified',
49 'vc/doc',
50 'vc/docgroup',
51 'vc/docgroupref',
52 'vc/menu',
53 'vc/statistic',
54 'datepicker',
55 'buttongroup',
hebasta2535c762018-11-21 16:27:33 +010056 'panel/vcpanel',
Akronb19803c2018-08-16 16:39:42 +020057 'view/corpstatv',
Akronec6bb8e2018-08-29 13:07:56 +020058 'buttongroup',
Akronb19803c2018-08-16 16:39:42 +020059 'util'
60], function(
61 unspecDocClass,
62 docClass,
63 docGroupClass,
64 docGroupRefClass,
65 menuClass,
66 statClass,
67 dpClass,
68 buttonGrClass,
hebasta2535c762018-11-21 16:27:33 +010069 vcPanelClass,
Akronec6bb8e2018-08-29 13:07:56 +020070 corpStatVClass,
71 buttonGroupClass) {
Akronb19803c2018-08-16 16:39:42 +020072 "use strict";
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000073
Akronb19803c2018-08-16 16:39:42 +020074 KorAP._validUnspecMatchRE = new RegExp(
75 "^(?:eq|ne|contains(?:not)?|excludes)$");
76 KorAP._validStringMatchRE = new RegExp("^(?:eq|ne)$");
77 KorAP._validTextMatchRE = KorAP._validUnspecMatchRE;
78 KorAP._validTextOnlyMatchRE = new RegExp(
79 "^(?:contains(?:not)?|excludes)$");
80 KorAP._overrideStyles = false;
81 // KorAP._validDateMatchRE is defined in datepicker.js!
Nils Diewaldd0770492014-12-19 03:55:00 +000082
Akronb19803c2018-08-16 16:39:42 +020083 const loc = KorAP.Locale;
hebastaa0282be2018-12-05 16:58:00 +010084 loc.SHOW_STAT = loc.SHOW_STAT || 'Statistics';
85 loc.VERB_SHOWSTAT = loc.VERB_SHOWSTAT || 'Corpus Statistics';
Akron8a670162018-08-28 10:09:13 +020086 loc.VC_allCorpora = loc.VC_allCorpora || 'all corpora';
87 loc.VC_oneCollection = loc.VC_oneCollection || 'a virtual corpus';
Akronec6bb8e2018-08-29 13:07:56 +020088 loc.MINIMIZE = loc.MINIMIZE || 'Minimize';
Nils Diewald3a2d8022014-12-16 02:45:41 +000089
Akronb19803c2018-08-16 16:39:42 +020090 KorAP._vcKeyMenu = undefined;
91 KorAP._vcDatePicker = dpClass.create();
Nils Diewald3a2d8022014-12-16 02:45:41 +000092
Akronb19803c2018-08-16 16:39:42 +020093 // Create match menus ....
94 KorAP._vcMatchopMenu = {
95 'string' : menuClass.create([
96 [ 'eq', null ],
97 [ 'ne', null ]
98 ]),
99 'text' : menuClass.create([
100 [ 'eq', null ], // Requires exact match
101 [ 'ne', null ],
102 [ 'contains', null ], // Requires token sequence match
103 [ 'containsnot', null ]
104 ]),
105 'date' : menuClass.create([
106 [ 'eq', null ],
107 [ 'ne', null ],
108 [ 'geq', null ],
109 [ 'leq', null ]
110 ]),
111 'regex' : menuClass.create([
112 [ 'eq', null ],
113 [ 'ne', null ]
114 ])
115 };
116
117 /**
118 * Virtual Collection
119 */
120 return {
121
122 /**
123 * The JSON-LD type of the virtual collection
124 */
125 ldType : function() {
126 return null;
127 },
128
129 // Initialize virtual collection
130 _init : function(keyList) {
131
132 // Inject localized css styles
133 if (!KorAP._overrideStyles) {
134 var sheet = KorAP.newStyleSheet();
135
136 // Add css rule for OR operations
137 sheet.insertRule('.vc .docGroup[data-operation=or] > .doc::before,'
138 + '.vc .docGroup[data-operation=or] > .docGroup::before '
139 + '{ content: "' + loc.OR + '" }', 0);
140
141 // Add css rule for AND operations
142 sheet.insertRule(
143 '.vc .docGroup[data-operation=and] > .doc::before,'
144 + '.vc .docGroup[data-operation=and] > .docGroup::before '
145 + '{ content: "' + loc.AND + '" }', 1);
146
147 KorAP._overrideStyles = true;
Nils Diewald359a72c2015-04-20 17:40:29 +0000148 };
149
Akron3ad46942018-08-22 16:47:14 +0200150 var l;
151 if (keyList) {
Akronadab5e52018-08-20 13:50:53 +0200152 l = keyList.slice();
Akron3ad46942018-08-22 16:47:14 +0200153 l.unshift(['referTo', 'ref']);
154 }
155 else {
156 l = [['referTo', 'ref']];
157 }
Akronadab5e52018-08-20 13:50:53 +0200158
Akronb19803c2018-08-16 16:39:42 +0200159 // Create key menu
Akron3ad46942018-08-22 16:47:14 +0200160 KorAP._vcKeyMenu = menuClass.create(l);
Akronb19803c2018-08-16 16:39:42 +0200161 KorAP._vcKeyMenu.limit(6);
Akron712733a2018-04-05 18:17:47 +0200162
Akronb19803c2018-08-16 16:39:42 +0200163 return this;
164 },
Nils Diewald359a72c2015-04-20 17:40:29 +0000165
Akronb19803c2018-08-16 16:39:42 +0200166 /**
167 * Create a new virtual collection.
168 */
169 create : function(keyList) {
170 var obj = Object.create(this)._init(keyList);
171 obj._root = unspecDocClass.create(obj);
172 return obj;
173 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000174
Akron8a670162018-08-28 10:09:13 +0200175
Akronb19803c2018-08-16 16:39:42 +0200176 /**
177 * Create and render a new virtual collection based on a KoralQuery
178 * collection document
179 */
180 fromJson : function(json) {
181 if (json !== undefined) {
182 // Parse root document
183 if (json['@type'] == 'koral:doc') {
184 this._root = docClass.create(this, json);
hebastaa79d69d2018-07-24 12:13:02 +0200185 }
Akronb19803c2018-08-16 16:39:42 +0200186 // parse root group
187 else if (json['@type'] == 'koral:docGroup') {
188 this._root = docGroupClass.create(this, json);
189 }
190
191 // parse root reference
192 else if (json['@type'] == 'koral:docGroupRef') {
193 this._root = docGroupRefClass.create(this, json);
194 }
195
196 // Unknown collection type
197 else {
198 KorAP.log(813, "Collection type is not supported");
199 return;
200 };
201 }
202
203 else {
204 // Add unspecified object
205 this._root = unspecDocClass.create(this);
Nils Diewald845282c2015-05-14 07:53:03 +0000206 };
Akronb19803c2018-08-16 16:39:42 +0200207
208 // Init element and update
209 this.update();
210
211 return this;
212 },
Akrond2474aa2018-08-28 12:06:27 +0200213
214
215 // Check if the virtual corpus contains a rerite
216 wasRewritten : function (obj) {
217
218 var obj;
219 if (arguments.length === 1) {
220 obj = arguments[0];
221 }
222 else {
223 obj = this._root;
224 };
225
226 // Check for rewrite
227 if (obj.rewrites() && obj.rewrites().length() > 0) {
228 return true;
229 }
230
231 // Check recursively
232 else if (obj.ldType() === 'docGroup') {
233 for (var i in obj.operands()) {
234 if (this.wasRewritten(obj.getOperand(i))) {
235 return true;
236 }
237 };
238 };
239
240 return false;
241 },
Akron43c5cc62018-08-28 13:10:25 +0200242
Akron8a670162018-08-28 10:09:13 +0200243
Akronb19803c2018-08-16 16:39:42 +0200244 /**
hebasta3f4be922018-12-11 10:41:46 +0100245 * Clean the virtual document to unspecified doc.
Akronb19803c2018-08-16 16:39:42 +0200246 */
247 clean : function() {
248 if (this._root.ldType() !== "non") {
249 this._root.destroy();
250 this.root(unspecDocClass.create(this));
251 };
hebasta3f4be922018-12-11 10:41:46 +0100252 //update for graying corpus statistic by deleting the first line of the vc builder
253 this.update();
Akronb19803c2018-08-16 16:39:42 +0200254 return this;
255 },
256
257 /**
258 * Get or set the root object of the virtual collection.
259 */
260 root : function(obj) {
261 if (arguments.length === 1) {
Akronadab5e52018-08-20 13:50:53 +0200262 var e = this.builder();
Akronb19803c2018-08-16 16:39:42 +0200263
264 if (e.firstChild !== null) {
Akronadab5e52018-08-20 13:50:53 +0200265
266 // Object not yet set
Akronb19803c2018-08-16 16:39:42 +0200267 if (e.firstChild !== obj.element()) {
268 e.replaceChild(obj.element(), e.firstChild);
269 };
270 }
271
272 // Append root element
273 else {
274 e.appendChild(obj.element());
275 };
276
277 // Update parent child relations
278 this._root = obj;
279 obj.parent(this);
280
281 this.update();
282 };
283 return this._root;
284 },
285
Akronadab5e52018-08-20 13:50:53 +0200286
287 /**
288 * Get the wrapper element associated with the vc
289 */
290 builder : function () {
291
292 // Initialize if necessary
293 if (this._builder !== undefined)
294 return this._builder;
295
296 this.element();
297 return this._builder;
298 },
299
Akronb19803c2018-08-16 16:39:42 +0200300 /**
Akron68d28322018-08-27 15:02:42 +0200301 * Get the element associated with the virtual corpus
Akronb19803c2018-08-16 16:39:42 +0200302 */
303 element : function() {
Akronb19803c2018-08-16 16:39:42 +0200304 if (this._element !== undefined) {
305 return this._element;
306 };
307
308 this._element = document.createElement('div');
Akronec6bb8e2018-08-29 13:07:56 +0200309 this._element.classList.add('vc');
310
Akronb19803c2018-08-16 16:39:42 +0200311
Akronadab5e52018-08-20 13:50:53 +0200312 this._builder = this._element.addE('div');
313 this._builder.setAttribute('class', 'builder');
314
Akronec6bb8e2018-08-29 13:07:56 +0200315 var btn = buttonGroupClass.create(
316 ['action','button-view']
317 );
318 var that = this;
319 btn.add(loc.MINIMIZE, ['button-icon','minimize'], function () {
320 that.minimize();
321 });
322 this._element.appendChild(btn.element());
323
324
325
Akronb19803c2018-08-16 16:39:42 +0200326 // Initialize root
Akronadab5e52018-08-20 13:50:53 +0200327 this._builder.appendChild(this._root.element());
Akronb19803c2018-08-16 16:39:42 +0200328
329 // Add panel to display corpus statistic, ...
330 this.addVcInfPanel();
331
332 return this._element;
333 },
334
Akronec6bb8e2018-08-29 13:07:56 +0200335
336 /**
337 * Check, if the VC is open
338 */
339 isOpen : function () {
340 if (!this._element)
341 return false;
342 return this._element.classList.contains('active');
343 },
344
345 /**
346 * Open the VC view
347 */
348 open : function () {
349 this.element().classList.add('active');
350 if (this.onOpen)
351 this.onOpen();
352 },
353
354
355 /**
356 * Minimize the VC view
357 */
358 minimize : function () {
359 this.element().classList.remove('active');
360 if (this.onMinimize)
361 this.onMinimize();
362 },
363
364
Akronb19803c2018-08-16 16:39:42 +0200365 /**
366 * Update the whole object based on the underlying data structure
hebastaa0282be2018-12-05 16:58:00 +0100367 */
Akronb19803c2018-08-16 16:39:42 +0200368 update : function() {
369 this._root.update();
hebasta48842cf2018-12-11 12:57:38 +0100370 var vcchevent = new CustomEvent('vcChange', {'detail':this});
371 document.dispatchEvent(vcchevent);
372
Akronb19803c2018-08-16 16:39:42 +0200373 return this;
374 },
Akronb19803c2018-08-16 16:39:42 +0200375 /**
376 * Make the vc persistant by injecting the current timestamp as a
377 * creation date limit criterion.
378 * THIS IS CURRENTLY NOT USED
379 */
380 makePersistant : function() {
381 // this.root().wrapOnRoot('and');
382 var todayStr = KorAP._vcDatePicker.today();
383 var doc = docClass.create();
384 var root = this.root();
385
386 if (root.ldType() === 'docGroup' && root.operation === 'and') {
387 root.append(cond);
388 } else {
389 root.wrapOnRoot('and');
390 root.append(doc);
391 };
392
393 doc.key("creationDate");
394 doc.type("date");
395 doc.matchop("leq");
396 doc.value(todayStr);
397
398 /*
399 * { "@type" : "koral:doc", "key" : "creationDate", "type" :
400 * "type:date", "match" : "match:leq", "value" : todayStr }
401 * this.root().append(cond);
402 */
403 this.update();
404 },
405
Akron8a670162018-08-28 10:09:13 +0200406
407 // Get the reference name
408 getName : function () {
409 if (this._root.ldType() === 'non') {
410 return loc.VC_allCorpora;
411 }
412 else if (this._root.ldType() === 'docGroupRef') {
413 return this._root.ref();
414 }
415 else {
416 return loc.VC_oneCollection;
417 }
418 },
419
Akron4feec9d2018-11-20 17:00:50 +0100420 // Add "and" constraint to VC
421 addRequired : function (doc) {
422
423 let root = this.root();
424 let ldType = root.ldType();
425
426 let parent = root.parent();
427 if (ldType === 'non') {
428 parent.root(doc);
429 }
430
431 // root is doc
432 else if (
433 ldType === 'doc' ||
434 ldType === 'docGroupRef' ||
435 (ldType === 'docGroup' &&
436 root.operation() === 'or'
437 )) {
438 let group = require('vc/docgroup').create(
439 parent
440 );
441 group.operation("and");
442 group.append(root);
443 group.append(doc);
444 group.element(); // Init (seems to be necessary)
445 parent.root(group);
446 }
447
448 // root is a docGroup
449 // and is already an 'and'-Group
450 else if (ldType === 'docGroup') {
451 root.append(doc);
452 }
453
454 // Unknown
455 else {
456 console.log("Unknown root object");
457 };
458
459 // Init element and update
460 this.update();
461 },
Akron8a670162018-08-28 10:09:13 +0200462
Akronb19803c2018-08-16 16:39:42 +0200463 /**
464 * Get the generated json string
465 */
466 toJson : function() {
467 return this._root.toJson();
468 },
469
470 /**
471 * Get the generated query string
472 */
473 toQuery : function() {
474 return this._root.toQuery();
475 },
476
hebasta2535c762018-11-21 16:27:33 +0100477
478 /**
Akronb19803c2018-08-16 16:39:42 +0200479 * Add panel to display virtual corpus information
480 */
481 addVcInfPanel : function() {
Akronb19803c2018-08-16 16:39:42 +0200482 var dv = this._element.addE('div');
hebasta2535c762018-11-21 16:27:33 +0100483 //Create panel
484 this.panel = vcPanelClass.create(this);
485 dv.appendChild(this.panel.element());
hebastaa0282be2018-12-05 16:58:00 +0100486
hebasta2535c762018-11-21 16:27:33 +0100487 },
488
hebastaa0282be2018-12-05 16:58:00 +0100489 /**
hebasta48842cf2018-12-11 12:57:38 +0100490 * Checks if corpus statistic has to be disabled,
hebastaa0282be2018-12-05 16:58:00 +0100491 * and to be updated after clicking at the "reload-button"
492 */
hebasta48842cf2018-12-11 12:57:38 +0100493 checkStatActive : function(){
hebastaa0282be2018-12-05 16:58:00 +0100494 if(this.panel !== undefined && this.panel.statView !==undefined){
hebasta48842cf2018-12-11 12:57:38 +0100495 this.panel.statView.checkStatActive();
hebastaa0282be2018-12-05 16:58:00 +0100496 }
497 }
Akronb19803c2018-08-16 16:39:42 +0200498 };
499});