diff --git a/rd_ui/app/scripts/app.js b/rd_ui/app/scripts/app.js index 471cdd4e6e..9835d089b1 100644 --- a/rd_ui/app/scripts/app.js +++ b/rd_ui/app/scripts/app.js @@ -1,19 +1,58 @@ -angular.module('redash', ['redash.directives', 'redash.admin_controllers', 'redash.controllers', 'redash.filters', 'redash.services', - 'redash.renderers', - 'ui.codemirror', 'highchart', 'angular-growl', 'angularMoment', 'ui.bootstrap', 'smartTable.table', 'ngResource']). - config(['$routeProvider', '$locationProvider', '$compileProvider', function ($routeProvider, $locationProvider, $compileProvider) { +angular.module('redash', [ + 'redash.directives', + 'redash.admin_controllers', + 'redash.controllers', + 'redash.filters', + 'redash.services', + 'redash.renderers', + 'ui.codemirror', + 'highchart', + 'angular-growl', + 'angularMoment', + 'ui.bootstrap', + 'smartTable.table', + 'ngResource' +]).config(['$routeProvider', '$locationProvider', '$compileProvider', 'growlProvider', + function($routeProvider, $locationProvider, $compileProvider, growlProvider) { $compileProvider.urlSanitizationWhitelist(/^\s*(https?|http|data):/); - $locationProvider.html5Mode(true); - $routeProvider.when('/dashboard/:dashboardSlug', {templateUrl: '/views/dashboard.html', controller: 'DashboardCtrl'}); - $routeProvider.when('/queries', {templateUrl: '/views/queries.html', controller: 'QueriesCtrl', reloadOnSearch: false}); - $routeProvider.when('/queries/new', {templateUrl: '/views/queryfiddle.html', controller: 'QueryFiddleCtrl', reloadOnSearch: false}); - $routeProvider.when('/queries/:queryId', {templateUrl: '/views/queryfiddle.html', controller: 'QueryFiddleCtrl', reloadOnSearch: false}); - $routeProvider.when('/admin/status', {templateUrl: '/views/admin_status.html', controller: 'AdminStatusCtrl'}); - $routeProvider.when('/', {templateUrl: '/views/index.html', controller: 'IndexCtrl'}); - $routeProvider.otherwise({redirectTo: '/'}); + growlProvider.globalTimeToLive(2000); - Highcharts.setOptions({colors: ["#4572A7", "#AA4643", "#89A54E", "#80699B", "#3D96AE", "#DB843D", "#92A8CD", "#A47D7C", "#B5CA92"]}); - }]); + $routeProvider.when('/dashboard/:dashboardSlug', { + templateUrl: '/views/dashboard.html', + controller: 'DashboardCtrl' + }); + $routeProvider.when('/queries', { + templateUrl: '/views/queries.html', + controller: 'QueriesCtrl', + reloadOnSearch: false + }); + $routeProvider.when('/queries/new', { + templateUrl: '/views/queryfiddle.html', + controller: 'QueryFiddleCtrl', + reloadOnSearch: false + }); + $routeProvider.when('/queries/:queryId', { + templateUrl: '/views/queryfiddle.html', + controller: 'QueryFiddleCtrl', + reloadOnSearch: false + }); + $routeProvider.when('/admin/status', { + templateUrl: '/views/admin_status.html', + controller: 'AdminStatusCtrl' + }); + $routeProvider.when('/', { + templateUrl: '/views/index.html', + controller: 'IndexCtrl' + }); + $routeProvider.otherwise({ + redirectTo: '/' + }); + Highcharts.setOptions({ + colors: ["#4572A7", "#AA4643", "#89A54E", "#80699B", "#3D96AE", + "#DB843D", "#92A8CD", "#A47D7C", "#B5CA92"] + }); + } +]); \ No newline at end of file diff --git a/rd_ui/app/scripts/controllers.js b/rd_ui/app/scripts/controllers.js index 03b5b19dae..aca4c78625 100644 --- a/rd_ui/app/scripts/controllers.js +++ b/rd_ui/app/scripts/controllers.js @@ -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'}, @@ -54,6 +70,9 @@ } delete $scope.query.latest_query_data; $scope.query.$save(function (q) { + pristineQuery = q.query; + $scope.queryChanged = false; + if (duplicate) { growl.addInfoMessage("Query duplicated.", {ttl: 2000}); } else{ @@ -68,6 +87,8 @@ $location.path($location.path().replace(oldId, q.id)).replace(); } } + }, function(httpResponse) { + growl.addErrorMessage("Query could not be saved"); }); }; @@ -142,7 +163,8 @@ }); if ($routeParams.queryId != undefined) { - $scope.query = Query.get({id: $routeParams.queryId}, function() { + $scope.query = Query.get({id: $routeParams.queryId}, function(q) { + pristineQuery = q.query; $scope.queryResult = $scope.query.getQueryResult(); }); } else { @@ -153,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); @@ -320,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]); })(); diff --git a/rd_ui/app/styles/redash.css b/rd_ui/app/styles/redash.css index aeb0268c2b..e935e6b33a 100644 --- a/rd_ui/app/styles/redash.css +++ b/rd_ui/app/styles/redash.css @@ -53,6 +53,7 @@ a.navbar-brand { right: 10px; float: right; width: 250px; + z-index: 10000; } .growl-item.ng-enter, diff --git a/rd_ui/app/views/queryfiddle.html b/rd_ui/app/views/queryfiddle.html index 4ff57c0d86..3614ddb1a0 100644 --- a/rd_ui/app/views/queryfiddle.html +++ b/rd_ui/app/views/queryfiddle.html @@ -17,7 +17,9 @@

- +