dashnav.ts 6.1 KB

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