viewStateSrv.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. define([
  2. 'angular',
  3. 'lodash',
  4. 'jquery',
  5. 'app/core/config'
  6. ],
  7. function (angular, _, $, config) {
  8. 'use strict';
  9. var module = angular.module('grafana.services');
  10. module.factory('dashboardViewStateSrv', function($location, $timeout, $rootScope) {
  11. // represents the transient view state
  12. // like fullscreen panel & edit
  13. function DashboardViewState($scope) {
  14. var self = this;
  15. self.state = {};
  16. self.panelScopes = [];
  17. self.$scope = $scope;
  18. self.dashboard = $scope.dashboard;
  19. $scope.onAppEvent('$routeUpdate', function() {
  20. var urlState = self.getQueryStringState();
  21. if (self.needsSync(urlState)) {
  22. self.update(urlState, true);
  23. }
  24. });
  25. $scope.onAppEvent('panel-change-view', function(evt, payload) {
  26. self.update(payload);
  27. });
  28. $scope.onAppEvent('panel-initialized', function(evt, payload) {
  29. self.registerPanel(payload.scope);
  30. });
  31. // this marks changes to location during this digest cycle as not to add history item
  32. // dont want url changes like adding orgId to add browser history
  33. $location.replace();
  34. this.update(this.getQueryStringState());
  35. }
  36. DashboardViewState.prototype.needsSync = function(urlState) {
  37. return _.isEqual(this.state, urlState) === false;
  38. };
  39. DashboardViewState.prototype.getQueryStringState = function() {
  40. var state = $location.search();
  41. state.panelId = parseInt(state.panelId) || null;
  42. state.fullscreen = state.fullscreen ? true : null;
  43. state.edit = (state.edit === "true" || state.edit === true) || null;
  44. state.editview = state.editview || null;
  45. state.orgId = config.bootData.user.orgId;
  46. return state;
  47. };
  48. DashboardViewState.prototype.serializeToUrl = function() {
  49. var urlState = _.clone(this.state);
  50. urlState.fullscreen = this.state.fullscreen ? true : null;
  51. urlState.edit = this.state.edit ? true : null;
  52. return urlState;
  53. };
  54. DashboardViewState.prototype.update = function(state, fromRouteUpdated) {
  55. // implement toggle logic
  56. if (state.toggle) {
  57. delete state.toggle;
  58. if (this.state.fullscreen && state.fullscreen) {
  59. if (this.state.edit === state.edit) {
  60. state.fullscreen = !state.fullscreen;
  61. }
  62. }
  63. }
  64. // remember if editStateChanged
  65. this.editStateChanged = state.edit !== this.state.edit;
  66. _.extend(this.state, state);
  67. this.dashboard.meta.fullscreen = this.state.fullscreen;
  68. if (!this.state.fullscreen) {
  69. this.state.fullscreen = null;
  70. this.state.edit = null;
  71. // clear panel id unless in solo mode
  72. if (!this.dashboard.meta.soloMode) {
  73. this.state.panelId = null;
  74. }
  75. }
  76. // if no edit state cleanup tab parm
  77. if (!this.state.edit) {
  78. delete this.state.tab;
  79. }
  80. // do not update url params if we are here
  81. // from routeUpdated event
  82. if (fromRouteUpdated !== true) {
  83. $location.search(this.serializeToUrl());
  84. }
  85. this.syncState();
  86. };
  87. DashboardViewState.prototype.syncState = function() {
  88. if (this.panelScopes.length === 0) { return; }
  89. if (this.dashboard.meta.fullscreen) {
  90. var panelScope = this.getPanelScope(this.state.panelId);
  91. if (!panelScope) {
  92. return;
  93. }
  94. if (this.fullscreenPanel) {
  95. // if already fullscreen
  96. if (this.fullscreenPanel === panelScope && this.editStateChanged === false) {
  97. return;
  98. } else {
  99. this.leaveFullscreen(false);
  100. }
  101. }
  102. if (!panelScope.ctrl.editModeInitiated) {
  103. panelScope.ctrl.initEditMode();
  104. }
  105. if (!panelScope.ctrl.fullscreen) {
  106. this.enterFullscreen(panelScope);
  107. }
  108. } else if (this.fullscreenPanel) {
  109. this.leaveFullscreen(true);
  110. }
  111. };
  112. DashboardViewState.prototype.getPanelScope = function(id) {
  113. return _.find(this.panelScopes, function(panelScope) {
  114. return panelScope.ctrl.panel.id === id;
  115. });
  116. };
  117. DashboardViewState.prototype.leaveFullscreen = function(render) {
  118. var self = this;
  119. var ctrl = self.fullscreenPanel.ctrl;
  120. ctrl.editMode = false;
  121. ctrl.fullscreen = false;
  122. this.dashboard.setViewMode(ctrl.panel, false, false);
  123. this.$scope.appEvent('panel-fullscreen-exit', {panelId: ctrl.panel.id});
  124. if (!render) { return false;}
  125. $timeout(function() {
  126. if (self.oldTimeRange !== ctrl.range) {
  127. $rootScope.$broadcast('refresh');
  128. } else {
  129. $rootScope.$broadcast('render');
  130. }
  131. delete self.fullscreenPanel;
  132. });
  133. };
  134. DashboardViewState.prototype.enterFullscreen = function(panelScope) {
  135. var ctrl = panelScope.ctrl;
  136. ctrl.editMode = this.state.edit && this.dashboard.meta.canEdit;
  137. ctrl.fullscreen = true;
  138. this.oldTimeRange = ctrl.range;
  139. this.fullscreenPanel = panelScope;
  140. this.dashboard.setViewMode(ctrl.panel, true, ctrl.editMode);
  141. this.$scope.appEvent('panel-fullscreen-enter', {panelId: ctrl.panel.id});
  142. };
  143. DashboardViewState.prototype.registerPanel = function(panelScope) {
  144. var self = this;
  145. self.panelScopes.push(panelScope);
  146. if (!self.dashboard.meta.soloMode) {
  147. if (self.state.panelId === panelScope.ctrl.panel.id) {
  148. if (self.state.edit) {
  149. panelScope.ctrl.editPanel();
  150. } else {
  151. panelScope.ctrl.viewPanel();
  152. }
  153. }
  154. }
  155. var unbind = panelScope.$on('$destroy', function() {
  156. self.panelScopes = _.without(self.panelScopes, panelScope);
  157. unbind();
  158. });
  159. };
  160. return {
  161. create: function($scope) {
  162. return new DashboardViewState($scope);
  163. }
  164. };
  165. });
  166. });