Improve status codes and support HTML match responses

Change-Id: Ie11b68eb0836bb537a2869b87e78f3a695203e11
diff --git a/dev/js/spec/matchSpec.js b/dev/js/spec/matchSpec.js
index 08c0ced..79133ee 100644
--- a/dev/js/spec/matchSpec.js
+++ b/dev/js/spec/matchSpec.js
@@ -395,6 +395,31 @@
       expect(m.matchID).toEqual("p85183-85184");
     });
 
+    it('should be initializable when active', function () {
+      var e = matchElementFactory();
+      e.setAttribute('class', 'active');
+
+      expect(e.classList.contains('active')).toBe(true);
+      expect(e["_match"]).toBe(undefined);
+
+      var m = matchClass.create(e);
+
+      expect(e["_match"]).not.toBe(undefined);
+      
+      // Open the match
+      m.init();
+      
+      expect(e["_match"]).not.toBe(undefined);
+
+      actions = e.querySelector("p.ref > div.action.button-group").children;
+      
+      expect(actions[0].getAttribute("class")).toEqual("metatable");
+      expect(actions[1].getAttribute("class")).toEqual("info");
+      expect(actions[2].getAttribute("class")).toEqual("tree");
+      
+      // Close the match
+      expect(e.querySelector("div.action.button-group > span.minimize")).toBe(null);
+    });
     
     it('should react to gui actions', function () {
       var e = matchElementFactory();
@@ -418,13 +443,15 @@
       expect(actions[0].getAttribute("class")).toEqual("metatable");
       expect(actions[1].getAttribute("class")).toEqual("info");
       expect(actions[2].getAttribute("class")).toEqual("tree");
+
+      expect(e.querySelector("div.action.button-group > span.minimize")).not.toBe(null);
       
       // Close the match
       m.minimize();
       expect(e.classList.contains('active')).toBe(false);
       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/init.js b/dev/js/src/init.js
index 478f87e..62d0800 100644
--- a/dev/js/src/init.js
+++ b/dev/js/src/init.js
@@ -141,39 +141,53 @@
     /**
      * Add actions to match entries
      */
-    var inactiveLi = d.querySelectorAll(
-      '#search > ol > li:not(.active)'
+    var li = d.querySelectorAll(
+      '#search > ol > li'
     );
     var matchCount = 0;
 
-    for (matchCount = 0; matchCount < inactiveLi.length; matchCount++) {
-      inactiveLi[matchCount].addEventListener('click', function (e) {
-        if (this._match !== undefined)
-          this._match.open();
-        else {
+    for (matchCount = 0; matchCount < li.length; matchCount++) {
+
+      let e = li[matchCount];
+
+      // Define class for active elements
+      if (e.classList.contains('active')) {
+        if (this._match === undefined) {
           // lazyLoad
-          matchClass.create(this).open();
+          matchClass.create(e).init();
         };
-        // This would prevent the sidebar to go back
-        // e.halt();
-      });
-      inactiveLi[matchCount].addEventListener('keydown', function (e) {
-        var code = _codeFromEvent(e);
-        
-        switch (code) {
-        case 32:
+      }
+
+      // Define class for inactive elements
+      else {
+        e.addEventListener('click', function (e) {
           if (this._match !== undefined)
-            this._match.toggle();
+            this._match.open();
           else {
             // lazyLoad
             matchClass.create(this).open();
           };
-          e.halt();
-          break;
-        };
-      });
+          // This would prevent the sidebar to go back
+          // e.halt();
+        });
+        e.addEventListener('keydown', function (e) {
+          var code = _codeFromEvent(e);
+          
+          switch (code) {
+          case 32:
+            if (this._match !== undefined)
+              this._match.toggle();
+            else {
+              // lazyLoad
+              matchClass.create(this).open();
+            };
+            e.halt();
+            break;
+          };
+        });
+      };
     };
-
+    
     // Add focus listener to aside
     var aside = d.getElementsByTagName('aside')[0];
 
diff --git a/dev/js/src/match.js b/dev/js/src/match.js
index 3c44656..f38a68a 100644
--- a/dev/js/src/match.js
+++ b/dev/js/src/match.js
@@ -137,6 +137,44 @@
       return this._avail.rels;
     },
 
+    /**
+     * Initialize match
+     */
+    init : function () {
+      if (this._initialized)
+        return this;
+
+      // Add actions unless it's already activated
+      var element = this._element;
+
+      // There is an element to open
+      if (element === undefined || element === null)
+        return undefined;
+      
+      // Add meta button
+      var refLine = element.querySelector("p.ref");
+
+      // No reference found
+      if (!refLine)
+        return undefined;
+
+      // Create panel
+      this.panel = matchPanelClass.create(this);
+
+      this._element.insertBefore(
+        this.panel.element(),
+        this._element.querySelector("p.ref")
+      );
+
+      // Insert before reference line
+      refLine.insertBefore(
+        this.panel.actions.element(),
+        refLine.firstChild
+      );
+
+      this._initialized = true;
+      return this;
+    },
 
     /**
      * Open match
@@ -157,14 +195,6 @@
       // Add active class to element
       element.classList.add('active');
 
-      // Already there
-      /*
-        if (element.classList.contains('action'))
-        return true;
-      */
-      if (this._initialized)
-        return true;
-      
       var btn = buttonGroupClass.create(
         ['action','button-view']
       );
@@ -174,29 +204,10 @@
         that.minimize();
       });
       element.appendChild(btn.element());
-
-      // Add meta button
-      var refLine = element.querySelector("p.ref");
-
-      // No reference found
-      if (!refLine)
-        return;
-
-      // Create panel
-      this.panel = matchPanelClass.create(this);
-
-      this._element.insertBefore(
-        this.panel.element(),
-        this._element.querySelector("p.ref")
-      );
-
-      // Insert before reference line
-      refLine.insertBefore(
-        this.panel.actions.element(),
-        refLine.firstChild
-      );
-
-      this._initialized = true;
+      
+      if (this.init() == undefined) {
+        return false;
+      };
       
       return true;
     },