dashboardNavCtrl.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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/shareModal.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.manageButtonClickAction = function() {
  46. angular.element('#manageDashboardButton').tooltip('hide');
  47. $scope.appEvent('hide-dash-search');
  48. };
  49. $scope.saveDashboard = function(options) {
  50. if ($scope.dashboardMeta.canSave === false) {
  51. return;
  52. }
  53. var clone = $scope.dashboard.getSaveModelClone();
  54. backendSrv.saveDashboard(clone, options).then(function(data) {
  55. $scope.dashboard.version = data.version;
  56. $scope.appEvent('dashboard-saved', $scope.dashboard);
  57. var dashboardUrl = '/dashboard/db/' + data.slug;
  58. if (dashboardUrl !== $location.path()) {
  59. $location.url(dashboardUrl);
  60. }
  61. $scope.appEvent('alert-success', ['Dashboard saved', 'Saved as ' + clone.title]);
  62. }, $scope.handleSaveDashError);
  63. };
  64. $scope.handleSaveDashError = function(err) {
  65. if (err.data && err.data.status === "version-mismatch") {
  66. err.isHandled = true;
  67. $scope.appEvent('confirm-modal', {
  68. title: 'Someone else has updated this dashboard!',
  69. text: "Would you still like to save this dashboard?",
  70. yesText: "Save & Overwrite",
  71. icon: "fa-warning",
  72. onConfirm: function() {
  73. $scope.saveDashboard({overwrite: true});
  74. }
  75. });
  76. }
  77. if (err.data && err.data.status === "name-exists") {
  78. err.isHandled = true;
  79. $scope.appEvent('confirm-modal', {
  80. title: 'Another dashboard with the same name exists',
  81. text: "Would you still like to save this dashboard?",
  82. yesText: "Save & Overwrite",
  83. icon: "fa-warning",
  84. onConfirm: function() {
  85. $scope.saveDashboard({overwrite: true});
  86. }
  87. });
  88. }
  89. };
  90. $scope.deleteDashboard = function() {
  91. $scope.appEvent('confirm-modal', {
  92. title: 'Do you want to delete dashboard ' + $scope.dashboard.title + '?',
  93. icon: 'fa-trash',
  94. yesText: 'Delete',
  95. onConfirm: function() {
  96. $scope.deleteDashboardConfirmed();
  97. }
  98. });
  99. };
  100. $scope.deleteDashboardConfirmed = function() {
  101. backendSrv.delete('/api/dashboards/db/' + $scope.dashboardMeta.slug).then(function() {
  102. $scope.appEvent('alert-success', ['Dashboard Deleted', $scope.dashboard.title + ' has been deleted']);
  103. $location.url('/');
  104. });
  105. };
  106. $scope.saveDashboardAs = function() {
  107. var newScope = $rootScope.$new();
  108. newScope.clone = $scope.dashboard.getSaveModelClone();
  109. newScope.clone.editable = true;
  110. newScope.clone.hideControls = false;
  111. $scope.appEvent('show-modal', {
  112. src: './app/features/dashboard/partials/saveDashboardAs.html',
  113. scope: newScope,
  114. });
  115. };
  116. $scope.exportDashboard = function() {
  117. var clone = $scope.dashboard.getSaveModelClone();
  118. var blob = new Blob([angular.toJson(clone, true)], { type: "application/json;charset=utf-8" });
  119. window.saveAs(blob, $scope.dashboard.title + '-' + new Date().getTime());
  120. };
  121. $scope.snapshot = function() {
  122. $scope.dashboard.snapshot = true;
  123. $rootScope.$broadcast('refresh');
  124. $timeout(function() {
  125. $scope.exportDashboard();
  126. $scope.dashboard.snapshot = false;
  127. $scope.appEvent('dashboard-snapshot-cleanup');
  128. }, 1000);
  129. };
  130. $scope.editJson = function() {
  131. var clone = $scope.dashboard.getSaveModelClone();
  132. $scope.appEvent('show-json-editor', { object: clone });
  133. };
  134. $scope.stopPlaylist = function() {
  135. playlistSrv.stop(1);
  136. };
  137. });
  138. });