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