dashnav.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. import {DashboardExporter} from '../export/exporter';
  8. export class DashNavCtrl {
  9. dashboard: DashboardModel;
  10. navModel: NavModel;
  11. titleTooltip: string;
  12. /** @ngInject */
  13. constructor(
  14. private $scope,
  15. private $rootScope,
  16. private dashboardSrv,
  17. private $location,
  18. private playlistSrv,
  19. private backendSrv,
  20. private $timeout,
  21. private datasourceSrv,
  22. private navModelSrv,
  23. private contextSrv) {
  24. this.navModel = navModelSrv.getDashboardNav(this.dashboard, this);
  25. appEvents.on('save-dashboard', this.saveDashboard.bind(this), $scope);
  26. appEvents.on('delete-dashboard', this.deleteDashboard.bind(this), $scope);
  27. if (this.dashboard.meta.isSnapshot) {
  28. var meta = this.dashboard.meta;
  29. this.titleTooltip = 'Created: &nbsp;' + moment(meta.created).calendar();
  30. if (meta.expires) {
  31. this.titleTooltip += '<br>Expires: &nbsp;' + moment(meta.expires).fromNow() + '<br>';
  32. }
  33. }
  34. }
  35. toggleSideMenu() {
  36. this.contextSrv.toggleSideMenu();
  37. }
  38. openEditView(editview) {
  39. var search = _.extend(this.$location.search(), {editview: editview});
  40. this.$location.search(search);
  41. }
  42. showHelpModal() {
  43. appEvents.emit('show-modal', {templateHtml: '<help-modal></help-modal>'});
  44. }
  45. starDashboard() {
  46. if (this.dashboard.meta.isStarred) {
  47. return this.backendSrv.delete('/api/user/stars/dashboard/' + this.dashboard.id).then(() => {
  48. this.dashboard.meta.isStarred = false;
  49. });
  50. }
  51. this.backendSrv.post('/api/user/stars/dashboard/' + this.dashboard.id).then(() => {
  52. this.dashboard.meta.isStarred = true;
  53. });
  54. }
  55. shareDashboard(tabIndex) {
  56. var modalScope = this.$scope.$new();
  57. modalScope.tabIndex = tabIndex;
  58. modalScope.dashboard = this.dashboard;
  59. appEvents.emit('show-modal', {
  60. src: 'public/app/features/dashboard/partials/shareModal.html',
  61. scope: modalScope
  62. });
  63. }
  64. hideTooltip(evt) {
  65. angular.element(evt.currentTarget).tooltip('hide');
  66. }
  67. makeEditable() {
  68. this.dashboard.editable = true;
  69. return this.dashboardSrv.saveDashboard({makeEditable: true, overwrite: false}).then(() => {
  70. // force refresh whole page
  71. window.location.href = window.location.href;
  72. });
  73. }
  74. exitFullscreen() {
  75. this.$rootScope.appEvent('panel-change-view', {fullscreen: false, edit: false});
  76. }
  77. saveDashboard() {
  78. return this.dashboardSrv.saveDashboard();
  79. }
  80. deleteDashboard() {
  81. var confirmText = "";
  82. var text2 = this.dashboard.title;
  83. var alerts = this.dashboard.rows.reduce((memo, row) => {
  84. memo += row.panels.filter(panel => panel.alert).length;
  85. return memo;
  86. }, 0);
  87. if (alerts > 0) {
  88. confirmText = 'DELETE';
  89. text2 = `This dashboad contains ${alerts} alerts. Deleting this dashboad 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. var html = angular.toJson(clone, true);
  116. var uri = "data:application/json;charset=utf-8," + encodeURIComponent(html);
  117. var newWindow = window.open(uri);
  118. }
  119. showSearch() {
  120. this.$rootScope.appEvent('show-dash-search');
  121. }
  122. navItemClicked(navItem, evt) {
  123. if (navItem.clickHandler) {
  124. navItem.clickHandler();
  125. evt.preventDefault();
  126. }
  127. }
  128. }
  129. export function dashNavDirective() {
  130. return {
  131. restrict: 'E',
  132. templateUrl: 'public/app/features/dashboard/dashnav/dashnav.html',
  133. controller: DashNavCtrl,
  134. bindToController: true,
  135. controllerAs: 'ctrl',
  136. transclude: true,
  137. scope: { dashboard: "=" }
  138. };
  139. }
  140. angular.module('grafana.directives').directive('dashnav', dashNavDirective);