Skip to content

Commit

Permalink
Show confirm box, when trying to leave the page before saving the que…
Browse files Browse the repository at this point in the history
…ry (FED #1)
  • Loading branch information
amirnissim committed Dec 30, 2013
1 parent 7e94cc7 commit 6c48017
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
30 changes: 26 additions & 4 deletions rd_ui/app/scripts/controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,23 @@
$scope.updateTime = '';
}

var QueryFiddleCtrl = function ($scope, $routeParams, $http, $location, growl, notifications, Query) {
var QueryFiddleCtrl = function ($scope, $window, $routeParams, $http, $location, growl, notifications, Query) {
var leavingPageText = "You will lose your changes if you leave";
var pristineQuery = null;

$window.onbeforeunload = function(){
if ($scope.queryChanged) {
return leavingPageText;
}
}

$scope.$on('$locationChangeStart', function(event, next, current) {
if($scope.queryChanged &&
!confirm(leavingPageText + "\n\nAre you sure you want to leave this page?")) {
event.preventDefault();
}
});

$scope.$parent.pageTitle = "Query Fiddle";

$scope.tabs = [{'key': 'table', 'name': 'Table'}, {'key': 'chart', 'name': 'Chart'},
Expand All @@ -54,7 +70,8 @@
}
delete $scope.query.latest_query_data;
$scope.query.$save(function (q) {
$scope.pristineQuery = q.query;
pristineQuery = q.query;
$scope.queryChanged = false;

if (duplicate) {
growl.addInfoMessage("Query duplicated.", {ttl: 2000});
Expand Down Expand Up @@ -147,7 +164,7 @@

if ($routeParams.queryId != undefined) {
$scope.query = Query.get({id: $routeParams.queryId}, function(q) {
$scope.pristineQuery = q.query;
pristineQuery = q.query;
$scope.queryResult = $scope.query.getQueryResult();
});
} else {
Expand All @@ -158,6 +175,11 @@
$scope.$watch('query.name', function() {
$scope.$parent.pageTitle = $scope.query.name;
});
$scope.$watch('query.query', function(q) {
if (q) {
$scope.queryChanged = (q != pristineQuery);
}
});

$scope.executeQuery = function() {
$scope.queryResult = $scope.query.getQueryResult(0);
Expand Down Expand Up @@ -325,7 +347,7 @@
.controller('DashboardCtrl', ['$scope', '$routeParams', '$http', 'Dashboard', DashboardCtrl])
.controller('WidgetCtrl', ['$scope', '$http', 'Query', WidgetCtrl])
.controller('QueriesCtrl', ['$scope', '$http', '$location', '$filter', 'Query', QueriesCtrl])
.controller('QueryFiddleCtrl', ['$scope', '$routeParams', '$http', '$location', 'growl', 'notifications', 'Query', QueryFiddleCtrl])
.controller('QueryFiddleCtrl', ['$scope', '$window', '$routeParams', '$http', '$location', 'growl', 'notifications', 'Query', QueryFiddleCtrl])
.controller('IndexCtrl', ['$scope', 'Dashboard', IndexCtrl])
.controller('MainCtrl', ['$scope', 'Dashboard', 'notifications', MainCtrl]);
})();
2 changes: 1 addition & 1 deletion rd_ui/app/views/queryfiddle.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ <h3 class="panel-title">
<div class="btn-group pull-right">
<button type="button" class="btn btn-default" ng-click="duplicateQuery()">Duplicate</button>
<button type="button" class="btn btn-default" ng-disabled="currentUser.name != query.user" ng-click="saveQuery()">Save
<span ng-show="query.query != pristineQuery">&#42;</span>
<span ng-show="queryChanged">&#42;</span>
</button>
<button type="button" class="btn btn-primary" ng-disabled="queryExecuting" ng-click="executeQuery()">Execute</button>
</div>
Expand Down

0 comments on commit 6c48017

Please sign in to comment.