blob: 2c068ee3b783468088ad9c0e0e77c5b1d31d8166 [file] [log] [blame]
Nils Diewald86dad5b2015-01-28 15:09:07 +00001/**
Akroncd42a142019-07-12 18:55:37 +02002 * Create virtual corpora with a visual user interface. This resembles the
3 * corpus/collection type objects of a KoralQuery "collection"/"corpus" object.
hebastaa79d69d2018-07-24 12:13:02 +02004 *
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 */
Akron88d237e2020-10-21 08:05:18 +020046"use strict";
Nils Diewald86dad5b2015-01-28 15:09:07 +000047
Akronb19803c2018-08-16 16:39:42 +020048define([
49 'vc/unspecified',
50 'vc/doc',
51 'vc/docgroup',
52 'vc/docgroupref',
53 'vc/menu',
54 'vc/statistic',
55 'datepicker',
56 'buttongroup',
Akronaa613222019-11-19 13:57:12 +010057 'panel/vc',
58 'view/vc/corpstatv',
Akronec6bb8e2018-08-29 13:07:56 +020059 'buttongroup',
Akronb19803c2018-08-16 16:39:42 +020060 'util'
61], function(
62 unspecDocClass,
63 docClass,
64 docGroupClass,
65 docGroupRefClass,
66 menuClass,
67 statClass,
68 dpClass,
69 buttonGrClass,
hebasta2535c762018-11-21 16:27:33 +010070 vcPanelClass,
Akronec6bb8e2018-08-29 13:07:56 +020071 corpStatVClass,
72 buttonGroupClass) {
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
Akron88d237e2020-10-21 08:05:18 +0200117
Akronb19803c2018-08-16 16:39:42 +0200118 /**
Akroncd42a142019-07-12 18:55:37 +0200119 * Virtual corpus
Akronb19803c2018-08-16 16:39:42 +0200120 */
121 return {
122
123 /**
Akroncd42a142019-07-12 18:55:37 +0200124 * The JSON-LD type of the virtual corpus
Akronb19803c2018-08-16 16:39:42 +0200125 */
126 ldType : function() {
127 return null;
128 },
129
Akron88d237e2020-10-21 08:05:18 +0200130
Akroncd42a142019-07-12 18:55:37 +0200131 // Initialize virtual corpus
Akronb19803c2018-08-16 16:39:42 +0200132 _init : function(keyList) {
133
134 // Inject localized css styles
135 if (!KorAP._overrideStyles) {
Akron88d237e2020-10-21 08:05:18 +0200136
137 const sheet = KorAP.newStyleSheet();
Akronb19803c2018-08-16 16:39:42 +0200138
139 // Add css rule for OR operations
140 sheet.insertRule('.vc .docGroup[data-operation=or] > .doc::before,'
141 + '.vc .docGroup[data-operation=or] > .docGroup::before '
142 + '{ content: "' + loc.OR + '" }', 0);
143
144 // Add css rule for AND operations
145 sheet.insertRule(
146 '.vc .docGroup[data-operation=and] > .doc::before,'
147 + '.vc .docGroup[data-operation=and] > .docGroup::before '
148 + '{ content: "' + loc.AND + '" }', 1);
149
150 KorAP._overrideStyles = true;
Nils Diewald359a72c2015-04-20 17:40:29 +0000151 };
152
Akron88d237e2020-10-21 08:05:18 +0200153 let l;
Akron3ad46942018-08-22 16:47:14 +0200154 if (keyList) {
Akronadab5e52018-08-20 13:50:53 +0200155 l = keyList.slice();
Akron3ad46942018-08-22 16:47:14 +0200156 l.unshift(['referTo', 'ref']);
157 }
158 else {
159 l = [['referTo', 'ref']];
160 }
Akronadab5e52018-08-20 13:50:53 +0200161
Akronb19803c2018-08-16 16:39:42 +0200162 // Create key menu
Akron3ad46942018-08-22 16:47:14 +0200163 KorAP._vcKeyMenu = menuClass.create(l);
Akronb19803c2018-08-16 16:39:42 +0200164 KorAP._vcKeyMenu.limit(6);
Akron712733a2018-04-05 18:17:47 +0200165
Akronb19803c2018-08-16 16:39:42 +0200166 return this;
167 },
Nils Diewald359a72c2015-04-20 17:40:29 +0000168
Akron88d237e2020-10-21 08:05:18 +0200169
Akronb19803c2018-08-16 16:39:42 +0200170 /**
Akroncd42a142019-07-12 18:55:37 +0200171 * Create a new virtual corpus
Akronb19803c2018-08-16 16:39:42 +0200172 */
Akron88d237e2020-10-21 08:05:18 +0200173 create : function (keyList) {
174 const obj = Object.create(this)._init(keyList);
Akronb19803c2018-08-16 16:39:42 +0200175 obj._root = unspecDocClass.create(obj);
176 return obj;
177 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000178
Akron8a670162018-08-28 10:09:13 +0200179
Akronb19803c2018-08-16 16:39:42 +0200180 /**
Akroncd42a142019-07-12 18:55:37 +0200181 * Create and render a new virtual corpus based on a KoralQuery
182 * corpus document
Akronb19803c2018-08-16 16:39:42 +0200183 */
184 fromJson : function(json) {
Akron13af2f42019-07-25 15:06:21 +0200185
186 let obj;
187
Akronb19803c2018-08-16 16:39:42 +0200188 if (json !== undefined) {
Akron88d237e2020-10-21 08:05:18 +0200189
Akronb19803c2018-08-16 16:39:42 +0200190 // Parse root document
191 if (json['@type'] == 'koral:doc') {
Akron13af2f42019-07-25 15:06:21 +0200192 obj = docClass.create(this, json);
hebastaa79d69d2018-07-24 12:13:02 +0200193 }
Akron88d237e2020-10-21 08:05:18 +0200194
Akronb19803c2018-08-16 16:39:42 +0200195 // parse root group
196 else if (json['@type'] == 'koral:docGroup') {
Akron13af2f42019-07-25 15:06:21 +0200197 obj = docGroupClass.create(this, json);
Akronb19803c2018-08-16 16:39:42 +0200198 }
199
200 // parse root reference
201 else if (json['@type'] == 'koral:docGroupRef') {
Akron13af2f42019-07-25 15:06:21 +0200202 obj = docGroupRefClass.create(this, json);
Akronb19803c2018-08-16 16:39:42 +0200203 }
204
205 // Unknown collection type
206 else {
207 KorAP.log(813, "Collection type is not supported");
208 return;
209 };
210 }
211
212 else {
213 // Add unspecified object
Akron13af2f42019-07-25 15:06:21 +0200214 obj = unspecDocClass.create(this);
Nils Diewald845282c2015-05-14 07:53:03 +0000215 };
Akronb19803c2018-08-16 16:39:42 +0200216
217 // Init element and update
Akron13af2f42019-07-25 15:06:21 +0200218 this.root(obj);
219
Akronb19803c2018-08-16 16:39:42 +0200220 return this;
221 },
Akrond2474aa2018-08-28 12:06:27 +0200222
223
224 // Check if the virtual corpus contains a rerite
225 wasRewritten : function (obj) {
226
Akron88d237e2020-10-21 08:05:18 +0200227 if (arguments.length !== 1) {
Akrond2474aa2018-08-28 12:06:27 +0200228 obj = this._root;
229 };
230
231 // Check for rewrite
232 if (obj.rewrites() && obj.rewrites().length() > 0) {
233 return true;
234 }
235
236 // Check recursively
237 else if (obj.ldType() === 'docGroup') {
Akron678c26f2020-10-09 08:52:50 +0200238
239 // If there was a rewritten object
240 if (obj.operands().find(op => this.wasRewritten(op)) !== undefined) {
241 return true;
Akrond2474aa2018-08-28 12:06:27 +0200242 };
243 };
Akron88d237e2020-10-21 08:05:18 +0200244
Akrond2474aa2018-08-28 12:06:27 +0200245 return false;
246 },
Akron43c5cc62018-08-28 13:10:25 +0200247
Akron8a670162018-08-28 10:09:13 +0200248
Akronb19803c2018-08-16 16:39:42 +0200249 /**
hebasta3f4be922018-12-11 10:41:46 +0100250 * Clean the virtual document to unspecified doc.
Akronb19803c2018-08-16 16:39:42 +0200251 */
252 clean : function() {
Akron88d237e2020-10-21 08:05:18 +0200253 const t = this;
254 if (t._root.ldType() !== "non") {
255 t._root.destroy();
256 t.root(unspecDocClass.create(t));
Akronb19803c2018-08-16 16:39:42 +0200257 };
Akron88d237e2020-10-21 08:05:18 +0200258
259 // update for graying corpus statistic by deleting the first line of the vc builder
260 t.update();
261 return t;
Akronb19803c2018-08-16 16:39:42 +0200262 },
263
Akron88d237e2020-10-21 08:05:18 +0200264
Akronb19803c2018-08-16 16:39:42 +0200265 /**
Akroncd42a142019-07-12 18:55:37 +0200266 * Get or set the root object of the virtual corpus
Akronb19803c2018-08-16 16:39:42 +0200267 */
268 root : function(obj) {
269 if (arguments.length === 1) {
Akron88d237e2020-10-21 08:05:18 +0200270 const e = this.builder();
271
Akronb19803c2018-08-16 16:39:42 +0200272 if (e.firstChild !== null) {
Akronadab5e52018-08-20 13:50:53 +0200273
274 // Object not yet set
Akronb19803c2018-08-16 16:39:42 +0200275 if (e.firstChild !== obj.element()) {
276 e.replaceChild(obj.element(), e.firstChild);
277 };
278 }
279
280 // Append root element
281 else {
282 e.appendChild(obj.element());
283 };
284
285 // Update parent child relations
286 this._root = obj;
287 obj.parent(this);
288
289 this.update();
290 };
Akron88d237e2020-10-21 08:05:18 +0200291
Akronb19803c2018-08-16 16:39:42 +0200292 return this._root;
293 },
294
Akronadab5e52018-08-20 13:50:53 +0200295
296 /**
297 * Get the wrapper element associated with the vc
298 */
299 builder : function () {
Akron88d237e2020-10-21 08:05:18 +0200300 const t = this;
Akronadab5e52018-08-20 13:50:53 +0200301
302 // Initialize if necessary
Akron88d237e2020-10-21 08:05:18 +0200303 if (t._builder !== undefined)
304 return t._builder;
Akronadab5e52018-08-20 13:50:53 +0200305
Akron88d237e2020-10-21 08:05:18 +0200306 t.element();
307 return t._builder;
Akronadab5e52018-08-20 13:50:53 +0200308 },
309
Akron88d237e2020-10-21 08:05:18 +0200310
Akronb19803c2018-08-16 16:39:42 +0200311 /**
Akron68d28322018-08-27 15:02:42 +0200312 * Get the element associated with the virtual corpus
Akronb19803c2018-08-16 16:39:42 +0200313 */
314 element : function() {
Akron88d237e2020-10-21 08:05:18 +0200315 const t = this;
Akron24aa0052020-11-10 11:00:34 +0100316 let e = t._el;
Akronb19803c2018-08-16 16:39:42 +0200317
Akron88d237e2020-10-21 08:05:18 +0200318 if (e !== undefined)
319 return e;
Akronec6bb8e2018-08-29 13:07:56 +0200320
Akronb19803c2018-08-16 16:39:42 +0200321
Akron24aa0052020-11-10 11:00:34 +0100322 e = t._el = document.createElement('div');
Akron88d237e2020-10-21 08:05:18 +0200323 e.classList.add('vc');
Akronadab5e52018-08-20 13:50:53 +0200324
Akron88d237e2020-10-21 08:05:18 +0200325
326 t._builder = e.addE('div');
327 t._builder.setAttribute('class', 'builder');
328
329 const btn = buttonGroupClass.create(
Akronec6bb8e2018-08-29 13:07:56 +0200330 ['action','button-view']
331 );
Akronec6bb8e2018-08-29 13:07:56 +0200332
Akron88d237e2020-10-21 08:05:18 +0200333 btn.add(loc.MINIMIZE, {'cls':['button-icon','minimize']}, function () {
334 this.minimize();
335 }.bind(t));
336
337 e.appendChild(btn.element());
338
Akronb19803c2018-08-16 16:39:42 +0200339 // Initialize root
Akron88d237e2020-10-21 08:05:18 +0200340 t._builder.appendChild(t._root.element());
Akronb19803c2018-08-16 16:39:42 +0200341
342 // Add panel to display corpus statistic, ...
Akron88d237e2020-10-21 08:05:18 +0200343 t.addVcInfPanel();
Akronb19803c2018-08-16 16:39:42 +0200344
hebasta4dd77bc2019-02-07 12:57:57 +0100345 //Adds EventListener for corpus changes
Akron24aa0052020-11-10 11:00:34 +0100346 t._el.addEventListener('vcChange', function (e) {
Akron88d237e2020-10-21 08:05:18 +0200347 this.checkStatActive(e.detail);
348 }.bind(t), false);
hebasta4dd77bc2019-02-07 12:57:57 +0100349
Akron88d237e2020-10-21 08:05:18 +0200350 return e;
Akronb19803c2018-08-16 16:39:42 +0200351 },
352
Akronec6bb8e2018-08-29 13:07:56 +0200353
354 /**
355 * Check, if the VC is open
356 */
357 isOpen : function () {
Akron24aa0052020-11-10 11:00:34 +0100358 if (!this._el)
Akronec6bb8e2018-08-29 13:07:56 +0200359 return false;
Akron24aa0052020-11-10 11:00:34 +0100360 return this._el.classList.contains('active');
Akronec6bb8e2018-08-29 13:07:56 +0200361 },
362
Akron88d237e2020-10-21 08:05:18 +0200363
Akronec6bb8e2018-08-29 13:07:56 +0200364 /**
365 * Open the VC view
366 */
367 open : function () {
368 this.element().classList.add('active');
369 if (this.onOpen)
370 this.onOpen();
371 },
372
373
374 /**
375 * Minimize the VC view
376 */
377 minimize : function () {
378 this.element().classList.remove('active');
379 if (this.onMinimize)
380 this.onMinimize();
381 },
382
383
Akronb19803c2018-08-16 16:39:42 +0200384 /**
385 * Update the whole object based on the underlying data structure
hebastaa0282be2018-12-05 16:58:00 +0100386 */
Akronb19803c2018-08-16 16:39:42 +0200387 update : function() {
388 this._root.update();
Akron88d237e2020-10-21 08:05:18 +0200389 if (KorAP.vc) {
390 this.element().dispatchEvent(
391 new CustomEvent('vcChange', {'detail':this})
392 );
hebasta4dd77bc2019-02-07 12:57:57 +0100393 };
Akronb19803c2018-08-16 16:39:42 +0200394 return this;
395 },
Akron88d237e2020-10-21 08:05:18 +0200396
397
Akronb19803c2018-08-16 16:39:42 +0200398 /**
399 * Make the vc persistant by injecting the current timestamp as a
400 * creation date limit criterion.
401 * THIS IS CURRENTLY NOT USED
402 */
Akron88d237e2020-10-21 08:05:18 +0200403 /*
Akronb19803c2018-08-16 16:39:42 +0200404 makePersistant : function() {
405 // this.root().wrapOnRoot('and');
406 var todayStr = KorAP._vcDatePicker.today();
407 var doc = docClass.create();
408 var root = this.root();
409
410 if (root.ldType() === 'docGroup' && root.operation === 'and') {
411 root.append(cond);
412 } else {
413 root.wrapOnRoot('and');
414 root.append(doc);
415 };
416
417 doc.key("creationDate");
418 doc.type("date");
419 doc.matchop("leq");
420 doc.value(todayStr);
421
Akron88d237e2020-10-21 08:05:18 +0200422 // { "@type" : "koral:doc", "key" : "creationDate", "type" :
423 // "type:date", "match" : "match:leq", "value" : todayStr }
424 // this.root().append(cond);
Akronb19803c2018-08-16 16:39:42 +0200425 this.update();
426 },
Akron88d237e2020-10-21 08:05:18 +0200427 */
Akronb19803c2018-08-16 16:39:42 +0200428
Akron8a670162018-08-28 10:09:13 +0200429
430 // Get the reference name
431 getName : function () {
432 if (this._root.ldType() === 'non') {
433 return loc.VC_allCorpora;
434 }
435 else if (this._root.ldType() === 'docGroupRef') {
436 return this._root.ref();
437 }
438 else {
439 return loc.VC_oneCollection;
440 }
441 },
442
Akron88d237e2020-10-21 08:05:18 +0200443
Akron4feec9d2018-11-20 17:00:50 +0100444 // Add "and" constraint to VC
445 addRequired : function (doc) {
Akron88d237e2020-10-21 08:05:18 +0200446 const root = this.root();
447 const ldType = root.ldType();
448 const parent = root.parent();
Akron4feec9d2018-11-20 17:00:50 +0100449
Akron4feec9d2018-11-20 17:00:50 +0100450 if (ldType === 'non') {
451 parent.root(doc);
452 }
453
454 // root is doc
455 else if (
456 ldType === 'doc' ||
457 ldType === 'docGroupRef' ||
458 (ldType === 'docGroup' &&
459 root.operation() === 'or'
460 )) {
Akron88d237e2020-10-21 08:05:18 +0200461 const group = require('vc/docgroup').create(
Akron4feec9d2018-11-20 17:00:50 +0100462 parent
463 );
464 group.operation("and");
465 group.append(root);
466 group.append(doc);
467 group.element(); // Init (seems to be necessary)
468 parent.root(group);
469 }
470
471 // root is a docGroup
472 // and is already an 'and'-Group
473 else if (ldType === 'docGroup') {
474 root.append(doc);
475 }
476
477 // Unknown
478 else {
479 console.log("Unknown root object");
480 };
481
482 // Init element and update
483 this.update();
484 },
Akron8a670162018-08-28 10:09:13 +0200485
Akron88d237e2020-10-21 08:05:18 +0200486
Akronb19803c2018-08-16 16:39:42 +0200487 /**
488 * Get the generated json string
489 */
Akron88d237e2020-10-21 08:05:18 +0200490 toJson : function () {
Akronb19803c2018-08-16 16:39:42 +0200491 return this._root.toJson();
492 },
493
Akron88d237e2020-10-21 08:05:18 +0200494
Akronb19803c2018-08-16 16:39:42 +0200495 /**
496 * Get the generated query string
497 */
Akron88d237e2020-10-21 08:05:18 +0200498 toQuery : function () {
Akronb19803c2018-08-16 16:39:42 +0200499 return this._root.toQuery();
500 },
501
hebasta2535c762018-11-21 16:27:33 +0100502
503 /**
Akronb19803c2018-08-16 16:39:42 +0200504 * Add panel to display virtual corpus information
505 */
Akron88d237e2020-10-21 08:05:18 +0200506 addVcInfPanel : function () {
507 // Create panel
hebasta2535c762018-11-21 16:27:33 +0100508 this.panel = vcPanelClass.create(this);
Akron24aa0052020-11-10 11:00:34 +0100509 this._el.addE('div').appendChild(this.panel.element());
hebastaa0282be2018-12-05 16:58:00 +0100510
hebasta2535c762018-11-21 16:27:33 +0100511 },
512
hebastaa0282be2018-12-05 16:58:00 +0100513 /**
hebasta48842cf2018-12-11 12:57:38 +0100514 * Checks if corpus statistic has to be disabled,
hebastaa0282be2018-12-05 16:58:00 +0100515 * and to be updated after clicking at the "reload-button"
516 */
Akron88d237e2020-10-21 08:05:18 +0200517 checkStatActive : function (){
518 if (this.panel !== undefined && this.panel.statView !== undefined){
hebasta48842cf2018-12-11 12:57:38 +0100519 this.panel.statView.checkStatActive();
Akron88d237e2020-10-21 08:05:18 +0200520 };
hebastaa0282be2018-12-05 16:58:00 +0100521 }
Akronb19803c2018-08-16 16:39:42 +0200522 };
523});