Make VC replaceable via KorAP.vc.fromJson(...)
Change-Id: Ic7de60fd4b67036e52467b8c0c99b59d504a240b
diff --git a/dev/js/spec/vcSpec.js b/dev/js/spec/vcSpec.js
index 388a26d..5c1fdfd 100644
--- a/dev/js/spec/vcSpec.js
+++ b/dev/js/spec/vcSpec.js
@@ -1869,6 +1869,42 @@
vc.addRequired(doc5);
expect(vc.toQuery()).toEqual('referTo "@max/myCorpus" & author = "Goethe"');
});
+
+ it('should be replaceble via JSON', function () {
+ let vc = KorAP.vc = vcClass.create().fromJson({
+ "@type" : 'koral:docGroup',
+ 'operation' : 'operation:or',
+ 'operands' : [
+ {
+ '@type' : 'koral:doc',
+ 'key' : 'title',
+ 'value' : 'Hello World!'
+ },
+ {
+ '@type' : 'koral:doc',
+ 'key' : 'foo',
+ 'value' : 'bar'
+ }
+ ]
+ });
+
+ expect(vc.toQuery()).toEqual('title = "Hello World!" | foo = "bar"')
+
+ let e = vc.element();
+ expect(e.firstChild.firstChild.firstChild.children[0].textContent).toEqual("title");
+ expect(e.firstChild.firstChild.firstChild.children[2].textContent).toEqual("Hello World!");
+
+ vc.fromJson({
+ '@type' : 'koral:doc',
+ 'key' : 'foo',
+ 'value' : 'bar'
+ });
+
+ expect(vc.toQuery()).toEqual('foo = "bar"');
+ e = vc.element();
+ expect(e.firstChild.firstChild.children[0].textContent).toEqual("foo");
+ expect(e.firstChild.firstChild.children[2].textContent).toEqual("bar");
+ })
});
diff --git a/dev/js/src/vc.js b/dev/js/src/vc.js
index 5d1ccf0..fd22a97 100644
--- a/dev/js/src/vc.js
+++ b/dev/js/src/vc.js
@@ -178,19 +178,22 @@
* corpus document
*/
fromJson : function(json) {
+
+ let obj;
+
if (json !== undefined) {
// Parse root document
if (json['@type'] == 'koral:doc') {
- this._root = docClass.create(this, json);
+ obj = docClass.create(this, json);
}
// parse root group
else if (json['@type'] == 'koral:docGroup') {
- this._root = docGroupClass.create(this, json);
+ obj = docGroupClass.create(this, json);
}
// parse root reference
else if (json['@type'] == 'koral:docGroupRef') {
- this._root = docGroupRefClass.create(this, json);
+ obj = docGroupRefClass.create(this, json);
}
// Unknown collection type
@@ -202,12 +205,12 @@
else {
// Add unspecified object
- this._root = unspecDocClass.create(this);
+ obj = unspecDocClass.create(this);
};
// Init element and update
- this.update();
-
+ this.root(obj);
+
return this;
},
diff --git a/dev/js/src/vc/docgroup.js b/dev/js/src/vc/docgroup.js
index c73a325..3a22034 100644
--- a/dev/js/src/vc/docgroup.js
+++ b/dev/js/src/vc/docgroup.js
@@ -210,8 +210,10 @@
group.appendChild(op.element());
- var vcchevent = new CustomEvent('vcChange', {'detail':this});
- KorAP.vc.element().dispatchEvent(vcchevent);
+ if (KorAP.vc) {
+ var vcchevent = new CustomEvent('vcChange', {'detail':this});
+ KorAP.vc.element().dispatchEvent(vcchevent);
+ };
return this;
},