viewStateSrv.js 6.2 KB

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