dashboardNavCtrl.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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, timeSrv) {
  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.openSearch = function() {
  55. $scope.emitAppEvent('show-dash-editor', { src: 'app/partials/search.html' });
  56. };
  57. $scope.saveDashboard = function() {
  58. if (!this.isAdmin()) { return false; }
  59. var clone = angular.copy($scope.dashboard);
  60. $scope.db.saveDashboard(clone)
  61. .then(function(result) {
  62. alertSrv.set('Dashboard Saved', 'Dashboard has been saved as "' + result.title + '"','success', 5000);
  63. $location.search({});
  64. $location.path(result.url);
  65. $rootScope.$emit('dashboard-saved', $scope.dashboard);
  66. }, function(err) {
  67. alertSrv.set('Save failed', err, 'error',5000);
  68. });
  69. };
  70. $scope.deleteDashboard = function(id, $event) {
  71. $event.stopPropagation();
  72. if (!confirm('Are you sure you want to delete dashboard?')) {
  73. return;
  74. }
  75. if (!this.isAdmin()) { return false; }
  76. $scope.db.deleteDashboard(id).then(function(id) {
  77. alertSrv.set('Dashboard Deleted', id + ' has been deleted', 'success', 5000);
  78. }, function() {
  79. alertSrv.set('Dashboard Not Deleted', 'An error occurred deleting the dashboard', 'error', 5000);
  80. });
  81. };
  82. $scope.exportDashboard = function() {
  83. var blob = new Blob([angular.toJson($scope.dashboard, true)], { type: "application/json;charset=utf-8" });
  84. window.saveAs(blob, $scope.dashboard.title + '-' + new Date().getTime());
  85. };
  86. $scope.zoom = function(factor) {
  87. var _range = timeSrv.timeRange();
  88. var _timespan = (_range.to.valueOf() - _range.from.valueOf());
  89. var _center = _range.to.valueOf() - _timespan/2;
  90. var _to = (_center + (_timespan*factor)/2);
  91. var _from = (_center - (_timespan*factor)/2);
  92. // If we're not already looking into the future, don't.
  93. if(_to > Date.now() && _range.to < Date.now()) {
  94. var _offset = _to - Date.now();
  95. _from = _from - _offset;
  96. _to = Date.now();
  97. }
  98. timeSrv.setTime({
  99. from:moment.utc(_from).toDate(),
  100. to:moment.utc(_to).toDate(),
  101. });
  102. };
  103. $scope.styleUpdated = function() {
  104. $scope.grafana.style = $scope.dashboard.style;
  105. };
  106. $scope.editJson = function() {
  107. var editScope = $rootScope.$new();
  108. editScope.json = angular.toJson($scope.dashboard, true);
  109. $scope.emitAppEvent('show-dash-editor', { src: 'app/partials/edit_json.html', scope: editScope });
  110. };
  111. $scope.openSaveDropdown = function() {
  112. $scope.isFavorite = playlistSrv.isCurrentFavorite($scope.dashboard);
  113. $scope.saveDropdownOpened = true;
  114. };
  115. $scope.markAsFavorite = function() {
  116. playlistSrv.markAsFavorite($scope.dashboard);
  117. $scope.isFavorite = true;
  118. };
  119. $scope.removeAsFavorite = function() {
  120. playlistSrv.removeAsFavorite($scope.dashboard);
  121. $scope.isFavorite = false;
  122. };
  123. $scope.stopPlaylist = function() {
  124. playlistSrv.stop(1);
  125. };
  126. });
  127. });