blob: 77f6a26302e9edae4ea523ce8ebb81b90cbc0154 [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',
Nils Diewald87507832015-05-01 23:36:41 +000054 'datepicker',
Nils Diewald0e6992a2015-04-14 20:13:52 +000055 'util'
Nils Diewald87507832015-05-01 23:36:41 +000056], function (unspecDocClass, docClass, docGroupClass, menuClass, dpClass) {
Nils Diewald3a2d8022014-12-16 02:45:41 +000057 "use strict";
58
Nils Diewald359a72c2015-04-20 17:40:29 +000059 // ???
Akron6a535d42015-08-26 20:16:58 +020060 KorAP._validStringMatchRE = new RegExp("^(?:eq|ne|contains(?:not)?|excludes)$");
Nils Diewald359a72c2015-04-20 17:40:29 +000061 KorAP._overrideStyles = false;
Nils Diewald3a2d8022014-12-16 02:45:41 +000062
Akron0b489ad2018-02-02 16:49:32 +010063 const loc = KorAP.Locale;
Nils Diewaldd599d542015-01-08 20:41:34 +000064
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000065 KorAP._vcKeyMenu = undefined;
Nils Diewald87507832015-05-01 23:36:41 +000066 KorAP._vcDatePicker = dpClass.create();
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000067
Akron712733a2018-04-05 18:17:47 +020068 // Create match menus ....
69 KorAP._vcMatchopMenu = {
70 'string' : menuClass.create([
71 ['eq', null],
72 ['ne', null]
73 ]),
74 'text' : menuClass.create([
75 ['eq', null], // Requires exact match
76 ['ne', null],
77 ['contains', null], // Requires token sequence match
78 ['containsnot', null]
79 ]),
80 'date' : menuClass.create([
81 ['eq', null],
82 ['geq', null],
83 ['leq', null]
84 ]),
85 'regex' : menuClass.create([
86 ['eq', null],
87 ['ne', null]
88 ])
89 };
90
Nils Diewaldd599d542015-01-08 20:41:34 +000091 /**
92 * Virtual Collection
93 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000094 return {
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000095
96 /**
97 * The JSON-LD type of the virtual collection
98 */
Nils Diewaldf219eb82015-01-07 20:15:42 +000099 ldType : function () {
100 return null;
101 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000102
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000103 // Initialize virtual collection
104 _init : function (keyList) {
105
106 // Inject localized css styles
Nils Diewald359a72c2015-04-20 17:40:29 +0000107 if (!KorAP._overrideStyles) {
Akrone4961b12017-05-10 21:04:46 +0200108 var sheet = KorAP.newStyleSheet();
Nils Diewald359a72c2015-04-20 17:40:29 +0000109
Akrone4961b12017-05-10 21:04:46 +0200110 // Add css rule for OR operations
111 sheet.insertRule(
112 '.vc .docGroup[data-operation=or] > .doc::before,' +
113 '.vc .docGroup[data-operation=or] > .docGroup::before ' +
114 '{ content: "' + loc.OR + '" }',
115 0
116 );
Nils Diewald359a72c2015-04-20 17:40:29 +0000117
Akrone4961b12017-05-10 21:04:46 +0200118 // Add css rule for AND operations
119 sheet.insertRule(
120 '.vc .docGroup[data-operation=and] > .doc::before,' +
121 '.vc .docGroup[data-operation=and] > .docGroup::before ' +
122 '{ content: "' + loc.AND + '" }',
123 1
124 );
Nils Diewald359a72c2015-04-20 17:40:29 +0000125
Akrone4961b12017-05-10 21:04:46 +0200126 KorAP._overrideStyles = true;
Nils Diewald359a72c2015-04-20 17:40:29 +0000127 };
128
Akron712733a2018-04-05 18:17:47 +0200129 // Create key menu
130 KorAP._vcKeyMenu = menuClass.create(keyList);
131 KorAP._vcKeyMenu.limit(6);
132
Nils Diewald359a72c2015-04-20 17:40:29 +0000133 return this;
134 },
135
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000136 /**
137 * Create a new virtual collection.
138 */
139 create : function (keyList) {
Nils Diewald6283d692015-04-23 20:32:53 +0000140 var obj = Object.create(this)._init(keyList);
Akron712733a2018-04-05 18:17:47 +0200141 console.log(keyList);
Nils Diewald6283d692015-04-23 20:32:53 +0000142 obj._root = unspecDocClass.create(obj);
143 return obj;
Nils Diewald4019bd22015-01-08 19:57:50 +0000144 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000145
Nils Diewald7148c6f2015-05-04 15:07:53 +0000146
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000147 /**
148 * Create and render a new virtual collection
149 * based on a KoralQuery collection document
150 */
Nils Diewald6283d692015-04-23 20:32:53 +0000151 fromJson : function (json) {
Nils Diewaldd0770492014-12-19 03:55:00 +0000152 if (json !== undefined) {
Akrone4961b12017-05-10 21:04:46 +0200153 // Parse root document
154 if (json['@type'] == 'koral:doc') {
155 this._root = docClass.create(this, json);
156 }
157 // parse root group
158 else if (json['@type'] == 'koral:docGroup') {
159 this._root = docGroupClass.create(this, json);
160 }
161 // Unknown collection type
162 else {
163 KorAP.log(813, "Collection type is not supported");
164 return;
165 };
Nils Diewaldd0770492014-12-19 03:55:00 +0000166 }
167
168 else {
Akrone4961b12017-05-10 21:04:46 +0200169 // Add unspecified object
170 this._root = unspecDocClass.create(this);
Nils Diewaldd0770492014-12-19 03:55:00 +0000171 };
172
Nils Diewald8e7182e2015-01-08 15:02:07 +0000173 // Init element and update
Nils Diewald6283d692015-04-23 20:32:53 +0000174 this.update();
Nils Diewaldd0770492014-12-19 03:55:00 +0000175
Nils Diewald6283d692015-04-23 20:32:53 +0000176 return this;
177 },
178
Nils Diewald7148c6f2015-05-04 15:07:53 +0000179
Akron04671e72017-05-11 20:47:32 +0200180 // Check if the virtual corpus contains a rewrite
181 // This is a class method
182 checkRewrite : function (json) {
183
184 // There is a rewrite attribute
185 if (json['rewrites'] !== undefined) {
186 return true;
187 }
188
189 // There is a group to check for rewrites
190 else if (json['@type'] === 'koral:docGroup') {
191 var ops = json['operands'];
192 if (ops === undefined)
193 return false;
194
195 for (var i in ops) {
196
197 // "this" is the class
198 if (this.checkRewrite(ops[i])) {
199 return true;
200 };
201 };
202 };
203 return false;
204 },
205
Nils Diewald7148c6f2015-05-04 15:07:53 +0000206 /**
207 * Clean the virtual document to uspecified doc.
208 */
Nils Diewald6283d692015-04-23 20:32:53 +0000209 clean : function () {
210 if (this._root.ldType() !== "non") {
Akron04671e72017-05-11 20:47:32 +0200211 this._root.destroy();
212 this.root(unspecDocClass.create(this));
Nils Diewald6283d692015-04-23 20:32:53 +0000213 };
214 return this;
Nils Diewald3a2d8022014-12-16 02:45:41 +0000215 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000216
Nils Diewald7148c6f2015-05-04 15:07:53 +0000217
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000218 /**
219 * Get or set the root object of the
220 * virtual collection.
221 */
Nils Diewaldf219eb82015-01-07 20:15:42 +0000222 root : function (obj) {
Nils Diewald8e7182e2015-01-08 15:02:07 +0000223 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200224 var e = this.element();
Nils Diewald845282c2015-05-14 07:53:03 +0000225
Akrone4961b12017-05-10 21:04:46 +0200226 if (e.firstChild !== null) {
227 if (e.firstChild !== obj.element()) {
228 e.replaceChild(obj.element(), e.firstChild);
229 };
230 }
Nils Diewald8f6b6102015-01-08 18:25:33 +0000231
Akrone4961b12017-05-10 21:04:46 +0200232 // Append root element
233 else {
234 e.appendChild(obj.element());
235 };
Nils Diewald8f6b6102015-01-08 18:25:33 +0000236
Akrone4961b12017-05-10 21:04:46 +0200237 // Update parent child relations
238 this._root = obj;
239 obj.parent(this);
Nils Diewald8f6b6102015-01-08 18:25:33 +0000240
Akrone4961b12017-05-10 21:04:46 +0200241 this.update();
Nils Diewald8e7182e2015-01-08 15:02:07 +0000242 };
Nils Diewaldd0770492014-12-19 03:55:00 +0000243 return this._root;
Nils Diewald3a2d8022014-12-16 02:45:41 +0000244 },
Nils Diewald8f6b6102015-01-08 18:25:33 +0000245
Nils Diewald7148c6f2015-05-04 15:07:53 +0000246
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000247 /**
248 * Get the element associated with the virtual collection
249 */
Nils Diewaldd0770492014-12-19 03:55:00 +0000250 element : function () {
Akron12971a02018-01-03 16:38:10 +0100251 if (this._element !== undefined) {
252 return this._element;
253 };
Nils Diewaldd0770492014-12-19 03:55:00 +0000254
255 this._element = document.createElement('div');
256 this._element.setAttribute('class', 'vc');
Nils Diewald8e7182e2015-01-08 15:02:07 +0000257
Nils Diewald8f6b6102015-01-08 18:25:33 +0000258 // Initialize root
259 this._element.appendChild(this._root.element());
Nils Diewald845282c2015-05-14 07:53:03 +0000260
Nils Diewaldd0770492014-12-19 03:55:00 +0000261 return this._element;
Nils Diewaldf219eb82015-01-07 20:15:42 +0000262 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000263
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000264
265 /**
266 * Update the whole object based on the underlying
267 * data structure
268 */
Nils Diewaldd599d542015-01-08 20:41:34 +0000269 update : function () {
270 this._root.update();
271 return this;
272 },
273
Nils Diewald845282c2015-05-14 07:53:03 +0000274 /**
275 * Make the vc persistant by injecting the current timestamp
276 * as a creation date limit criterion.
277 */
278 makePersistant : function () {
Akron12971a02018-01-03 16:38:10 +0100279 // this.root().wrapOnRoot('and');
Nils Diewald845282c2015-05-14 07:53:03 +0000280 var todayStr = KorAP._vcDatePicker.today();
281 var doc = docClass.create();
282 var root = this.root();
283
284 if (root.ldType() === 'docGroup' &&
Akron12971a02018-01-03 16:38:10 +0100285 root.operation === 'and') {
286 root.append(cond);
Nils Diewald845282c2015-05-14 07:53:03 +0000287 }
288 else {
Akron12971a02018-01-03 16:38:10 +0100289 root.wrapOnRoot('and');
290 root.append(doc);
Nils Diewald845282c2015-05-14 07:53:03 +0000291 };
292
293 doc.key("creationDate");
294 doc.type("date");
295 doc.matchop("leq");
296 doc.value(todayStr);
297
298/*
299 {
300 "@type" : "koral:doc",
301 "key" : "creationDate",
302 "type" : "type:date",
303 "match" : "match:leq",
304 "value" : todayStr
305 }
306 this.root().append(cond);
307*/
308 this.update();
309 },
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000310
311 /**
312 * Get the generated json string
313 */
Nils Diewaldf219eb82015-01-07 20:15:42 +0000314 toJson : function () {
315 return this._root.toJson();
316 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000317
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000318
319 /**
320 * Get the generated query string
321 */
Nils Diewaldd599d542015-01-08 20:41:34 +0000322 toQuery : function () {
323 return this._root.toQuery();
Nils Diewald3a2d8022014-12-16 02:45:41 +0000324 }
325 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000326});