viewStateSrv.js 4.8 KB

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