view_state_srv.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. import angular from 'angular';
  2. import _ from 'lodash';
  3. import config from 'app/core/config';
  4. import { DashboardModel } from './dashboard_model';
  5. // represents the transient view state
  6. // like fullscreen panel & edit
  7. export class DashboardViewState {
  8. state: any;
  9. panelScopes: any;
  10. $scope: any;
  11. dashboard: DashboardModel;
  12. editStateChanged: any;
  13. fullscreenPanel: any;
  14. oldTimeRange: any;
  15. /** @ngInject */
  16. constructor($scope, private $location, private $timeout, private $rootScope) {
  17. var self = this;
  18. self.state = {};
  19. self.panelScopes = [];
  20. self.$scope = $scope;
  21. self.dashboard = $scope.dashboard;
  22. $scope.onAppEvent('$routeUpdate', function() {
  23. var urlState = self.getQueryStringState();
  24. if (self.needsSync(urlState)) {
  25. self.update(urlState, true);
  26. }
  27. });
  28. $scope.onAppEvent('panel-change-view', function(evt, payload) {
  29. self.update(payload);
  30. });
  31. $scope.onAppEvent('panel-initialized', function(evt, payload) {
  32. self.registerPanel(payload.scope);
  33. });
  34. // this marks changes to location during this digest cycle as not to add history item
  35. // dont want url changes like adding orgId to add browser history
  36. $location.replace();
  37. this.update(this.getQueryStringState());
  38. }
  39. needsSync(urlState) {
  40. return _.isEqual(this.state, urlState) === false;
  41. }
  42. getQueryStringState() {
  43. var state = this.$location.search();
  44. state.panelId = parseInt(state.panelId) || null;
  45. state.fullscreen = state.fullscreen ? true : null;
  46. state.edit = state.edit === 'true' || state.edit === true || null;
  47. state.editview = state.editview || null;
  48. state.orgId = config.bootData.user.orgId;
  49. return state;
  50. }
  51. serializeToUrl() {
  52. var urlState = _.clone(this.state);
  53. urlState.fullscreen = this.state.fullscreen ? true : null;
  54. urlState.edit = this.state.edit ? true : null;
  55. return urlState;
  56. }
  57. update(state, fromRouteUpdated?) {
  58. // implement toggle logic
  59. if (state.toggle) {
  60. delete state.toggle;
  61. if (this.state.fullscreen && state.fullscreen) {
  62. if (this.state.edit === state.edit) {
  63. state.fullscreen = !state.fullscreen;
  64. }
  65. }
  66. }
  67. // remember if editStateChanged
  68. this.editStateChanged = (state.edit || false) !== (this.state.edit || false);
  69. _.extend(this.state, state);
  70. this.dashboard.meta.fullscreen = this.state.fullscreen;
  71. if (!this.state.fullscreen) {
  72. this.state.fullscreen = null;
  73. this.state.edit = null;
  74. // clear panel id unless in solo mode
  75. if (!this.dashboard.meta.soloMode) {
  76. this.state.panelId = null;
  77. }
  78. }
  79. if ((this.state.fullscreen || this.dashboard.meta.soloMode) && this.state.panelId) {
  80. // Trying to render panel in fullscreen when it's in the collapsed row causes an issue.
  81. // So in this case expand collapsed row first.
  82. this.toggleCollapsedPanelRow(this.state.panelId);
  83. }
  84. // if no edit state cleanup tab parm
  85. if (!this.state.edit) {
  86. delete this.state.tab;
  87. }
  88. // do not update url params if we are here
  89. // from routeUpdated event
  90. if (fromRouteUpdated !== true) {
  91. this.$location.search(this.serializeToUrl());
  92. }
  93. this.syncState();
  94. }
  95. toggleCollapsedPanelRow(panelId) {
  96. for (let panel of this.dashboard.panels) {
  97. if (panel.collapsed) {
  98. for (let rowPanel of panel.panels) {
  99. if (rowPanel.id === panelId) {
  100. this.dashboard.toggleRow(panel);
  101. return;
  102. }
  103. }
  104. }
  105. }
  106. }
  107. syncState() {
  108. if (this.panelScopes.length === 0) {
  109. return;
  110. }
  111. if (this.dashboard.meta.fullscreen) {
  112. var panelScope = this.getPanelScope(this.state.panelId);
  113. if (!panelScope) {
  114. return;
  115. }
  116. if (this.fullscreenPanel) {
  117. // if already fullscreen
  118. if (this.fullscreenPanel === panelScope && this.editStateChanged === false) {
  119. return;
  120. } else {
  121. this.leaveFullscreen(false);
  122. }
  123. }
  124. if (!panelScope.ctrl.editModeInitiated) {
  125. panelScope.ctrl.initEditMode();
  126. }
  127. if (!panelScope.ctrl.fullscreen) {
  128. this.enterFullscreen(panelScope);
  129. }
  130. } else if (this.fullscreenPanel) {
  131. this.leaveFullscreen(true);
  132. }
  133. }
  134. getPanelScope(id) {
  135. return _.find(this.panelScopes, function(panelScope) {
  136. return panelScope.ctrl.panel.id === id;
  137. });
  138. }
  139. leaveFullscreen(render) {
  140. var self = this;
  141. var ctrl = self.fullscreenPanel.ctrl;
  142. ctrl.editMode = false;
  143. ctrl.fullscreen = false;
  144. this.dashboard.setViewMode(ctrl.panel, false, false);
  145. this.$scope.appEvent('panel-fullscreen-exit', { panelId: ctrl.panel.id });
  146. this.$scope.appEvent('dash-scroll', { restore: true });
  147. if (!render) {
  148. return false;
  149. }
  150. this.$timeout(function() {
  151. if (self.oldTimeRange !== ctrl.range) {
  152. self.$rootScope.$broadcast('refresh');
  153. } else {
  154. self.$rootScope.$broadcast('render');
  155. }
  156. delete self.fullscreenPanel;
  157. });
  158. return true;
  159. }
  160. enterFullscreen(panelScope) {
  161. var ctrl = panelScope.ctrl;
  162. ctrl.editMode = this.state.edit && this.dashboard.meta.canEdit;
  163. ctrl.fullscreen = true;
  164. this.oldTimeRange = ctrl.range;
  165. this.fullscreenPanel = panelScope;
  166. this.dashboard.setViewMode(ctrl.panel, true, ctrl.editMode);
  167. this.$scope.appEvent('panel-fullscreen-enter', { panelId: ctrl.panel.id });
  168. this.$scope.appEvent('dash-scroll', { animate: false, pos: 0 });
  169. }
  170. registerPanel(panelScope) {
  171. var self = this;
  172. self.panelScopes.push(panelScope);
  173. if (!self.dashboard.meta.soloMode) {
  174. if (self.state.panelId === panelScope.ctrl.panel.id) {
  175. if (self.state.edit) {
  176. panelScope.ctrl.editPanel();
  177. } else {
  178. panelScope.ctrl.viewPanel();
  179. }
  180. }
  181. }
  182. var unbind = panelScope.$on('$destroy', function() {
  183. self.panelScopes = _.without(self.panelScopes, panelScope);
  184. unbind();
  185. });
  186. }
  187. }
  188. /** @ngInject */
  189. export function dashboardViewStateSrv($location, $timeout, $rootScope) {
  190. return {
  191. create: function($scope) {
  192. return new DashboardViewState($scope, $location, $timeout, $rootScope);
  193. },
  194. };
  195. }
  196. angular.module('grafana.services').factory('dashboardViewStateSrv', dashboardViewStateSrv);