blob: b0ebc44da6f4b7bed363f340aae13b115e06b2fa [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';
Nils Diewald3a2d8022014-12-16 02:45:41 +000084
Akronb19803c2018-08-16 16:39:42 +020085 KorAP._vcKeyMenu = undefined;
86 KorAP._vcDatePicker = dpClass.create();
Nils Diewald3a2d8022014-12-16 02:45:41 +000087
Akronb19803c2018-08-16 16:39:42 +020088 // Create match menus ....
89 KorAP._vcMatchopMenu = {
90 'string' : menuClass.create([
91 [ 'eq', null ],
92 [ 'ne', null ]
93 ]),
94 'text' : menuClass.create([
95 [ 'eq', null ], // Requires exact match
96 [ 'ne', null ],
97 [ 'contains', null ], // Requires token sequence match
98 [ 'containsnot', null ]
99 ]),
100 'date' : menuClass.create([
101 [ 'eq', null ],
102 [ 'ne', null ],
103 [ 'geq', null ],
104 [ 'leq', null ]
105 ]),
106 'regex' : menuClass.create([
107 [ 'eq', null ],
108 [ 'ne', null ]
109 ])
110 };
111
112 /**
113 * Virtual Collection
114 */
115 return {
116
117 /**
118 * The JSON-LD type of the virtual collection
119 */
120 ldType : function() {
121 return null;
122 },
123
124 // Initialize virtual collection
125 _init : function(keyList) {
126
127 // Inject localized css styles
128 if (!KorAP._overrideStyles) {
129 var sheet = KorAP.newStyleSheet();
130
131 // Add css rule for OR operations
132 sheet.insertRule('.vc .docGroup[data-operation=or] > .doc::before,'
133 + '.vc .docGroup[data-operation=or] > .docGroup::before '
134 + '{ content: "' + loc.OR + '" }', 0);
135
136 // Add css rule for AND operations
137 sheet.insertRule(
138 '.vc .docGroup[data-operation=and] > .doc::before,'
139 + '.vc .docGroup[data-operation=and] > .docGroup::before '
140 + '{ content: "' + loc.AND + '" }', 1);
141
142 KorAP._overrideStyles = true;
Nils Diewald359a72c2015-04-20 17:40:29 +0000143 };
144
Akronb19803c2018-08-16 16:39:42 +0200145 // Create key menu
146 KorAP._vcKeyMenu = menuClass.create(keyList);
147 KorAP._vcKeyMenu.limit(6);
Akron712733a2018-04-05 18:17:47 +0200148
Akronb19803c2018-08-16 16:39:42 +0200149 return this;
150 },
Nils Diewald359a72c2015-04-20 17:40:29 +0000151
Akronb19803c2018-08-16 16:39:42 +0200152 /**
153 * Create a new virtual collection.
154 */
155 create : function(keyList) {
156 var obj = Object.create(this)._init(keyList);
157 obj._root = unspecDocClass.create(obj);
158 return obj;
159 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000160
Akronb19803c2018-08-16 16:39:42 +0200161 /**
162 * Create and render a new virtual collection based on a KoralQuery
163 * collection document
164 */
165 fromJson : function(json) {
166 if (json !== undefined) {
167 // Parse root document
168 if (json['@type'] == 'koral:doc') {
169 this._root = docClass.create(this, json);
hebastaa79d69d2018-07-24 12:13:02 +0200170 }
Akronb19803c2018-08-16 16:39:42 +0200171 // parse root group
172 else if (json['@type'] == 'koral:docGroup') {
173 this._root = docGroupClass.create(this, json);
174 }
175
176 // parse root reference
177 else if (json['@type'] == 'koral:docGroupRef') {
178 this._root = docGroupRefClass.create(this, json);
179 }
180
181 // Unknown collection type
182 else {
183 KorAP.log(813, "Collection type is not supported");
184 return;
185 };
186 }
187
188 else {
189 // Add unspecified object
190 this._root = unspecDocClass.create(this);
Nils Diewald845282c2015-05-14 07:53:03 +0000191 };
Akronb19803c2018-08-16 16:39:42 +0200192
193 // Init element and update
194 this.update();
195
196 return this;
197 },
198
199 // Check if the virtual corpus contains a rewrite
200 // This is a class method
201 checkRewrite : function(json) {
202
203 // There is a rewrite attribute
204 if (json['rewrites'] !== undefined) {
205 return true;
206 }
207
208 // There is a group to check for rewrites
209 else if (json['@type'] === 'koral:docGroup') {
210 var ops = json['operands'];
211 if (ops === undefined)
212 return false;
213
214 for ( var i in ops) {
215
216 // "this" is the class
217 if (this.checkRewrite(ops[i])) {
218 return true;
219 };
220 };
221 };
222 return false;
223 },
224
225 /**
226 * Clean the virtual document to uspecified doc.
227 */
228 clean : function() {
229 if (this._root.ldType() !== "non") {
230 this._root.destroy();
231 this.root(unspecDocClass.create(this));
232 };
233 return this;
234 },
235
236 /**
237 * Get or set the root object of the virtual collection.
238 */
239 root : function(obj) {
240 if (arguments.length === 1) {
241 var e = this.element();
242
243 if (e.firstChild !== null) {
244 if (e.firstChild !== obj.element()) {
245 e.replaceChild(obj.element(), e.firstChild);
246 };
247 }
248
249 // Append root element
250 else {
251 e.appendChild(obj.element());
252 };
253
254 // Update parent child relations
255 this._root = obj;
256 obj.parent(this);
257
258 this.update();
259 };
260 return this._root;
261 },
262
263 /**
264 * Get the element associated with the virtual collection
265 */
266 element : function() {
267
268
269 if (this._element !== undefined) {
270 return this._element;
271 };
272
273 this._element = document.createElement('div');
274 this._element.setAttribute('class', 'vc');
275
276 // Initialize root
277 this._element.appendChild(this._root.element());
278
279 /*
280 * TODO by Helge Hack! additional div, because statistic button is
281 * removed after choosing and/or/x in vc builder. REMOVE this lines
282 * after solving the problem!!!!
283 */
284 this._element.addE('div');
285 this._element.addE('div');
286 this._element.addE('div');
287
288 // Add panel to display corpus statistic, ...
289 this.addVcInfPanel();
290
291 return this._element;
292 },
293
294 /**
295 * Update the whole object based on the underlying data structure
296 */
297 update : function() {
298 this._root.update();
299 return this;
300 },
301
302 /**
303 * Make the vc persistant by injecting the current timestamp as a
304 * creation date limit criterion.
305 * THIS IS CURRENTLY NOT USED
306 */
307 makePersistant : function() {
308 // this.root().wrapOnRoot('and');
309 var todayStr = KorAP._vcDatePicker.today();
310 var doc = docClass.create();
311 var root = this.root();
312
313 if (root.ldType() === 'docGroup' && root.operation === 'and') {
314 root.append(cond);
315 } else {
316 root.wrapOnRoot('and');
317 root.append(doc);
318 };
319
320 doc.key("creationDate");
321 doc.type("date");
322 doc.matchop("leq");
323 doc.value(todayStr);
324
325 /*
326 * { "@type" : "koral:doc", "key" : "creationDate", "type" :
327 * "type:date", "match" : "match:leq", "value" : todayStr }
328 * this.root().append(cond);
329 */
330 this.update();
331 },
332
333 /**
334 * Get the generated json string
335 */
336 toJson : function() {
337 return this._root.toJson();
338 },
339
340 /**
341 * Get the generated query string
342 */
343 toQuery : function() {
344 return this._root.toQuery();
345 },
346
347
348 /*
349 * Add panel to display virtual corpus information
350 */
351 addVcInfPanel : function() {
352
353 var dv = this._element.addE('div');
354 var panel = panelClass.create([ 'vcinfo' ]);
355 dv.appendChild(panel.element());
356
357 var that = this;
358 var actions = panel.actions;
359 var statView;
360
361 actions.add(loc.SHOW_STAT, [ 'statistic' ], function() {
362 if (statView === undefined || !statView.shown()) {
363 statView = corpStatVClass.create(that);
364 panel.add(statView);
365 }
366 });
367 }
368 };
369});