viewStateSrv.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. this.enterFullscreen(panelScope);
  76. return;
  77. }
  78. if (this.fullscreenPanel) {
  79. this.leaveFullscreen(true);
  80. }
  81. };
  82. DashboardViewState.prototype.getPanelScope = function(id) {
  83. return _.find(this.panelScopes, function(panelScope) {
  84. return panelScope.panel.id === id;
  85. });
  86. };
  87. DashboardViewState.prototype.leaveFullscreen = function(render) {
  88. var self = this;
  89. self.fullscreenPanel.editMode = false;
  90. self.fullscreenPanel.fullscreen = false;
  91. delete self.fullscreenPanel.height;
  92. if (!render) { return false;}
  93. $timeout(function() {
  94. if (self.oldTimeRange !== self.fullscreenPanel.range) {
  95. self.$scope.broadcastRefresh();
  96. }
  97. else {
  98. self.fullscreenPanel.$broadcast('render');
  99. }
  100. delete self.fullscreenPanel;
  101. });
  102. };
  103. DashboardViewState.prototype.enterFullscreen = function(panelScope) {
  104. this.$scope.appEvent('hide-dash-editor');
  105. var docHeight = $(window).height();
  106. var editHeight = Math.floor(docHeight * 0.3);
  107. var fullscreenHeight = Math.floor(docHeight * 0.7);
  108. panelScope.editMode = this.state.edit && this.$scope.dashboardMeta.canEdit;
  109. panelScope.height = panelScope.editMode ? editHeight : fullscreenHeight;
  110. this.oldTimeRange = panelScope.range;
  111. this.fullscreenPanel = panelScope;
  112. $(window).scrollTop(0);
  113. panelScope.fullscreen = true;
  114. $timeout(function() {
  115. panelScope.$broadcast('render');
  116. });
  117. };
  118. DashboardViewState.prototype.registerPanel = function(panelScope) {
  119. var self = this;
  120. self.panelScopes.push(panelScope);
  121. if (self.state.panelId === panelScope.panel.id) {
  122. self.enterFullscreen(panelScope);
  123. }
  124. panelScope.$on('$destroy', function() {
  125. self.panelScopes = _.without(self.panelScopes, panelScope);
  126. });
  127. };
  128. return {
  129. create: function($scope) {
  130. return new DashboardViewState($scope);
  131. }
  132. };
  133. });
  134. });