Integration of plugin buttons in result panel

Change-Id: I74c8264f02a64c6f339dbb46295d6be92807d833
diff --git a/dev/js/spec/pluginSpec.js b/dev/js/spec/pluginSpec.js
index b84fc65..31ee790 100644
--- a/dev/js/spec/pluginSpec.js
+++ b/dev/js/spec/pluginSpec.js
@@ -1,4 +1,4 @@
-define(['plugin/server','plugin/widget','panel', 'panel/query'], function (pluginServerClass, widgetClass, panelClass, queryPanelClass) {
+define(['plugin/server','plugin/widget','panel', 'panel/query', 'panel/result'], function (pluginServerClass, widgetClass, panelClass, queryPanelClass, resultPanelClass) {
 
   describe('KorAP.Plugin.Server', function () {
 
@@ -107,6 +107,26 @@
       expect(manager.buttonGroup('query').length).toEqual(1);
       manager.destroy();
     });
+    
+
+    it('should accept valid registrations for result', function () {
+      var manager = pluginServerClass.create();
+
+      manager.register({
+        name : 'Check',
+        embed : [{
+          panel : 'result',
+          title : 'Translate',
+          onClick : {
+            template : 'test'
+          }
+        }]
+      });
+
+      expect(manager.buttonGroup('result').length).toEqual(1);
+      manager.destroy();
+    });
+    
   });
   
   describe('KorAP.Plugin.Widget', function () {
@@ -221,7 +241,73 @@
       KorAP.Plugin.destroy();
       KorAP.Plugin = undefined;
     });  
-      
-  
   });
+  
+  describe('KorAP.Plugin.ResultPanel', function () {
+    
+    it('Plugin is registered second: buttons should be added to panel', function () {
+      
+      var resultPanel = resultPanelClass.create();
+      resultPanel.addAlignAction(); 
+      var div = document.createElement('div');
+
+      div.appendChild(resultPanel.element());
+      KorAP.Panel = KorAP.Panel || {};
+      KorAP.Panel['result'] = resultPanel;
+
+      // Register plugin afterwards
+      var manager = pluginServerClass.create();
+
+      manager.register({
+        name : 'ResultPlugin',
+        embed : [{
+          panel : 'result',
+          title : 'Dosomething',
+          onClick : {
+            template : 'test'
+          }
+        }]
+      });
+
+      expect(manager.buttonGroup('result').length).toEqual(0);
+      expect(KorAP.Panel['result'].actions.element().innerHTML).toContain('Dosomething');
+
+      // Clean up
+      KorAP.Panel['result'] = undefined;
+      manager.destroy();
+  });
+    
+    it('Plugin is registered first: Buttons should be added to panel and cleared', function () {
+      
+      // Register plugin first
+      KorAP.Plugin = pluginServerClass.create();
+
+      KorAP.Plugin.register({
+        name : 'ResultPlugin',
+        embed : [{
+          panel : 'result',
+          title : 'Dosomething',
+          onClick : {
+            template : 'test'
+          }
+        }]
+      });
+
+      expect(KorAP.Plugin.buttonGroup('result').length).toEqual(1);
+      
+      var resultPanel = resultPanelClass.create();
+      var div = document.createElement('div');
+      div.appendChild(resultPanel.element());
+      KorAP.Panel = KorAP.Panel || {};
+      KorAP.Panel['result'] = resultPanel;
+      expect(KorAP.Plugin.buttonGroup('result').length).toEqual(0);
+      expect(KorAP.Panel['result'].actions.element().innerHTML).toContain('Dosomething');
+     
+      // Clean up
+      KorAP.Panel['result'] = undefined;
+      KorAP.Plugin.destroy();
+      KorAP.Plugin = undefined;
+    });  
+  });
+  
 });
diff --git a/dev/js/src/init.js b/dev/js/src/init.js
index 82d9de9..0b31e23 100644
--- a/dev/js/src/init.js
+++ b/dev/js/src/init.js
@@ -84,6 +84,8 @@
 
     // What should be visible in the beginning?
     var show = KorAP.session.get('show') || {};
+    
+    KorAP.Panel = KorAP.Panel || {}
 
     /**
      * Release notifications
@@ -268,7 +270,7 @@
     if (matchCount > 0)
       resultPanel.addAlignAction();
 
-
+    KorAP.Panel['result'] = resultPanel;
     /*
      * Toggle the Virtual Corpus builder
      */
@@ -390,7 +392,6 @@
     if (sform && vcView) {
       // The views are below the query bar
       sform.insertBefore(queryPanel.element(), vcView);
-      KorAP.Panel = KorAP.Panel || {};
       KorAP.Panel['query'] = queryPanel;
     }
     
diff --git a/dev/js/src/panel/result.js b/dev/js/src/panel/result.js
index 978a1b6..02c6e53 100644
--- a/dev/js/src/panel/result.js
+++ b/dev/js/src/panel/result.js
@@ -23,6 +23,21 @@
     // Initialize panel
     _init : function (opened) {
       this._opened = opened;
+
+      
+      // If plugins are enabled, add all buttons for the result panel
+     if (KorAP.Plugin) {
+        var resultButtons = KorAP.Plugin.buttonGroup("result");
+
+        // Add all result buttons in order
+        for (i in resultButtons) {
+          this.actions.add.apply(this.actions, resultButtons[i]);
+        };
+
+        KorAP.Plugin.clearButtonGroup("result");
+      };
+      
+      
       return this;
     },
 
@@ -82,6 +97,8 @@
           this.button.toggleClass("left", "right");
         };        
       });
+
+      
     }
   }
 });
diff --git a/dev/js/src/plugin/server.js b/dev/js/src/plugin/server.js
index 69318b0..163a315 100644
--- a/dev/js/src/plugin/server.js
+++ b/dev/js/src/plugin/server.js
@@ -31,7 +31,8 @@
   // List of panels with static buttons, i.e.
   // panels that occur only once.
   var buttonsSingle = {
-    query : []
+    query : [],
+    result : []
   }
   
   // This is a counter to limit acceptable incoming messages
@@ -117,10 +118,9 @@
           throw new Error("Embedding of plugin is no object");
 
         var panel = embed["panel"];
-
+        
         if (!panel || !(buttons[panel] || buttonsSingle[panel]))
           throw new Error("Panel for plugin is invalid");
-
         var onClick = embed["onClick"];
 
         // Needs to be localized as well