dashnav.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. ///<reference path="../../../headers/common.d.ts" />
  2. import _ from 'lodash';
  3. import moment from 'moment';
  4. import angular from 'angular';
  5. export class DashNavCtrl {
  6. /** @ngInject */
  7. constructor($scope, $rootScope, alertSrv, $location, playlistSrv, backendSrv, $timeout) {
  8. $scope.init = function() {
  9. $scope.onAppEvent('save-dashboard', $scope.saveDashboard);
  10. $scope.onAppEvent('delete-dashboard', $scope.deleteDashboard);
  11. $scope.showSettingsMenu = $scope.dashboardMeta.canEdit || $scope.contextSrv.isEditor;
  12. if ($scope.dashboardMeta.isSnapshot) {
  13. $scope.showSettingsMenu = false;
  14. var meta = $scope.dashboardMeta;
  15. $scope.titleTooltip = 'Created: &nbsp;' + moment(meta.created).calendar();
  16. if (meta.expires) {
  17. $scope.titleTooltip += '<br>Expires: &nbsp;' + moment(meta.expires).fromNow() + '<br>';
  18. }
  19. }
  20. };
  21. $scope.openEditView = function(editview) {
  22. var search = _.extend($location.search(), {editview: editview});
  23. $location.search(search);
  24. };
  25. $scope.starDashboard = function() {
  26. if ($scope.dashboardMeta.isStarred) {
  27. backendSrv.delete('/api/user/stars/dashboard/' + $scope.dashboard.id).then(function() {
  28. $scope.dashboardMeta.isStarred = false;
  29. });
  30. } else {
  31. backendSrv.post('/api/user/stars/dashboard/' + $scope.dashboard.id).then(function() {
  32. $scope.dashboardMeta.isStarred = true;
  33. });
  34. }
  35. };
  36. $scope.shareDashboard = function(tabIndex) {
  37. var modalScope = $scope.$new();
  38. modalScope.tabIndex = tabIndex;
  39. $scope.appEvent('show-modal', {
  40. src: 'public/app/features/dashboard/partials/shareModal.html',
  41. scope: modalScope
  42. });
  43. };
  44. $scope.openSearch = function() {
  45. $scope.appEvent('show-dash-search');
  46. };
  47. $scope.hideTooltip = function(evt) {
  48. angular.element(evt.currentTarget).tooltip('hide');
  49. $scope.appEvent('hide-dash-search');
  50. };
  51. $scope.makeEditable = function() {
  52. $scope.dashboard.editable = true;
  53. var clone = $scope.dashboard.getSaveModelClone();
  54. backendSrv.saveDashboard(clone, {overwrite: false}).then(function(data) {
  55. $scope.dashboard.version = data.version;
  56. $scope.appEvent('dashboard-saved', $scope.dashboard);
  57. $scope.appEvent('alert-success', ['Dashboard saved', 'Saved as ' + clone.title]);
  58. // force refresh whole page
  59. window.location.href = window.location.href;
  60. }, $scope.handleSaveDashError);
  61. };
  62. $scope.saveDashboard = function(options) {
  63. if ($scope.dashboardMeta.canSave === false) {
  64. return;
  65. }
  66. var clone = $scope.dashboard.getSaveModelClone();
  67. backendSrv.saveDashboard(clone, options).then(function(data) {
  68. $scope.dashboard.version = data.version;
  69. $scope.appEvent('dashboard-saved', $scope.dashboard);
  70. var dashboardUrl = '/dashboard/db/' + data.slug;
  71. if (dashboardUrl !== $location.path()) {
  72. $location.url(dashboardUrl);
  73. }
  74. $scope.appEvent('alert-success', ['Dashboard saved', 'Saved as ' + clone.title]);
  75. }, $scope.handleSaveDashError);
  76. };
  77. $scope.handleSaveDashError = function(err) {
  78. if (err.data && err.data.status === "version-mismatch") {
  79. err.isHandled = true;
  80. $scope.appEvent('confirm-modal', {
  81. title: 'Someone else has updated this dashboard!',
  82. text: "Would you still like to save this dashboard?",
  83. yesText: "Save & Overwrite",
  84. icon: "fa-warning",
  85. onConfirm: function() {
  86. $scope.saveDashboard({overwrite: true});
  87. }
  88. });
  89. }
  90. if (err.data && err.data.status === "name-exists") {
  91. err.isHandled = true;
  92. $scope.appEvent('confirm-modal', {
  93. title: 'Another dashboard with the same name exists',
  94. text: "Would you still like to save this dashboard?",
  95. yesText: "Save & Overwrite",
  96. icon: "fa-warning",
  97. onConfirm: function() {
  98. $scope.saveDashboard({overwrite: true});
  99. }
  100. });
  101. }
  102. };
  103. $scope.deleteDashboard = function() {
  104. $scope.appEvent('confirm-modal', {
  105. title: 'Do you want to delete dashboard ' + $scope.dashboard.title + '?',
  106. icon: 'fa-trash',
  107. yesText: 'Delete',
  108. onConfirm: function() {
  109. $scope.deleteDashboardConfirmed();
  110. }
  111. });
  112. };
  113. $scope.deleteDashboardConfirmed = function() {
  114. backendSrv.delete('/api/dashboards/db/' + $scope.dashboardMeta.slug).then(function() {
  115. $scope.appEvent('alert-success', ['Dashboard Deleted', $scope.dashboard.title + ' has been deleted']);
  116. $location.url('/');
  117. });
  118. };
  119. $scope.saveDashboardAs = function() {
  120. var newScope = $rootScope.$new();
  121. newScope.clone = $scope.dashboard.getSaveModelClone();
  122. newScope.clone.editable = true;
  123. newScope.clone.hideControls = false;
  124. $scope.appEvent('show-modal', {
  125. src: 'public/app/features/dashboard/partials/saveDashboardAs.html',
  126. scope: newScope,
  127. });
  128. };
  129. $scope.exportDashboard = function() {
  130. var clone = $scope.dashboard.getSaveModelClone();
  131. var blob = new Blob([angular.toJson(clone, true)], { type: "application/json;charset=utf-8" });
  132. var wnd: any = window;
  133. wnd.saveAs(blob, $scope.dashboard.title + '-' + new Date().getTime());
  134. };
  135. $scope.snapshot = function() {
  136. $scope.dashboard.snapshot = true;
  137. $rootScope.$broadcast('refresh');
  138. $timeout(function() {
  139. $scope.exportDashboard();
  140. $scope.dashboard.snapshot = false;
  141. $scope.appEvent('dashboard-snapshot-cleanup');
  142. }, 1000);
  143. };
  144. $scope.editJson = function() {
  145. var clone = $scope.dashboard.getSaveModelClone();
  146. $scope.appEvent('show-json-editor', { object: clone });
  147. };
  148. $scope.stopPlaylist = function() {
  149. playlistSrv.stop(1);
  150. };
  151. $scope.init();
  152. }
  153. }
  154. export function dashNavDirective() {
  155. return {
  156. restrict: 'E',
  157. templateUrl: 'public/app/features/dashboard/dashnav/dashnav.html',
  158. controller: DashNavCtrl,
  159. transclude: true,
  160. };
  161. }
  162. angular.module('grafana.directives').directive('dashnav', dashNavDirective);