Modernize match object and improve test coverage

Change-Id: Ifde30d7eb9f985c14bb3c885a93c991790ff204e
diff --git a/dev/js/spec/matchSpec.js b/dev/js/spec/matchSpec.js
index ddd482d..6166138 100644
--- a/dev/js/spec/matchSpec.js
+++ b/dev/js/spec/matchSpec.js
@@ -451,7 +451,35 @@
       expect(e.classList.contains('active')).toBe(false);
       expect(e["_match"]).not.toBe(undefined);
     });
-   
+
+    it('should toggle', function () {
+      var e = matchElementFactory();
+
+      expect(e.classList.contains('active')).toBe(false);
+      expect(e["_match"]).toBe(undefined);
+
+      var m = matchClass.create(e);
+
+      expect(e.classList.contains('active')).toBe(false);
+      expect(e["_match"]).not.toBe(undefined);
+      
+      // Open the match
+      m.open();
+      
+      expect(e.classList.contains('active')).toBe(true);
+      expect(e["_match"]).not.toBe(undefined);
+
+      m.toggle();
+
+      expect(e.classList.contains('active')).toBe(false);
+      expect(e["_match"]).not.toBe(undefined);
+
+      m.toggle();
+
+      expect(e.classList.contains('active')).toBe(true);
+      expect(e["_match"]).not.toBe(undefined);
+    });
+    
     it('should open tree menu', function () {      
       var e = matchElementFactory();
       var m = matchClass.create(e);
diff --git a/dev/js/src/match.js b/dev/js/src/match.js
index 4f5aedf..761b916 100644
--- a/dev/js/src/match.js
+++ b/dev/js/src/match.js
@@ -13,7 +13,7 @@
   'buttongroup',
   'panel/match',
 	'util'
-], function (buttonGroupClass,matchPanelClass) { //, refClass) {
+], function (buttonGroupClass,matchPanelClass) {
 
   // Localization values
   const loc   = KorAP.Locale;
@@ -42,8 +42,9 @@
      * Initialize match.
      */
     _init : function (match) {
-      this._element = null;
-      this._initialized = false;
+      const t= this;
+      t._element = null;
+      t._initialized = false;
 
       // No match defined
       if (arguments.length < 1 ||
@@ -54,31 +55,26 @@
 
       // Match defined as a node
       else if (match instanceof Node) {
-        this._element  = match;
+        t._element  = match;
 
         // Circular reference !!
-        match["_match"] = this;
+        match["_match"] = t;
 
-        /*
-          this.corpusID  = match.getAttribute('data-corpus-id'),
-          this.docID     = match.getAttribute('data-doc-id'),
-          this.textID    = match.getAttribute('data-text-id'),
-        */
         if (match.hasAttribute('data-text-sigle')) {
-          this.textSigle = match.getAttribute('data-text-sigle')
+          t.textSigle = match.getAttribute('data-text-sigle')
         }
         else {
-          this.textSigle = match.getAttribute('data-corpus-id') +
+          t.textSigle = match.getAttribute('data-corpus-id') +
             '/' +
             match.getAttribute('data-doc-id') +
             '/' +
             match.getAttribute('data-text-id');
         };
 
-        this.matchID   = match.getAttribute('data-match-id');
-
+        t.matchID   = match.getAttribute('data-match-id');
+        
         // List of available annotations
-        this.available = match.getAttribute('data-available-info').split(' ');
+        t.available = match.getAttribute('data-available-info').split(' ');
       }
 
       // Match as an object
@@ -87,10 +83,10 @@
         // Iterate over allowed match terms
         _matchTerms.forEach(function(term) {
           this[term] = match[term] !== undefined ? match[term] : undefined;
-        }, this);
+        }, t);
       };
       
-      this._avail = {
+      t._avail = {
         tokens : [],
         spans  : [],
         rels   : []
@@ -98,7 +94,7 @@
 
       // Iterate over info layers
       let layer;
-      this.available.forEach(function(term){
+      t.available.forEach(function(term){
 
         // Create info layer objects
         try {
@@ -108,9 +104,9 @@
         catch (e) {
           return;
         };
-      }, this);
+      }, t);
       
-      return this;
+      return t;
     },
 
     /**
@@ -140,39 +136,40 @@
      * Initialize match
      */
     init : function () {
-      if (this._initialized)
-        return this;
+      const t = this;
+      if (t._initialized)
+        return t;
 
       // Add actions unless it's already activated
-      var element = this._element;
+      const element = t._element;
 
       // There is an element to open
       if (element === undefined || element === null)
         return undefined;
       
       // Add meta button
-      var refLine = element.querySelector("p.ref");
+      const refLine = element.querySelector("p.ref");
 
       // No reference found
       if (!refLine)
         return undefined;
 
       // Create panel
-      this.panel = matchPanelClass.create(this);
+      t.panel = matchPanelClass.create(t);
 
-      this._element.insertBefore(
-        this.panel.element(),
-        this._element.querySelector("p.ref")
+      t._element.insertBefore(
+        t.panel.element(),
+        t._element.querySelector("p.ref")
       );
 
       // Insert before reference line
       refLine.insertBefore(
-        this.panel.actions.element(),
+        t.panel.actions.element(),
         refLine.firstChild
       );
 
-      this._initialized = true;
-      return this;
+      t._initialized = true;
+      return t;
     },
 
     /**
@@ -181,7 +178,7 @@
     open : function () {
       
       // Add actions unless it's already activated
-      var element = this._element;
+      const element = this._element;
 
       // There is an element to open
       if (element === undefined || element === null)
@@ -194,11 +191,11 @@
       // Add active class to element
       element.classList.add('active');
 
-      var btn = buttonGroupClass.create(
+      const btn = buttonGroupClass.create(
         ['action','button-view']
       );
 
-      var that = this;
+      const that = this;
       btn.add(loc.MINIMIZE, {'cls':['button-icon','minimize']}, function () {
         that.minimize();
       });
@@ -212,7 +209,9 @@
     },
 
     
-    // Todo: Test toggle
+    /**
+     * Toggle match view
+     */
     toggle : function () {
       if (this._element.classList.contains('active'))
         this.minimize();