From b93132e5d9ac66e806724fd41359c705043a58ef Mon Sep 17 00:00:00 2001 From: Amir Nissim Date: Tue, 25 Mar 2014 12:28:18 +0200 Subject: [PATCH] Navigation service --- rd_ui/app/scripts/controllers/query_edit.js | 7 +++- rd_ui/app/scripts/directives/directives.js | 37 ----------------- rd_ui/app/scripts/services/services.js | 46 +++++++++++++++++++++ rd_ui/app/views/query.html | 2 - 4 files changed, 51 insertions(+), 41 deletions(-) diff --git a/rd_ui/app/scripts/controllers/query_edit.js b/rd_ui/app/scripts/controllers/query_edit.js index 295b0f1f..6d055394 100644 --- a/rd_ui/app/scripts/controllers/query_edit.js +++ b/rd_ui/app/scripts/controllers/query_edit.js @@ -1,7 +1,7 @@ (function() { 'use strict'; - function QueryEditCtrl($controller, $scope, $location, growl, Query, Visualization, KeyboardShortcuts) { + function QueryEditCtrl($controller, $scope, $location, growl, Query, Visualization, KeyboardShortcuts, Navigation) { // extends QueryViewCtrl $controller('QueryViewCtrl', {$scope: $scope}); @@ -17,6 +17,9 @@ $scope.newVisualization = undefined; KeyboardShortcuts.bind(shortcuts); + Navigation.addWatch(function dirtyTest() { + return $scope.isDirty; + }); $scope.deleteVisualization = function($e, vis) { $e.preventDefault(); @@ -51,6 +54,6 @@ angular.module('redash.controllers').controller('QueryEditCtrl', [ '$controller', '$scope', '$location', 'growl', 'Query', - 'Visualization', 'KeyboardShortcuts', QueryEditCtrl + 'Visualization', 'KeyboardShortcuts', 'Navigation', QueryEditCtrl ]); })(); \ No newline at end of file diff --git a/rd_ui/app/scripts/directives/directives.js b/rd_ui/app/scripts/directives/directives.js index b7824640..2e20026d 100644 --- a/rd_ui/app/scripts/directives/directives.js +++ b/rd_ui/app/scripts/directives/directives.js @@ -3,43 +3,6 @@ var directives = angular.module('redash.directives', []); - directives.directive('alertUnsavedChanges', ['$window', function($window) { - return { - restrict: 'E', - replace: true, - scope: { - 'isDirty': '=' - }, - link: function($scope) { - var - - unloadMessage = "You will lose your changes if you leave", - confirmMessage = unloadMessage + "\n\nAre you sure you want to leave this page?", - - // store original handler (if any) - _onbeforeunload = $window.onbeforeunload; - - $window.onbeforeunload = function() { - return $scope.isDirty ? unloadMessage : null; - } - - $scope.$on('$locationChangeStart', function(event, next, current) { - if (next.split("#")[0] == current.split("#")[0]) { - return; - } - - if ($scope.isDirty && !confirm(confirmMessage)) { - event.preventDefault(); - } - }); - - $scope.$on('$destroy', function() { - $window.onbeforeunload = _onbeforeunload; - }); - } - } - }]); - directives.directive('rdTab', function() { return { restrict: 'E', diff --git a/rd_ui/app/scripts/services/services.js b/rd_ui/app/scripts/services/services.js index feb2189d..60b7e1fc 100644 --- a/rd_ui/app/scripts/services/services.js +++ b/rd_ui/app/scripts/services/services.js @@ -19,6 +19,52 @@ } } + function Navigation($window, $rootScope) { + var + + dirtyChecks = [], + + unloadMessage = "You will lose your changes if you leave", + confirmMessage = unloadMessage + "\n\nAre you sure you want to leave this page?"; + + function isDirty() { + return _.some(dirtyChecks, function(fn) { + return fn(); + }); + } + + $window.onbeforeunload = function() { + return isDirty() ? unloadMessage : null; + } + + $rootScope.$on('$locationChangeStart', function(event, next, current) { + console.log('evme', 'locationChangeStart'); + if (next.split("#")[0] == current.split("#")[0]) { + return; + } + + if (isDirty()) { + if (confirm(confirmMessage)) { + dirtyChecks = []; + } else { + event.preventDefault(); + } + } + }); + + $rootScope.$on('$destroy', function() { + console.log('evme', 'rootScope destroy'); + $window.onbeforeunload = _onbeforeunload; + }); + + // register a function that if returns true, + // will trigger unsaved changes alert + this.addWatch = function(fn) { + dirtyChecks.push(fn); + } + } + angular.module('redash.services', []) .service('KeyboardShortcuts', [KeyboardShortcuts]) + .service('Navigation', ['$window', '$rootScope', Navigation]) })(); \ No newline at end of file diff --git a/rd_ui/app/views/query.html b/rd_ui/app/views/query.html index 3dfcdf56..361a58cd 100644 --- a/rd_ui/app/views/query.html +++ b/rd_ui/app/views/query.html @@ -1,8 +1,6 @@
- -