dashnav.ts 6.2 KB

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