blob: 48c569464ef08118eb73f5c03ab46aba1b92a38d [file] [log] [blame]
Nils Diewald86dad5b2015-01-28 15:09:07 +00001/**
2 * Create virtual collections with a visual user interface.
3 *
4 * @author Nils Diewald
5 */
Nils Diewald2fe12e12015-03-06 16:47:06 +00006/*
7 * Replaces a previous version written by Mengfei Zhou
8 */
Nils Diewald2fe12e12015-03-06 16:47:06 +00009
Nils Diewaldd0770492014-12-19 03:55:00 +000010/*
Nils Diewald86dad5b2015-01-28 15:09:07 +000011 TODO: Implement a working localization solution!
12 TODO: Disable "and" or "or" in case it's followed
13 by an unspecified document
Nils Diewald6e43ffd2015-03-25 18:55:39 +000014 TODO: Implement "persistance"-Option,
15 injecting the current creation date stamp
Nils Diewald86dad5b2015-01-28 15:09:07 +000016
Nils Diewaldd599d542015-01-08 20:41:34 +000017 Error codes:
Nils Diewaldd0770492014-12-19 03:55:00 +000018 701: "JSON-LD group has no @type attribute"
19 704: "Operation needs operand list"
Nils Diewald3a2d8022014-12-16 02:45:41 +000020 802: "Match type is not supported by value type"
21 804: "Unknown value type"
22 805: "Value is invalid"
23 806: "Value is not a valid date string"
24 807: "Value is not a valid regular expression"
Nils Diewald3a2d8022014-12-16 02:45:41 +000025 810: "Unknown document group operation" (like 711)
26 811: "Document group expects operation" (like 703)
27 812: "Operand not supported in document group" (like 744)
Nils Diewaldd0770492014-12-19 03:55:00 +000028 813: "Collection type is not supported" (like 713)
Nils Diewald86dad5b2015-01-28 15:09:07 +000029 814: "Unknown rewrite operation"
30 815: "Rewrite expects source"
Nils Diewaldd0770492014-12-19 03:55:00 +000031*/
32
Nils Diewald0e6992a2015-04-14 20:13:52 +000033define([
34 'vc/unspecified',
35 'vc/doc',
36 'vc/docgroup',
37 'util'
38], function (unspecDocClass, docClass, docGroupClass) {
Nils Diewald3a2d8022014-12-16 02:45:41 +000039 "use strict";
40
Nils Diewald86dad5b2015-01-28 15:09:07 +000041 KorAP._validStringMatchRE = new RegExp("^(?:eq|ne|contains|excludes)$");
Nils Diewald0e6992a2015-04-14 20:13:52 +000042 // KorAP._validRegexMatchRE = new RegExp("^(?:eq|ne)$");
Nils Diewaldd0770492014-12-19 03:55:00 +000043 KorAP._validDateMatchRE = new RegExp("^[lg]?eq$");
Nils Diewald3a2d8022014-12-16 02:45:41 +000044 KorAP._validDateRE = new RegExp("^(?:\\d{4})(?:-\\d\\d(?:-\\d\\d)?)?$");
Nils Diewald0e6992a2015-04-14 20:13:52 +000045 // KorAP._validGroupOpRE = new RegExp("^(?:and|or)$");
46 // KorAP._quote = new RegExp("([\"\\\\])", 'g');
Nils Diewald3a2d8022014-12-16 02:45:41 +000047
Nils Diewalde15b7a22015-01-09 21:50:21 +000048 // Localization values
Nils Diewaldf219eb82015-01-07 20:15:42 +000049 var loc = (KorAP.Locale = KorAP.Locale || {} );
Nils Diewald0e6992a2015-04-14 20:13:52 +000050 /*
51 loc.AND = loc.AND || 'and';
52 loc.OR = loc.OR || 'or';
53 loc.DEL = loc.DEL || '×';
54 loc.EMPTY = loc.EMPTY || '⋯'
55 */
Nils Diewaldd599d542015-01-08 20:41:34 +000056
57 /**
58 * Virtual Collection
59 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000060 return {
Nils Diewaldf219eb82015-01-07 20:15:42 +000061 ldType : function () {
62 return null;
63 },
Nils Diewaldd599d542015-01-08 20:41:34 +000064
Nils Diewald3a2d8022014-12-16 02:45:41 +000065 create : function () {
Nils Diewald0e6992a2015-04-14 20:13:52 +000066 return Object.create(this);
Nils Diewald3a2d8022014-12-16 02:45:41 +000067 },
Nils Diewaldd599d542015-01-08 20:41:34 +000068
Nils Diewald4019bd22015-01-08 19:57:50 +000069 clean : function () {
70 if (this._root.ldType() !== "non") {
71 this._root.destroy();
Nils Diewald0e6992a2015-04-14 20:13:52 +000072 this.root(unspecDocClass.create(this));
Nils Diewald4019bd22015-01-08 19:57:50 +000073 };
74 return this;
75 },
Nils Diewaldd599d542015-01-08 20:41:34 +000076
Nils Diewaldd0770492014-12-19 03:55:00 +000077 render : function (json) {
Nils Diewald0e6992a2015-04-14 20:13:52 +000078 var obj = Object.create(this);
Nils Diewaldd0770492014-12-19 03:55:00 +000079
80 if (json !== undefined) {
81 // Root object
Nils Diewald2fe12e12015-03-06 16:47:06 +000082 if (json['@type'] == 'koral:doc') {
Nils Diewald0e6992a2015-04-14 20:13:52 +000083 obj._root = docClass.create(obj, json);
Nils Diewaldd0770492014-12-19 03:55:00 +000084 }
Nils Diewald2fe12e12015-03-06 16:47:06 +000085 else if (json['@type'] == 'koral:docGroup') {
Nils Diewald0e6992a2015-04-14 20:13:52 +000086 obj._root = docGroupClass.create(obj, json);
Nils Diewaldd0770492014-12-19 03:55:00 +000087 }
88 else {
89 KorAP.log(813, "Collection type is not supported");
90 return;
91 };
92 }
93
94 else {
95 // Add unspecified object
Nils Diewaldf219eb82015-01-07 20:15:42 +000096 obj._root = KorAP.UnspecifiedDoc.create(obj);
Nils Diewaldd0770492014-12-19 03:55:00 +000097 };
98
Nils Diewald8e7182e2015-01-08 15:02:07 +000099 // Init element and update
100 obj.update();
Nils Diewaldd0770492014-12-19 03:55:00 +0000101
102 return obj;
Nils Diewald3a2d8022014-12-16 02:45:41 +0000103 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000104
Nils Diewaldf219eb82015-01-07 20:15:42 +0000105 root : function (obj) {
Nils Diewald8e7182e2015-01-08 15:02:07 +0000106 if (arguments.length === 1) {
Nils Diewald8f6b6102015-01-08 18:25:33 +0000107 var e = this.element();
108 if (e.firstChild !== null) {
Nils Diewaldd5070b02015-01-11 01:44:47 +0000109 if (e.firstChild !== obj.element()) {
Nils Diewald8f6b6102015-01-08 18:25:33 +0000110 e.replaceChild(obj.element(), e.firstChild);
Nils Diewaldd5070b02015-01-11 01:44:47 +0000111 };
Nils Diewald8f6b6102015-01-08 18:25:33 +0000112 }
113
114 // Append root element
115 else {
116 e.appendChild(obj.element());
117 };
118
119 // Update parent child relations
Nils Diewaldf219eb82015-01-07 20:15:42 +0000120 this._root = obj;
Nils Diewald8f6b6102015-01-08 18:25:33 +0000121 obj.parent(this);
122
Nils Diewald8e7182e2015-01-08 15:02:07 +0000123 this.update();
124 };
Nils Diewaldd0770492014-12-19 03:55:00 +0000125 return this._root;
Nils Diewald3a2d8022014-12-16 02:45:41 +0000126 },
Nils Diewald8f6b6102015-01-08 18:25:33 +0000127
Nils Diewaldd0770492014-12-19 03:55:00 +0000128 element : function () {
129 if (this._element !== undefined)
130 return this._element;
131
132 this._element = document.createElement('div');
133 this._element.setAttribute('class', 'vc');
Nils Diewald8e7182e2015-01-08 15:02:07 +0000134
Nils Diewald8f6b6102015-01-08 18:25:33 +0000135 // Initialize root
136 this._element.appendChild(this._root.element());
137
Nils Diewaldd0770492014-12-19 03:55:00 +0000138 return this._element;
Nils Diewaldf219eb82015-01-07 20:15:42 +0000139 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000140
141 update : function () {
142 this._root.update();
143 return this;
144 },
145
Nils Diewaldf219eb82015-01-07 20:15:42 +0000146 toJson : function () {
147 return this._root.toJson();
148 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000149
150 toQuery : function () {
151 return this._root.toQuery();
Nils Diewald3a2d8022014-12-16 02:45:41 +0000152 }
153 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000154});