From 44567af728c54b7d26591e9b9dcb2f8e4dd96a69 Mon Sep 17 00:00:00 2001
From: OllisGit <olaf.kaus+github@gmail.com>
Date: Fri, 24 May 2019 17:55:19 +0200
Subject: [PATCH] fixed #4 UI refreshing

---
 octoprint_DryRun/__init__.py         | 11 +++++++++++
 octoprint_DryRun/static/js/DryRun.js |  8 ++------
 setup.py                             |  2 +-
 3 files changed, 14 insertions(+), 7 deletions(-)

diff --git a/octoprint_DryRun/__init__.py b/octoprint_DryRun/__init__.py
index 7655998..b365c7b 100644
--- a/octoprint_DryRun/__init__.py
+++ b/octoprint_DryRun/__init__.py
@@ -1,6 +1,7 @@
 # coding=utf-8
 from __future__ import absolute_import
 from octoprint.server import user_permission
+from octoprint.events import eventManager, Events
 
 import octoprint.plugin
 from flask import make_response
@@ -10,6 +11,7 @@
 class DryrunPlugin(octoprint.plugin.SettingsPlugin,
                    octoprint.plugin.AssetPlugin,
                    octoprint.plugin.TemplatePlugin,
+                   octoprint.plugin.EventHandlerPlugin,
                    octoprint.plugin.SimpleApiPlugin):
 
     # var
@@ -35,6 +37,15 @@ def on_gcode_queuing(self, comm_instance, phase, cmd, cmd_type, gcode, *args, **
         # Send the original unaltered command
         return None
 
+
+    def on_event(self, event, payload):
+        if event == Events.CLIENT_OPENED:
+            # Send initial values to the frontend
+            self._plugin_manager.send_plugin_message(self._identifier,
+                                                     dict(dryRunEnabled=self.dryRunEnabled)
+                                                     )
+
+
     ## Web-UI - Stuff
     # list all called commands/parameter that are valid to send from the web-ui
     def get_api_commands(self):
diff --git a/octoprint_DryRun/static/js/DryRun.js b/octoprint_DryRun/static/js/DryRun.js
index db0731c..588b1a2 100644
--- a/octoprint_DryRun/static/js/DryRun.js
+++ b/octoprint_DryRun/static/js/DryRun.js
@@ -72,22 +72,18 @@ $(function() {
                     }) ;
             });
 
-
         // receive data from backend
         self.onDataUpdaterPluginMessage = function(plugin, data) {
             if (plugin != PLUGIN_ID) {
                 return;
             }
-            alert("Data from backend!!!" +JSON.stringify(data));
+            //alert("Data from backend!!!" +JSON.stringify(data));
             if (data.dryRunEnabled != undefined){
-                dryRunCheckBox.attr('checked', data.dryRunEnabled);
+                dryRunCheckBoxPicker.prop('checked', data.dryRunEnabled);
             }
         }
 
         });
-        // TODO not needed any more -> cleanup
-        //$("#excludeExpressionsTextArea").numberedtextarea();
-        //$("#excludeExpressions").autogrow({vertical: false, horizontal: false});
     }
 
     /* view model class, parameters for constructor, container to bind to
diff --git a/setup.py b/setup.py
index 830b577..4fb84a7 100644
--- a/setup.py
+++ b/setup.py
@@ -14,7 +14,7 @@
 plugin_name = "DryRun"
 
 # The plugin's version. Can be overwritten within OctoPrint's internal data via __plugin_version__ in the plugin module
-plugin_version = "1.1.0"
+plugin_version = "1.1.1"
 
 # The plugin's description. Can be overwritten within OctoPrint's internal data via __plugin_description__ in the plugin
 # module