DashboardViewStateSrv.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 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. this.syncState();
  88. }
  89. toggleCollapsedPanelRow(panelId) {
  90. for (const panel of this.dashboard.panels) {
  91. if (panel.collapsed) {
  92. for (const rowPanel of panel.panels) {
  93. if (rowPanel.id === panelId) {
  94. this.dashboard.toggleRow(panel);
  95. return;
  96. }
  97. }
  98. }
  99. }
  100. }
  101. syncState() {
  102. if (this.state.fullscreen) {
  103. const panel = this.dashboard.getPanelById(this.state.panelId);
  104. if (!panel) {
  105. this.state.fullscreen = null;
  106. this.state.panelId = null;
  107. this.state.edit = null;
  108. this.update(this.state);
  109. setTimeout(() => {
  110. appEvents.emit('alert-error', ['Error', 'Panel not found']);
  111. }, 100);
  112. return;
  113. }
  114. if (!panel.fullscreen) {
  115. this.enterFullscreen(panel);
  116. } else if (this.dashboard.meta.isEditing !== this.state.edit) {
  117. this.dashboard.setViewMode(panel, this.state.fullscreen, this.state.edit);
  118. }
  119. } else if (this.fullscreenPanel) {
  120. this.leaveFullscreen();
  121. }
  122. }
  123. leaveFullscreen() {
  124. const panel = this.fullscreenPanel;
  125. this.dashboard.setViewMode(panel, false, false);
  126. delete this.fullscreenPanel;
  127. this.$timeout(() => {
  128. appEvents.emit('dash-scroll', { restore: true });
  129. if (this.oldTimeRange !== this.dashboard.time) {
  130. this.dashboard.startRefresh();
  131. } else {
  132. this.dashboard.render();
  133. }
  134. });
  135. }
  136. enterFullscreen(panel) {
  137. const isEditing = this.state.edit && this.dashboard.meta.canEdit;
  138. this.oldTimeRange = this.dashboard.time;
  139. this.fullscreenPanel = panel;
  140. // Firefox doesn't return scrollTop position properly if 'dash-scroll' is emitted after setViewMode()
  141. this.$scope.appEvent('dash-scroll', { animate: false, pos: 0 });
  142. this.dashboard.setViewMode(panel, true, isEditing);
  143. }
  144. }
  145. /** @ngInject */
  146. export function dashboardViewStateSrv($location, $timeout) {
  147. return {
  148. create: $scope => {
  149. return new DashboardViewStateSrv($scope, $location, $timeout);
  150. },
  151. };
  152. }
  153. angular.module('grafana.services').factory('dashboardViewStateSrv', dashboardViewStateSrv);