dashnav.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. 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. const alerts = _.sumBy(this.dashboard.panels, panel => {
  81. return panel.alert ? 1 : 0;
  82. });
  83. if (alerts > 0) {
  84. confirmText = 'DELETE';
  85. text2 = `This dashboard contains ${alerts} alerts. Deleting this dashboard 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. onFolderChange(folderId) {
  116. this.dashboard.folderId = folderId;
  117. }
  118. showSearch() {
  119. this.$rootScope.appEvent('show-dash-search');
  120. }
  121. addPanel() {
  122. if (this.dashboard.panels.length > 0 && this.dashboard.panels[0].type === 'add-panel') {
  123. this.dashboard.removePanel(this.dashboard.panels[0]);
  124. return;
  125. }
  126. this.dashboard.addPanel({
  127. type: 'add-panel',
  128. gridPos: {x: 0, y: 0, w: 12, h: 9},
  129. title: 'New Graph',
  130. });
  131. }
  132. navItemClicked(navItem, evt) {
  133. if (navItem.clickHandler) {
  134. navItem.clickHandler();
  135. evt.preventDefault();
  136. }
  137. }
  138. }
  139. export function dashNavDirective() {
  140. return {
  141. restrict: 'E',
  142. templateUrl: 'public/app/features/dashboard/dashnav/dashnav.html',
  143. controller: DashNavCtrl,
  144. bindToController: true,
  145. controllerAs: 'ctrl',
  146. transclude: true,
  147. scope: { dashboard: "=" }
  148. };
  149. }
  150. angular.module('grafana.directives').directive('dashnav', dashNavDirective);