dashnav.ts 4.7 KB

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