dashboardNavCtrl.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. define([
  2. 'angular',
  3. 'lodash',
  4. 'moment',
  5. 'config',
  6. 'store',
  7. 'filesaver'
  8. ],
  9. function (angular, _, moment, config, store) {
  10. 'use strict';
  11. var module = angular.module('grafana.controllers');
  12. module.controller('DashboardNavCtrl', function($scope, $rootScope, alertSrv, $location, playlistSrv, datasourceSrv) {
  13. $scope.init = function() {
  14. $scope.db = datasourceSrv.getGrafanaDB();
  15. $scope.onAppEvent('save-dashboard', function() {
  16. $scope.saveDashboard();
  17. });
  18. $scope.onAppEvent('zoom-out', function() {
  19. $scope.zoom(2);
  20. });
  21. };
  22. $scope.set_default = function() {
  23. store.set('grafanaDashboardDefault', $location.path());
  24. alertSrv.set('Home Set','This page has been set as your default dashboard','success',5000);
  25. };
  26. $scope.purge_default = function() {
  27. store.delete('grafanaDashboardDefault');
  28. alertSrv.set('Local Default Clear','Your default dashboard has been reset to the default','success', 5000);
  29. };
  30. $scope.saveForSharing = function() {
  31. var clone = angular.copy($scope.dashboard);
  32. clone.temp = true;
  33. $scope.db.saveDashboard(clone)
  34. .then(function(result) {
  35. $scope.share = { url: result.url, title: result.title };
  36. }, function(err) {
  37. alertSrv.set('Save for sharing failed', err, 'error',5000);
  38. });
  39. };
  40. $scope.passwordCache = function(pwd) {
  41. if (!window.sessionStorage) { return null; }
  42. if (!pwd) { return window.sessionStorage["grafanaAdminPassword"]; }
  43. window.sessionStorage["grafanaAdminPassword"] = pwd;
  44. };
  45. $scope.isAdmin = function() {
  46. if (!config.admin || !config.admin.password) { return true; }
  47. if (this.passwordCache() === config.admin.password) { return true; }
  48. var password = window.prompt("Admin password", "");
  49. this.passwordCache(password);
  50. if (password === config.admin.password) { return true; }
  51. alertSrv.set('Save failed', 'Password incorrect', 'error');
  52. return false;
  53. };
  54. $scope.saveDashboard = function() {
  55. if (!this.isAdmin()) { return false; }
  56. var clone = angular.copy($scope.dashboard);
  57. $scope.db.saveDashboard(clone)
  58. .then(function(result) {
  59. alertSrv.set('Dashboard Saved', 'Dashboard has been saved as "' + result.title + '"','success', 5000);
  60. $location.search({});
  61. $location.path(result.url);
  62. $rootScope.$emit('dashboard-saved', $scope.dashboard);
  63. }, function(err) {
  64. alertSrv.set('Save failed', err, 'error',5000);
  65. });
  66. };
  67. $scope.deleteDashboard = function(id, $event) {
  68. $event.stopPropagation();
  69. if (!confirm('Are you sure you want to delete dashboard?')) {
  70. return;
  71. }
  72. if (!this.isAdmin()) { return false; }
  73. $scope.db.deleteDashboard(id).then(function(id) {
  74. alertSrv.set('Dashboard Deleted', id + ' has been deleted', 'success', 5000);
  75. }, function() {
  76. alertSrv.set('Dashboard Not Deleted', 'An error occurred deleting the dashboard', 'error', 5000);
  77. });
  78. };
  79. $scope.exportDashboard = function() {
  80. var blob = new Blob([angular.toJson($scope.dashboard, true)], { type: "application/json;charset=utf-8" });
  81. window.saveAs(blob, $scope.dashboard.title + '-' + new Date().getTime());
  82. };
  83. // function $scope.zoom
  84. // factor :: Zoom factor, so 0.5 = cuts timespan in half, 2 doubles timespan
  85. $scope.zoom = function(factor) {
  86. var _range = $scope.filter.timeRange();
  87. var _timespan = (_range.to.valueOf() - _range.from.valueOf());
  88. var _center = _range.to.valueOf() - _timespan/2;
  89. var _to = (_center + (_timespan*factor)/2);
  90. var _from = (_center - (_timespan*factor)/2);
  91. // If we're not already looking into the future, don't.
  92. if(_to > Date.now() && _range.to < Date.now()) {
  93. var _offset = _to - Date.now();
  94. _from = _from - _offset;
  95. _to = Date.now();
  96. }
  97. $scope.filter.setTime({
  98. from:moment.utc(_from).toDate(),
  99. to:moment.utc(_to).toDate(),
  100. });
  101. };
  102. $scope.styleUpdated = function() {
  103. $scope.grafana.style = $scope.dashboard.style;
  104. };
  105. $scope.openSaveDropdown = function() {
  106. $scope.isFavorite = playlistSrv.isCurrentFavorite($scope.dashboard);
  107. $scope.saveDropdownOpened = true;
  108. };
  109. $scope.markAsFavorite = function() {
  110. playlistSrv.markAsFavorite($scope.dashboard);
  111. $scope.isFavorite = true;
  112. };
  113. $scope.removeAsFavorite = function() {
  114. playlistSrv.removeAsFavorite($scope.dashboard);
  115. $scope.isFavorite = false;
  116. };
  117. $scope.stopPlaylist = function() {
  118. playlistSrv.stop(1);
  119. };
  120. });
  121. });