dashnav.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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, contextSrv, $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.saveDashboardAsHome = function() {
  83. // TODO: this backend method needs to be implemented
  84. backendSrv.post('/api/preferences/set-home-dash', {
  85. homeDashboardId: $scope.dashboard.id
  86. });
  87. };
  88. $scope.handleSaveDashError = function(err) {
  89. if (err.data && err.data.status === "version-mismatch") {
  90. err.isHandled = true;
  91. $scope.appEvent('confirm-modal', {
  92. title: 'Conflict',
  93. text: 'Someone else has updated this dashboard.',
  94. text2: '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. if (err.data && err.data.status === "name-exists") {
  103. err.isHandled = true;
  104. $scope.appEvent('confirm-modal', {
  105. title: 'Conflict',
  106. text: 'Dashboard with the same name exists.',
  107. text2: 'Would you still like to save this dashboard?',
  108. yesText: "Save & Overwrite",
  109. icon: "fa-warning",
  110. onConfirm: function() {
  111. $scope.saveDashboard({overwrite: true});
  112. }
  113. });
  114. }
  115. };
  116. $scope.deleteDashboard = function() {
  117. $scope.appEvent('confirm-modal', {
  118. title: 'Delete',
  119. text: 'Do you want to delete this dashboard?',
  120. text2: $scope.dashboard.title,
  121. icon: 'fa-trash',
  122. yesText: 'Delete',
  123. onConfirm: function() {
  124. $scope.deleteDashboardConfirmed();
  125. }
  126. });
  127. };
  128. $scope.deleteDashboardConfirmed = function() {
  129. backendSrv.delete('/api/dashboards/db/' + $scope.dashboardMeta.slug).then(function() {
  130. $scope.appEvent('alert-success', ['Dashboard Deleted', $scope.dashboard.title + ' has been deleted']);
  131. $location.url('/');
  132. });
  133. };
  134. $scope.saveDashboardAs = function() {
  135. var newScope = $rootScope.$new();
  136. newScope.clone = $scope.dashboard.getSaveModelClone();
  137. newScope.clone.editable = true;
  138. newScope.clone.hideControls = false;
  139. $scope.appEvent('show-modal', {
  140. src: 'public/app/features/dashboard/partials/saveDashboardAs.html',
  141. scope: newScope,
  142. modalClass: 'modal--narrow'
  143. });
  144. };
  145. $scope.exportDashboard = function() {
  146. var clone = $scope.dashboard.getSaveModelClone();
  147. var blob = new Blob([angular.toJson(clone, true)], { type: "application/json;charset=utf-8" });
  148. var wnd: any = window;
  149. wnd.saveAs(blob, $scope.dashboard.title + '-' + new Date().getTime());
  150. };
  151. $scope.snapshot = function() {
  152. $scope.dashboard.snapshot = true;
  153. $rootScope.$broadcast('refresh');
  154. $timeout(function() {
  155. $scope.exportDashboard();
  156. $scope.dashboard.snapshot = false;
  157. $scope.appEvent('dashboard-snapshot-cleanup');
  158. }, 1000);
  159. };
  160. $scope.editJson = function() {
  161. var clone = $scope.dashboard.getSaveModelClone();
  162. $scope.appEvent('show-json-editor', { object: clone });
  163. };
  164. $scope.stopPlaylist = function() {
  165. playlistSrv.stop(1);
  166. };
  167. $scope.init();
  168. }
  169. }
  170. export function dashNavDirective() {
  171. return {
  172. restrict: 'E',
  173. templateUrl: 'public/app/features/dashboard/dashnav/dashnav.html',
  174. controller: DashNavCtrl,
  175. transclude: true,
  176. };
  177. }
  178. angular.module('grafana.directives').directive('dashnav', dashNavDirective);