DashboardPage.tsx 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. // Libraries
  2. import $ from 'jquery';
  3. import React, { PureComponent, MouseEvent } from 'react';
  4. import { hot } from 'react-hot-loader';
  5. import { connect } from 'react-redux';
  6. import classNames from 'classnames';
  7. // Services & Utils
  8. import { createErrorNotification } from 'app/core/copy/appNotification';
  9. import { getMessageFromError } from 'app/core/utils/errors';
  10. // Components
  11. import { DashboardGrid } from '../dashgrid/DashboardGrid';
  12. import { DashNav } from '../components/DashNav';
  13. import { SubMenu } from '../components/SubMenu';
  14. import { DashboardSettings } from '../components/DashboardSettings';
  15. import { CustomScrollbar, Alert } from '@grafana/ui';
  16. // Redux
  17. import { initDashboard } from '../state/initDashboard';
  18. import { cleanUpDashboard } from '../state/actions';
  19. import { updateLocation } from 'app/core/actions';
  20. import { notifyApp } from 'app/core/actions';
  21. // Types
  22. import {
  23. StoreState,
  24. DashboardInitPhase,
  25. DashboardRouteInfo,
  26. DashboardInitError,
  27. AppNotificationSeverity,
  28. } from 'app/types';
  29. import { DashboardModel, PanelModel } from 'app/features/dashboard/state';
  30. export interface Props {
  31. urlUid?: string;
  32. urlSlug?: string;
  33. urlType?: string;
  34. editview?: string;
  35. urlPanelId?: string;
  36. urlFolderId?: string;
  37. $scope: any;
  38. $injector: any;
  39. routeInfo: DashboardRouteInfo;
  40. urlEdit: boolean;
  41. urlFullscreen: boolean;
  42. initPhase: DashboardInitPhase;
  43. isInitSlow: boolean;
  44. dashboard: DashboardModel | null;
  45. initError?: DashboardInitError;
  46. initDashboard: typeof initDashboard;
  47. cleanUpDashboard: typeof cleanUpDashboard;
  48. notifyApp: typeof notifyApp;
  49. updateLocation: typeof updateLocation;
  50. }
  51. export interface State {
  52. isSettingsOpening: boolean;
  53. isEditing: boolean;
  54. isFullscreen: boolean;
  55. fullscreenPanel: PanelModel | null;
  56. scrollTop: number;
  57. updateScrollTop: number;
  58. rememberScrollTop: number;
  59. showLoadingState: boolean;
  60. }
  61. export class DashboardPage extends PureComponent<Props, State> {
  62. state: State = {
  63. isSettingsOpening: false,
  64. isEditing: false,
  65. isFullscreen: false,
  66. showLoadingState: false,
  67. fullscreenPanel: null,
  68. scrollTop: 0,
  69. updateScrollTop: null,
  70. rememberScrollTop: 0,
  71. };
  72. async componentDidMount() {
  73. this.props.initDashboard({
  74. $injector: this.props.$injector,
  75. $scope: this.props.$scope,
  76. urlSlug: this.props.urlSlug,
  77. urlUid: this.props.urlUid,
  78. urlType: this.props.urlType,
  79. urlFolderId: this.props.urlFolderId,
  80. routeInfo: this.props.routeInfo,
  81. fixUrl: true,
  82. });
  83. }
  84. componentWillUnmount() {
  85. if (this.props.dashboard) {
  86. this.props.cleanUpDashboard();
  87. this.setPanelFullscreenClass(false);
  88. }
  89. }
  90. componentDidUpdate(prevProps: Props) {
  91. const { dashboard, editview, urlEdit, urlFullscreen, urlPanelId, urlUid } = this.props;
  92. if (!dashboard) {
  93. return;
  94. }
  95. // if we just got dashboard update title
  96. if (!prevProps.dashboard) {
  97. document.title = dashboard.title + ' - Energy Link'; // OLEIVA - Change Title
  98. }
  99. // Due to the angular -> react url bridge we can ge an update here with new uid before the container unmounts
  100. // Can remove this condition after we switch to react router
  101. if (prevProps.urlUid !== urlUid) {
  102. return;
  103. }
  104. // handle animation states when opening dashboard settings
  105. if (!prevProps.editview && editview) {
  106. this.setState({ isSettingsOpening: true });
  107. setTimeout(() => {
  108. this.setState({ isSettingsOpening: false });
  109. }, 10);
  110. }
  111. // Sync url state with model
  112. if (urlFullscreen !== dashboard.meta.fullscreen || urlEdit !== dashboard.meta.isEditing) {
  113. if (!isNaN(parseInt(urlPanelId, 10))) {
  114. this.onEnterFullscreen();
  115. } else {
  116. this.onLeaveFullscreen();
  117. }
  118. }
  119. }
  120. onEnterFullscreen() {
  121. const { dashboard, urlEdit, urlFullscreen, urlPanelId } = this.props;
  122. const panelId = parseInt(urlPanelId, 10);
  123. // need to expand parent row if this panel is inside a row
  124. dashboard.expandParentRowFor(panelId);
  125. const panel = dashboard.getPanelById(panelId);
  126. if (panel) {
  127. dashboard.setViewMode(panel, urlFullscreen, urlEdit);
  128. this.setState({
  129. isEditing: urlEdit && dashboard.meta.canEdit,
  130. isFullscreen: urlFullscreen,
  131. fullscreenPanel: panel,
  132. rememberScrollTop: this.state.scrollTop,
  133. });
  134. this.setPanelFullscreenClass(urlFullscreen);
  135. } else {
  136. this.handleFullscreenPanelNotFound(urlPanelId);
  137. }
  138. }
  139. onLeaveFullscreen() {
  140. const { dashboard } = this.props;
  141. if (this.state.fullscreenPanel) {
  142. dashboard.setViewMode(this.state.fullscreenPanel, false, false);
  143. }
  144. this.setState(
  145. {
  146. isEditing: false,
  147. isFullscreen: false,
  148. fullscreenPanel: null,
  149. updateScrollTop: this.state.rememberScrollTop,
  150. },
  151. this.triggerPanelsRendering.bind(this)
  152. );
  153. this.setPanelFullscreenClass(false);
  154. }
  155. triggerPanelsRendering() {
  156. try {
  157. this.props.dashboard.render();
  158. } catch (err) {
  159. this.props.notifyApp(createErrorNotification(`Panel rendering error`, err));
  160. }
  161. }
  162. handleFullscreenPanelNotFound(urlPanelId: string) {
  163. // Panel not found
  164. this.props.notifyApp(createErrorNotification(`Panel with id ${urlPanelId} not found`));
  165. // Clear url state
  166. this.props.updateLocation({
  167. query: {
  168. edit: null,
  169. fullscreen: null,
  170. panelId: null,
  171. },
  172. partial: true,
  173. });
  174. }
  175. setPanelFullscreenClass(isFullscreen: boolean) {
  176. $('body').toggleClass('panel-in-fullscreen', isFullscreen);
  177. }
  178. setScrollTop = (e: MouseEvent<HTMLElement>): void => {
  179. const target = e.target as HTMLElement;
  180. this.setState({ scrollTop: target.scrollTop, updateScrollTop: null });
  181. };
  182. onAddPanel = () => {
  183. const { dashboard } = this.props;
  184. // Return if the "Add panel" exists already
  185. if (dashboard.panels.length > 0 && dashboard.panels[0].type === 'add-panel') {
  186. return;
  187. }
  188. dashboard.addPanel({
  189. type: 'add-panel',
  190. gridPos: { x: 0, y: 0, w: 12, h: 8 },
  191. title: 'Panel Title',
  192. });
  193. // scroll to top after adding panel
  194. this.setState({ scrollTop: 0 });
  195. };
  196. renderSlowInitState() {
  197. return (
  198. <div className="dashboard-loading">
  199. <div className="dashboard-loading__text">
  200. <i className="fa fa-spinner fa-spin" /> {this.props.initPhase}
  201. </div>
  202. </div>
  203. );
  204. }
  205. renderInitFailedState() {
  206. const { initError } = this.props;
  207. return (
  208. <div className="dashboard-loading">
  209. <Alert
  210. severity={AppNotificationSeverity.Error}
  211. title={initError.message}
  212. children={getMessageFromError(initError.error)}
  213. />
  214. </div>
  215. );
  216. }
  217. render() {
  218. const { dashboard, editview, $injector, isInitSlow, initError } = this.props;
  219. const { isSettingsOpening, isEditing, isFullscreen, scrollTop, updateScrollTop } = this.state;
  220. if (!dashboard) {
  221. if (isInitSlow) {
  222. return this.renderSlowInitState();
  223. }
  224. return null;
  225. }
  226. const classes = classNames({
  227. 'dashboard-page--settings-opening': isSettingsOpening,
  228. 'dashboard-page--settings-open': !isSettingsOpening && editview,
  229. });
  230. const gridWrapperClasses = classNames({
  231. 'dashboard-container': true,
  232. 'dashboard-container--has-submenu': dashboard.meta.submenuEnabled,
  233. });
  234. // Only trigger render when the scroll has moved by 25
  235. const approximateScrollTop = Math.round(scrollTop / 25) * 25;
  236. return (
  237. <div className={classes}>
  238. <DashNav
  239. dashboard={dashboard}
  240. isEditing={isEditing}
  241. isFullscreen={isFullscreen}
  242. editview={editview}
  243. $injector={$injector}
  244. onAddPanel={this.onAddPanel}
  245. />
  246. <div className="scroll-canvas scroll-canvas--dashboard">
  247. <CustomScrollbar
  248. autoHeightMin="100%"
  249. setScrollTop={this.setScrollTop}
  250. scrollTop={updateScrollTop}
  251. updateAfterMountMs={500}
  252. className="custom-scrollbar--page"
  253. >
  254. {editview && <DashboardSettings dashboard={dashboard} />}
  255. {initError && this.renderInitFailedState()}
  256. <div className={gridWrapperClasses}>
  257. {dashboard.meta.submenuEnabled && <SubMenu dashboard={dashboard} />}
  258. <DashboardGrid
  259. dashboard={dashboard}
  260. isEditing={isEditing}
  261. isFullscreen={isFullscreen}
  262. scrollTop={approximateScrollTop}
  263. />
  264. </div>
  265. </CustomScrollbar>
  266. </div>
  267. </div>
  268. );
  269. }
  270. }
  271. export const mapStateToProps = (state: StoreState) => ({
  272. urlUid: state.location.routeParams.uid,
  273. urlSlug: state.location.routeParams.slug,
  274. urlType: state.location.routeParams.type,
  275. editview: state.location.query.editview,
  276. urlPanelId: state.location.query.panelId,
  277. urlFolderId: state.location.query.folderId,
  278. urlFullscreen: !!state.location.query.fullscreen,
  279. urlEdit: !!state.location.query.edit,
  280. initPhase: state.dashboard.initPhase,
  281. isInitSlow: state.dashboard.isInitSlow,
  282. initError: state.dashboard.initError,
  283. dashboard: state.dashboard.model as DashboardModel,
  284. });
  285. const mapDispatchToProps = {
  286. initDashboard,
  287. cleanUpDashboard,
  288. notifyApp,
  289. updateLocation,
  290. };
  291. export default hot(module)(
  292. connect(
  293. mapStateToProps,
  294. mapDispatchToProps
  295. )(DashboardPage)
  296. );