Simplified plugin registration and improved registration checks

Change-Id: I60486be70091a5bdc6c2703c73fe6adb52514f32
diff --git a/dev/demo/plugin-server.html b/dev/demo/plugin-server.html
index d91b9e2..15a69d2 100644
--- a/dev/demo/plugin-server.html
+++ b/dev/demo/plugin-server.html
@@ -15,7 +15,7 @@
       <div id="search">
         <ol>
           <li class="active">
-            <div id="container"></div>
+            <div id="match"></div>
             <div id="buttons"></div>
           </li>
         </ol>
diff --git a/dev/demo/plugin-serverdemo.js b/dev/demo/plugin-serverdemo.js
index a6ba4a2..92c4611 100644
--- a/dev/demo/plugin-serverdemo.js
+++ b/dev/demo/plugin-serverdemo.js
@@ -15,12 +15,11 @@
       'desc' : 'Some content about cats',
       // 'about' : 'https://localhost:5678/',
       'embed' : [{
-        'buttonGroup' : 'match',
+        'panel' : 'match',
         'title' : 'Translate',
         'classes' : ['translate'],
         'onClick' : {
           'action' : 'addWidget',
-          'panel' : 'container',
           'template' : 'http://localhost:3003/demo/plugin-client.html',
         }
       }]
diff --git a/dev/js/spec/pluginSpec.js b/dev/js/spec/pluginSpec.js
index 62aa3c7..50c4982 100644
--- a/dev/js/spec/pluginSpec.js
+++ b/dev/js/spec/pluginSpec.js
@@ -17,6 +17,31 @@
       expect(div.firstChild.firstChild.tagName).toEqual("IFRAME");
       manager.destroy();
     });
+
+    it('should fail on invalid registries', function () {
+      var manager = pluginServerClass.create();
+
+      expect(
+	      function() { manager.register({}) }
+      ).toThrow(new Error("Missing name of plugin"));
+
+      expect(
+	      function() { manager.register({
+          name : 'Example',
+          embed : ''
+        })}
+      ).toThrow(new Error("Embedding of plugin is no list"));
+
+      expect(
+	      function() { manager.register({
+          name : 'Example',
+          embed : [{
+            panel : ''
+          }]
+        })}
+      ).toThrow(new Error("Panel for plugin is invalid"));
+
+    });
   });
 
   describe('KorAP.Plugin.Widget', function () {
diff --git a/dev/js/src/plugin/server.js b/dev/js/src/plugin/server.js
index 6689529..c8df357 100644
--- a/dev/js/src/plugin/server.js
+++ b/dev/js/src/plugin/server.js
@@ -15,10 +15,17 @@
   // messages to them
   var widgets = {};
   var plugins = {};
+
+  // TODO:
+  //   These should better be panels and every panel
+  //   has a buttonGroup
   var buttons = {
     match : []
   };
-
+  var panels = {
+    match : 1
+  };
+  
   // This is a counter to limit acceptable incoming messages
   // to a certain amount. For every message, this counter will
   // be decreased (down to 0), for every second this will be
@@ -52,39 +59,34 @@
      * Register a plugin described as a JSON object.
      *
      * This is work in progress.
+     *
+     * Example:
+     *
+     *   KorAP.Plugin.register({
+     *     'name' : 'CatContent',
+     *     'desc' : 'Some content about cats',
+     *     'about' : 'https://localhost:5678/',
+     *     'embed' : [{
+     *       'panel' : 'match',
+     *       'title' : loc.TRANSLATE,
+     *       'classes' : ['translate']
+     *       'onClick' : {
+     *         'action' : 'addWidget',
+     *         'template' : 'https://localhost:5678/?match={matchid}',
+     *       }
+     *     }]
+     *   });
+     *
      */
     register : function (obj) {
-
-      /* Example:
-
-         KorAP.Plugin.register({
-           'name' : 'CatContent',
-           'desc' : 'Some content about cats',
-           'about' : 'https://localhost:5678/',
-           'embed' : [{
-             'buttonGroup' : 'match',
-             'title' : loc.TRANSLATE,
-             'classes' : ['translate']
-             'onClick' : {
-               'action' : 'addWidget',
-               'panel' : 'match',
-               'template' : 'https://localhost:5678/?match={matchid}',
-             }
-           }]
-         });
-      */
-
       // TODO:
-      //   These fields need to be localized by a structure like
-      //   {
-      //     de : {
-      //       name : '..'
-      //     }
-      //     en : ...
-      //   }
-      // for display
+      //   These fields need to be localized for display by a structure like
+      //   { de : { name : '..' }, en : { .. } }
       var name = obj["name"];
 
+      if (!name)
+        throw new Error("Missing name of plugin");
+
       // Register plugin by name
       var plugin = plugins[name] = {
         name : name,
@@ -92,20 +94,31 @@
         about : obj["about"],
         widgets : []
       };
+
+      if (typeof obj["embed"] !== 'object')
+        throw new Error("Embedding of plugin is no list");
  
       // Embed all embeddings of the plugin
       for (var i in obj["embed"]) {
         var embed = obj["embed"][i];
-        var addTo = embed["buttonGroup"];
+
+        if (typeof embed !== 'object')
+          throw new Error("Embedding of plugin is no object");
+
+        var panel = embed["panel"];
+
+        if (!panel || !buttons[panel])
+          throw new Error("Panel for plugin is invalid");
+
         var onClick = embed["onClick"];
 
         // Needs to be localized as well
         var title = embed["title"];
 
         // The embedding will open a widget
-        if (onClick["action"] == "addWidget") {
+        if (!onClick["action"] || onClick["action"] == "addWidget") {
 
-          var panel = onClick["panel"];
+          var panel = document.getElementById(panel);
           var that = this;
           var cb = function (e) {
 
@@ -113,11 +126,11 @@
             var url = onClick["template"]; // that._interpolateURI(onClick["template"], this.match);
 
             // Add the widget to the panel
-            var id = that.addWidget(document.getElementById(panel), name, url);
+            var id = that.addWidget(panel, name, url);
             plugin["widgets"].push(id);
           };
 
-          buttons["match"].push([title, embed["classes"], cb]);
+          buttons[pannel].push([title, embed["classes"], cb]);
         };
       };
     },