dashnav.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import _ from 'lodash';
  2. import moment from 'moment';
  3. import angular from 'angular';
  4. import {appEvents, NavModel} from 'app/core/core';
  5. import {DashboardModel} from '../dashboard_model';
  6. export class DashNavCtrl {
  7. dashboard: DashboardModel;
  8. navModel: NavModel;
  9. titleTooltip: string;
  10. /** @ngInject */
  11. constructor(
  12. private $scope,
  13. private $rootScope,
  14. private dashboardSrv,
  15. private $location,
  16. private backendSrv,
  17. public playlistSrv,
  18. navModelSrv) {
  19. this.navModel = navModelSrv.getDashboardNav(this.dashboard, this);
  20. appEvents.on('save-dashboard', this.saveDashboard.bind(this), $scope);
  21. appEvents.on('delete-dashboard', this.deleteDashboard.bind(this), $scope);
  22. if (this.dashboard.meta.isSnapshot) {
  23. var meta = this.dashboard.meta;
  24. this.titleTooltip = 'Created:  ' + moment(meta.created).calendar();
  25. if (meta.expires) {
  26. this.titleTooltip += '<br>Expires: &nbsp;' + moment(meta.expires).fromNow() + '<br>';
  27. }
  28. }
  29. }
  30. toggleSettings() {
  31. let search = this.$location.search();
  32. if (search.editview) {
  33. delete search.editview;
  34. } else {
  35. search.editview = 'settings';
  36. }
  37. this.$location.search(search);
  38. }
  39. close() {
  40. let search = this.$location.search();
  41. if (search.editview) {
  42. delete search.editview;
  43. }
  44. if (search.fullscreen) {
  45. delete search.fullscreen;
  46. delete search.edit;
  47. }
  48. this.$location.search(search);
  49. }
  50. starDashboard() {
  51. this.dashboardSrv.starDashboard(this.dashboard.id, this.dashboard.meta.isStarred)
  52. .then(newState => {
  53. this.dashboard.meta.isStarred = newState;
  54. });
  55. }
  56. shareDashboard(tabIndex) {
  57. var modalScope = this.$scope.$new();
  58. modalScope.tabIndex = tabIndex;
  59. modalScope.dashboard = this.dashboard;
  60. appEvents.emit('show-modal', {
  61. src: 'public/app/features/dashboard/partials/shareModal.html',
  62. scope: modalScope
  63. });
  64. }
  65. hideTooltip(evt) {
  66. angular.element(evt.currentTarget).tooltip('hide');
  67. }
  68. makeEditable() {
  69. this.dashboard.editable = true;
  70. return this.dashboardSrv.saveDashboard({makeEditable: true, overwrite: false}).then(() => {
  71. // force refresh whole page
  72. window.location.href = window.location.href;
  73. });
  74. }
  75. exitFullscreen() {
  76. this.$rootScope.appEvent('panel-change-view', {fullscreen: false, edit: false});
  77. }
  78. saveDashboard() {
  79. return this.dashboardSrv.saveDashboard();
  80. }
  81. deleteDashboard() {
  82. var confirmText = '';
  83. var text2 = this.dashboard.title;
  84. const alerts = _.sumBy(this.dashboard.panels, panel => {
  85. return panel.alert ? 1 : 0;
  86. });
  87. if (alerts > 0) {
  88. confirmText = 'DELETE';
  89. text2 = `This dashboard contains ${alerts} alerts. Deleting this dashboard will also delete those alerts`;
  90. }
  91. appEvents.emit('confirm-modal', {
  92. title: 'Delete',
  93. text: 'Do you want to delete this dashboard?',
  94. text2: text2,
  95. icon: 'fa-trash',
  96. confirmText: confirmText,
  97. yesText: 'Delete',
  98. onConfirm: () => {
  99. this.dashboard.meta.canSave = false;
  100. this.deleteDashboardConfirmed();
  101. }
  102. });
  103. }
  104. deleteDashboardConfirmed() {
  105. this.backendSrv.delete('/api/dashboards/db/' + this.dashboard.meta.slug).then(() => {
  106. appEvents.emit('alert-success', ['Dashboard Deleted', this.dashboard.title + ' has been deleted']);
  107. this.$location.url('/');
  108. });
  109. }
  110. saveDashboardAs() {
  111. return this.dashboardSrv.showSaveAsModal();
  112. }
  113. viewJson() {
  114. var clone = this.dashboard.getSaveModelClone();
  115. this.$rootScope.appEvent('show-json-editor', {
  116. object: clone,
  117. });
  118. }
  119. onFolderChange(folderId) {
  120. this.dashboard.folderId = folderId;
  121. }
  122. showSearch() {
  123. this.$rootScope.appEvent('show-dash-search');
  124. }
  125. addPanel() {
  126. if (this.dashboard.panels.length > 0 && this.dashboard.panels[0].type === 'add-panel') {
  127. this.dashboard.removePanel(this.dashboard.panels[0]);
  128. return;
  129. }
  130. this.dashboard.addPanel({
  131. type: 'add-panel',
  132. gridPos: {x: 0, y: 0, w: 12, h: 9},
  133. title: 'Panel Title',
  134. });
  135. }
  136. navItemClicked(navItem, evt) {
  137. if (navItem.clickHandler) {
  138. navItem.clickHandler();
  139. evt.preventDefault();
  140. }
  141. }
  142. }
  143. export function dashNavDirective() {
  144. return {
  145. restrict: 'E',
  146. templateUrl: 'public/app/features/dashboard/dashnav/dashnav.html',
  147. controller: DashNavCtrl,
  148. bindToController: true,
  149. controllerAs: 'ctrl',
  150. transclude: true,
  151. scope: { dashboard: "=" }
  152. };
  153. }
  154. angular.module('grafana.directives').directive('dashnav', dashNavDirective);