view_state_srv.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 (
  101. this.fullscreenPanel === panelScope &&
  102. this.editStateChanged === false
  103. ) {
  104. return;
  105. } else {
  106. this.leaveFullscreen(false);
  107. }
  108. }
  109. if (!panelScope.ctrl.editModeInitiated) {
  110. panelScope.ctrl.initEditMode();
  111. }
  112. if (!panelScope.ctrl.fullscreen) {
  113. this.enterFullscreen(panelScope);
  114. }
  115. } else if (this.fullscreenPanel) {
  116. this.leaveFullscreen(true);
  117. }
  118. }
  119. getPanelScope(id) {
  120. return _.find(this.panelScopes, function(panelScope) {
  121. return panelScope.ctrl.panel.id === id;
  122. });
  123. }
  124. leaveFullscreen(render) {
  125. var self = this;
  126. var ctrl = self.fullscreenPanel.ctrl;
  127. ctrl.editMode = false;
  128. ctrl.fullscreen = false;
  129. this.dashboard.setViewMode(ctrl.panel, false, false);
  130. this.$scope.appEvent("panel-fullscreen-exit", { panelId: ctrl.panel.id });
  131. if (!render) {
  132. return false;
  133. }
  134. this.$timeout(function() {
  135. if (self.oldTimeRange !== ctrl.range) {
  136. self.$rootScope.$broadcast("refresh");
  137. } else {
  138. self.$rootScope.$broadcast("render");
  139. }
  140. delete self.fullscreenPanel;
  141. });
  142. return true;
  143. }
  144. enterFullscreen(panelScope) {
  145. var ctrl = panelScope.ctrl;
  146. ctrl.editMode = this.state.edit && this.dashboard.meta.canEdit;
  147. ctrl.fullscreen = true;
  148. this.oldTimeRange = ctrl.range;
  149. this.fullscreenPanel = panelScope;
  150. this.dashboard.setViewMode(ctrl.panel, true, ctrl.editMode);
  151. this.$scope.appEvent("panel-fullscreen-enter", { panelId: ctrl.panel.id });
  152. }
  153. registerPanel(panelScope) {
  154. var self = this;
  155. self.panelScopes.push(panelScope);
  156. if (!self.dashboard.meta.soloMode) {
  157. if (self.state.panelId === panelScope.ctrl.panel.id) {
  158. if (self.state.edit) {
  159. panelScope.ctrl.editPanel();
  160. } else {
  161. panelScope.ctrl.viewPanel();
  162. }
  163. }
  164. }
  165. var unbind = panelScope.$on("$destroy", function() {
  166. self.panelScopes = _.without(self.panelScopes, panelScope);
  167. unbind();
  168. });
  169. }
  170. }
  171. /** @ngInject */
  172. export function dashboardViewStateSrv($location, $timeout, $rootScope) {
  173. return {
  174. create: function($scope) {
  175. return new DashboardViewState($scope, $location, $timeout, $rootScope);
  176. }
  177. };
  178. }
  179. angular
  180. .module("grafana.services")
  181. .factory("dashboardViewStateSrv", dashboardViewStateSrv);