blob: 45982752d80c6ca4ff152760440e05b611115906 [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 Diewald359a72c2015-04-20 17:40:29 +000041 // ???
Nils Diewald86dad5b2015-01-28 15:09:07 +000042 KorAP._validStringMatchRE = new RegExp("^(?:eq|ne|contains|excludes)$");
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 Diewald359a72c2015-04-20 17:40:29 +000045 KorAP._overrideStyles = false;
Nils Diewald3a2d8022014-12-16 02:45:41 +000046
Nils Diewald359a72c2015-04-20 17:40:29 +000047 var loc = KorAP.Locale;
Nils Diewaldd599d542015-01-08 20:41:34 +000048
49 /**
50 * Virtual Collection
51 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000052 return {
Nils Diewaldf219eb82015-01-07 20:15:42 +000053 ldType : function () {
54 return null;
55 },
Nils Diewaldd599d542015-01-08 20:41:34 +000056
Nils Diewald359a72c2015-04-20 17:40:29 +000057 _init : function () {
58 if (!KorAP._overrideStyles) {
59 var sheet = KorAP.newStyleSheet();
60
61 // Add css rule for OR operations
62 sheet.insertRule(
63 '.vc .docGroup[data-operation=or] > .doc::before,' +
64 '.vc .docGroup[data-operation=or] > .docGroup::before ' +
65 '{ content: "' + loc.OR + '" }',
66 0
67 );
68
69 // Add css rule for AND operations
70 sheet.insertRule(
71 '.vc .docGroup[data-operation=and] > .doc::before,' +
72 '.vc .docGroup[data-operation=and] > .docGroup::before ' +
73 '{ content: "' + loc.AND + '" }',
74 1
75 );
76
77 console.log(sheet);
78
79 KorAP._overrideStyles = true;
80 };
81
82 return this;
83 },
84
Nils Diewald3a2d8022014-12-16 02:45:41 +000085 create : function () {
Nils Diewald359a72c2015-04-20 17:40:29 +000086 return Object.create(this)._init();
Nils Diewald3a2d8022014-12-16 02:45:41 +000087 },
Nils Diewaldd599d542015-01-08 20:41:34 +000088
Nils Diewald4019bd22015-01-08 19:57:50 +000089 clean : function () {
90 if (this._root.ldType() !== "non") {
91 this._root.destroy();
Nils Diewald0e6992a2015-04-14 20:13:52 +000092 this.root(unspecDocClass.create(this));
Nils Diewald4019bd22015-01-08 19:57:50 +000093 };
94 return this;
95 },
Nils Diewaldd599d542015-01-08 20:41:34 +000096
Nils Diewaldd0770492014-12-19 03:55:00 +000097 render : function (json) {
Nils Diewald359a72c2015-04-20 17:40:29 +000098 var obj = Object.create(this)._init();
Nils Diewaldd0770492014-12-19 03:55:00 +000099
100 if (json !== undefined) {
101 // Root object
Nils Diewald2fe12e12015-03-06 16:47:06 +0000102 if (json['@type'] == 'koral:doc') {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000103 obj._root = docClass.create(obj, json);
Nils Diewaldd0770492014-12-19 03:55:00 +0000104 }
Nils Diewald2fe12e12015-03-06 16:47:06 +0000105 else if (json['@type'] == 'koral:docGroup') {
Nils Diewald0e6992a2015-04-14 20:13:52 +0000106 obj._root = docGroupClass.create(obj, json);
Nils Diewaldd0770492014-12-19 03:55:00 +0000107 }
108 else {
109 KorAP.log(813, "Collection type is not supported");
110 return;
111 };
112 }
113
114 else {
115 // Add unspecified object
Nils Diewald7c8ced22015-04-15 19:21:00 +0000116 obj._root = unspecDocClass.create(obj);
Nils Diewaldd0770492014-12-19 03:55:00 +0000117 };
118
Nils Diewald8e7182e2015-01-08 15:02:07 +0000119 // Init element and update
120 obj.update();
Nils Diewaldd0770492014-12-19 03:55:00 +0000121
122 return obj;
Nils Diewald3a2d8022014-12-16 02:45:41 +0000123 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000124
Nils Diewaldf219eb82015-01-07 20:15:42 +0000125 root : function (obj) {
Nils Diewald8e7182e2015-01-08 15:02:07 +0000126 if (arguments.length === 1) {
Nils Diewald8f6b6102015-01-08 18:25:33 +0000127 var e = this.element();
128 if (e.firstChild !== null) {
Nils Diewaldd5070b02015-01-11 01:44:47 +0000129 if (e.firstChild !== obj.element()) {
Nils Diewald8f6b6102015-01-08 18:25:33 +0000130 e.replaceChild(obj.element(), e.firstChild);
Nils Diewaldd5070b02015-01-11 01:44:47 +0000131 };
Nils Diewald8f6b6102015-01-08 18:25:33 +0000132 }
133
134 // Append root element
135 else {
136 e.appendChild(obj.element());
137 };
138
139 // Update parent child relations
Nils Diewaldf219eb82015-01-07 20:15:42 +0000140 this._root = obj;
Nils Diewald8f6b6102015-01-08 18:25:33 +0000141 obj.parent(this);
142
Nils Diewald8e7182e2015-01-08 15:02:07 +0000143 this.update();
144 };
Nils Diewaldd0770492014-12-19 03:55:00 +0000145 return this._root;
Nils Diewald3a2d8022014-12-16 02:45:41 +0000146 },
Nils Diewald8f6b6102015-01-08 18:25:33 +0000147
Nils Diewaldd0770492014-12-19 03:55:00 +0000148 element : function () {
149 if (this._element !== undefined)
150 return this._element;
151
152 this._element = document.createElement('div');
153 this._element.setAttribute('class', 'vc');
Nils Diewald8e7182e2015-01-08 15:02:07 +0000154
Nils Diewald8f6b6102015-01-08 18:25:33 +0000155 // Initialize root
156 this._element.appendChild(this._root.element());
157
Nils Diewaldd0770492014-12-19 03:55:00 +0000158 return this._element;
Nils Diewaldf219eb82015-01-07 20:15:42 +0000159 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000160
161 update : function () {
162 this._root.update();
163 return this;
164 },
165
Nils Diewaldf219eb82015-01-07 20:15:42 +0000166 toJson : function () {
167 return this._root.toJson();
168 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000169
170 toQuery : function () {
171 return this._root.toQuery();
Nils Diewald3a2d8022014-12-16 02:45:41 +0000172 }
173 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000174});