dashnav.ts 4.5 KB

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