blob: 3259083a465c819e040f29a19e2893517fdc474c [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',
56 'panel',
57 'view/corpstatv',
58 'util'
59], function(
60 unspecDocClass,
61 docClass,
62 docGroupClass,
63 docGroupRefClass,
64 menuClass,
65 statClass,
66 dpClass,
67 buttonGrClass,
68 panelClass,
69 corpStatVClass) {
70 "use strict";
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000071
Akronb19803c2018-08-16 16:39:42 +020072 KorAP._validUnspecMatchRE = new RegExp(
73 "^(?:eq|ne|contains(?:not)?|excludes)$");
74 KorAP._validStringMatchRE = new RegExp("^(?:eq|ne)$");
75 KorAP._validTextMatchRE = KorAP._validUnspecMatchRE;
76 KorAP._validTextOnlyMatchRE = new RegExp(
77 "^(?:contains(?:not)?|excludes)$");
78 KorAP._overrideStyles = false;
79 // KorAP._validDateMatchRE is defined in datepicker.js!
Nils Diewaldd0770492014-12-19 03:55:00 +000080
Akronb19803c2018-08-16 16:39:42 +020081 const loc = KorAP.Locale;
82 loc.SHOW_STAT = loc.SHOW_STAT || 'Statistics';
83 loc.VERB_SHOWSTAT = loc.VERB_SHOWSTAT || 'Corpus Statistics';
Akron8a670162018-08-28 10:09:13 +020084 loc.VC_allCorpora = loc.VC_allCorpora || 'all corpora';
85 loc.VC_oneCollection = loc.VC_oneCollection || 'a virtual corpus';
Nils Diewald3a2d8022014-12-16 02:45:41 +000086
Akronb19803c2018-08-16 16:39:42 +020087 KorAP._vcKeyMenu = undefined;
88 KorAP._vcDatePicker = dpClass.create();
Nils Diewald3a2d8022014-12-16 02:45:41 +000089
Akronb19803c2018-08-16 16:39:42 +020090 // Create match menus ....
91 KorAP._vcMatchopMenu = {
92 'string' : menuClass.create([
93 [ 'eq', null ],
94 [ 'ne', null ]
95 ]),
96 'text' : menuClass.create([
97 [ 'eq', null ], // Requires exact match
98 [ 'ne', null ],
99 [ 'contains', null ], // Requires token sequence match
100 [ 'containsnot', null ]
101 ]),
102 'date' : menuClass.create([
103 [ 'eq', null ],
104 [ 'ne', null ],
105 [ 'geq', null ],
106 [ 'leq', null ]
107 ]),
108 'regex' : menuClass.create([
109 [ 'eq', null ],
110 [ 'ne', null ]
111 ])
112 };
113
114 /**
115 * Virtual Collection
116 */
117 return {
118
119 /**
120 * The JSON-LD type of the virtual collection
121 */
122 ldType : function() {
123 return null;
124 },
125
126 // Initialize virtual collection
127 _init : function(keyList) {
128
129 // Inject localized css styles
130 if (!KorAP._overrideStyles) {
131 var sheet = KorAP.newStyleSheet();
132
133 // Add css rule for OR operations
134 sheet.insertRule('.vc .docGroup[data-operation=or] > .doc::before,'
135 + '.vc .docGroup[data-operation=or] > .docGroup::before '
136 + '{ content: "' + loc.OR + '" }', 0);
137
138 // Add css rule for AND operations
139 sheet.insertRule(
140 '.vc .docGroup[data-operation=and] > .doc::before,'
141 + '.vc .docGroup[data-operation=and] > .docGroup::before '
142 + '{ content: "' + loc.AND + '" }', 1);
143
144 KorAP._overrideStyles = true;
Nils Diewald359a72c2015-04-20 17:40:29 +0000145 };
146
Akron3ad46942018-08-22 16:47:14 +0200147 var l;
148 if (keyList) {
149 l = keyList.slice();
150 l.unshift(['referTo', 'ref']);
151 }
152 else {
153 l = [['referTo', 'ref']];
154 }
155
Akronb19803c2018-08-16 16:39:42 +0200156 // Create key menu
Akron3ad46942018-08-22 16:47:14 +0200157 KorAP._vcKeyMenu = menuClass.create(l);
Akronb19803c2018-08-16 16:39:42 +0200158 KorAP._vcKeyMenu.limit(6);
Akron712733a2018-04-05 18:17:47 +0200159
Akronb19803c2018-08-16 16:39:42 +0200160 return this;
161 },
Nils Diewald359a72c2015-04-20 17:40:29 +0000162
Akronb19803c2018-08-16 16:39:42 +0200163 /**
164 * Create a new virtual collection.
165 */
166 create : function(keyList) {
167 var obj = Object.create(this)._init(keyList);
168 obj._root = unspecDocClass.create(obj);
169 return obj;
170 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000171
Akron8a670162018-08-28 10:09:13 +0200172
Akronb19803c2018-08-16 16:39:42 +0200173 /**
174 * Create and render a new virtual collection based on a KoralQuery
175 * collection document
176 */
177 fromJson : function(json) {
178 if (json !== undefined) {
179 // Parse root document
180 if (json['@type'] == 'koral:doc') {
181 this._root = docClass.create(this, json);
hebastaa79d69d2018-07-24 12:13:02 +0200182 }
Akronb19803c2018-08-16 16:39:42 +0200183 // parse root group
184 else if (json['@type'] == 'koral:docGroup') {
185 this._root = docGroupClass.create(this, json);
186 }
187
188 // parse root reference
189 else if (json['@type'] == 'koral:docGroupRef') {
190 this._root = docGroupRefClass.create(this, json);
191 }
192
193 // Unknown collection type
194 else {
195 KorAP.log(813, "Collection type is not supported");
196 return;
197 };
198 }
199
200 else {
201 // Add unspecified object
202 this._root = unspecDocClass.create(this);
Nils Diewald845282c2015-05-14 07:53:03 +0000203 };
Akronb19803c2018-08-16 16:39:42 +0200204
205 // Init element and update
206 this.update();
207
208 return this;
209 },
Akrond2474aa2018-08-28 12:06:27 +0200210
211
212 // Check if the virtual corpus contains a rerite
213 wasRewritten : function (obj) {
214
215 var obj;
216 if (arguments.length === 1) {
217 obj = arguments[0];
218 }
219 else {
220 obj = this._root;
221 };
222
223 // Check for rewrite
224 if (obj.rewrites() && obj.rewrites().length() > 0) {
225 return true;
226 }
227
228 // Check recursively
229 else if (obj.ldType() === 'docGroup') {
230 for (var i in obj.operands()) {
231 if (this.wasRewritten(obj.getOperand(i))) {
232 return true;
233 }
234 };
235 };
236
237 return false;
238 },
Akron8a670162018-08-28 10:09:13 +0200239
Akronb19803c2018-08-16 16:39:42 +0200240 // Check if the virtual corpus contains a rewrite
241 // This is a class method
242 checkRewrite : function(json) {
243
244 // There is a rewrite attribute
245 if (json['rewrites'] !== undefined) {
246 return true;
247 }
248
249 // There is a group to check for rewrites
250 else if (json['@type'] === 'koral:docGroup') {
251 var ops = json['operands'];
252 if (ops === undefined)
253 return false;
254
255 for ( var i in ops) {
256
257 // "this" is the class
258 if (this.checkRewrite(ops[i])) {
259 return true;
260 };
261 };
262 };
263 return false;
264 },
265
266 /**
267 * Clean the virtual document to uspecified doc.
268 */
269 clean : function() {
270 if (this._root.ldType() !== "non") {
271 this._root.destroy();
272 this.root(unspecDocClass.create(this));
273 };
274 return this;
275 },
276
277 /**
278 * Get or set the root object of the virtual collection.
279 */
280 root : function(obj) {
281 if (arguments.length === 1) {
282 var e = this.element();
283
284 if (e.firstChild !== null) {
285 if (e.firstChild !== obj.element()) {
286 e.replaceChild(obj.element(), e.firstChild);
287 };
288 }
289
290 // Append root element
291 else {
292 e.appendChild(obj.element());
293 };
294
295 // Update parent child relations
296 this._root = obj;
297 obj.parent(this);
298
299 this.update();
300 };
301 return this._root;
302 },
303
304 /**
305 * Get the element associated with the virtual collection
306 */
307 element : function() {
308
309
310 if (this._element !== undefined) {
311 return this._element;
312 };
313
314 this._element = document.createElement('div');
315 this._element.setAttribute('class', 'vc');
316
317 // Initialize root
318 this._element.appendChild(this._root.element());
319
320 /*
321 * TODO by Helge Hack! additional div, because statistic button is
322 * removed after choosing and/or/x in vc builder. REMOVE this lines
323 * after solving the problem!!!!
324 */
325 this._element.addE('div');
326 this._element.addE('div');
327 this._element.addE('div');
328
329 // Add panel to display corpus statistic, ...
330 this.addVcInfPanel();
331
332 return this._element;
333 },
334
335 /**
336 * Update the whole object based on the underlying data structure
337 */
338 update : function() {
339 this._root.update();
340 return this;
341 },
342
343 /**
344 * Make the vc persistant by injecting the current timestamp as a
345 * creation date limit criterion.
346 * THIS IS CURRENTLY NOT USED
347 */
348 makePersistant : function() {
349 // this.root().wrapOnRoot('and');
350 var todayStr = KorAP._vcDatePicker.today();
351 var doc = docClass.create();
352 var root = this.root();
353
354 if (root.ldType() === 'docGroup' && root.operation === 'and') {
355 root.append(cond);
356 } else {
357 root.wrapOnRoot('and');
358 root.append(doc);
359 };
360
361 doc.key("creationDate");
362 doc.type("date");
363 doc.matchop("leq");
364 doc.value(todayStr);
365
366 /*
367 * { "@type" : "koral:doc", "key" : "creationDate", "type" :
368 * "type:date", "match" : "match:leq", "value" : todayStr }
369 * this.root().append(cond);
370 */
371 this.update();
372 },
373
Akron8a670162018-08-28 10:09:13 +0200374
375 // Get the reference name
376 getName : function () {
377 if (this._root.ldType() === 'non') {
378 return loc.VC_allCorpora;
379 }
380 else if (this._root.ldType() === 'docGroupRef') {
381 return this._root.ref();
382 }
383 else {
384 return loc.VC_oneCollection;
385 }
386 },
387
388
Akronb19803c2018-08-16 16:39:42 +0200389 /**
390 * Get the generated json string
391 */
392 toJson : function() {
393 return this._root.toJson();
394 },
395
396 /**
397 * Get the generated query string
398 */
399 toQuery : function() {
400 return this._root.toQuery();
401 },
402
403
404 /*
405 * Add panel to display virtual corpus information
406 */
407 addVcInfPanel : function() {
408
409 var dv = this._element.addE('div');
410 var panel = panelClass.create([ 'vcinfo' ]);
411 dv.appendChild(panel.element());
412
413 var that = this;
414 var actions = panel.actions;
415 var statView;
416
417 actions.add(loc.SHOW_STAT, [ 'statistic' ], function() {
418 if (statView === undefined || !statView.shown()) {
419 statView = corpStatVClass.create(that);
420 panel.add(statView);
421 }
422 });
423 }
424 };
425});