dashboardNavCtrl.js 4.9 KB

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