viewStateSrv.js 5.0 KB

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