blob: 1726ca2ab07bc4cbac8874ecd27c3ab237ad527f [file] [log] [blame]
Nils Diewald86dad5b2015-01-28 15:09:07 +00001/**
2 * Create virtual collections with a visual user interface.
Nils Diewald4c221252015-04-21 20:19:25 +00003 * This resembles the collection type objects of a KoralQuery
4 * "collection" object.
5 *
6 * KoralQuery v0.3 is expected.
Nils Diewald86dad5b2015-01-28 15:09:07 +00007 *
8 * @author Nils Diewald
9 */
Nils Diewald2fe12e12015-03-06 16:47:06 +000010/*
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000011 * This replaces a previous version written by Mengfei Zhou
Nils Diewald2fe12e12015-03-06 16:47:06 +000012 */
Nils Diewald2fe12e12015-03-06 16:47:06 +000013
Nils Diewaldd0770492014-12-19 03:55:00 +000014/*
Nils Diewald86dad5b2015-01-28 15:09:07 +000015 TODO: Disable "and" or "or" in case it's followed
16 by an unspecified document
Akron0a6768f2016-07-13 18:00:43 +020017 TODO: Add "and"-method to root to add further constraints based on match-
18 input (like clicking on a pubDate timestamp in a match)
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000019 TODO: Implement "persistence"-Option,
Nils Diewald6e43ffd2015-03-25 18:55:39 +000020 injecting the current creation date stamp
Nils Diewald709f52f2015-05-21 18:32:58 +000021 TODO: Implement vec-Type for document-id vectors
22 like docID in [1,2,3,4 ...]
Nils Diewald86dad5b2015-01-28 15:09:07 +000023
Nils Diewaldd599d542015-01-08 20:41:34 +000024 Error codes:
Nils Diewaldd0770492014-12-19 03:55:00 +000025 701: "JSON-LD group has no @type attribute"
26 704: "Operation needs operand list"
Nils Diewald3a2d8022014-12-16 02:45:41 +000027 802: "Match type is not supported by value type"
28 804: "Unknown value type"
29 805: "Value is invalid"
30 806: "Value is not a valid date string"
31 807: "Value is not a valid regular expression"
Nils Diewald3a2d8022014-12-16 02:45:41 +000032 810: "Unknown document group operation" (like 711)
33 811: "Document group expects operation" (like 703)
34 812: "Operand not supported in document group" (like 744)
Nils Diewaldd0770492014-12-19 03:55:00 +000035 813: "Collection type is not supported" (like 713)
Nils Diewald86dad5b2015-01-28 15:09:07 +000036 814: "Unknown rewrite operation"
37 815: "Rewrite expects source"
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000038
39 Localization strings:
40 KorAP.Locale = {
41 EMPTY : '...',
42 AND : 'and',
43 OR : 'or',
44 DELETE : 'x'
45 }
Nils Diewald4c221252015-04-21 20:19:25 +000046 and various field names with the prefix 'VC_'
Nils Diewaldd0770492014-12-19 03:55:00 +000047*/
48
Nils Diewald0e6992a2015-04-14 20:13:52 +000049define([
50 'vc/unspecified',
51 'vc/doc',
52 'vc/docgroup',
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000053 'vc/menu',
hebastaccb8fa32018-06-05 17:02:16 +020054 'vc/statistic',
Nils Diewald87507832015-05-01 23:36:41 +000055 'datepicker',
hebastaccb8fa32018-06-05 17:02:16 +020056 'util',
57], function (unspecDocClass, docClass, docGroupClass, menuClass, statClass, dpClass) {
Nils Diewald3a2d8022014-12-16 02:45:41 +000058 "use strict";
59
Akron31d89942018-04-06 16:44:51 +020060 KorAP._validUnspecMatchRE = new RegExp("^(?:eq|ne|contains(?:not)?|excludes)$");
61 KorAP._validStringMatchRE = new RegExp("^(?:eq|ne)$");
62 KorAP._validTextMatchRE = KorAP._validUnspecMatchRE;
63 KorAP._validTextOnlyMatchRE = new RegExp("^(?:contains(?:not)?|excludes)$");
Nils Diewald359a72c2015-04-20 17:40:29 +000064 KorAP._overrideStyles = false;
Akron31d89942018-04-06 16:44:51 +020065 // KorAP._validDateMatchRE is defined in datepicker.js!
Nils Diewald3a2d8022014-12-16 02:45:41 +000066
Akron0b489ad2018-02-02 16:49:32 +010067 const loc = KorAP.Locale;
hebasta80d26422018-06-29 10:11:15 +020068 loc.SHOWSTAT = loc.SHOWSTAT || 'Statistics';
69 loc.VERB_SHOWSTAT = loc.VERB_SHOWSTAT || 'Corpus Statistics';
hebasta2e2b9612018-06-20 16:00:57 +020070
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000071 KorAP._vcKeyMenu = undefined;
Nils Diewald87507832015-05-01 23:36:41 +000072 KorAP._vcDatePicker = dpClass.create();
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000073
Akron712733a2018-04-05 18:17:47 +020074 // Create match menus ....
75 KorAP._vcMatchopMenu = {
76 'string' : menuClass.create([
77 ['eq', null],
78 ['ne', null]
79 ]),
80 'text' : menuClass.create([
81 ['eq', null], // Requires exact match
82 ['ne', null],
83 ['contains', null], // Requires token sequence match
84 ['containsnot', null]
85 ]),
86 'date' : menuClass.create([
87 ['eq', null],
Akron31d89942018-04-06 16:44:51 +020088 ['ne', null],
Akron712733a2018-04-05 18:17:47 +020089 ['geq', null],
90 ['leq', null]
91 ]),
92 'regex' : menuClass.create([
93 ['eq', null],
94 ['ne', null]
95 ])
96 };
97
Nils Diewaldd599d542015-01-08 20:41:34 +000098 /**
99 * Virtual Collection
100 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000101 return {
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000102
103 /**
104 * The JSON-LD type of the virtual collection
105 */
Nils Diewaldf219eb82015-01-07 20:15:42 +0000106 ldType : function () {
107 return null;
108 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000109
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000110 // Initialize virtual collection
111 _init : function (keyList) {
112
113 // Inject localized css styles
Nils Diewald359a72c2015-04-20 17:40:29 +0000114 if (!KorAP._overrideStyles) {
Akrone4961b12017-05-10 21:04:46 +0200115 var sheet = KorAP.newStyleSheet();
Nils Diewald359a72c2015-04-20 17:40:29 +0000116
Akrone4961b12017-05-10 21:04:46 +0200117 // Add css rule for OR operations
118 sheet.insertRule(
119 '.vc .docGroup[data-operation=or] > .doc::before,' +
120 '.vc .docGroup[data-operation=or] > .docGroup::before ' +
121 '{ content: "' + loc.OR + '" }',
122 0
123 );
Nils Diewald359a72c2015-04-20 17:40:29 +0000124
Akrone4961b12017-05-10 21:04:46 +0200125 // Add css rule for AND operations
126 sheet.insertRule(
127 '.vc .docGroup[data-operation=and] > .doc::before,' +
128 '.vc .docGroup[data-operation=and] > .docGroup::before ' +
129 '{ content: "' + loc.AND + '" }',
130 1
131 );
Nils Diewald359a72c2015-04-20 17:40:29 +0000132
Akrone4961b12017-05-10 21:04:46 +0200133 KorAP._overrideStyles = true;
Nils Diewald359a72c2015-04-20 17:40:29 +0000134 };
135
Akron712733a2018-04-05 18:17:47 +0200136 // Create key menu
137 KorAP._vcKeyMenu = menuClass.create(keyList);
138 KorAP._vcKeyMenu.limit(6);
139
Nils Diewald359a72c2015-04-20 17:40:29 +0000140 return this;
141 },
142
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000143 /**
144 * Create a new virtual collection.
145 */
146 create : function (keyList) {
Nils Diewald6283d692015-04-23 20:32:53 +0000147 var obj = Object.create(this)._init(keyList);
148 obj._root = unspecDocClass.create(obj);
149 return obj;
Nils Diewald4019bd22015-01-08 19:57:50 +0000150 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000151
Nils Diewald7148c6f2015-05-04 15:07:53 +0000152
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000153 /**
154 * Create and render a new virtual collection
155 * based on a KoralQuery collection document
156 */
Nils Diewald6283d692015-04-23 20:32:53 +0000157 fromJson : function (json) {
Nils Diewaldd0770492014-12-19 03:55:00 +0000158 if (json !== undefined) {
Akrone4961b12017-05-10 21:04:46 +0200159 // Parse root document
160 if (json['@type'] == 'koral:doc') {
161 this._root = docClass.create(this, json);
162 }
163 // parse root group
164 else if (json['@type'] == 'koral:docGroup') {
165 this._root = docGroupClass.create(this, json);
166 }
167 // Unknown collection type
168 else {
169 KorAP.log(813, "Collection type is not supported");
170 return;
171 };
Nils Diewaldd0770492014-12-19 03:55:00 +0000172 }
173
174 else {
Akrone4961b12017-05-10 21:04:46 +0200175 // Add unspecified object
176 this._root = unspecDocClass.create(this);
Nils Diewaldd0770492014-12-19 03:55:00 +0000177 };
178
Nils Diewald8e7182e2015-01-08 15:02:07 +0000179 // Init element and update
Nils Diewald6283d692015-04-23 20:32:53 +0000180 this.update();
Nils Diewaldd0770492014-12-19 03:55:00 +0000181
Nils Diewald6283d692015-04-23 20:32:53 +0000182 return this;
183 },
184
Nils Diewald7148c6f2015-05-04 15:07:53 +0000185
Akron04671e72017-05-11 20:47:32 +0200186 // Check if the virtual corpus contains a rewrite
187 // This is a class method
188 checkRewrite : function (json) {
189
190 // There is a rewrite attribute
191 if (json['rewrites'] !== undefined) {
192 return true;
193 }
194
195 // There is a group to check for rewrites
196 else if (json['@type'] === 'koral:docGroup') {
197 var ops = json['operands'];
198 if (ops === undefined)
199 return false;
200
201 for (var i in ops) {
202
203 // "this" is the class
204 if (this.checkRewrite(ops[i])) {
205 return true;
206 };
207 };
208 };
209 return false;
210 },
211
Nils Diewald7148c6f2015-05-04 15:07:53 +0000212 /**
213 * Clean the virtual document to uspecified doc.
214 */
Nils Diewald6283d692015-04-23 20:32:53 +0000215 clean : function () {
216 if (this._root.ldType() !== "non") {
Akron04671e72017-05-11 20:47:32 +0200217 this._root.destroy();
218 this.root(unspecDocClass.create(this));
Nils Diewald6283d692015-04-23 20:32:53 +0000219 };
220 return this;
Nils Diewald3a2d8022014-12-16 02:45:41 +0000221 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000222
Nils Diewald7148c6f2015-05-04 15:07:53 +0000223
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000224 /**
225 * Get or set the root object of the
226 * virtual collection.
227 */
Nils Diewaldf219eb82015-01-07 20:15:42 +0000228 root : function (obj) {
Nils Diewald8e7182e2015-01-08 15:02:07 +0000229 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200230 var e = this.element();
Nils Diewald845282c2015-05-14 07:53:03 +0000231
Akrone4961b12017-05-10 21:04:46 +0200232 if (e.firstChild !== null) {
233 if (e.firstChild !== obj.element()) {
234 e.replaceChild(obj.element(), e.firstChild);
235 };
236 }
Nils Diewald8f6b6102015-01-08 18:25:33 +0000237
Akrone4961b12017-05-10 21:04:46 +0200238 // Append root element
239 else {
240 e.appendChild(obj.element());
241 };
Nils Diewald8f6b6102015-01-08 18:25:33 +0000242
Akrone4961b12017-05-10 21:04:46 +0200243 // Update parent child relations
244 this._root = obj;
245 obj.parent(this);
Nils Diewald8f6b6102015-01-08 18:25:33 +0000246
Akrone4961b12017-05-10 21:04:46 +0200247 this.update();
Nils Diewald8e7182e2015-01-08 15:02:07 +0000248 };
Nils Diewaldd0770492014-12-19 03:55:00 +0000249 return this._root;
Nils Diewald3a2d8022014-12-16 02:45:41 +0000250 },
Nils Diewald8f6b6102015-01-08 18:25:33 +0000251
Nils Diewald7148c6f2015-05-04 15:07:53 +0000252
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000253 /**
254 * Get the element associated with the virtual collection
255 */
Nils Diewaldd0770492014-12-19 03:55:00 +0000256 element : function () {
Akron12971a02018-01-03 16:38:10 +0100257 if (this._element !== undefined) {
258 return this._element;
259 };
Nils Diewaldd0770492014-12-19 03:55:00 +0000260
261 this._element = document.createElement('div');
262 this._element.setAttribute('class', 'vc');
Nils Diewald8e7182e2015-01-08 15:02:07 +0000263
Nils Diewald8f6b6102015-01-08 18:25:33 +0000264 // Initialize root
265 this._element.appendChild(this._root.element());
Nils Diewald845282c2015-05-14 07:53:03 +0000266
hebasta8f750df2018-06-29 14:27:50 +0200267 /* TODO by Helge
268 Hack! additional div, because statistic button is removed after
269 choosing and/or/x in vc builder. REMOVE this lines after solving the problem!!!! */
270 this._element.addE('div');
271 this._element.addE('div');
272 //Add corpus statistic button
273 this.addStatBut();
hebasta80d26422018-06-29 10:11:15 +0200274
Nils Diewaldd0770492014-12-19 03:55:00 +0000275 return this._element;
Nils Diewaldf219eb82015-01-07 20:15:42 +0000276 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000277
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000278
279 /**
280 * Update the whole object based on the underlying
281 * data structure
282 */
Nils Diewaldd599d542015-01-08 20:41:34 +0000283 update : function () {
284 this._root.update();
285 return this;
286 },
287
Nils Diewald845282c2015-05-14 07:53:03 +0000288 /**
289 * Make the vc persistant by injecting the current timestamp
290 * as a creation date limit criterion.
291 */
292 makePersistant : function () {
Akron12971a02018-01-03 16:38:10 +0100293 // this.root().wrapOnRoot('and');
Nils Diewald845282c2015-05-14 07:53:03 +0000294 var todayStr = KorAP._vcDatePicker.today();
295 var doc = docClass.create();
296 var root = this.root();
297
298 if (root.ldType() === 'docGroup' &&
Akron12971a02018-01-03 16:38:10 +0100299 root.operation === 'and') {
300 root.append(cond);
Nils Diewald845282c2015-05-14 07:53:03 +0000301 }
302 else {
Akron12971a02018-01-03 16:38:10 +0100303 root.wrapOnRoot('and');
304 root.append(doc);
Nils Diewald845282c2015-05-14 07:53:03 +0000305 };
306
307 doc.key("creationDate");
308 doc.type("date");
309 doc.matchop("leq");
310 doc.value(todayStr);
311
312/*
313 {
314 "@type" : "koral:doc",
315 "key" : "creationDate",
316 "type" : "type:date",
317 "match" : "match:leq",
318 "value" : todayStr
319 }
320 this.root().append(cond);
321*/
322 this.update();
323 },
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000324
325 /**
326 * Get the generated json string
327 */
Nils Diewaldf219eb82015-01-07 20:15:42 +0000328 toJson : function () {
329 return this._root.toJson();
330 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000331
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000332
333 /**
334 * Get the generated query string
335 */
Nils Diewaldd599d542015-01-08 20:41:34 +0000336 toQuery : function () {
337 return this._root.toQuery();
hebastaccb8fa32018-06-05 17:02:16 +0200338 },
339
hebastad0ac2d42018-06-29 14:24:15 +0200340 /*
341 * Returns ld type of associated doc, docGroup or unspecified doc
342 */
343 getLdType:function(){
344 return this._root.ldType();
345 },
hebasta2e2b9612018-06-20 16:00:57 +0200346
347 /**
348 * Adds Corpus Statistic Button
349 **/
350 addStatBut : function(){
hebasta8f750df2018-06-29 14:27:50 +0200351
352 //add div element 'corpStat'
hebasta2e2b9612018-06-20 16:00:57 +0200353 var dv = this._element.addE('div');
hebasta8f750df2018-06-29 14:27:50 +0200354 dv.classList.add('dCorpStat');
355
hebasta2e2b9612018-06-20 16:00:57 +0200356 //add button
357 var bu = dv.addE('div');
hebasta80d26422018-06-29 10:11:15 +0200358 bu.classList.add('bottom', 'button-stat');
hebasta2e2b9612018-06-20 16:00:57 +0200359 var stat = bu.addE('span');
hebasta80d26422018-06-29 10:11:15 +0200360 stat.addT(loc.SHOWSTAT);
hebasta2e2b9612018-06-20 16:00:57 +0200361 //localize the buttons title attribute
hebasta80d26422018-06-29 10:11:15 +0200362 //stat.setAttribute('title', loc.SHOWSTAT);
hebasta2e2b9612018-06-20 16:00:57 +0200363 stat.classList.add('statistic');
364
365 /*
366 * In ECMAScript Language Specification this is set incorrectly for inner functions,
367 * therefore thatelement and that is defined.
368 */
369 var thatelement = this._element;
370 var that = this;
371
372 //show corpus statistic if button is clicked
373 stat.addEventListener('click', function(e){
374 e.halt();
375 statClass.showCorpStat(thatelement, that);
376 });
377
378 }
379
Nils Diewald3a2d8022014-12-16 02:45:41 +0000380 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000381});