blob: 65ac5ca16cfabb888a86b961c816e82002d58897 [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 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 Diewald3a2d8022014-12-16 02:45:41 +000057 create : function () {
Nils Diewald0e6992a2015-04-14 20:13:52 +000058 return Object.create(this);
Nils Diewald3a2d8022014-12-16 02:45:41 +000059 },
Nils Diewaldd599d542015-01-08 20:41:34 +000060
Nils Diewald4019bd22015-01-08 19:57:50 +000061 clean : function () {
62 if (this._root.ldType() !== "non") {
63 this._root.destroy();
Nils Diewald0e6992a2015-04-14 20:13:52 +000064 this.root(unspecDocClass.create(this));
Nils Diewald4019bd22015-01-08 19:57:50 +000065 };
66 return this;
67 },
Nils Diewaldd599d542015-01-08 20:41:34 +000068
Nils Diewaldd0770492014-12-19 03:55:00 +000069 render : function (json) {
Nils Diewald0e6992a2015-04-14 20:13:52 +000070 var obj = Object.create(this);
Nils Diewaldd0770492014-12-19 03:55:00 +000071
72 if (json !== undefined) {
73 // Root object
Nils Diewald2fe12e12015-03-06 16:47:06 +000074 if (json['@type'] == 'koral:doc') {
Nils Diewald0e6992a2015-04-14 20:13:52 +000075 obj._root = docClass.create(obj, json);
Nils Diewaldd0770492014-12-19 03:55:00 +000076 }
Nils Diewald2fe12e12015-03-06 16:47:06 +000077 else if (json['@type'] == 'koral:docGroup') {
Nils Diewald0e6992a2015-04-14 20:13:52 +000078 obj._root = docGroupClass.create(obj, json);
Nils Diewaldd0770492014-12-19 03:55:00 +000079 }
80 else {
81 KorAP.log(813, "Collection type is not supported");
82 return;
83 };
84 }
85
86 else {
87 // Add unspecified object
Nils Diewald7c8ced22015-04-15 19:21:00 +000088 obj._root = unspecDocClass.create(obj);
Nils Diewaldd0770492014-12-19 03:55:00 +000089 };
90
Nils Diewald8e7182e2015-01-08 15:02:07 +000091 // Init element and update
92 obj.update();
Nils Diewaldd0770492014-12-19 03:55:00 +000093
94 return obj;
Nils Diewald3a2d8022014-12-16 02:45:41 +000095 },
Nils Diewaldd599d542015-01-08 20:41:34 +000096
Nils Diewaldf219eb82015-01-07 20:15:42 +000097 root : function (obj) {
Nils Diewald8e7182e2015-01-08 15:02:07 +000098 if (arguments.length === 1) {
Nils Diewald8f6b6102015-01-08 18:25:33 +000099 var e = this.element();
100 if (e.firstChild !== null) {
Nils Diewaldd5070b02015-01-11 01:44:47 +0000101 if (e.firstChild !== obj.element()) {
Nils Diewald8f6b6102015-01-08 18:25:33 +0000102 e.replaceChild(obj.element(), e.firstChild);
Nils Diewaldd5070b02015-01-11 01:44:47 +0000103 };
Nils Diewald8f6b6102015-01-08 18:25:33 +0000104 }
105
106 // Append root element
107 else {
108 e.appendChild(obj.element());
109 };
110
111 // Update parent child relations
Nils Diewaldf219eb82015-01-07 20:15:42 +0000112 this._root = obj;
Nils Diewald8f6b6102015-01-08 18:25:33 +0000113 obj.parent(this);
114
Nils Diewald8e7182e2015-01-08 15:02:07 +0000115 this.update();
116 };
Nils Diewaldd0770492014-12-19 03:55:00 +0000117 return this._root;
Nils Diewald3a2d8022014-12-16 02:45:41 +0000118 },
Nils Diewald8f6b6102015-01-08 18:25:33 +0000119
Nils Diewaldd0770492014-12-19 03:55:00 +0000120 element : function () {
121 if (this._element !== undefined)
122 return this._element;
123
124 this._element = document.createElement('div');
125 this._element.setAttribute('class', 'vc');
Nils Diewald8e7182e2015-01-08 15:02:07 +0000126
Nils Diewald8f6b6102015-01-08 18:25:33 +0000127 // Initialize root
128 this._element.appendChild(this._root.element());
129
Nils Diewaldd0770492014-12-19 03:55:00 +0000130 return this._element;
Nils Diewaldf219eb82015-01-07 20:15:42 +0000131 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000132
133 update : function () {
134 this._root.update();
135 return this;
136 },
137
Nils Diewaldf219eb82015-01-07 20:15:42 +0000138 toJson : function () {
139 return this._root.toJson();
140 },
Nils Diewaldd599d542015-01-08 20:41:34 +0000141
142 toQuery : function () {
143 return this._root.toQuery();
Nils Diewald3a2d8022014-12-16 02:45:41 +0000144 }
145 };
Nils Diewald0e6992a2015-04-14 20:13:52 +0000146});