viewStateSrv.js 6.2 KB

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