dashboardNavCtrl.js 4.9 KB

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