viewStateSrv.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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('$routeUpdate', function() {
  32. var urlState = self.getQueryStringState();
  33. if (self.needsSync(urlState)) {
  34. self.update(urlState, true);
  35. }
  36. });
  37. $scope.onAppEvent('panel-change-view', function(evt, payload) {
  38. self.update(payload);
  39. });
  40. $scope.onAppEvent('panel-initialized', function(evt, payload) {
  41. self.registerPanel(payload.scope);
  42. });
  43. this.update(this.getQueryStringState());
  44. this.expandRowForPanel();
  45. }
  46. DashboardViewState.prototype.expandRowForPanel = function() {
  47. if (!this.state.panelId) { return; }
  48. var panelInfo = this.$scope.dashboard.getPanelInfoById(this.state.panelId);
  49. if (panelInfo) {
  50. panelInfo.row.collapse = false;
  51. }
  52. };
  53. DashboardViewState.prototype.needsSync = function(urlState) {
  54. return _.isEqual(this.state, urlState) === false;
  55. };
  56. DashboardViewState.prototype.getQueryStringState = function() {
  57. var state = $location.search();
  58. state.panelId = parseInt(state.panelId) || null;
  59. state.fullscreen = state.fullscreen ? true : null;
  60. state.edit = (state.edit === "true" || state.edit === true) || null;
  61. state.editview = state.editview || null;
  62. return state;
  63. };
  64. DashboardViewState.prototype.serializeToUrl = function() {
  65. var urlState = _.clone(this.state);
  66. urlState.fullscreen = this.state.fullscreen ? true : null;
  67. urlState.edit = this.state.edit ? true : null;
  68. return urlState;
  69. };
  70. DashboardViewState.prototype.update = function(state) {
  71. _.extend(this.state, state);
  72. this.dashboard.meta.fullscreen = this.state.fullscreen;
  73. if (!this.state.fullscreen) {
  74. this.state.fullscreen = null;
  75. this.state.edit = null;
  76. // clear panel id unless in solo mode
  77. if (!this.dashboard.meta.soloMode) {
  78. this.state.panelId = null;
  79. }
  80. }
  81. // if no edit state cleanup tab parm
  82. if (!this.state.edit) {
  83. delete this.state.tab;
  84. }
  85. $location.search(this.serializeToUrl());
  86. this.syncState();
  87. };
  88. DashboardViewState.prototype.syncState = function() {
  89. if (this.panelScopes.length === 0) { return; }
  90. if (this.dashboard.meta.fullscreen) {
  91. var panelScope = this.getPanelScope(this.state.panelId);
  92. if (!panelScope) {
  93. return;
  94. }
  95. if (this.fullscreenPanel) {
  96. // if already fullscreen
  97. if (this.fullscreenPanel === panelScope) {
  98. return;
  99. } else {
  100. this.leaveFullscreen(false);
  101. }
  102. }
  103. if (!panelScope.ctrl.editModeInitiated) {
  104. panelScope.ctrl.initEditMode();
  105. }
  106. if (!panelScope.ctrl.fullscreen) {
  107. this.enterFullscreen(panelScope);
  108. }
  109. } else if (this.fullscreenPanel) {
  110. this.leaveFullscreen(true);
  111. }
  112. };
  113. DashboardViewState.prototype.getPanelScope = function(id) {
  114. return _.find(this.panelScopes, function(panelScope) {
  115. return panelScope.ctrl.panel.id === id;
  116. });
  117. };
  118. DashboardViewState.prototype.leaveFullscreen = function(render) {
  119. var self = this;
  120. var ctrl = self.fullscreenPanel.ctrl;
  121. ctrl.editMode = false;
  122. ctrl.fullscreen = false;
  123. this.$scope.appEvent('panel-fullscreen-exit', {panelId: ctrl.panel.id});
  124. if (!render) { return false;}
  125. $timeout(function() {
  126. if (self.oldTimeRange !== ctrl.range) {
  127. self.$scope.broadcastRefresh();
  128. }
  129. else {
  130. ctrl.render();
  131. }
  132. delete self.fullscreenPanel;
  133. });
  134. };
  135. DashboardViewState.prototype.enterFullscreen = function(panelScope) {
  136. var ctrl = panelScope.ctrl;
  137. ctrl.editMode = this.state.edit && this.dashboard.meta.canEdit;
  138. ctrl.fullscreen = true;
  139. this.oldTimeRange = ctrl.range;
  140. this.fullscreenPanel = panelScope;
  141. $(window).scrollTop(0);
  142. this.$scope.appEvent('panel-fullscreen-enter', {panelId: ctrl.panel.id});
  143. $timeout(function() {
  144. ctrl.render();
  145. });
  146. };
  147. DashboardViewState.prototype.registerPanel = function(panelScope) {
  148. var self = this;
  149. self.panelScopes.push(panelScope);
  150. if (!self.dashboard.meta.soloMode) {
  151. if (self.state.panelId === panelScope.ctrl.panel.id) {
  152. if (self.state.edit) {
  153. panelScope.ctrl.editPanel();
  154. } else {
  155. panelScope.ctrl.viewPanel();
  156. }
  157. }
  158. }
  159. panelScope.$on('$destroy', function() {
  160. self.panelScopes = _.without(self.panelScopes, panelScope);
  161. });
  162. };
  163. return {
  164. create: function($scope) {
  165. return new DashboardViewState($scope);
  166. }
  167. };
  168. });
  169. });