dashboardNavCtrl.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. define([
  2. 'angular',
  3. 'lodash',
  4. 'moment',
  5. 'config',
  6. 'store',
  7. 'filesaver'
  8. ],
  9. function (angular, _, moment) {
  10. 'use strict';
  11. var module = angular.module('grafana.controllers');
  12. module.controller('DashboardNavCtrl', function($scope, $rootScope, alertSrv, $location, playlistSrv, backendSrv, timeSrv, $timeout) {
  13. $scope.init = function() {
  14. $scope.onAppEvent('save-dashboard', $scope.saveDashboard);
  15. $scope.onAppEvent('delete-dashboard', $scope.deleteDashboard);
  16. $scope.onAppEvent('zoom-out', function() {
  17. $scope.zoom(2);
  18. });
  19. };
  20. $scope.openEditView = function(editview) {
  21. var search = _.extend($location.search(), {editview: editview});
  22. $location.search(search);
  23. };
  24. $scope.starDashboard = function() {
  25. if ($scope.dashboardMeta.isStarred) {
  26. backendSrv.delete('/api/user/stars/dashboard/' + $scope.dashboard.id).then(function() {
  27. $scope.dashboardMeta.isStarred = false;
  28. });
  29. }
  30. else {
  31. backendSrv.post('/api/user/stars/dashboard/' + $scope.dashboard.id).then(function() {
  32. $scope.dashboardMeta.isStarred = true;
  33. });
  34. }
  35. };
  36. $scope.shareDashboard = function() {
  37. $scope.appEvent('show-modal', {
  38. src: './app/features/dashboard/partials/shareModal.html',
  39. scope: $scope.$new(),
  40. });
  41. };
  42. $scope.openSearch = function() {
  43. $scope.appEvent('show-dash-search');
  44. };
  45. $scope.dashboardTitleAction = function() {
  46. $scope.appEvent('hide-dash-editor');
  47. $scope.exitFullscreen();
  48. };
  49. $scope.saveDashboard = function(options) {
  50. var clone = angular.copy($scope.dashboard);
  51. backendSrv.saveDashboard(clone, options).then(function(data) {
  52. $scope.dashboard.version = data.version;
  53. $scope.appEvent('dashboard-saved', $scope.dashboard);
  54. var dashboardUrl = '/dashboard/db/' + data.slug;
  55. if (dashboardUrl !== $location.path()) {
  56. $location.url(dashboardUrl);
  57. }
  58. $scope.appEvent('alert-success', ['Dashboard saved', 'Saved as ' + clone.title]);
  59. }, $scope.handleSaveDashError);
  60. };
  61. $scope.handleSaveDashError = function(err) {
  62. if (err.data && err.data.status === "version-mismatch") {
  63. err.isHandled = true;
  64. $scope.appEvent('confirm-modal', {
  65. title: 'Someone else has updated this dashboard!',
  66. text: "Would you still like to save this dashboard?",
  67. yesText: "Save & Overwrite",
  68. icon: "fa-warning",
  69. onConfirm: function() {
  70. $scope.saveDashboard({overwrite: true});
  71. }
  72. });
  73. }
  74. if (err.data && err.data.status === "name-exists") {
  75. err.isHandled = true;
  76. $scope.appEvent('confirm-modal', {
  77. title: 'Another dashboard with the same name exists',
  78. text: "Would you still like to save this dashboard?",
  79. yesText: "Save & Overwrite",
  80. icon: "fa-warning",
  81. onConfirm: function() {
  82. $scope.saveDashboard({overwrite: true});
  83. }
  84. });
  85. }
  86. };
  87. $scope.deleteDashboard = function() {
  88. $scope.appEvent('confirm-modal', {
  89. title: 'Do you want to delete dashboard ' + $scope.dashboard.title + '?',
  90. icon: 'fa-trash',
  91. yesText: 'Delete',
  92. onConfirm: function() {
  93. $scope.deleteDashboardConfirmed();
  94. }
  95. });
  96. };
  97. $scope.deleteDashboardConfirmed = function() {
  98. backendSrv.delete('/api/dashboards/db/' + $scope.dashboardMeta.slug).then(function() {
  99. $scope.appEvent('alert-success', ['Dashboard Deleted', $scope.dashboard.title + ' has been deleted']);
  100. $location.url('/');
  101. });
  102. };
  103. $scope.saveDashboardAs = function() {
  104. var newScope = $rootScope.$new();
  105. newScope.clone = angular.copy($scope.dashboard);
  106. $scope.appEvent('show-modal', {
  107. src: './app/features/dashboard/partials/saveDashboardAs.html',
  108. scope: newScope,
  109. });
  110. };
  111. $scope.exportDashboard = function() {
  112. var blob = new Blob([angular.toJson($scope.dashboard, true)], { type: "application/json;charset=utf-8" });
  113. window.saveAs(blob, $scope.dashboard.title + '-' + new Date().getTime());
  114. };
  115. $scope.zoom = function(factor) {
  116. var range = timeSrv.timeRange();
  117. var timespan = (range.to.valueOf() - range.from.valueOf());
  118. var center = range.to.valueOf() - timespan/2;
  119. var to = (center + (timespan*factor)/2);
  120. var from = (center - (timespan*factor)/2);
  121. if(to > Date.now() && range.to <= Date.now()) {
  122. var offset = to - Date.now();
  123. from = from - offset;
  124. to = Date.now();
  125. }
  126. timeSrv.setTime({
  127. from: moment.utc(from).toDate(),
  128. to: moment.utc(to).toDate(),
  129. });
  130. };
  131. $scope.snapshot = function() {
  132. $scope.dashboard.snapshot = true;
  133. $rootScope.$broadcast('refresh');
  134. $timeout(function() {
  135. $scope.exportDashboard();
  136. $scope.dashboard.snapshot = false;
  137. $scope.appEvent('dashboard-snapshot-cleanup');
  138. }, 1000);
  139. };
  140. $scope.editJson = function() {
  141. $scope.appEvent('show-json-editor', { object: $scope.dashboard });
  142. };
  143. $scope.stopPlaylist = function() {
  144. playlistSrv.stop(1);
  145. };
  146. });
  147. });