blob: d6a26f2831675c0479ead84a54f978bc40940da8 [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
Nils Diewald359a72c2015-04-20 17:40:29 +000063 var 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
Nils Diewaldd599d542015-01-08 20:41:34 +000068 /**
69 * Virtual Collection
70 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000071 return {
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000072
73 /**
74 * The JSON-LD type of the virtual collection
75 */
Nils Diewaldf219eb82015-01-07 20:15:42 +000076 ldType : function () {
77 return null;
78 },
Nils Diewaldd599d542015-01-08 20:41:34 +000079
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000080 // Initialize virtual collection
81 _init : function (keyList) {
82
83 // Inject localized css styles
Nils Diewald359a72c2015-04-20 17:40:29 +000084 if (!KorAP._overrideStyles) {
Akrone4961b12017-05-10 21:04:46 +020085 var sheet = KorAP.newStyleSheet();
Nils Diewald359a72c2015-04-20 17:40:29 +000086
Akrone4961b12017-05-10 21:04:46 +020087 // Add css rule for OR operations
88 sheet.insertRule(
89 '.vc .docGroup[data-operation=or] > .doc::before,' +
90 '.vc .docGroup[data-operation=or] > .docGroup::before ' +
91 '{ content: "' + loc.OR + '" }',
92 0
93 );
Nils Diewald359a72c2015-04-20 17:40:29 +000094
Akrone4961b12017-05-10 21:04:46 +020095 // Add css rule for AND operations
96 sheet.insertRule(
97 '.vc .docGroup[data-operation=and] > .doc::before,' +
98 '.vc .docGroup[data-operation=and] > .docGroup::before ' +
99 '{ content: "' + loc.AND + '" }',
100 1
101 );
Nils Diewald359a72c2015-04-20 17:40:29 +0000102
Akrone4961b12017-05-10 21:04:46 +0200103 KorAP._overrideStyles = true;
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000104
Akrone4961b12017-05-10 21:04:46 +0200105 // Create key menu
106 KorAP._vcKeyMenu = menuClass.create(keyList);
107 KorAP._vcKeyMenu.limit(6);
Nils Diewald4c221252015-04-21 20:19:25 +0000108
Akrone4961b12017-05-10 21:04:46 +0200109 // Create match menus ....
110 KorAP._vcMatchopMenu = {
111 'string' : menuClass.create([
112 ['eq', null],
113 ['ne', null],
114 ['contains', null],
115 ['containsnot', null]
116 ]),
117 'date' : menuClass.create([
118 ['eq', null],
119 ['geq', null],
120 ['leq', null]
121 ]),
122 'regex' : menuClass.create([
123 ['eq', null],
Akrondec2d692017-07-06 16:15:12 +0200124 ['ne', null]
Akrone4961b12017-05-10 21:04:46 +0200125 ])
126 };
Nils Diewald359a72c2015-04-20 17:40:29 +0000127 };
128
129 return this;
130 },
131
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000132 /**
133 * Create a new virtual collection.
134 */
135 create : function (keyList) {
Nils Diewald6283d692015-04-23 20:32:53 +0000136 var obj = Object.create(this)._init(keyList);
137 obj._root = unspecDocClass.create(obj);
138 return obj;
Nils Diewald4019bd22015-01-08 19:57:50 +0000139 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000140
Nils Diewald7148c6f2015-05-04 15:07:53 +0000141
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000142 /**
143 * Create and render a new virtual collection
144 * based on a KoralQuery collection document
145 */
Nils Diewald6283d692015-04-23 20:32:53 +0000146 fromJson : function (json) {
Nils Diewaldd0770492014-12-19 03:55:00 +0000147 if (json !== undefined) {
Akrone4961b12017-05-10 21:04:46 +0200148 // Parse root document
149 if (json['@type'] == 'koral:doc') {
150 this._root = docClass.create(this, json);
151 }
152 // parse root group
153 else if (json['@type'] == 'koral:docGroup') {
154 this._root = docGroupClass.create(this, json);
155 }
156 // Unknown collection type
157 else {
158 KorAP.log(813, "Collection type is not supported");
159 return;
160 };
Nils Diewaldd0770492014-12-19 03:55:00 +0000161 }
162
163 else {
Akrone4961b12017-05-10 21:04:46 +0200164 // Add unspecified object
165 this._root = unspecDocClass.create(this);
Nils Diewaldd0770492014-12-19 03:55:00 +0000166 };
167
Nils Diewald8e7182e2015-01-08 15:02:07 +0000168 // Init element and update
Nils Diewald6283d692015-04-23 20:32:53 +0000169 this.update();
Nils Diewaldd0770492014-12-19 03:55:00 +0000170
Nils Diewald6283d692015-04-23 20:32:53 +0000171 return this;
172 },
173
Nils Diewald7148c6f2015-05-04 15:07:53 +0000174
Akron04671e72017-05-11 20:47:32 +0200175 // Check if the virtual corpus contains a rewrite
176 // This is a class method
177 checkRewrite : function (json) {
178
179 // There is a rewrite attribute
180 if (json['rewrites'] !== undefined) {
181 return true;
182 }
183
184 // There is a group to check for rewrites
185 else if (json['@type'] === 'koral:docGroup') {
186 var ops = json['operands'];
187 if (ops === undefined)
188 return false;
189
190 for (var i in ops) {
191
192 // "this" is the class
193 if (this.checkRewrite(ops[i])) {
194 return true;
195 };
196 };
197 };
198 return false;
199 },
200
Nils Diewald7148c6f2015-05-04 15:07:53 +0000201 /**
202 * Clean the virtual document to uspecified doc.
203 */
Nils Diewald6283d692015-04-23 20:32:53 +0000204 clean : function () {
205 if (this._root.ldType() !== "non") {
Akron04671e72017-05-11 20:47:32 +0200206 this._root.destroy();
207 this.root(unspecDocClass.create(this));
Nils Diewald6283d692015-04-23 20:32:53 +0000208 };
209 return this;
Nils Diewald3a2d8022014-12-16 02:45:41 +0000210 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000211
Nils Diewald7148c6f2015-05-04 15:07:53 +0000212
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000213 /**
214 * Get or set the root object of the
215 * virtual collection.
216 */
Nils Diewaldf219eb82015-01-07 20:15:42 +0000217 root : function (obj) {
Nils Diewald8e7182e2015-01-08 15:02:07 +0000218 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200219 var e = this.element();
Nils Diewald845282c2015-05-14 07:53:03 +0000220
Akrone4961b12017-05-10 21:04:46 +0200221 if (e.firstChild !== null) {
222 if (e.firstChild !== obj.element()) {
223 e.replaceChild(obj.element(), e.firstChild);
224 };
225 }
Nils Diewald8f6b6102015-01-08 18:25:33 +0000226
Akrone4961b12017-05-10 21:04:46 +0200227 // Append root element
228 else {
229 e.appendChild(obj.element());
230 };
Nils Diewald8f6b6102015-01-08 18:25:33 +0000231
Akrone4961b12017-05-10 21:04:46 +0200232 // Update parent child relations
233 this._root = obj;
234 obj.parent(this);
Nils Diewald8f6b6102015-01-08 18:25:33 +0000235
Akrone4961b12017-05-10 21:04:46 +0200236 this.update();
Nils Diewald8e7182e2015-01-08 15:02:07 +0000237 };
Nils Diewaldd0770492014-12-19 03:55:00 +0000238 return this._root;
Nils Diewald3a2d8022014-12-16 02:45:41 +0000239 },
Nils Diewald8f6b6102015-01-08 18:25:33 +0000240
Nils Diewald7148c6f2015-05-04 15:07:53 +0000241
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000242 /**
243 * Get the element associated with the virtual collection
244 */
Nils Diewaldd0770492014-12-19 03:55:00 +0000245 element : function () {
246 if (this._element !== undefined)
247 return this._element;
248
249 this._element = document.createElement('div');
250 this._element.setAttribute('class', 'vc');
Nils Diewald8e7182e2015-01-08 15:02:07 +0000251
Nils Diewald8f6b6102015-01-08 18:25:33 +0000252 // Initialize root
253 this._element.appendChild(this._root.element());
Nils Diewald845282c2015-05-14 07:53:03 +0000254
Nils Diewaldd0770492014-12-19 03:55:00 +0000255 return this._element;
Nils Diewaldf219eb82015-01-07 20:15:42 +0000256 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000257
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000258
259 /**
260 * Update the whole object based on the underlying
261 * data structure
262 */
Nils Diewaldd599d542015-01-08 20:41:34 +0000263 update : function () {
264 this._root.update();
265 return this;
266 },
267
Nils Diewald845282c2015-05-14 07:53:03 +0000268 /**
269 * Make the vc persistant by injecting the current timestamp
270 * as a creation date limit criterion.
271 */
272 makePersistant : function () {
273// this.root().wrapOnRoot('and');
274 var todayStr = KorAP._vcDatePicker.today();
275 var doc = docClass.create();
276 var root = this.root();
277
278 if (root.ldType() === 'docGroup' &&
279 root.operation === 'and') {
280 root.append(cond);
281 }
282 else {
283 root.wrapOnRoot('and');
284 root.append(doc);
285 };
286
287 doc.key("creationDate");
288 doc.type("date");
289 doc.matchop("leq");
290 doc.value(todayStr);
291
292/*
293 {
294 "@type" : "koral:doc",
295 "key" : "creationDate",
296 "type" : "type:date",
297 "match" : "match:leq",
298 "value" : todayStr
299 }
300 this.root().append(cond);
301*/
302 this.update();
303 },
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000304
305 /**
306 * Get the generated json string
307 */
Nils Diewaldf219eb82015-01-07 20:15:42 +0000308 toJson : function () {
309 return this._root.toJson();
310 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000311
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000312
313 /**
314 * Get the generated query string
315 */
Nils Diewaldd599d542015-01-08 20:41:34 +0000316 toQuery : function () {
317 return this._root.toQuery();
Nils Diewald3a2d8022014-12-16 02:45:41 +0000318 }
319 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000320});