viewStateSrv.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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) {
  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. $scope.exitFullscreen = function() {
  18. if (self.state.fullscreen) {
  19. self.update({ fullscreen: false });
  20. }
  21. };
  22. $scope.onAppEvent('$routeUpdate', function() {
  23. var urlState = self.getQueryStringState();
  24. if (self.needsSync(urlState)) {
  25. self.update(urlState, true);
  26. }
  27. });
  28. $scope.onAppEvent('panel-change-view', function(evt, payload) {
  29. self.update(payload);
  30. });
  31. $scope.onAppEvent('panel-instantiated', function(evt, payload) {
  32. self.registerPanel(payload.scope);
  33. });
  34. this.update(this.getQueryStringState(), true);
  35. this.expandRowForPanel();
  36. }
  37. DashboardViewState.prototype.expandRowForPanel = function() {
  38. if (!this.state.panelId) { return; }
  39. var panelInfo = this.$scope.dashboard.getPanelInfoById(this.state.panelId);
  40. if (panelInfo) {
  41. panelInfo.row.collapse = false;
  42. }
  43. };
  44. DashboardViewState.prototype.needsSync = function(urlState) {
  45. return _.isEqual(this.state, urlState) === false;
  46. };
  47. DashboardViewState.prototype.getQueryStringState = function() {
  48. var state = $location.search();
  49. state.panelId = parseInt(state.panelId) || null;
  50. state.fullscreen = state.fullscreen ? true : null;
  51. state.edit = (state.edit === "true" || state.edit === true) || null;
  52. state.editview = state.editview || null;
  53. return state;
  54. };
  55. DashboardViewState.prototype.serializeToUrl = function() {
  56. var urlState = _.clone(this.state);
  57. urlState.fullscreen = this.state.fullscreen ? true : null;
  58. urlState.edit = this.state.edit ? true : null;
  59. return urlState;
  60. };
  61. DashboardViewState.prototype.update = function(state, skipUrlSync) {
  62. _.extend(this.state, state);
  63. this.fullscreen = this.state.fullscreen;
  64. if (!this.state.fullscreen) {
  65. this.state.panelId = null;
  66. this.state.fullscreen = null;
  67. this.state.edit = null;
  68. }
  69. if (!skipUrlSync) {
  70. $location.search(this.serializeToUrl());
  71. }
  72. this.syncState();
  73. };
  74. DashboardViewState.prototype.syncState = function() {
  75. if (this.panelScopes.length === 0) { return; }
  76. if (this.fullscreen) {
  77. if (this.fullscreenPanel) {
  78. this.leaveFullscreen(false);
  79. }
  80. var panelScope = this.getPanelScope(this.state.panelId);
  81. // panel could be about to be created/added and scope does
  82. // not exist yet
  83. if (!panelScope) {
  84. return;
  85. }
  86. this.enterFullscreen(panelScope);
  87. return;
  88. }
  89. if (this.fullscreenPanel) {
  90. this.leaveFullscreen(true);
  91. }
  92. };
  93. DashboardViewState.prototype.getPanelScope = function(id) {
  94. return _.find(this.panelScopes, function(panelScope) {
  95. return panelScope.ctrl.panel.id === id;
  96. });
  97. };
  98. DashboardViewState.prototype.leaveFullscreen = function(render) {
  99. var self = this;
  100. var ctrl = self.fullscreenPanel.ctrl;
  101. ctrl.editMode = false;
  102. ctrl.fullscreen = false;
  103. delete ctrl.height;
  104. this.$scope.appEvent('panel-fullscreen-exit', {panelId: ctrl.panel.id});
  105. if (!render) { return false;}
  106. $timeout(function() {
  107. if (self.oldTimeRange !== ctrl.range) {
  108. self.$scope.broadcastRefresh();
  109. }
  110. else {
  111. self.fullscreenPanel.$broadcast('render');
  112. }
  113. delete self.fullscreenPanel;
  114. });
  115. };
  116. DashboardViewState.prototype.enterFullscreen = function(panelScope) {
  117. var docHeight = $(window).height();
  118. var editHeight = Math.floor(docHeight * 0.3);
  119. var fullscreenHeight = Math.floor(docHeight * 0.7);
  120. var ctrl = panelScope.ctrl;
  121. ctrl.editMode = this.state.edit && this.$scope.dashboardMeta.canEdit;
  122. ctrl.height = ctrl.editMode ? editHeight : fullscreenHeight;
  123. this.oldTimeRange = ctrl.range;
  124. this.fullscreenPanel = panelScope;
  125. $(window).scrollTop(0);
  126. panelScope.fullscreen = true;
  127. this.$scope.appEvent('panel-fullscreen-enter', {panelId: ctrl.panel.id});
  128. $timeout(function() {
  129. panelScope.$broadcast('render');
  130. });
  131. };
  132. DashboardViewState.prototype.registerPanel = function(panelScope) {
  133. var self = this;
  134. self.panelScopes.push(panelScope);
  135. if (self.state.panelId === panelScope.ctrl.panel.id) {
  136. if (self.state.edit) {
  137. panelScope.ctrl.editPanel();
  138. } else {
  139. panelScope.ctrl.viewPanel();
  140. }
  141. }
  142. panelScope.$on('$destroy', function() {
  143. self.panelScopes = _.without(self.panelScopes, panelScope);
  144. });
  145. };
  146. return {
  147. create: function($scope) {
  148. return new DashboardViewState($scope);
  149. }
  150. };
  151. });
  152. });