Fix 'setWidget' action for the case a widget was closed instead of minimized

Change-Id: I74339e444fcca43aee3c694c2a24fe7e68980d81
diff --git a/Changes b/Changes
index df603c1..46a7bcf 100755
--- a/Changes
+++ b/Changes
@@ -23,6 +23,8 @@
         - Add minimize() method to views.
         - Add "setWidget" action for plugin registration.
         - Add clear() method to state objects.
+        - Fix "setWidget" action when the service was closed
+          instead of minimized.
 
         WARNING: If you relied on the former default API endpoint
           being http://localhost:9999/, this will break your
diff --git a/dev/js/spec/pluginSpec.js b/dev/js/spec/pluginSpec.js
index 36d6f64..5cd7ce5 100644
--- a/dev/js/spec/pluginSpec.js
+++ b/dev/js/spec/pluginSpec.js
@@ -227,6 +227,16 @@
       expect(p.element().querySelectorAll("iframe").length).toEqual(1);
       expect(p.element().querySelectorAll("div.view.widget").length).toEqual(1);
       expect(p.element().querySelectorAll("div.view.show.widget").length).toEqual(0);
+
+      p.element().querySelector("span.close").click();
+
+      expect(p.element().querySelectorAll("iframe").length).toEqual(0);
+
+      b.click();
+
+      expect(p.element().querySelectorAll("iframe").length).toEqual(1);
+      expect(p.element().querySelectorAll("div.view.widget").length).toEqual(1);
+      expect(p.element().querySelectorAll("div.view.show.widget").length).toEqual(1);
       
       manager.destroy();
 
diff --git a/dev/js/src/plugin/server.js b/dev/js/src/plugin/server.js
index ce36354..7f9bc85 100644
--- a/dev/js/src/plugin/server.js
+++ b/dev/js/src/plugin/server.js
@@ -157,15 +157,28 @@
             // 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 {
+
+              // The associated service is existent
+              if (services[this.button['widgetID']]) {
+              
+                // TODO:
+                //   Use roll() when existing
+                if (s.get()) {
+                  s.set(false);
+                } else {
+                  s.set(true);
+                };
+                return;
+              }
+
+              // The service is not existent
+              else {
+
+                // Remove broken state associations
+                s.clear();
                 s.set(true);
-              };
-              return;
+              }
             };
 
             // Add the widget to the panel
@@ -174,6 +187,7 @@
             
             // If a state exists, associate with a mediator object
             if ('state' in this.button) {
+              this.button['widgetID'] = id;
               this.button.state.associate({
                 setState : function (value) {
                   // Minimize the widget
@@ -493,7 +507,7 @@
     // Close the service
     _closeService : function (id) {
       delete limits[id];
-
+      
       // Close the iframe
       if (services[id] && services[id]._closeIframe) {
         services[id]._closeIframe();
diff --git a/dev/js/src/plugin/widget.js b/dev/js/src/plugin/widget.js
index 25bc6cc..a845ebc 100644
--- a/dev/js/src/plugin/widget.js
+++ b/dev/js/src/plugin/widget.js
@@ -65,7 +65,7 @@
     // On closing the widget view
     onClose : function () {
       if (this._mgr) {
-        this._mgr._closeService(this._id);
+        this._mgr._closeService(this.id);
         this._mgr = undefined;
       };
     }