DashboardViewStateSrv.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import angular from 'angular';
  2. import _ from 'lodash';
  3. import config from 'app/core/config';
  4. import appEvents from 'app/core/app_events';
  5. import { DashboardModel } from '../state/DashboardModel';
  6. // represents the transient view state
  7. // like fullscreen panel & edit
  8. export class DashboardViewStateSrv {
  9. state: any;
  10. panelScopes: any;
  11. $scope: any;
  12. dashboard: DashboardModel;
  13. fullscreenPanel: any;
  14. oldTimeRange: any;
  15. /** @ngInject */
  16. constructor($scope, private $location, private $timeout) {
  17. const self = this;
  18. self.state = {};
  19. self.panelScopes = [];
  20. self.$scope = $scope;
  21. self.dashboard = $scope.dashboard;
  22. $scope.onAppEvent('$routeUpdate', () => {
  23. const urlState = self.getQueryStringState();
  24. if (self.needsSync(urlState)) {
  25. self.update(urlState, true);
  26. }
  27. });
  28. $scope.onAppEvent('panel-change-view', (evt, payload) => {
  29. self.update(payload);
  30. });
  31. // this marks changes to location during this digest cycle as not to add history item
  32. // don't want url changes like adding orgId to add browser history
  33. $location.replace();
  34. this.update(this.getQueryStringState());
  35. }
  36. needsSync(urlState) {
  37. return _.isEqual(this.state, urlState) === false;
  38. }
  39. getQueryStringState() {
  40. const state = this.$location.search();
  41. state.panelId = parseInt(state.panelId, 10) || null;
  42. state.fullscreen = state.fullscreen ? true : null;
  43. state.edit = state.edit === 'true' || state.edit === true || null;
  44. state.editview = state.editview || null;
  45. state.orgId = config.bootData.user.orgId;
  46. return state;
  47. }
  48. serializeToUrl() {
  49. const urlState = _.clone(this.state);
  50. urlState.fullscreen = this.state.fullscreen ? true : null;
  51. urlState.edit = this.state.edit ? true : null;
  52. return urlState;
  53. }
  54. update(state, fromRouteUpdated?) {
  55. // implement toggle logic
  56. if (state.toggle) {
  57. delete state.toggle;
  58. if (this.state.fullscreen && state.fullscreen) {
  59. if (this.state.edit === state.edit) {
  60. state.fullscreen = !state.fullscreen;
  61. }
  62. }
  63. }
  64. _.extend(this.state, state);
  65. if (!this.state.fullscreen) {
  66. this.state.fullscreen = null;
  67. this.state.edit = null;
  68. // clear panel id unless in solo mode
  69. if (!this.dashboard.meta.soloMode) {
  70. this.state.panelId = null;
  71. }
  72. }
  73. if ((this.state.fullscreen || this.dashboard.meta.soloMode) && this.state.panelId) {
  74. // Trying to render panel in fullscreen when it's in the collapsed row causes an issue.
  75. // So in this case expand collapsed row first.
  76. this.toggleCollapsedPanelRow(this.state.panelId);
  77. }
  78. // if no edit state cleanup tab parm
  79. if (!this.state.edit) {
  80. delete this.state.tab;
  81. }
  82. // do not update url params if we are here
  83. // from routeUpdated event
  84. if (fromRouteUpdated !== true) {
  85. this.$location.search(this.serializeToUrl());
  86. }
  87. }
  88. toggleCollapsedPanelRow(panelId) {
  89. for (const panel of this.dashboard.panels) {
  90. if (panel.collapsed) {
  91. for (const rowPanel of panel.panels) {
  92. if (rowPanel.id === panelId) {
  93. this.dashboard.toggleRow(panel);
  94. return;
  95. }
  96. }
  97. }
  98. }
  99. }
  100. leaveFullscreen() {
  101. const panel = this.fullscreenPanel;
  102. this.dashboard.setViewMode(panel, false, false);
  103. delete this.fullscreenPanel;
  104. this.$timeout(() => {
  105. appEvents.emit('dash-scroll', { restore: true });
  106. if (this.oldTimeRange !== this.dashboard.time) {
  107. this.dashboard.startRefresh();
  108. } else {
  109. this.dashboard.render();
  110. }
  111. });
  112. }
  113. enterFullscreen(panel) {
  114. const isEditing = this.state.edit && this.dashboard.meta.canEdit;
  115. this.oldTimeRange = this.dashboard.time;
  116. this.fullscreenPanel = panel;
  117. // Firefox doesn't return scrollTop position properly if 'dash-scroll' is emitted after setViewMode()
  118. this.$scope.appEvent('dash-scroll', { animate: false, pos: 0 });
  119. this.dashboard.setViewMode(panel, true, isEditing);
  120. }
  121. }
  122. /** @ngInject */
  123. export function dashboardViewStateSrv($location, $timeout) {
  124. return {
  125. create: $scope => {
  126. return new DashboardViewStateSrv($scope, $location, $timeout);
  127. },
  128. };
  129. }
  130. angular.module('grafana.services').factory('dashboardViewStateSrv', dashboardViewStateSrv);