ソースを参照

began work on unsaved changes warning when changing dashboard, Issue #324

Torkel Ödegaard 11 年 前
コミット
c26926148c
1 ファイル変更23 行追加0 行削除
  1. 23 0
      src/app/services/dashboard.js

+ 23 - 0
src/app/services/dashboard.js

@@ -60,6 +60,12 @@ function (angular, $, kbn, _, config, moment, Modernizr) {
     this.last = {};
     this.last = {};
     this.availablePanels = [];
     this.availablePanels = [];
 
 
+    $rootScope.$on("$locationChangeStart", function(event, next, current) {
+      if (!self.confirm_dash_change()) {
+        event.preventDefault();
+      }
+    });
+
     $rootScope.$on('$routeChangeSuccess',function(){
     $rootScope.$on('$routeChangeSuccess',function(){
       // Clear the current dashboard to prevent reloading
       // Clear the current dashboard to prevent reloading
       self.current = {};
       self.current = {};
@@ -156,6 +162,7 @@ function (angular, $, kbn, _, config, moment, Modernizr) {
 
 
       // Set the current dashboard
       // Set the current dashboard
       self.current = angular.copy(dashboard);
       self.current = angular.copy(dashboard);
+      self.original = angular.copy(dashboard);
 
 
       // Delay this until we're sure that querySrv and filterSrv are ready
       // Delay this until we're sure that querySrv and filterSrv are ready
       $timeout(function() {
       $timeout(function() {
@@ -184,6 +191,22 @@ function (angular, $, kbn, _, config, moment, Modernizr) {
       return true;
       return true;
     };
     };
 
 
+   this.confirm_dash_change = function() {
+      if (!self.original) {
+        return true;
+      }
+
+      var current = angular.copy(self.current);
+      var currentJson = angular.toJson(current);
+      var originalJson = angular.toJson(self.original);
+
+      if (currentJson !== originalJson) {
+        return confirm('There are unsaved changes, are you sure you want to change dashboard?');
+      }
+
+      return true;
+    };
+
     this.gist_id = function(string) {
     this.gist_id = function(string) {
       if(self.is_gist(string)) {
       if(self.is_gist(string)) {
         return string.match(gist_pattern)[0].replace(/.*\//, '');
         return string.match(gist_pattern)[0].replace(/.*\//, '');