viewStateSrv.js 6.2 KB

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