dashnav.ts 4.7 KB

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