dashboardNavCtrl.js 5.0 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', $scope.saveDashboard);
  16. $scope.onAppEvent('delete-dashboard', $scope.deleteDashboard);
  17. $scope.onAppEvent('zoom-out', function() {
  18. $scope.zoom(2);
  19. });
  20. };
  21. $scope.set_default = function() {
  22. store.set('grafanaDashboardDefault', $location.path());
  23. alertSrv.set('Home Set','This page has been set as your default dashboard','success',5000);
  24. };
  25. $scope.purge_default = function() {
  26. store.delete('grafanaDashboardDefault');
  27. alertSrv.set('Local Default Clear','Your default dashboard has been reset to the default','success', 5000);
  28. };
  29. $scope.openEditView = function(editview) {
  30. var search = _.extend($location.search(), {editview: editview});
  31. $location.search(search);
  32. };
  33. $scope.starDashboard = function() {
  34. if ($scope.dashboardMeta.isStarred) {
  35. $scope.db.unstarDashboard($scope.dashboard.id).then(function() {
  36. $scope.dashboardMeta.isStarred = false;
  37. });
  38. }
  39. else {
  40. $scope.db.starDashboard($scope.dashboard.id).then(function() {
  41. $scope.dashboardMeta.isStarred = true;
  42. });
  43. }
  44. };
  45. $scope.shareDashboard = function() {
  46. $scope.appEvent('show-modal', {
  47. src: './app/features/dashboard/partials/shareModal.html',
  48. scope: $scope.$new(),
  49. });
  50. };
  51. $scope.passwordCache = function(pwd) {
  52. if (!window.sessionStorage) { return null; }
  53. if (!pwd) { return window.sessionStorage["grafanaAdminPassword"]; }
  54. window.sessionStorage["grafanaAdminPassword"] = pwd;
  55. };
  56. $scope.openSearch = function() {
  57. $scope.appEvent('show-dash-search');
  58. };
  59. $scope.saveDashboard = function() {
  60. var clone = angular.copy($scope.dashboard);
  61. $scope.db.saveDashboard(clone)
  62. .then(function(result) {
  63. $scope.appEvent('alert-success', ['Dashboard saved', 'Saved as ' + result.title]);
  64. if (result.url !== $location.path()) {
  65. $location.search({});
  66. $location.path(result.url);
  67. }
  68. $scope.appEvent('dashboard-saved', $scope.dashboard);
  69. }, function(err) {
  70. $scope.appEvent('alert-error', ['Save failed', err]);
  71. });
  72. };
  73. $scope.deleteDashboard = function() {
  74. $scope.appEvent('confirm-modal', {
  75. title: 'Delete dashboard',
  76. text: 'Do you want to delete dashboard ' + $scope.dashboard.title + '?',
  77. onConfirm: function() {
  78. $scope.deleteDashboardConfirmed();
  79. }
  80. });
  81. };
  82. $scope.deleteDashboardConfirmed = function() {
  83. $scope.db.deleteDashboard($scope.dashboardMeta.slug).then(function() {
  84. $scope.appEvent('alert-success', ['Dashboard Deleted', $scope.dashboard.title + ' has been deleted']);
  85. }, function(err) {
  86. $scope.appEvent('alert-error', ['Deleted failed', err]);
  87. });
  88. };
  89. $scope.cloneDashboard = function() {
  90. var newScope = $rootScope.$new();
  91. newScope.clone = angular.copy($scope.dashboard);
  92. $scope.appEvent('show-modal', {
  93. src: './app/features/dashboard/partials/cloneDashboard.html',
  94. scope: newScope,
  95. });
  96. };
  97. $scope.exportDashboard = function() {
  98. var blob = new Blob([angular.toJson($scope.dashboard, true)], { type: "application/json;charset=utf-8" });
  99. window.saveAs(blob, $scope.dashboard.title + '-' + new Date().getTime());
  100. };
  101. $scope.zoom = function(factor) {
  102. var range = timeSrv.timeRange();
  103. var timespan = (range.to.valueOf() - range.from.valueOf());
  104. var center = range.to.valueOf() - timespan/2;
  105. var to = (center + (timespan*factor)/2);
  106. var from = (center - (timespan*factor)/2);
  107. if(to > Date.now() && range.to <= Date.now()) {
  108. var offset = to - Date.now();
  109. from = from - offset;
  110. to = Date.now();
  111. }
  112. timeSrv.setTime({
  113. from: moment.utc(from).toDate(),
  114. to: moment.utc(to).toDate(),
  115. });
  116. };
  117. $scope.editJson = function() {
  118. $scope.appEvent('show-json-editor', { object: $scope.dashboard });
  119. };
  120. $scope.openSaveDropdown = function() {
  121. $scope.isFavorite = playlistSrv.isCurrentFavorite($scope.dashboard);
  122. $scope.saveDropdownOpened = true;
  123. };
  124. $scope.markAsFavorite = function() {
  125. playlistSrv.markAsFavorite($scope.dashboard);
  126. $scope.isFavorite = true;
  127. };
  128. $scope.removeAsFavorite = function() {
  129. playlistSrv.removeAsFavorite($scope.dashboard);
  130. $scope.isFavorite = false;
  131. };
  132. $scope.stopPlaylist = function() {
  133. playlistSrv.stop(1);
  134. };
  135. });
  136. });