dashnav.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. navModelSrv) {
  20. this.navModel = navModelSrv.getDashboardNav(this.dashboard, this);
  21. appEvents.on('save-dashboard', this.saveDashboard.bind(this), $scope);
  22. appEvents.on('delete-dashboard', this.deleteDashboard.bind(this), $scope);
  23. if (this.dashboard.meta.isSnapshot) {
  24. var meta = this.dashboard.meta;
  25. this.titleTooltip = 'Created: &nbsp;' + moment(meta.created).calendar();
  26. if (meta.expires) {
  27. this.titleTooltip += '<br>Expires: &nbsp;' + moment(meta.expires).fromNow() + '<br>';
  28. }
  29. }
  30. }
  31. toggleSideMenu() {
  32. this.contextSrv.toggleSideMenu();
  33. }
  34. openEditView(editview) {
  35. var search = _.extend(this.$location.search(), {editview: editview});
  36. this.$location.search(search);
  37. }
  38. showHelpModal() {
  39. appEvents.emit('show-modal', {templateHtml: '<help-modal></help-modal>'});
  40. }
  41. starDashboard() {
  42. if (this.dashboard.meta.isStarred) {
  43. return this.backendSrv.delete('/api/user/stars/dashboard/' + this.dashboard.id).then(() => {
  44. this.dashboard.meta.isStarred = false;
  45. });
  46. }
  47. this.backendSrv.post('/api/user/stars/dashboard/' + this.dashboard.id).then(() => {
  48. this.dashboard.meta.isStarred = true;
  49. });
  50. }
  51. shareDashboard(tabIndex) {
  52. var modalScope = this.$scope.$new();
  53. modalScope.tabIndex = tabIndex;
  54. modalScope.dashboard = this.dashboard;
  55. appEvents.emit('show-modal', {
  56. src: 'public/app/features/dashboard/partials/shareModal.html',
  57. scope: modalScope
  58. });
  59. }
  60. hideTooltip(evt) {
  61. angular.element(evt.currentTarget).tooltip('hide');
  62. }
  63. makeEditable() {
  64. this.dashboard.editable = true;
  65. return this.dashboardSrv.saveDashboard({makeEditable: true, overwrite: false}).then(() => {
  66. // force refresh whole page
  67. window.location.href = window.location.href;
  68. });
  69. }
  70. exitFullscreen() {
  71. this.$rootScope.appEvent('panel-change-view', {fullscreen: false, edit: false});
  72. }
  73. saveDashboard() {
  74. return this.dashboardSrv.saveDashboard();
  75. }
  76. deleteDashboard() {
  77. var confirmText = "";
  78. var text2 = this.dashboard.title;
  79. var alerts = this.dashboard.rows.reduce((memo, row) => {
  80. memo += row.panels.filter(panel => panel.alert).length;
  81. return memo;
  82. }, 0);
  83. if (alerts > 0) {
  84. confirmText = 'DELETE';
  85. text2 = `This dashboad contains ${alerts} alerts. Deleting this dashboad will also delete those alerts`;
  86. }
  87. appEvents.emit('confirm-modal', {
  88. title: 'Delete',
  89. text: 'Do you want to delete this dashboard?',
  90. text2: text2,
  91. icon: 'fa-trash',
  92. confirmText: confirmText,
  93. yesText: 'Delete',
  94. onConfirm: () => {
  95. this.dashboard.meta.canSave = false;
  96. this.deleteDashboardConfirmed();
  97. }
  98. });
  99. }
  100. deleteDashboardConfirmed() {
  101. this.backendSrv.delete('/api/dashboards/db/' + this.dashboard.meta.slug).then(() => {
  102. appEvents.emit('alert-success', ['Dashboard Deleted', this.dashboard.title + ' has been deleted']);
  103. this.$location.url('/');
  104. });
  105. }
  106. saveDashboardAs() {
  107. return this.dashboardSrv.showSaveAsModal();
  108. }
  109. viewJson() {
  110. var clone = this.dashboard.getSaveModelClone();
  111. this.$rootScope.appEvent('show-json-editor', {
  112. object: clone,
  113. });
  114. }
  115. showSearch() {
  116. this.$rootScope.appEvent('show-dash-search');
  117. }
  118. navItemClicked(navItem, evt) {
  119. if (navItem.clickHandler) {
  120. navItem.clickHandler();
  121. evt.preventDefault();
  122. }
  123. }
  124. }
  125. export function dashNavDirective() {
  126. return {
  127. restrict: 'E',
  128. templateUrl: 'public/app/features/dashboard/dashnav/dashnav.html',
  129. controller: DashNavCtrl,
  130. bindToController: true,
  131. controllerAs: 'ctrl',
  132. transclude: true,
  133. scope: { dashboard: "=" }
  134. };
  135. }
  136. angular.module('grafana.directives').directive('dashnav', dashNavDirective);