Add 'setWidget' action for plugin registration

Change-Id: I1ecdc14b7f906e2d3e15f3775af1dc98fd5bf2f8
diff --git a/dev/js/src/buttongroup.js b/dev/js/src/buttongroup.js
index e71d082..65ea860 100644
--- a/dev/js/src/buttongroup.js
+++ b/dev/js/src/buttongroup.js
@@ -59,9 +59,13 @@
           b.classList.add.apply(b.classList, data['cls']);
         };
      
-        if (data['icon'] !== undefined){ 
+        if (data['icon'] !== undefined) { 
           b.setAttribute('data-icon', data['icon']);
         };
+
+        if (data['state'] !== undefined) {
+          b['state'] = data['state'];
+        }
       };
      
       b.addE('span').addT(title);
@@ -102,6 +106,12 @@
      * Add button that can toggle a state.
      * The state has to be a state object.
      */
+    /*
+     * TODO:
+     *   Do not add a state object here, but embed the
+     *   state in the data and "roll()" through the different
+     *   state options instead.
+     */
     addToggle : function (title, data, state) {
       let b = this._element.addE('span');
       b.setAttribute('title',title);
diff --git a/dev/js/src/plugin/server.js b/dev/js/src/plugin/server.js
index f862770..ce36354 100644
--- a/dev/js/src/plugin/server.js
+++ b/dev/js/src/plugin/server.js
@@ -136,44 +136,85 @@
         let icon = embed["icon"];
         
         if (!panel || !(buttons[panel] || buttonsSingle[panel]))
-          throw new Error("Panel for plugin is invalid");        
+          throw new Error("Panel for plugin is invalid"); 
 
         // The embedding will open a widget
-        if (!onClick["action"] || onClick["action"] == "addWidget") {
-
+        if (!onClick["action"] ||
+            onClick["action"] == "addWidget" ||
+            onClick["action"] == "setWidget") {
           
           let cb = function (e) {
 
             // "this" is bind to the panel
+            // "this".button is the button
+            // "that" is the server object
 
             // Get the URL of the widget
             let url = onClick["template"];
             // that._interpolateURI(onClick["template"], this.match);
 
+            // The button has a state and the state is associated to the
+            // a intermediate object to toggle the view
+            if ('state' in this.button && this.button.state.associates() > 0) {
+
+              // TODO:
+              //   Use roll() when existing
+              let s = this.button.state;
+              if (s.get()) {
+                s.set(false);
+              } else {
+                s.set(true);
+              };
+              return;
+            };
+
             // Add the widget to the panel
             let id = that.addWidget(this, name, url);
             plugin["widgets"].push(id);
+            
+            // If a state exists, associate with a mediator object
+            if ('state' in this.button) {
+              this.button.state.associate({
+                setState : function (value) {
+                  // Minimize the widget
+                  if (value == false) {
+                    services[id].minimize();
+                  }
+                  else {
+                    services[id].show();                    
+                  };
+                }
+              });
+            }
           };
 
-          // TODO:
-          //   Create button class to be stored and loaded in button groups!
 
+          // Button object
+          let obj = {'cls':embed["classes"], 'icon': icon }
+
+          if (onClick["action"] && onClick["action"] == "setWidget") {
+
+            // Create a boolean state value, that initializes to true == opened
+            obj['state'] = stateClass.create(true);
+          };
+          
           // Add to dynamic button list (e.g. for matches)
           if (buttons[panel]) {
-            buttons[panel].push([title, {'cls':embed["classes"], 'icon': icon }, cb]);
+            buttons[panel].push([title, obj, cb]);
           }
 
           // Add to static button list (e.g. for query) already loaded
           else if (KorAP.Panel[panel]) {
-            KorAP.Panel[panel].actions.add(title, {'cls':embed["classes"], 'icon':icon}, cb);
+            KorAP.Panel[panel].actions.add(title, obj, cb);
           }
 
           // Add to static button list (e.g. for query) not yet loaded
           else {
-            buttonsSingle[panel].push([title, {'cls':embed["classes"], 'icon':icon}, cb]);
+            buttonsSingle[panel].push([title, obj, cb]);
           }
         }
-        //TODO There is no possibility to add icons to an plugin toggle button right now. 
+
+        // TODO There is no possibility to add icons to an plugin toggle button right now. 
         else if (onClick["action"] == "toggle") {
 
           // Todo: Initially false
@@ -288,13 +329,9 @@
       // Create a new service
       let service = serviceClass.create(name, src, id);
       
-      // TODO!
-      // Store the service based on the identifier
       services[id] = service;
       limits[id] = maxMessages;
 
-      // widget._mgr = this;
-
       // Add service to panel
       this.element().appendChild(
         service.load()