dashboardNavCtrl.js 4.6 KB

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