dashnav.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 '../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. var alerts = this.dashboard.rows.reduce((memo, row) => {
  77. memo += row.panels.filter(panel => panel.alert).length;
  78. return memo;
  79. }, 0);
  80. if (alerts > 0) {
  81. confirmText = 'DELETE';
  82. text2 = `This dashboad contains ${alerts} alerts. Deleting this dashboad will also delete those alerts`;
  83. }
  84. appEvents.emit('confirm-modal', {
  85. title: 'Delete',
  86. text: 'Do you want to delete this dashboard?',
  87. text2: text2,
  88. icon: 'fa-trash',
  89. confirmText: confirmText,
  90. yesText: 'Delete',
  91. onConfirm: () => {
  92. this.dashboard.meta.canSave = false;
  93. this.deleteDashboardConfirmed();
  94. }
  95. });
  96. }
  97. deleteDashboardConfirmed() {
  98. this.backendSrv.delete('/api/dashboards/db/' + this.dashboard.meta.slug).then(() => {
  99. appEvents.emit('alert-success', ['Dashboard Deleted', this.dashboard.title + ' has been deleted']);
  100. this.$location.url('/');
  101. });
  102. }
  103. saveDashboardAs() {
  104. return this.dashboardSrv.showSaveAsModal();
  105. }
  106. viewJson() {
  107. var clone = this.dashboard.getSaveModelClone();
  108. this.$rootScope.appEvent('show-json-editor', {
  109. object: clone,
  110. });
  111. }
  112. showSearch() {
  113. this.$rootScope.appEvent('show-dash-search');
  114. }
  115. navItemClicked(navItem, evt) {
  116. if (navItem.clickHandler) {
  117. navItem.clickHandler();
  118. evt.preventDefault();
  119. }
  120. }
  121. }
  122. export function dashNavDirective() {
  123. return {
  124. restrict: 'E',
  125. templateUrl: 'public/app/features/dashboard/dashnav/dashnav.html',
  126. controller: DashNavCtrl,
  127. bindToController: true,
  128. controllerAs: 'ctrl',
  129. transclude: true,
  130. scope: { dashboard: "=" }
  131. };
  132. }
  133. angular.module('grafana.directives').directive('dashnav', dashNavDirective);