Add appending of unspecified documents
diff --git a/public/js/demo/vc.html b/public/js/demo/vc.html
index c971dd7..372571c 100644
--- a/public/js/demo/vc.html
+++ b/public/js/demo/vc.html
@@ -73,8 +73,8 @@
document.getElementById("json").innerHTML = JSON.stringify(vc.root().toJson());
};
- function showString() {
- document.getElementById("string").innerHTML = vc.root().toString();
+ function showQuery() {
+ document.getElementById("query").innerHTML = vc.root().toQuery();
};
</script>
@@ -86,8 +86,8 @@
<hr />
- <p><a onclick="showString()" style="cursor:pointer">show String!</a></p>
- <div id="string" class="info"></div>
+ <p><a onclick="showQuery()" style="cursor:pointer">show Query!</a></p>
+ <div id="query" class="info"></div>
</body>
</html>
diff --git a/public/js/spec/vcSpec.js b/public/js/spec/vcSpec.js
index 5f7d3ad..767f010 100644
--- a/public/js/spec/vcSpec.js
+++ b/public/js/spec/vcSpec.js
@@ -237,32 +237,32 @@
// Empty doc
var doc = KorAP.Doc.create();
- expect(doc.toString()).toEqual("");
+ expect(doc.toQuery()).toEqual("");
// Serialize string
doc = stringFactory.create();
- expect(doc.toString()).toEqual('author = "Max Birkendale"');
+ expect(doc.toQuery()).toEqual('author = "Max Birkendale"');
// Serialize string with quotes
doc = stringFactory.create({ "value" : 'Max "Der Coole" Birkendate'});
- expect(doc.toString()).toEqual('author = "Max \\"Der Coole\\" Birkendate"');
+ expect(doc.toQuery()).toEqual('author = "Max \\"Der Coole\\" Birkendate"');
// Serialize regex
doc = regexFactory.create();
- expect(doc.toString()).toEqual('title = /[^b]ee.+?/');
+ expect(doc.toQuery()).toEqual('title = /[^b]ee.+?/');
doc = regexFactory.create({
match: "match:ne"
});
- expect(doc.toString()).toEqual('title != /[^b]ee.+?/');
+ expect(doc.toQuery()).toEqual('title != /[^b]ee.+?/');
doc = dateFactory.create();
- expect(doc.toString()).toEqual('pubDate in 2014-11-05');
+ expect(doc.toQuery()).toEqual('pubDate in 2014-11-05');
doc = dateFactory.create({
value : "2014"
});
- expect(doc.toString()).toEqual('pubDate in 2014');
+ expect(doc.toQuery()).toEqual('pubDate in 2014');
});
});
@@ -411,7 +411,7 @@
it('should be serializable to String', function () {
var docGroup = docGroupFactory.create();
- expect(docGroup.toString()).toEqual('author = "Max Birkendale" & pubDate in 2014-12-05');
+ expect(docGroup.toQuery()).toEqual('author = "Max Birkendale" & pubDate in 2014-12-05');
docGroup = docGroupFactory.create({
"@type" : "korap:docGroup",
@@ -453,7 +453,7 @@
}
]
});
- expect(docGroup.toString()).toEqual(
+ expect(docGroup.toQuery()).toEqual(
'author = "Max Birkendale" | (pubDate since 2014-05-12 & pubDate until 2014-12-05 & foo != /[a]?bar/)'
);
});
@@ -463,10 +463,10 @@
it('should be initializable', function () {
var doc = KorAP.UnspecifiedDoc.create();
var docElement = doc.element();
- expect(docElement.getAttribute('class')).toEqual('unspecified');
+ expect(docElement.getAttribute('class')).toEqual('doc unspecified');
expect(docElement.firstChild.firstChild.data).toEqual('⋯');
expect(docElement.lastChild.getAttribute('class')).toEqual('operators');
- expect(doc.toString()).toEqual('⋯');
+ expect(doc.toQuery()).toEqual('⋯');
// Only removable
expect(docElement.lastChild.children.length).toEqual(0);
@@ -492,7 +492,7 @@
expect(docGroup.element().children[0].getAttribute('class')).toEqual('doc');
var unspec = docGroup.element().children[1];
- expect(unspec.getAttribute('class')).toEqual('unspecified');
+ expect(unspec.getAttribute('class')).toEqual('doc unspecified');
// Removable
expect(unspec.lastChild.children.length).toEqual(1);
@@ -781,7 +781,7 @@
it('should be initializable', function () {
var vc = KorAP.VirtualCollection.render();
expect(vc.element().getAttribute('class')).toEqual('vc');
- expect(vc.root().element().getAttribute('class')).toEqual('unspecified');
+ expect(vc.root().element().getAttribute('class')).toEqual('doc unspecified');
// Not removable
expect(vc.root().element().lastChild.children.length).toEqual(0);
@@ -882,7 +882,7 @@
it('should be modifiable by deletion in nested docGroups (root case)', function () {
var vc = nestedGroupFactory.create();
- expect(vc.toString()).toEqual(
+ expect(vc.toQuery()).toEqual(
'author = "Max Birkendale" | (pubDate since 2014-05-12 & pubDate until 2014-12-05)'
);
@@ -920,7 +920,7 @@
expect(vc.root().operation()).toEqual("and");
expect(vc.root().getOperand(0).ldType()).toEqual("doc");
- expect(vc.toString()).toEqual(
+ expect(vc.toQuery()).toEqual(
'pubDate since 2014-05-12 & pubDate until 2014-12-05'
);
});
@@ -961,7 +961,7 @@
// Structur is now:
// or(doc, and(doc, doc, or(doc, doc)))
- expect(vc.toString()).toEqual(
+ expect(vc.toQuery()).toEqual(
'author = "Max Birkendale" | (pubDate since 2014-05-12 & pubDate until 2014-12-05 & (title = "Der Birnbaum" | pubDate in 2014-12-05))'
);
@@ -980,8 +980,10 @@
// Structur is now:
// or(doc, and(doc, or(doc, doc)))
- expect(vc.toString()).toEqual(
- 'author = "Max Birkendale" | (pubDate since 2014-05-12 & (title = "Der Birnbaum" | pubDate in 2014-12-05))'
+ expect(vc.toQuery()).toEqual(
+ 'author = "Max Birkendale"' +
+ ' | (pubDate since 2014-05-12 & ' +
+ '(title = "Der Birnbaum" | pubDate in 2014-12-05))'
);
@@ -990,7 +992,7 @@
// Structur is now:
// or(doc, doc, doc)
- expect(vc.toString()).toEqual(
+ expect(vc.toQuery()).toEqual(
'author = "Max Birkendale" | title = "Der Birnbaum" | pubDate in 2014-12-05'
);
});
@@ -998,25 +1000,26 @@
it('should be reducible to unspecification', function () {
var vc = demoFactory.create();
- expect(vc.toString()).toEqual(vc.root().toString());
+ expect(vc.toQuery()).toEqual(vc.root().toQuery());
- expect(vc.toString()).toEqual('(Titel = "Baum" & Veröffentlichungsort = "hihi" & (Titel = "Baum" | Veröffentlichungsort = "hihi")) | Untertitel = "huhu"');
+ expect(vc.toQuery()).toEqual(
+ '(Titel = "Baum" & Veröffentlichungsort = "hihi" & ' +
+ '(Titel = "Baum" | Veröffentlichungsort = "hihi")) ' +
+ '| Untertitel = "huhu"');
expect(vc.root().element().lastChild.children[0].firstChild.nodeValue).toEqual('and');
expect(vc.root().element().lastChild.children[1].firstChild.nodeValue).toEqual('×');
-
expect(vc.root().delOperand(vc.root().getOperand(0)).update()).not.toBeUndefined();
+ expect(vc.toQuery()).toEqual('Untertitel = "huhu"');
- expect(vc.toString()).toEqual('Untertitel = "huhu"');
-
- expect(vc.root().element().lastChild.children[0].firstChild.nodeValue).toEqual('and');
- expect(vc.root().element().lastChild.children[1].firstChild.nodeValue).toEqual('or');
- expect(vc.root().element().lastChild.children[2].firstChild.nodeValue).toEqual('×');
+ var lc = vc.root().element().lastChild;
+ expect(lc.children[0].firstChild.nodeValue).toEqual('and');
+ expect(lc.children[1].firstChild.nodeValue).toEqual('or');
+ expect(lc.children[2].firstChild.nodeValue).toEqual('×');
// Clean everything
vc.clean();
- expect(vc.toString()).toEqual('⋯');
-
+ expect(vc.toQuery()).toEqual('⋯');
});
});
diff --git a/public/js/src/vc.js b/public/js/src/vc.js
index 290531a..52bf129 100644
--- a/public/js/src/vc.js
+++ b/public/js/src/vc.js
@@ -5,7 +5,7 @@
// TODO: Implement "toQuery"
/*
- * Error codes:
+ Error codes:
701: "JSON-LD group has no @type attribute"
704: "Operation needs operand list"
802: "Match type is not supported by value type"
@@ -40,10 +40,12 @@
loc.DEL = loc.DEL || '×';
loc.EMPTY = loc.EMPTY || '⋯'
+
function _bool (bool) {
return (bool === undefined || bool === false) ? false : true;
};
+
function _removeChildren (node) {
// Remove everything underneath
while (node.firstChild)
@@ -60,8 +62,15 @@
// Add 'and'-criterion
KorAP._and = function (e) {
var obj = this.parentNode.refTo;
+ if (obj.ldType() === 'docGroup') {
+ console.log('~~~~~~~~~');
+ }
+ else if (obj.ldType() === 'doc') {
+ obj.parent().newAfter(obj);
+ };
};
+
// Remove doc or docGroup
KorAP._delete = function (e) {
var obj = this.parentNode.refTo;
@@ -71,13 +80,19 @@
obj.parent().clean();
};
+
+ /**
+ * Virtual Collection
+ */
KorAP.VirtualCollection = {
ldType : function () {
return null;
},
+
create : function () {
return Object.create(KorAP.VirtualCollection);
},
+
clean : function () {
if (this._root.ldType() !== "non") {
this._root.destroy();
@@ -85,6 +100,7 @@
};
return this;
},
+
render : function (json) {
var obj = Object.create(KorAP.VirtualCollection);
@@ -112,6 +128,7 @@
return obj;
},
+
root : function (obj) {
if (arguments.length === 1) {
var e = this.element();
@@ -146,17 +163,22 @@
return this._element;
},
+
+ update : function () {
+ this._root.update();
+ return this;
+ },
+
toJson : function () {
return this._root.toJson();
},
- toString : function () {
- return this._root.toString();
- },
- update : function () {
- this._root.update();
+
+ toQuery : function () {
+ return this._root.toQuery();
}
};
+
/**
* Operators for criteria
*/
@@ -168,6 +190,7 @@
op.del(del);
return op;
},
+
update : function () {
// Init the element
if (this._element === undefined)
@@ -232,16 +255,19 @@
this.update();
return this._element;
},
+
and : function (bool) {
if (arguments.length === 1)
this._and = _bool(bool);
return this._and;
},
+
or : function (bool) {
if (arguments.length === 1)
this._or = _bool(bool);
return this._or;
},
+
del : function (bool) {
if (arguments.length === 1)
this._del = _bool(bool);
@@ -264,6 +290,7 @@
return obj;
},
+
update : function () {
if (this._element === undefined)
@@ -291,13 +318,12 @@
return this.element();
},
+
element : function () {
if (this._element !== undefined)
return this._element;
-
this._element = document.createElement('div');
- this._element.setAttribute('class', 'unspecified');
-
+ this._element.setAttribute('class', 'doc unspecified');
this.update();
return this._element;
}
@@ -305,7 +331,7 @@
/**
- * Virtual collection doc criterion.
+ * Document criterion
*/
KorAP.Doc = {
_ldType : "doc",
@@ -410,6 +436,7 @@
return div;
*/
},
+
// Deserialize from json
fromJson : function (json) {
if (json === undefined)
@@ -512,6 +539,7 @@
return this;
},
+
key : function (value) {
if (arguments.length === 1) {
this._key = value;
@@ -519,6 +547,7 @@
};
return this._key;
},
+
matchop : function (match) {
if (arguments.length === 1) {
this._matchop = match.replace(/^match:/, '');
@@ -526,6 +555,7 @@
};
return this._matchop || "eq";
},
+
type : function (type) {
if (arguments.length === 1) {
this._type = type;
@@ -533,6 +563,7 @@
};
return this._type || "string";
},
+
value : function (value) {
if (arguments.length === 1) {
this._value = value;
@@ -540,6 +571,7 @@
};
return this._value;
},
+
toJson : function () {
if (!this.matchop() || !this.key())
return {};
@@ -552,7 +584,8 @@
"type" : "type:" + this.type()
};
},
- toString : function () {
+
+ toQuery : function () {
if (!this.matchop() || !this.key())
return "";
@@ -593,12 +626,13 @@
break;
};
- return "...";
+ return "";
}
};
+
/**
- * Virtual collection group criterion.
+ * Document group criterion
*/
KorAP.DocGroup = {
_ldType : "docGroup",
@@ -611,6 +645,17 @@
obj._parent = parent;
return obj;
},
+
+ newAfter : function (obj) {
+ for (var i in this._operands) {
+ if (this._operands[i] === obj) {
+ var operand = KorAP.UnspecifiedDoc.create(this);
+ this._operands.splice(i + 1, 0, operand);
+ return this.update();
+ };
+ };
+ },
+
append : function (operand) {
// Append unspecified object
@@ -660,6 +705,7 @@
return;
};
},
+
update : function () {
// There is only one operand in group
if (this._operands.length === 1) {
@@ -713,6 +759,7 @@
return this;
},
+
element : function () {
if (this._element !== undefined)
return this._element;
@@ -725,6 +772,7 @@
return this._element;
},
+
operation : function (op) {
if (arguments.length === 1) {
if (KorAP._validGroupOpRE.test(op)) {
@@ -737,9 +785,11 @@
};
return this._op || 'and';
},
+
operands : function () {
return this._operands;
},
+
getOperand : function (index) {
return this._operands[index];
},
@@ -836,6 +886,7 @@
return this;
},
+
toJson : function () {
var opArray = new Array();
for (var i in this._operands) {
@@ -847,17 +898,21 @@
"operands" : opArray
};
},
- toString : function () {
+
+ toQuery : function () {
return this._operands.
map(function (op) {
- return op.ldType() === 'docGroup' ? '(' + op.toString() + ')' : op.toString()
- }).
- join(this.operation() === 'or' ? ' | ' : ' & ')
+ return (op.ldType() === 'docGroup') ?
+ '(' + op.toQuery() + ')' :
+ op.toQuery();
+ }).join(this.operation() === 'or' ? ' | ' : ' & ')
}
};
- // Abstract JsonLD object
+ /**
+ * Abstract JsonLD criterion object
+ */
KorAP.JsonLD = {
_changed : false,
@@ -928,7 +983,8 @@
"@type" : "korap:" + this.ldType()
};
},
- toString : function () {
+
+ toQuery : function () {
return loc.EMPTY;
}
};