viewStateSrv.js 6.1 KB

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