dashboardNavCtrl.js 4.8 KB

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