grafana/src/app/controllers/dashLoader.js

154 lines
4.6 KiB
JavaScript
Raw Normal View History

2013-09-13 15:52:13 -05:00
define([
'angular',
'underscore',
2014-06-13 06:50:09 -05:00
'moment',
'config',
2014-06-13 06:50:09 -05:00
'filesaver'
2013-09-13 15:52:13 -05:00
],
function (angular, _, moment, config) {
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
module.controller('dashLoader', function($scope, $rootScope, $http, alertSrv, $location, playlistSrv, datasourceSrv) {
2013-09-13 15:52:13 -05:00
$scope.init = function() {
$scope.db = datasourceSrv.getGrafanaDB();
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 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.exitFullscreen = function() {
2014-06-12 10:40:37 -05:00
$scope.emitAppEvent('panel-fullscreen-exit');
};
2013-09-13 15:52:13 -05:00
$scope.set_default = function() {
window.localStorage.grafanaDashboardDefault = $location.path();
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() {
delete window.localStorage.grafanaDashboardDefault;
alertSrv.set('Local Default Clear','Your default dashboard has been reset to the default','success', 5000);
2013-09-13 15:52:13 -05:00
};
$scope.saveForSharing = function() {
$scope.db.saveDashboardTemp($scope.dashboard)
.then(function(result) {
$scope.share = { url: result.url, title: result.title };
}, function(err) {
alertSrv.set('Save for sharing failed', err, 'error',5000);
});
};
$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;
};
$scope.saveDashboard = function() {
if (!this.isAdmin()) { return false; }
$scope.db.saveDashboard($scope.dashboard, $scope.dashboard.title)
.then(function(result) {
alertSrv.set('Dashboard Saved', 'Dashboard has been saved as "' + result.title + '"','success', 5000);
2014-02-01 03:51:35 -06: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);
}, function(err) {
alertSrv.set('Save failed', err, 'error',5000);
});
2013-09-13 15:52:13 -05:00
};
2014-06-12 10:40:37 -05:00
$scope.deleteDashboard = function(id) {
if (!confirm('Are you sure you want to delete dashboard?')) {
return;
}
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) {
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();
}
$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;
};
$scope.openSaveDropdown = function() {
$scope.isFavorite = playlistSrv.isCurrentFavorite($scope.dashboard);
};
$scope.markAsFavorite = function() {
playlistSrv.markAsFavorite($scope.dashboard);
$scope.isFavorite = true;
};
$scope.removeAsFavorite = function() {
2014-06-07 14:00:05 -05:00
playlistSrv.removeAsFavorite($scope.dashboard);
$scope.isFavorite = false;
};
$scope.stopPlaylist = function() {
playlistSrv.stop(1);
};
2013-09-13 15:52:13 -05:00
});
});