viewStateSrv.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. // remember if editStateChanged
  72. this.editStateChanged = state.edit !== this.state.edit;
  73. _.extend(this.state, state);
  74. this.dashboard.meta.fullscreen = this.state.fullscreen;
  75. if (!this.state.fullscreen) {
  76. this.state.fullscreen = null;
  77. this.state.edit = null;
  78. // clear panel id unless in solo mode
  79. if (!this.dashboard.meta.soloMode) {
  80. this.state.panelId = null;
  81. }
  82. }
  83. // if no edit state cleanup tab parm
  84. if (!this.state.edit) {
  85. delete this.state.tab;
  86. }
  87. $location.search(this.serializeToUrl());
  88. this.syncState();
  89. };
  90. DashboardViewState.prototype.syncState = function() {
  91. if (this.panelScopes.length === 0) { return; }
  92. if (this.dashboard.meta.fullscreen) {
  93. var panelScope = this.getPanelScope(this.state.panelId);
  94. if (!panelScope) {
  95. return;
  96. }
  97. if (this.fullscreenPanel) {
  98. // if already fullscreen
  99. if (this.fullscreenPanel === panelScope && this.editStateChanged === false) {
  100. return;
  101. } else {
  102. this.leaveFullscreen(false);
  103. }
  104. }
  105. if (!panelScope.ctrl.editModeInitiated) {
  106. panelScope.ctrl.initEditMode();
  107. }
  108. if (!panelScope.ctrl.fullscreen) {
  109. this.enterFullscreen(panelScope);
  110. }
  111. } else if (this.fullscreenPanel) {
  112. this.leaveFullscreen(true);
  113. }
  114. };
  115. DashboardViewState.prototype.getPanelScope = function(id) {
  116. return _.find(this.panelScopes, function(panelScope) {
  117. return panelScope.ctrl.panel.id === id;
  118. });
  119. };
  120. DashboardViewState.prototype.leaveFullscreen = function(render) {
  121. var self = this;
  122. var ctrl = self.fullscreenPanel.ctrl;
  123. ctrl.editMode = false;
  124. ctrl.fullscreen = false;
  125. this.$scope.appEvent('panel-fullscreen-exit', {panelId: ctrl.panel.id});
  126. if (!render) { return false;}
  127. $timeout(function() {
  128. if (self.oldTimeRange !== ctrl.range) {
  129. self.$scope.broadcastRefresh();
  130. }
  131. else {
  132. ctrl.render();
  133. }
  134. delete self.fullscreenPanel;
  135. });
  136. };
  137. DashboardViewState.prototype.enterFullscreen = function(panelScope) {
  138. var ctrl = panelScope.ctrl;
  139. ctrl.editMode = this.state.edit && this.dashboard.meta.canEdit;
  140. ctrl.fullscreen = true;
  141. this.oldTimeRange = ctrl.range;
  142. this.fullscreenPanel = panelScope;
  143. $(window).scrollTop(0);
  144. this.$scope.appEvent('panel-fullscreen-enter', {panelId: ctrl.panel.id});
  145. $timeout(function() {
  146. ctrl.render();
  147. });
  148. };
  149. DashboardViewState.prototype.registerPanel = function(panelScope) {
  150. var self = this;
  151. self.panelScopes.push(panelScope);
  152. if (!self.dashboard.meta.soloMode) {
  153. if (self.state.panelId === panelScope.ctrl.panel.id) {
  154. if (self.state.edit) {
  155. panelScope.ctrl.editPanel();
  156. } else {
  157. panelScope.ctrl.viewPanel();
  158. }
  159. }
  160. }
  161. panelScope.$on('$destroy', function() {
  162. self.panelScopes = _.without(self.panelScopes, panelScope);
  163. });
  164. };
  165. return {
  166. create: function($scope) {
  167. return new DashboardViewState($scope);
  168. }
  169. };
  170. });
  171. });