blob: 18c621a3a4a294bd978aabfe145a769686ff757d [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);
141 obj._root = unspecDocClass.create(obj);
142 return obj;
Nils Diewald4019bd22015-01-08 19:57:50 +0000143 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000144
Nils Diewald7148c6f2015-05-04 15:07:53 +0000145
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000146 /**
147 * Create and render a new virtual collection
148 * based on a KoralQuery collection document
149 */
Nils Diewald6283d692015-04-23 20:32:53 +0000150 fromJson : function (json) {
Nils Diewaldd0770492014-12-19 03:55:00 +0000151 if (json !== undefined) {
Akrone4961b12017-05-10 21:04:46 +0200152 // Parse root document
153 if (json['@type'] == 'koral:doc') {
154 this._root = docClass.create(this, json);
155 }
156 // parse root group
157 else if (json['@type'] == 'koral:docGroup') {
158 this._root = docGroupClass.create(this, json);
159 }
160 // Unknown collection type
161 else {
162 KorAP.log(813, "Collection type is not supported");
163 return;
164 };
Nils Diewaldd0770492014-12-19 03:55:00 +0000165 }
166
167 else {
Akrone4961b12017-05-10 21:04:46 +0200168 // Add unspecified object
169 this._root = unspecDocClass.create(this);
Nils Diewaldd0770492014-12-19 03:55:00 +0000170 };
171
Nils Diewald8e7182e2015-01-08 15:02:07 +0000172 // Init element and update
Nils Diewald6283d692015-04-23 20:32:53 +0000173 this.update();
Nils Diewaldd0770492014-12-19 03:55:00 +0000174
Nils Diewald6283d692015-04-23 20:32:53 +0000175 return this;
176 },
177
Nils Diewald7148c6f2015-05-04 15:07:53 +0000178
Akron04671e72017-05-11 20:47:32 +0200179 // Check if the virtual corpus contains a rewrite
180 // This is a class method
181 checkRewrite : function (json) {
182
183 // There is a rewrite attribute
184 if (json['rewrites'] !== undefined) {
185 return true;
186 }
187
188 // There is a group to check for rewrites
189 else if (json['@type'] === 'koral:docGroup') {
190 var ops = json['operands'];
191 if (ops === undefined)
192 return false;
193
194 for (var i in ops) {
195
196 // "this" is the class
197 if (this.checkRewrite(ops[i])) {
198 return true;
199 };
200 };
201 };
202 return false;
203 },
204
Nils Diewald7148c6f2015-05-04 15:07:53 +0000205 /**
206 * Clean the virtual document to uspecified doc.
207 */
Nils Diewald6283d692015-04-23 20:32:53 +0000208 clean : function () {
209 if (this._root.ldType() !== "non") {
Akron04671e72017-05-11 20:47:32 +0200210 this._root.destroy();
211 this.root(unspecDocClass.create(this));
Nils Diewald6283d692015-04-23 20:32:53 +0000212 };
213 return this;
Nils Diewald3a2d8022014-12-16 02:45:41 +0000214 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000215
Nils Diewald7148c6f2015-05-04 15:07:53 +0000216
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000217 /**
218 * Get or set the root object of the
219 * virtual collection.
220 */
Nils Diewaldf219eb82015-01-07 20:15:42 +0000221 root : function (obj) {
Nils Diewald8e7182e2015-01-08 15:02:07 +0000222 if (arguments.length === 1) {
Akrone4961b12017-05-10 21:04:46 +0200223 var e = this.element();
Nils Diewald845282c2015-05-14 07:53:03 +0000224
Akrone4961b12017-05-10 21:04:46 +0200225 if (e.firstChild !== null) {
226 if (e.firstChild !== obj.element()) {
227 e.replaceChild(obj.element(), e.firstChild);
228 };
229 }
Nils Diewald8f6b6102015-01-08 18:25:33 +0000230
Akrone4961b12017-05-10 21:04:46 +0200231 // Append root element
232 else {
233 e.appendChild(obj.element());
234 };
Nils Diewald8f6b6102015-01-08 18:25:33 +0000235
Akrone4961b12017-05-10 21:04:46 +0200236 // Update parent child relations
237 this._root = obj;
238 obj.parent(this);
Nils Diewald8f6b6102015-01-08 18:25:33 +0000239
Akrone4961b12017-05-10 21:04:46 +0200240 this.update();
Nils Diewald8e7182e2015-01-08 15:02:07 +0000241 };
Nils Diewaldd0770492014-12-19 03:55:00 +0000242 return this._root;
Nils Diewald3a2d8022014-12-16 02:45:41 +0000243 },
Nils Diewald8f6b6102015-01-08 18:25:33 +0000244
Nils Diewald7148c6f2015-05-04 15:07:53 +0000245
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000246 /**
247 * Get the element associated with the virtual collection
248 */
Nils Diewaldd0770492014-12-19 03:55:00 +0000249 element : function () {
Akron12971a02018-01-03 16:38:10 +0100250 if (this._element !== undefined) {
251 return this._element;
252 };
Nils Diewaldd0770492014-12-19 03:55:00 +0000253
254 this._element = document.createElement('div');
255 this._element.setAttribute('class', 'vc');
Nils Diewald8e7182e2015-01-08 15:02:07 +0000256
Nils Diewald8f6b6102015-01-08 18:25:33 +0000257 // Initialize root
258 this._element.appendChild(this._root.element());
Nils Diewald845282c2015-05-14 07:53:03 +0000259
Nils Diewaldd0770492014-12-19 03:55:00 +0000260 return this._element;
Nils Diewaldf219eb82015-01-07 20:15:42 +0000261 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000262
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000263
264 /**
265 * Update the whole object based on the underlying
266 * data structure
267 */
Nils Diewaldd599d542015-01-08 20:41:34 +0000268 update : function () {
269 this._root.update();
270 return this;
271 },
272
Nils Diewald845282c2015-05-14 07:53:03 +0000273 /**
274 * Make the vc persistant by injecting the current timestamp
275 * as a creation date limit criterion.
276 */
277 makePersistant : function () {
Akron12971a02018-01-03 16:38:10 +0100278 // this.root().wrapOnRoot('and');
Nils Diewald845282c2015-05-14 07:53:03 +0000279 var todayStr = KorAP._vcDatePicker.today();
280 var doc = docClass.create();
281 var root = this.root();
282
283 if (root.ldType() === 'docGroup' &&
Akron12971a02018-01-03 16:38:10 +0100284 root.operation === 'and') {
285 root.append(cond);
Nils Diewald845282c2015-05-14 07:53:03 +0000286 }
287 else {
Akron12971a02018-01-03 16:38:10 +0100288 root.wrapOnRoot('and');
289 root.append(doc);
Nils Diewald845282c2015-05-14 07:53:03 +0000290 };
291
292 doc.key("creationDate");
293 doc.type("date");
294 doc.matchop("leq");
295 doc.value(todayStr);
296
297/*
298 {
299 "@type" : "koral:doc",
300 "key" : "creationDate",
301 "type" : "type:date",
302 "match" : "match:leq",
303 "value" : todayStr
304 }
305 this.root().append(cond);
306*/
307 this.update();
308 },
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000309
310 /**
311 * Get the generated json string
312 */
Nils Diewaldf219eb82015-01-07 20:15:42 +0000313 toJson : function () {
314 return this._root.toJson();
315 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000316
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000317
318 /**
319 * Get the generated query string
320 */
Nils Diewaldd599d542015-01-08 20:41:34 +0000321 toQuery : function () {
322 return this._root.toQuery();
Nils Diewald3a2d8022014-12-16 02:45:41 +0000323 }
324 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000325});