Navigation service

This commit is contained in:
Amir Nissim 2014-03-25 12:28:18 +02:00
parent 156bf96788
commit b93132e5d9
4 changed files with 51 additions and 41 deletions

View File

@ -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
]);
})();

View File

@ -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',

View File

@ -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])
})();

View File

@ -1,8 +1,6 @@
<div class="container">
<alert-unsaved-changes ng-if="canEdit" is-dirty="isDirty"></alert-unsaved-changes>
<div class="row">
<div class="col-lg-12">
<div class="row">