dashboardNavCtrl.js 4.8 KB

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