dashnav.ts 6.4 KB

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