dashnav.ts 6.5 KB

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