blob: 22b2c845f82c06337aeb37db00e77b2e2f34b9b8 [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 Diewaldd0770492014-12-19 03:55:00 +000061 KorAP._validDateMatchRE = new RegExp("^[lg]?eq$");
Nils Diewald3a2d8022014-12-16 02:45:41 +000062 KorAP._validDateRE = new RegExp("^(?:\\d{4})(?:-\\d\\d(?:-\\d\\d)?)?$");
Nils Diewald359a72c2015-04-20 17:40:29 +000063 KorAP._overrideStyles = false;
Nils Diewald3a2d8022014-12-16 02:45:41 +000064
Nils Diewald359a72c2015-04-20 17:40:29 +000065 var loc = KorAP.Locale;
Nils Diewaldd599d542015-01-08 20:41:34 +000066
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000067 KorAP._vcKeyMenu = undefined;
Nils Diewald87507832015-05-01 23:36:41 +000068 KorAP._vcDatePicker = dpClass.create();
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000069
Nils Diewaldd599d542015-01-08 20:41:34 +000070 /**
71 * Virtual Collection
72 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000073 return {
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000074
75 /**
76 * The JSON-LD type of the virtual collection
77 */
Nils Diewaldf219eb82015-01-07 20:15:42 +000078 ldType : function () {
79 return null;
80 },
Nils Diewaldd599d542015-01-08 20:41:34 +000081
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000082 // Initialize virtual collection
83 _init : function (keyList) {
84
85 // Inject localized css styles
Nils Diewald359a72c2015-04-20 17:40:29 +000086 if (!KorAP._overrideStyles) {
Akrone4961b12017-05-10 21:04:46 +020087 var sheet = KorAP.newStyleSheet();
Nils Diewald359a72c2015-04-20 17:40:29 +000088
Akrone4961b12017-05-10 21:04:46 +020089 // Add css rule for OR operations
90 sheet.insertRule(
91 '.vc .docGroup[data-operation=or] > .doc::before,' +
92 '.vc .docGroup[data-operation=or] > .docGroup::before ' +
93 '{ content: "' + loc.OR + '" }',
94 0
95 );
Nils Diewald359a72c2015-04-20 17:40:29 +000096
Akrone4961b12017-05-10 21:04:46 +020097 // Add css rule for AND operations
98 sheet.insertRule(
99 '.vc .docGroup[data-operation=and] > .doc::before,' +
100 '.vc .docGroup[data-operation=and] > .docGroup::before ' +
101 '{ content: "' + loc.AND + '" }',
102 1
103 );
Nils Diewald359a72c2015-04-20 17:40:29 +0000104
Akrone4961b12017-05-10 21:04:46 +0200105 KorAP._overrideStyles = true;
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000106
Akrone4961b12017-05-10 21:04:46 +0200107 // Create key menu
108 KorAP._vcKeyMenu = menuClass.create(keyList);
109 KorAP._vcKeyMenu.limit(6);
Nils Diewald4c221252015-04-21 20:19:25 +0000110
Akrone4961b12017-05-10 21:04:46 +0200111 // Create match menus ....
112 KorAP._vcMatchopMenu = {
113 'string' : menuClass.create([
114 ['eq', null],
115 ['ne', null],
116 ['contains', null],
117 ['containsnot', null]
118 ]),
119 'date' : menuClass.create([
120 ['eq', null],
121 ['geq', null],
122 ['leq', null]
123 ]),
124 'regex' : menuClass.create([
125 ['eq', null],
Akrondec2d692017-07-06 16:15:12 +0200126 ['ne', null]
Akrone4961b12017-05-10 21:04:46 +0200127 ])
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 if (json !== undefined) {
Akrone4961b12017-05-10 21:04:46 +0200150 // Parse root document
151 if (json['@type'] == 'koral:doc') {
152 this._root = docClass.create(this, json);
153 }
154 // parse root group
155 else if (json['@type'] == 'koral:docGroup') {
156 this._root = docGroupClass.create(this, json);
157 }
158 // Unknown collection type
159 else {
160 KorAP.log(813, "Collection type is not supported");
161 return;
162 };
Nils Diewaldd0770492014-12-19 03:55:00 +0000163 }
164
165 else {
Akrone4961b12017-05-10 21:04:46 +0200166 // Add unspecified object
167 this._root = unspecDocClass.create(this);
Nils Diewaldd0770492014-12-19 03:55:00 +0000168 };
169
Nils Diewald8e7182e2015-01-08 15:02:07 +0000170 // Init element and update
Nils Diewald6283d692015-04-23 20:32:53 +0000171 this.update();
Nils Diewaldd0770492014-12-19 03:55:00 +0000172
Nils Diewald6283d692015-04-23 20:32:53 +0000173 return this;
174 },
175
Nils Diewald7148c6f2015-05-04 15:07:53 +0000176
Akron04671e72017-05-11 20:47:32 +0200177 // Check if the virtual corpus contains a rewrite
178 // This is a class method
179 checkRewrite : function (json) {
180
181 // There is a rewrite attribute
182 if (json['rewrites'] !== undefined) {
183 return true;
184 }
185
186 // There is a group to check for rewrites
187 else if (json['@type'] === 'koral:docGroup') {
188 var ops = json['operands'];
189 if (ops === undefined)
190 return false;
191
192 for (var i in ops) {
193
194 // "this" is the class
195 if (this.checkRewrite(ops[i])) {
196 return true;
197 };
198 };
199 };
200 return false;
201 },
202
Nils Diewald7148c6f2015-05-04 15:07:53 +0000203 /**
204 * Clean the virtual document to uspecified doc.
205 */
Nils Diewald6283d692015-04-23 20:32:53 +0000206 clean : function () {
207 if (this._root.ldType() !== "non") {
Akron04671e72017-05-11 20:47:32 +0200208 this._root.destroy();
209 this.root(unspecDocClass.create(this));
Nils Diewald6283d692015-04-23 20:32:53 +0000210 };
211 return this;
Nils Diewald3a2d8022014-12-16 02:45:41 +0000212 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000213
Nils Diewald7148c6f2015-05-04 15:07:53 +0000214
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000215 /**
216 * Get or set the root object of the
217 * virtual collection.
218 */
Nils Diewaldf219eb82015-01-07 20:15:42 +0000219 root : function (obj) {
Nils Diewald8e7182e2015-01-08 15:02:07 +0000220 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200221 var e = this.element();
Nils Diewald845282c2015-05-14 07:53:03 +0000222
Akrone4961b12017-05-10 21:04:46 +0200223 if (e.firstChild !== null) {
224 if (e.firstChild !== obj.element()) {
225 e.replaceChild(obj.element(), e.firstChild);
226 };
227 }
Nils Diewald8f6b6102015-01-08 18:25:33 +0000228
Akrone4961b12017-05-10 21:04:46 +0200229 // Append root element
230 else {
231 e.appendChild(obj.element());
232 };
Nils Diewald8f6b6102015-01-08 18:25:33 +0000233
Akrone4961b12017-05-10 21:04:46 +0200234 // Update parent child relations
235 this._root = obj;
236 obj.parent(this);
Nils Diewald8f6b6102015-01-08 18:25:33 +0000237
Akrone4961b12017-05-10 21:04:46 +0200238 this.update();
Nils Diewald8e7182e2015-01-08 15:02:07 +0000239 };
Nils Diewaldd0770492014-12-19 03:55:00 +0000240 return this._root;
Nils Diewald3a2d8022014-12-16 02:45:41 +0000241 },
Nils Diewald8f6b6102015-01-08 18:25:33 +0000242
Nils Diewald7148c6f2015-05-04 15:07:53 +0000243
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000244 /**
245 * Get the element associated with the virtual collection
246 */
Nils Diewaldd0770492014-12-19 03:55:00 +0000247 element : function () {
248 if (this._element !== undefined)
249 return this._element;
250
251 this._element = document.createElement('div');
252 this._element.setAttribute('class', 'vc');
Nils Diewald8e7182e2015-01-08 15:02:07 +0000253
Nils Diewald8f6b6102015-01-08 18:25:33 +0000254 // Initialize root
255 this._element.appendChild(this._root.element());
Nils Diewald845282c2015-05-14 07:53:03 +0000256
Nils Diewaldd0770492014-12-19 03:55:00 +0000257 return this._element;
Nils Diewaldf219eb82015-01-07 20:15:42 +0000258 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000259
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000260
261 /**
262 * Update the whole object based on the underlying
263 * data structure
264 */
Nils Diewaldd599d542015-01-08 20:41:34 +0000265 update : function () {
266 this._root.update();
267 return this;
268 },
269
Nils Diewald845282c2015-05-14 07:53:03 +0000270 /**
271 * Make the vc persistant by injecting the current timestamp
272 * as a creation date limit criterion.
273 */
274 makePersistant : function () {
275// this.root().wrapOnRoot('and');
276 var todayStr = KorAP._vcDatePicker.today();
277 var doc = docClass.create();
278 var root = this.root();
279
280 if (root.ldType() === 'docGroup' &&
281 root.operation === 'and') {
282 root.append(cond);
283 }
284 else {
285 root.wrapOnRoot('and');
286 root.append(doc);
287 };
288
289 doc.key("creationDate");
290 doc.type("date");
291 doc.matchop("leq");
292 doc.value(todayStr);
293
294/*
295 {
296 "@type" : "koral:doc",
297 "key" : "creationDate",
298 "type" : "type:date",
299 "match" : "match:leq",
300 "value" : todayStr
301 }
302 this.root().append(cond);
303*/
304 this.update();
305 },
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000306
307 /**
308 * Get the generated json string
309 */
Nils Diewaldf219eb82015-01-07 20:15:42 +0000310 toJson : function () {
311 return this._root.toJson();
312 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000313
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000314
315 /**
316 * Get the generated query string
317 */
Nils Diewaldd599d542015-01-08 20:41:34 +0000318 toQuery : function () {
319 return this._root.toQuery();
Nils Diewald3a2d8022014-12-16 02:45:41 +0000320 }
321 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000322});