Fix constraint removal in VC fragment
Change-Id: I979513cb6bd43b8fca1e96585977d0b014692d50
diff --git a/dev/js/spec/vcSpec.js b/dev/js/spec/vcSpec.js
index bab1747..61c3099 100644
--- a/dev/js/spec/vcSpec.js
+++ b/dev/js/spec/vcSpec.js
@@ -2872,6 +2872,7 @@
});
});
+ // Check fragment handling for corpusByMatch helper
describe('KorAP.VC.Fragment', function () {
it('should be initializable', function () {
var f = fragmentClass.create();
@@ -2923,10 +2924,15 @@
it('should be reducible', function () {
var f = fragmentClass.create();
+ expect(f.element().children.length).toEqual(0);
+
f.add("author", "Peter");
f.add("title", "Example");
+ expect(f.element().children.length).toEqual(1);
+
var root = f.element().firstChild;
+
expect(root.classList.contains("docGroup")).toBeTruthy();
expect(root.children.length).toEqual(2);
@@ -2942,6 +2948,10 @@
expect(root.children[1].textContent).toEqual("eq");
expect(root.children[2].tagName).toEqual("SPAN");
expect(root.children[2].textContent).toEqual("Example");
+
+ f.remove("title","Example");
+
+ expect(f.element().children.length).toEqual(0);
});
});
});
diff --git a/dev/js/src/vc/fragment.js b/dev/js/src/vc/fragment.js
index a1892c8..9b87ee2 100644
--- a/dev/js/src/vc/fragment.js
+++ b/dev/js/src/vc/fragment.js
@@ -1,6 +1,6 @@
/**
* Create a virtual corpus fragment,
- * that can be shown and merge with the
+ * that can be shown and merged with the
* main VC.
*
* @author Nils Diewald
@@ -29,11 +29,11 @@
return doc;
};
-
+ // Return object
return {
create : function () {
- var obj = Object.create(this);
+ const obj = Object.create(this);
obj._operands = [];
return obj;
},
@@ -43,8 +43,8 @@
* Add document constraint to fragment
*/
add : function (key, value, type) {
- for (var i in this._operands) {
- var op = this._operands[i];
+ for (let i in this._operands) {
+ let op = this._operands[i];
if (op[0] === key && op[1] === value) {
array.splice(index, 1);
};
@@ -58,8 +58,8 @@
* Remove document constraint from fragment
*/
remove : function (key, value) {
- for (var i in this._operands) {
- var op = this._operands[i];
+ for (let i in this._operands) {
+ let op = this._operands[i];
if (op[0] === key && op[1] === value) {
this._operands.splice(i, 1);
this.update();
@@ -69,6 +69,12 @@
return;
},
+ /**
+ * Check, if the fragment contains any constraints
+ */
+ isEmpty : function () {
+ return this._operands.length > 0 ? false : true;
+ },
/**
* Add fragment constraints to VC.
@@ -103,15 +109,15 @@
// <span class="value">Baum</span>
// </div>
// </div>
- var root;
- var l = this._operands.length;
+ let root;
+ let l = this._operands.length;
if (l > 1) {
root = document.createElement('div');
root.setAttribute('class','docGroup');
root.setAttribute('data-operation', 'and');
- for (var i in this._operands) {
+ for (let i in this._operands) {
root.appendChild(_doc(this._operands[i]));
};
}
@@ -119,8 +125,11 @@
root = _doc(this._operands[0]);
};
- var e = this.element();
- if (e.firstChild)
+ const e = this.element();
+ if (l === 0) {
+ _removeChildren(e);
+ }
+ else if (e.firstChild)
e.replaceChild(root, e.firstChild);
else
e.appendChild(root);