blob: 068524263c9710c0e3e4df5b043f33f963df94d5 [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
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000017 TODO: Implement "persistence"-Option,
Nils Diewald6e43ffd2015-03-25 18:55:39 +000018 injecting the current creation date stamp
Nils Diewald709f52f2015-05-21 18:32:58 +000019 TODO: Implement vec-Type for document-id vectors
20 like docID in [1,2,3,4 ...]
Nils Diewald86dad5b2015-01-28 15:09:07 +000021
Nils Diewaldd599d542015-01-08 20:41:34 +000022 Error codes:
Nils Diewaldd0770492014-12-19 03:55:00 +000023 701: "JSON-LD group has no @type attribute"
24 704: "Operation needs operand list"
Nils Diewald3a2d8022014-12-16 02:45:41 +000025 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"
Nils Diewald3a2d8022014-12-16 02:45:41 +000030 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)
Nils Diewaldd0770492014-12-19 03:55:00 +000033 813: "Collection type is not supported" (like 713)
Nils Diewald86dad5b2015-01-28 15:09:07 +000034 814: "Unknown rewrite operation"
35 815: "Rewrite expects source"
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000036
37 Localization strings:
38 KorAP.Locale = {
39 EMPTY : '...',
40 AND : 'and',
41 OR : 'or',
42 DELETE : 'x'
43 }
Nils Diewald4c221252015-04-21 20:19:25 +000044 and various field names with the prefix 'VC_'
Nils Diewaldd0770492014-12-19 03:55:00 +000045*/
46
Nils Diewald0e6992a2015-04-14 20:13:52 +000047define([
48 'vc/unspecified',
49 'vc/doc',
50 'vc/docgroup',
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000051 'vc/menu',
Nils Diewald87507832015-05-01 23:36:41 +000052 'datepicker',
Nils Diewald0e6992a2015-04-14 20:13:52 +000053 'util'
Nils Diewald87507832015-05-01 23:36:41 +000054], function (unspecDocClass, docClass, docGroupClass, menuClass, dpClass) {
Nils Diewald3a2d8022014-12-16 02:45:41 +000055 "use strict";
56
Nils Diewald359a72c2015-04-20 17:40:29 +000057 // ???
Nils Diewald86dad5b2015-01-28 15:09:07 +000058 KorAP._validStringMatchRE = new RegExp("^(?:eq|ne|contains|excludes)$");
Nils Diewaldd0770492014-12-19 03:55:00 +000059 KorAP._validDateMatchRE = new RegExp("^[lg]?eq$");
Nils Diewald3a2d8022014-12-16 02:45:41 +000060 KorAP._validDateRE = new RegExp("^(?:\\d{4})(?:-\\d\\d(?:-\\d\\d)?)?$");
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) {
85 var sheet = KorAP.newStyleSheet();
86
87 // 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 );
94
95 // 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 );
102
Nils Diewald359a72c2015-04-20 17:40:29 +0000103 KorAP._overrideStyles = true;
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000104
105 // Create key menu
106 KorAP._vcKeyMenu = menuClass.create(keyList);
Nils Diewald4c221252015-04-21 20:19:25 +0000107 KorAP._vcKeyMenu.limit(6);
108
109 // Create match menus ....
110 KorAP._vcMatchopMenu = {
111 'string' : menuClass.create([
112 ['eq', null],
Akron792f58b2015-07-08 18:59:36 +0200113 ['ne', null],
Nils Diewald4c221252015-04-21 20:19:25 +0000114 ['contains', null],
115 ['excludes', null]
116 ]),
117 'date' : menuClass.create([
118 ['eq', null],
119 ['geq', null],
120 ['leq', null]
121 ]),
122 'regex' : menuClass.create([
123 ['eq', null],
Akron792f58b2015-07-08 18:59:36 +0200124 ['ne', null],
Nils Diewald4c221252015-04-21 20:19:25 +0000125 ['contains', null],
Akron792f58b2015-07-08 18:59:36 +0200126 ['excludes', null]
Nils Diewald4c221252015-04-21 20:19:25 +0000127 ])
128 };
Nils Diewald359a72c2015-04-20 17:40:29 +0000129 };
130
131 return this;
132 },
133
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000134 /**
135 * Create a new virtual collection.
136 */
137 create : function (keyList) {
Nils Diewald6283d692015-04-23 20:32:53 +0000138 var obj = Object.create(this)._init(keyList);
139 obj._root = unspecDocClass.create(obj);
140 return obj;
Nils Diewald4019bd22015-01-08 19:57:50 +0000141 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000142
Nils Diewald7148c6f2015-05-04 15:07:53 +0000143
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000144 /**
145 * Create and render a new virtual collection
146 * based on a KoralQuery collection document
147 */
Nils Diewald6283d692015-04-23 20:32:53 +0000148 fromJson : function (json) {
Nils Diewaldd0770492014-12-19 03:55:00 +0000149
150 if (json !== undefined) {
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000151 // Parse root document
Nils Diewald2fe12e12015-03-06 16:47:06 +0000152 if (json['@type'] == 'koral:doc') {
Nils Diewald6283d692015-04-23 20:32:53 +0000153 this._root = docClass.create(this, json);
Nils Diewaldd0770492014-12-19 03:55:00 +0000154 }
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000155 // parse root group
Nils Diewald2fe12e12015-03-06 16:47:06 +0000156 else if (json['@type'] == 'koral:docGroup') {
Nils Diewald6283d692015-04-23 20:32:53 +0000157 this._root = docGroupClass.create(this, json);
Nils Diewaldd0770492014-12-19 03:55:00 +0000158 }
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000159 // Unknown collection type
Nils Diewaldd0770492014-12-19 03:55:00 +0000160 else {
161 KorAP.log(813, "Collection type is not supported");
162 return;
163 };
164 }
165
166 else {
167 // Add unspecified object
Nils Diewald6283d692015-04-23 20:32:53 +0000168 this._root = unspecDocClass.create(this);
Nils Diewaldd0770492014-12-19 03:55:00 +0000169 };
170
Nils Diewald8e7182e2015-01-08 15:02:07 +0000171 // Init element and update
Nils Diewald6283d692015-04-23 20:32:53 +0000172 this.update();
Nils Diewaldd0770492014-12-19 03:55:00 +0000173
Nils Diewald6283d692015-04-23 20:32:53 +0000174 return this;
175 },
176
Nils Diewald7148c6f2015-05-04 15:07:53 +0000177
178 /**
179 * Clean the virtual document to uspecified doc.
180 */
Nils Diewald6283d692015-04-23 20:32:53 +0000181 clean : function () {
182 if (this._root.ldType() !== "non") {
183 this._root.destroy();
184 this.root(unspecDocClass.create(this));
185 };
186 return this;
Nils Diewald3a2d8022014-12-16 02:45:41 +0000187 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000188
Nils Diewald7148c6f2015-05-04 15:07:53 +0000189
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000190 /**
191 * Get or set the root object of the
192 * virtual collection.
193 */
Nils Diewaldf219eb82015-01-07 20:15:42 +0000194 root : function (obj) {
Nils Diewald8e7182e2015-01-08 15:02:07 +0000195 if (arguments.length === 1) {
Nils Diewald8f6b6102015-01-08 18:25:33 +0000196 var e = this.element();
Nils Diewald845282c2015-05-14 07:53:03 +0000197
Nils Diewald8f6b6102015-01-08 18:25:33 +0000198 if (e.firstChild !== null) {
Nils Diewaldd5070b02015-01-11 01:44:47 +0000199 if (e.firstChild !== obj.element()) {
Nils Diewald8f6b6102015-01-08 18:25:33 +0000200 e.replaceChild(obj.element(), e.firstChild);
Nils Diewaldd5070b02015-01-11 01:44:47 +0000201 };
Nils Diewald8f6b6102015-01-08 18:25:33 +0000202 }
203
204 // Append root element
205 else {
206 e.appendChild(obj.element());
207 };
208
209 // Update parent child relations
Nils Diewaldf219eb82015-01-07 20:15:42 +0000210 this._root = obj;
Nils Diewald8f6b6102015-01-08 18:25:33 +0000211 obj.parent(this);
212
Nils Diewald8e7182e2015-01-08 15:02:07 +0000213 this.update();
214 };
Nils Diewaldd0770492014-12-19 03:55:00 +0000215 return this._root;
Nils Diewald3a2d8022014-12-16 02:45:41 +0000216 },
Nils Diewald8f6b6102015-01-08 18:25:33 +0000217
Nils Diewald7148c6f2015-05-04 15:07:53 +0000218
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000219 /**
220 * Get the element associated with the virtual collection
221 */
Nils Diewaldd0770492014-12-19 03:55:00 +0000222 element : function () {
223 if (this._element !== undefined)
224 return this._element;
225
226 this._element = document.createElement('div');
227 this._element.setAttribute('class', 'vc');
Nils Diewald8e7182e2015-01-08 15:02:07 +0000228
Nils Diewald8f6b6102015-01-08 18:25:33 +0000229 // Initialize root
230 this._element.appendChild(this._root.element());
Nils Diewald845282c2015-05-14 07:53:03 +0000231
Nils Diewaldd0770492014-12-19 03:55:00 +0000232 return this._element;
Nils Diewaldf219eb82015-01-07 20:15:42 +0000233 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000234
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000235
236 /**
237 * Update the whole object based on the underlying
238 * data structure
239 */
Nils Diewaldd599d542015-01-08 20:41:34 +0000240 update : function () {
241 this._root.update();
242 return this;
243 },
244
Nils Diewald845282c2015-05-14 07:53:03 +0000245 /**
246 * Make the vc persistant by injecting the current timestamp
247 * as a creation date limit criterion.
248 */
249 makePersistant : function () {
250// this.root().wrapOnRoot('and');
251 var todayStr = KorAP._vcDatePicker.today();
252 var doc = docClass.create();
253 var root = this.root();
254
255 if (root.ldType() === 'docGroup' &&
256 root.operation === 'and') {
257 root.append(cond);
258 }
259 else {
260 root.wrapOnRoot('and');
261 root.append(doc);
262 };
263
264 doc.key("creationDate");
265 doc.type("date");
266 doc.matchop("leq");
267 doc.value(todayStr);
268
269/*
270 {
271 "@type" : "koral:doc",
272 "key" : "creationDate",
273 "type" : "type:date",
274 "match" : "match:leq",
275 "value" : todayStr
276 }
277 this.root().append(cond);
278*/
279 this.update();
280 },
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000281
282 /**
283 * Get the generated json string
284 */
Nils Diewaldf219eb82015-01-07 20:15:42 +0000285 toJson : function () {
286 return this._root.toJson();
287 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000288
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000289
290 /**
291 * Get the generated query string
292 */
Nils Diewaldd599d542015-01-08 20:41:34 +0000293 toQuery : function () {
294 return this._root.toQuery();
Nils Diewald3a2d8022014-12-16 02:45:41 +0000295 }
296 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000297});