dashnav.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. this.navModel = navModelSrv.getDashboardNav(this.dashboard, this);
  24. appEvents.on('save-dashboard', this.saveDashboard.bind(this), $scope);
  25. appEvents.on('delete-dashboard', this.deleteDashboard.bind(this), $scope);
  26. if (this.dashboard.meta.isSnapshot) {
  27. var meta = this.dashboard.meta;
  28. this.titleTooltip = 'Created: &nbsp;' + moment(meta.created).calendar();
  29. if (meta.expires) {
  30. this.titleTooltip += '<br>Expires: &nbsp;' + moment(meta.expires).fromNow() + '<br>';
  31. }
  32. }
  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 dashboard 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. var html = angular.toJson(clone, true);
  112. var uri = "data:application/json;charset=utf-8," + encodeURIComponent(html);
  113. var newWindow = window.open(uri);
  114. }
  115. onFolderChange(parentId) {
  116. this.dashboard.parentId = parentId;
  117. }
  118. }
  119. export function dashNavDirective() {
  120. return {
  121. restrict: 'E',
  122. templateUrl: 'public/app/features/dashboard/dashnav/dashnav.html',
  123. controller: DashNavCtrl,
  124. bindToController: true,
  125. controllerAs: 'ctrl',
  126. transclude: true,
  127. scope: { dashboard: "=" }
  128. };
  129. }
  130. angular.module('grafana.directives').directive('dashnav', dashNavDirective);