Fix replacement of docs with docgrouprefs in VC builder

Change-Id: Ib6c594dc244b1b672a4ce5c1e3afab233f47fd8c
diff --git a/Changes b/Changes
index 5d1d42d..e76a4e8 100755
--- a/Changes
+++ b/Changes
@@ -1,6 +1,8 @@
-0.29 2018-08-30
+0.29 2018-08-31
         - Deprecated Kalamar.api configuration key
           in favor of Kalamar.api_path.
+        - Fix replacement of docs with docGroupRefs in
+          VC builder.
 
 0.28 2018-08-29
         - Introduced Widget based plugin system.
diff --git a/dev/js/spec/vcSpec.js b/dev/js/spec/vcSpec.js
index ef033ab..33953b5 100644
--- a/dev/js/spec/vcSpec.js
+++ b/dev/js/spec/vcSpec.js
@@ -827,7 +827,7 @@
     });
 
     
-    it('should be replaceable by a docGroupRef', function () {
+    it('should be replaceable by docGroupRef with deletion', function () {
       var vc = vcClass.create().fromJson({
         "@type" : "koral:doc",
         "key":"Titel",
@@ -854,9 +854,82 @@
 
       expect(vcE.firstChild.firstChild.textContent).toEqual("referTo");
       expect(vcE.firstChild.children[1].getAttribute("data-type")).toEqual("string");
+
       expect(vcE.firstChild.children.length).toEqual(3);
       expect(vcE.firstChild.children[1].classList.contains("unspecified")).toBeTruthy();      
     });
+
+
+    it('should be replaceable by docGroupRef on root', function () {
+      var vc = vcClass.create().fromJson({
+        "@type" : "koral:doc",
+        "key":"Titel",
+        "value":"Baum",
+        "match":"match:eq"
+      });
+
+      expect(vc.toQuery()).toEqual("Titel = \"Baum\"");
+
+      var vcE = vc.builder();
+      expect(vcE.firstChild.children.length).toEqual(4);
+
+      // Click on the key
+      vcE.firstChild.firstChild.click();
+
+      expect(vcE.firstChild.getElementsByTagName("LI")[0].textContent).toEqual("referTo");
+
+      // Click on referTo
+      vcE.firstChild.getElementsByTagName("LI")[0].click();
+
+      // Now it's a referTo element
+      expect(vcE.firstChild.firstChild.textContent).toEqual("referTo");
+      expect(vcE.firstChild.children.length).toEqual(3);
+      expect(vcE.firstChild.children[1].classList.contains("unspecified")).toBeTruthy();
+    });
+
+    it('should be replaceable by docGroupRef nested', function () {
+      var vc = vcClass.create().fromJson({
+        "@type" : "koral:docGroup",
+        "operation" : "operation:and",
+        "operands" : [
+          {
+            "@type": 'koral:doc',
+            "key" : 'author',
+            "match": 'match:eq',
+            "value": 'Max Birkendale',
+            "type": 'type:string'
+          },
+          {
+            "@type": 'koral:doc',
+            "key": 'pubDate',
+            "match": 'match:eq',
+            "value": '2014-12-05',
+            "type": 'type:date'
+          }
+        ]
+      });
+
+      expect(vc.toQuery()).toEqual("author = \"Max Birkendale\" & pubDate in 2014-12-05");
+
+      var vcE = vc.builder();
+
+      // First doc
+      var doc = vcE.firstChild.firstChild;
+      expect(doc.children.length).toEqual(4);
+
+      // Click on the key
+      doc.firstChild.click();
+
+      expect(doc.getElementsByTagName("LI")[0].textContent).toEqual("referTo");
+      
+      // Click on referTo
+      vcE.firstChild.getElementsByTagName("LI")[0].click();
+
+      // Now it's a referTo element
+      expect(vcE.firstChild.firstChild.firstChild.textContent).toEqual("referTo");
+      expect(vcE.firstChild.firstChild.children.length).toEqual(3);
+      expect(vcE.firstChild.firstChild.children[1].classList.contains("unspecified")).toBeTruthy();
+    });
   });
 
   describe('KorAP.DocGroup element', function () {
diff --git a/dev/js/src/vc/doc.js b/dev/js/src/vc/doc.js
index 821aa64..996c347 100644
--- a/dev/js/src/vc/doc.js
+++ b/dev/js/src/vc/doc.js
@@ -6,8 +6,9 @@
   'vc/jsonld',
   'vc/rewritelist',
   'vc/stringval',
+  'vc/docgroupref',
   'util'
-], function (jsonldClass, rewriteListClass, stringValClass) {
+], function (jsonldClass, rewriteListClass, stringValClass, docGroupRefClass) {
 
   /*
    * TODO:
@@ -178,6 +179,20 @@
       return parent.replaceOperand(this, group).update();
     },
 
+    replaceWith : function (op) {
+      var p = this.parent();
+
+      if (p.ldType() === 'docGroup') {
+        p.replaceOperand(this,op);
+      }
+      else if (p.ldType() == null) {
+        p.root(op);
+      };
+      p.update();
+
+      this.destroy();
+    },
+    
     /**
      * Deserialize from json
      */
@@ -372,14 +387,22 @@
       // Release event
       var that = this;
       menu.released(function (key, type) {
-        var doc = that.key(key).type(type);
 
-        // This may not be compatible - then switch to default
-        doc.matchop(doc.matchop());
-        doc.value(doc.value());
+        if (type === 'ref') {
+          // KorAP._delete.bind(that);
+          var ref = docGroupRefClass.create(that.parent());
+          that.replaceWith(ref);
+        }
+        else {
+          var doc = that.key(key).type(type);
 
-        // Update the doc
-        doc.update();
+          // This may not be compatible - then switch to default
+          doc.matchop(doc.matchop());
+          doc.value(doc.value());
+
+          // Update the doc
+          doc.update();
+        };
 
         // hide!
         this.hide();