view_state_srv.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 './dashboard_model';
  6. // represents the transient view state
  7. // like fullscreen panel & edit
  8. export class DashboardViewState {
  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. this.dashboard.meta.fullscreen = this.state.fullscreen;
  66. if (!this.state.fullscreen) {
  67. this.state.fullscreen = null;
  68. this.state.edit = null;
  69. // clear panel id unless in solo mode
  70. if (!this.dashboard.meta.soloMode) {
  71. this.state.panelId = null;
  72. }
  73. }
  74. if ((this.state.fullscreen || this.dashboard.meta.soloMode) && this.state.panelId) {
  75. // Trying to render panel in fullscreen when it's in the collapsed row causes an issue.
  76. // So in this case expand collapsed row first.
  77. this.toggleCollapsedPanelRow(this.state.panelId);
  78. }
  79. // if no edit state cleanup tab parm
  80. if (!this.state.edit) {
  81. delete this.state.tab;
  82. }
  83. // do not update url params if we are here
  84. // from routeUpdated event
  85. if (fromRouteUpdated !== true) {
  86. this.$location.search(this.serializeToUrl());
  87. }
  88. this.syncState();
  89. }
  90. toggleCollapsedPanelRow(panelId) {
  91. for (const panel of this.dashboard.panels) {
  92. if (panel.collapsed) {
  93. for (const rowPanel of panel.panels) {
  94. if (rowPanel.id === panelId) {
  95. this.dashboard.toggleRow(panel);
  96. return;
  97. }
  98. }
  99. }
  100. }
  101. }
  102. syncState() {
  103. if (this.dashboard.meta.fullscreen) {
  104. const panel = this.dashboard.getPanelById(this.state.panelId);
  105. if (!panel) {
  106. return;
  107. }
  108. if (!panel.fullscreen) {
  109. this.enterFullscreen(panel);
  110. } else if (this.dashboard.meta.isEditing !== this.state.edit) {
  111. this.dashboard.setViewMode(panel, this.state.fullscreen, this.state.edit);
  112. }
  113. } else if (this.fullscreenPanel) {
  114. this.leaveFullscreen();
  115. }
  116. }
  117. leaveFullscreen() {
  118. const panel = this.fullscreenPanel;
  119. this.dashboard.setViewMode(panel, false, false);
  120. delete this.fullscreenPanel;
  121. this.$timeout(() => {
  122. appEvents.emit('dash-scroll', { restore: true });
  123. if (this.oldTimeRange !== this.dashboard.time) {
  124. this.dashboard.startRefresh();
  125. } else {
  126. this.dashboard.render();
  127. }
  128. });
  129. }
  130. enterFullscreen(panel) {
  131. const isEditing = this.state.edit && this.dashboard.meta.canEdit;
  132. this.oldTimeRange = this.dashboard.time;
  133. this.fullscreenPanel = panel;
  134. // Firefox doesn't return scrollTop position properly if 'dash-scroll' is emitted after setViewMode()
  135. this.$scope.appEvent('dash-scroll', { animate: false, pos: 0 });
  136. this.dashboard.setViewMode(panel, true, isEditing);
  137. }
  138. }
  139. /** @ngInject */
  140. export function dashboardViewStateSrv($location, $timeout) {
  141. return {
  142. create: $scope => {
  143. return new DashboardViewState($scope, $location, $timeout);
  144. },
  145. };
  146. }
  147. angular.module('grafana.services').factory('dashboardViewStateSrv', dashboardViewStateSrv);