view_state_srv.ts 5.6 KB

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