blob: 0f38d122d8dbf2c3e1338d593b3f800564e2398a [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',
hebasta3dc77da2018-06-05 17:02:16 +020054 'vc/statistic',
Nils Diewald87507832015-05-01 23:36:41 +000055 'datepicker',
hebasta3dc77da2018-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;
Nils Diewaldd599d542015-01-08 20:41:34 +000068
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000069 KorAP._vcKeyMenu = undefined;
Nils Diewald87507832015-05-01 23:36:41 +000070 KorAP._vcDatePicker = dpClass.create();
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000071
Akron712733a2018-04-05 18:17:47 +020072 // Create match menus ....
73 KorAP._vcMatchopMenu = {
74 'string' : menuClass.create([
75 ['eq', null],
76 ['ne', null]
77 ]),
78 'text' : menuClass.create([
79 ['eq', null], // Requires exact match
80 ['ne', null],
81 ['contains', null], // Requires token sequence match
82 ['containsnot', null]
83 ]),
84 'date' : menuClass.create([
85 ['eq', null],
Akron31d89942018-04-06 16:44:51 +020086 ['ne', null],
Akron712733a2018-04-05 18:17:47 +020087 ['geq', null],
88 ['leq', null]
89 ]),
90 'regex' : menuClass.create([
91 ['eq', null],
92 ['ne', null]
93 ])
94 };
95
Nils Diewaldd599d542015-01-08 20:41:34 +000096 /**
97 * Virtual Collection
98 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000099 return {
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000100
101 /**
102 * The JSON-LD type of the virtual collection
103 */
Nils Diewaldf219eb82015-01-07 20:15:42 +0000104 ldType : function () {
105 return null;
106 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000107
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000108 // Initialize virtual collection
109 _init : function (keyList) {
110
111 // Inject localized css styles
Nils Diewald359a72c2015-04-20 17:40:29 +0000112 if (!KorAP._overrideStyles) {
Akrone4961b12017-05-10 21:04:46 +0200113 var sheet = KorAP.newStyleSheet();
Nils Diewald359a72c2015-04-20 17:40:29 +0000114
Akrone4961b12017-05-10 21:04:46 +0200115 // Add css rule for OR operations
116 sheet.insertRule(
117 '.vc .docGroup[data-operation=or] > .doc::before,' +
118 '.vc .docGroup[data-operation=or] > .docGroup::before ' +
119 '{ content: "' + loc.OR + '" }',
120 0
121 );
Nils Diewald359a72c2015-04-20 17:40:29 +0000122
Akrone4961b12017-05-10 21:04:46 +0200123 // Add css rule for AND operations
124 sheet.insertRule(
125 '.vc .docGroup[data-operation=and] > .doc::before,' +
126 '.vc .docGroup[data-operation=and] > .docGroup::before ' +
127 '{ content: "' + loc.AND + '" }',
128 1
129 );
Nils Diewald359a72c2015-04-20 17:40:29 +0000130
Akrone4961b12017-05-10 21:04:46 +0200131 KorAP._overrideStyles = true;
Nils Diewald359a72c2015-04-20 17:40:29 +0000132 };
133
Akron712733a2018-04-05 18:17:47 +0200134 // Create key menu
135 KorAP._vcKeyMenu = menuClass.create(keyList);
136 KorAP._vcKeyMenu.limit(6);
137
Nils Diewald359a72c2015-04-20 17:40:29 +0000138 return this;
139 },
140
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000141 /**
142 * Create a new virtual collection.
143 */
144 create : function (keyList) {
Nils Diewald6283d692015-04-23 20:32:53 +0000145 var obj = Object.create(this)._init(keyList);
146 obj._root = unspecDocClass.create(obj);
147 return obj;
Nils Diewald4019bd22015-01-08 19:57:50 +0000148 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000149
Nils Diewald7148c6f2015-05-04 15:07:53 +0000150
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000151 /**
152 * Create and render a new virtual collection
153 * based on a KoralQuery collection document
154 */
Nils Diewald6283d692015-04-23 20:32:53 +0000155 fromJson : function (json) {
Nils Diewaldd0770492014-12-19 03:55:00 +0000156 if (json !== undefined) {
Akrone4961b12017-05-10 21:04:46 +0200157 // Parse root document
158 if (json['@type'] == 'koral:doc') {
159 this._root = docClass.create(this, json);
160 }
161 // parse root group
162 else if (json['@type'] == 'koral:docGroup') {
163 this._root = docGroupClass.create(this, json);
164 }
165 // Unknown collection type
166 else {
167 KorAP.log(813, "Collection type is not supported");
168 return;
169 };
Nils Diewaldd0770492014-12-19 03:55:00 +0000170 }
171
172 else {
Akrone4961b12017-05-10 21:04:46 +0200173 // Add unspecified object
174 this._root = unspecDocClass.create(this);
Nils Diewaldd0770492014-12-19 03:55:00 +0000175 };
176
Nils Diewald8e7182e2015-01-08 15:02:07 +0000177 // Init element and update
Nils Diewald6283d692015-04-23 20:32:53 +0000178 this.update();
Nils Diewaldd0770492014-12-19 03:55:00 +0000179
Nils Diewald6283d692015-04-23 20:32:53 +0000180 return this;
181 },
182
Nils Diewald7148c6f2015-05-04 15:07:53 +0000183
Akron04671e72017-05-11 20:47:32 +0200184 // Check if the virtual corpus contains a rewrite
185 // This is a class method
186 checkRewrite : function (json) {
187
188 // There is a rewrite attribute
189 if (json['rewrites'] !== undefined) {
190 return true;
191 }
192
193 // There is a group to check for rewrites
194 else if (json['@type'] === 'koral:docGroup') {
195 var ops = json['operands'];
196 if (ops === undefined)
197 return false;
198
199 for (var i in ops) {
200
201 // "this" is the class
202 if (this.checkRewrite(ops[i])) {
203 return true;
204 };
205 };
206 };
207 return false;
208 },
209
Nils Diewald7148c6f2015-05-04 15:07:53 +0000210 /**
211 * Clean the virtual document to uspecified doc.
212 */
Nils Diewald6283d692015-04-23 20:32:53 +0000213 clean : function () {
214 if (this._root.ldType() !== "non") {
Akron04671e72017-05-11 20:47:32 +0200215 this._root.destroy();
216 this.root(unspecDocClass.create(this));
Nils Diewald6283d692015-04-23 20:32:53 +0000217 };
218 return this;
Nils Diewald3a2d8022014-12-16 02:45:41 +0000219 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000220
Nils Diewald7148c6f2015-05-04 15:07:53 +0000221
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000222 /**
223 * Get or set the root object of the
224 * virtual collection.
225 */
Nils Diewaldf219eb82015-01-07 20:15:42 +0000226 root : function (obj) {
Nils Diewald8e7182e2015-01-08 15:02:07 +0000227 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200228 var e = this.element();
Nils Diewald845282c2015-05-14 07:53:03 +0000229
Akrone4961b12017-05-10 21:04:46 +0200230 if (e.firstChild !== null) {
231 if (e.firstChild !== obj.element()) {
232 e.replaceChild(obj.element(), e.firstChild);
233 };
234 }
Nils Diewald8f6b6102015-01-08 18:25:33 +0000235
Akrone4961b12017-05-10 21:04:46 +0200236 // Append root element
237 else {
238 e.appendChild(obj.element());
239 };
Nils Diewald8f6b6102015-01-08 18:25:33 +0000240
Akrone4961b12017-05-10 21:04:46 +0200241 // Update parent child relations
242 this._root = obj;
243 obj.parent(this);
Nils Diewald8f6b6102015-01-08 18:25:33 +0000244
Akrone4961b12017-05-10 21:04:46 +0200245 this.update();
Nils Diewald8e7182e2015-01-08 15:02:07 +0000246 };
Nils Diewaldd0770492014-12-19 03:55:00 +0000247 return this._root;
Nils Diewald3a2d8022014-12-16 02:45:41 +0000248 },
Nils Diewald8f6b6102015-01-08 18:25:33 +0000249
Nils Diewald7148c6f2015-05-04 15:07:53 +0000250
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000251 /**
252 * Get the element associated with the virtual collection
253 */
Nils Diewaldd0770492014-12-19 03:55:00 +0000254 element : function () {
Akron12971a02018-01-03 16:38:10 +0100255 if (this._element !== undefined) {
256 return this._element;
257 };
Nils Diewaldd0770492014-12-19 03:55:00 +0000258
259 this._element = document.createElement('div');
260 this._element.setAttribute('class', 'vc');
Nils Diewald8e7182e2015-01-08 15:02:07 +0000261
Nils Diewald8f6b6102015-01-08 18:25:33 +0000262 // Initialize root
263 this._element.appendChild(this._root.element());
Nils Diewald845282c2015-05-14 07:53:03 +0000264
Nils Diewaldd0770492014-12-19 03:55:00 +0000265 return this._element;
Nils Diewaldf219eb82015-01-07 20:15:42 +0000266 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000267
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000268
269 /**
270 * Update the whole object based on the underlying
271 * data structure
272 */
Nils Diewaldd599d542015-01-08 20:41:34 +0000273 update : function () {
274 this._root.update();
275 return this;
276 },
277
Nils Diewald845282c2015-05-14 07:53:03 +0000278 /**
279 * Make the vc persistant by injecting the current timestamp
280 * as a creation date limit criterion.
281 */
282 makePersistant : function () {
Akron12971a02018-01-03 16:38:10 +0100283 // this.root().wrapOnRoot('and');
Nils Diewald845282c2015-05-14 07:53:03 +0000284 var todayStr = KorAP._vcDatePicker.today();
285 var doc = docClass.create();
286 var root = this.root();
287
288 if (root.ldType() === 'docGroup' &&
Akron12971a02018-01-03 16:38:10 +0100289 root.operation === 'and') {
290 root.append(cond);
Nils Diewald845282c2015-05-14 07:53:03 +0000291 }
292 else {
Akron12971a02018-01-03 16:38:10 +0100293 root.wrapOnRoot('and');
294 root.append(doc);
Nils Diewald845282c2015-05-14 07:53:03 +0000295 };
296
297 doc.key("creationDate");
298 doc.type("date");
299 doc.matchop("leq");
300 doc.value(todayStr);
301
302/*
303 {
304 "@type" : "koral:doc",
305 "key" : "creationDate",
306 "type" : "type:date",
307 "match" : "match:leq",
308 "value" : todayStr
309 }
310 this.root().append(cond);
311*/
312 this.update();
313 },
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000314
315 /**
316 * Get the generated json string
317 */
Nils Diewaldf219eb82015-01-07 20:15:42 +0000318 toJson : function () {
319 return this._root.toJson();
320 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000321
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000322
323 /**
324 * Get the generated query string
325 */
Nils Diewaldd599d542015-01-08 20:41:34 +0000326 toQuery : function () {
327 return this._root.toQuery();
hebasta3dc77da2018-06-05 17:02:16 +0200328 },
329
330
331 /**
332 * Get associated corpus statistic,
333 * create corpus statistic if it is unassigned
334 */
335 statistic : function(){
336 if(this.corpStat == undefined){
337 this.corpStat = statClass.create();
338 }
339 return this.corpStat;
340 }
341
Nils Diewald3a2d8022014-12-16 02:45:41 +0000342 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000343});