2013-09-13 15:52:13 -05:00
|
|
|
define([
|
|
|
|
'angular',
|
2014-08-07 07:35:19 -05:00
|
|
|
'lodash',
|
2014-06-13 06:50:09 -05:00
|
|
|
'moment',
|
2014-07-30 03:52:02 -05:00
|
|
|
'config',
|
2014-08-16 06:13:26 -05:00
|
|
|
'store',
|
2014-06-13 06:50:09 -05:00
|
|
|
'filesaver'
|
2013-09-13 15:52:13 -05:00
|
|
|
],
|
2014-08-16 06:13:26 -05:00
|
|
|
function (angular, _, moment, config, store) {
|
2013-09-13 15:52:13 -05:00
|
|
|
'use strict';
|
|
|
|
|
2014-07-28 11:11:52 -05:00
|
|
|
var module = angular.module('grafana.controllers');
|
2013-09-13 15:52:13 -05:00
|
|
|
|
2014-08-16 01:55:05 -05:00
|
|
|
module.controller('DashboardNavCtrl', function($scope, $rootScope, alertSrv, $location, playlistSrv, datasourceSrv) {
|
2013-09-13 15:52:13 -05:00
|
|
|
|
|
|
|
$scope.init = function() {
|
2014-07-30 03:52:02 -05:00
|
|
|
$scope.db = datasourceSrv.getGrafanaDB();
|
2014-08-13 03:07:32 -05:00
|
|
|
|
2014-06-12 10:40:37 -05:00
|
|
|
$scope.onAppEvent('save-dashboard', function() {
|
2014-06-22 11:21:38 -05:00
|
|
|
$scope.saveDashboard();
|
2013-12-29 04:46:36 -06:00
|
|
|
});
|
2013-12-29 06:17:04 -06:00
|
|
|
|
2014-06-12 10:40:37 -05:00
|
|
|
$scope.onAppEvent('zoom-out', function() {
|
2013-12-29 06:17:04 -06:00
|
|
|
$scope.zoom(2);
|
|
|
|
});
|
2013-09-13 15:52:13 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
$scope.set_default = function() {
|
2014-08-16 06:13:26 -05:00
|
|
|
store.set('grafanaDashboardDefault', $location.path());
|
2014-06-12 10:03:41 -05:00
|
|
|
alertSrv.set('Home Set','This page has been set as your default dashboard','success',5000);
|
2013-09-13 15:52:13 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
$scope.purge_default = function() {
|
2014-08-16 06:13:26 -05:00
|
|
|
store.delete('grafanaDashboardDefault');
|
2014-06-12 10:03:41 -05:00
|
|
|
alertSrv.set('Local Default Clear','Your default dashboard has been reset to the default','success', 5000);
|
2013-09-13 15:52:13 -05:00
|
|
|
};
|
|
|
|
|
2014-06-11 13:22:05 -05:00
|
|
|
$scope.saveForSharing = function() {
|
2014-08-03 05:07:50 -05:00
|
|
|
var clone = angular.copy($scope.dashboard);
|
|
|
|
clone.temp = true;
|
|
|
|
$scope.db.saveDashboard(clone)
|
2014-06-11 13:22:05 -05:00
|
|
|
.then(function(result) {
|
|
|
|
|
|
|
|
$scope.share = { url: result.url, title: result.title };
|
|
|
|
|
|
|
|
}, function(err) {
|
|
|
|
alertSrv.set('Save for sharing failed', err, 'error',5000);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-07-30 03:52:02 -05:00
|
|
|
$scope.passwordCache = function(pwd) {
|
|
|
|
if (!window.sessionStorage) { return null; }
|
|
|
|
if (!pwd) { return window.sessionStorage["grafanaAdminPassword"]; }
|
|
|
|
window.sessionStorage["grafanaAdminPassword"] = pwd;
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.isAdmin = function() {
|
|
|
|
if (!config.admin || !config.admin.password) { return true; }
|
|
|
|
if (this.passwordCache() === config.admin.password) { return true; }
|
|
|
|
|
|
|
|
var password = window.prompt("Admin password", "");
|
|
|
|
this.passwordCache(password);
|
|
|
|
|
|
|
|
if (password === config.admin.password) { return true; }
|
|
|
|
|
|
|
|
alertSrv.set('Save failed', 'Password incorrect', 'error');
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2014-06-11 13:22:05 -05:00
|
|
|
$scope.saveDashboard = function() {
|
2014-07-30 03:52:02 -05:00
|
|
|
if (!this.isAdmin()) { return false; }
|
|
|
|
|
2014-08-03 05:07:50 -05:00
|
|
|
var clone = angular.copy($scope.dashboard);
|
|
|
|
$scope.db.saveDashboard(clone)
|
2014-04-27 05:22:38 -05:00
|
|
|
.then(function(result) {
|
2014-07-30 03:52:02 -05:00
|
|
|
alertSrv.set('Dashboard Saved', 'Dashboard has been saved as "' + result.title + '"','success', 5000);
|
2014-02-01 03:51:35 -06:00
|
|
|
|
2014-08-13 10:20:54 -05:00
|
|
|
$location.search({});
|
2014-06-11 13:22:05 -05:00
|
|
|
$location.path(result.url);
|
2014-02-01 03:51:35 -06:00
|
|
|
|
2014-06-07 14:00:05 -05:00
|
|
|
$rootScope.$emit('dashboard-saved', $scope.dashboard);
|
2014-06-10 14:32:38 -05:00
|
|
|
|
|
|
|
}, function(err) {
|
|
|
|
alertSrv.set('Save failed', err, 'error',5000);
|
2014-04-27 05:22:38 -05:00
|
|
|
});
|
2013-09-13 15:52:13 -05:00
|
|
|
};
|
|
|
|
|
2014-08-18 09:38:04 -05:00
|
|
|
$scope.deleteDashboard = function(id, $event) {
|
|
|
|
$event.stopPropagation();
|
|
|
|
|
2013-12-28 16:54:07 -06:00
|
|
|
if (!confirm('Are you sure you want to delete dashboard?')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-30 03:52:02 -05:00
|
|
|
if (!this.isAdmin()) { return false; }
|
|
|
|
|
|
|
|
$scope.db.deleteDashboard(id).then(function(id) {
|
2014-06-12 10:40:37 -05:00
|
|
|
alertSrv.set('Dashboard Deleted', id + ' has been deleted', 'success', 5000);
|
|
|
|
}, function() {
|
|
|
|
alertSrv.set('Dashboard Not Deleted', 'An error occurred deleting the dashboard', 'error', 5000);
|
|
|
|
});
|
2013-09-13 15:52:13 -05:00
|
|
|
};
|
2014-06-13 06:50:09 -05:00
|
|
|
|
|
|
|
$scope.exportDashboard = function() {
|
|
|
|
var blob = new Blob([angular.toJson($scope.dashboard, true)], { type: "application/json;charset=utf-8" });
|
|
|
|
window.saveAs(blob, $scope.dashboard.title + '-' + new Date().getTime());
|
|
|
|
};
|
2013-09-13 15:52:13 -05:00
|
|
|
|
2013-12-29 06:17:04 -06:00
|
|
|
// function $scope.zoom
|
|
|
|
// factor :: Zoom factor, so 0.5 = cuts timespan in half, 2 doubles timespan
|
|
|
|
$scope.zoom = function(factor) {
|
2014-06-07 12:43:15 -05:00
|
|
|
var _range = $scope.filter.timeRange();
|
2013-12-29 06:17:04 -06:00
|
|
|
var _timespan = (_range.to.valueOf() - _range.from.valueOf());
|
|
|
|
var _center = _range.to.valueOf() - _timespan/2;
|
|
|
|
|
|
|
|
var _to = (_center + (_timespan*factor)/2);
|
|
|
|
var _from = (_center - (_timespan*factor)/2);
|
|
|
|
|
|
|
|
// If we're not already looking into the future, don't.
|
|
|
|
if(_to > Date.now() && _range.to < Date.now()) {
|
|
|
|
var _offset = _to - Date.now();
|
|
|
|
_from = _from - _offset;
|
|
|
|
_to = Date.now();
|
|
|
|
}
|
|
|
|
|
2014-06-07 12:43:15 -05:00
|
|
|
$scope.filter.setTime({
|
2013-12-29 06:17:04 -06:00
|
|
|
from:moment.utc(_from).toDate(),
|
|
|
|
to:moment.utc(_to).toDate(),
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-06-22 11:21:38 -05:00
|
|
|
$scope.styleUpdated = function() {
|
|
|
|
$scope.grafana.style = $scope.dashboard.style;
|
|
|
|
};
|
|
|
|
|
2014-03-08 09:27:01 -06:00
|
|
|
$scope.openSaveDropdown = function() {
|
2014-06-10 14:32:38 -05:00
|
|
|
$scope.isFavorite = playlistSrv.isCurrentFavorite($scope.dashboard);
|
2014-08-15 02:35:07 -05:00
|
|
|
$scope.saveDropdownOpened = true;
|
2014-03-08 09:27:01 -06:00
|
|
|
};
|
|
|
|
|
2014-03-07 12:24:45 -06:00
|
|
|
$scope.markAsFavorite = function() {
|
2014-06-10 14:32:38 -05:00
|
|
|
playlistSrv.markAsFavorite($scope.dashboard);
|
2014-03-08 09:27:01 -06:00
|
|
|
$scope.isFavorite = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.removeAsFavorite = function() {
|
2014-06-07 14:00:05 -05:00
|
|
|
playlistSrv.removeAsFavorite($scope.dashboard);
|
2014-03-08 09:27:01 -06:00
|
|
|
$scope.isFavorite = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.stopPlaylist = function() {
|
|
|
|
playlistSrv.stop(1);
|
2014-03-07 12:24:45 -06:00
|
|
|
};
|
|
|
|
|
2013-09-13 15:52:13 -05:00
|
|
|
});
|
|
|
|
|
2013-10-07 01:04:37 -05:00
|
|
|
});
|