// Libaries import React, { PureComponent, FC, ReactNode } from 'react'; import { connect, MapDispatchToProps } from 'react-redux'; import { css } from 'emotion'; // Utils & Services import { appEvents } from 'app/core/app_events'; import { PlaylistSrv } from 'app/features/playlist/playlist_srv'; // Components import { DashNavButton } from './DashNavButton'; import { DashNavTimeControls } from './DashNavTimeControls'; import { Icon, ModalsController } from '@grafana/ui'; import { textUtil } from '@grafana/data'; import { BackButton } from 'app/core/components/BackButton/BackButton'; // State import { updateLocation } from 'app/core/actions'; import { updateTimeZoneForSession } from 'app/features/profile/state/reducers'; // Types import { DashboardModel } from '../../state'; import { CoreEvents, StoreState } from 'app/types'; import { ShareModal } from 'app/features/dashboard/components/ShareModal'; import { SaveDashboardModalProxy } from 'app/features/dashboard/components/SaveDashboard/SaveDashboardModalProxy'; export interface OwnProps { dashboard: DashboardModel; isFullscreen: boolean; $injector: any; onAddPanel: () => void; } interface DispatchProps { updateTimeZoneForSession: typeof updateTimeZoneForSession; updateLocation: typeof updateLocation; } interface DashNavButtonModel { show: (props: Props) => boolean; component: FC>; index?: number | 'end'; } const customLeftActions: DashNavButtonModel[] = []; const customRightActions: DashNavButtonModel[] = []; export function addCustomLeftAction(content: DashNavButtonModel) { customLeftActions.push(content); } export function addCustomRightAction(content: DashNavButtonModel) { customRightActions.push(content); } export interface StateProps { location: any; } type Props = StateProps & OwnProps & DispatchProps; class DashNav extends PureComponent { playlistSrv: PlaylistSrv; constructor(props: Props) { super(props); this.playlistSrv = this.props.$injector.get('playlistSrv'); } onFolderNameClick = () => { this.props.updateLocation({ query: { search: 'open', folder: 'current' }, partial: true, }); }; onClose = () => { this.props.updateLocation({ query: { viewPanel: null }, partial: true, }); }; onToggleTVMode = () => { appEvents.emit(CoreEvents.toggleKioskMode); }; onOpenSettings = () => { this.props.updateLocation({ query: { editview: 'settings' }, partial: true, }); }; onStarDashboard = () => { const { dashboard, $injector } = this.props; const dashboardSrv = $injector.get('dashboardSrv'); dashboardSrv.starDashboard(dashboard.id, dashboard.meta.isStarred).then((newState: any) => { dashboard.meta.isStarred = newState; this.forceUpdate(); }); }; onPlaylistPrev = () => { this.playlistSrv.prev(); }; onPlaylistNext = () => { this.playlistSrv.next(); }; onPlaylistStop = () => { this.playlistSrv.stop(); this.forceUpdate(); }; onDashboardNameClick = () => { this.props.updateLocation({ query: { search: 'open' }, partial: true, }); }; addCustomContent(actions: DashNavButtonModel[], buttons: ReactNode[]) { actions.map((action, index) => { const Component = action.component; const element = ; typeof action.index === 'number' ? buttons.splice(action.index, 0, element) : buttons.push(element); }); } renderLeftActionsButton() { const { dashboard } = this.props; const { canStar, canShare, isStarred } = dashboard.meta; const buttons: ReactNode[] = []; if (canStar) { buttons.push( ); } if (canShare) { buttons.push( {({ showModal, hideModal }) => ( { showModal(ShareModal, { dashboard, onDismiss: hideModal, }); }} /> )} ); } this.addCustomContent(customLeftActions, buttons); return buttons; } renderDashboardTitleSearchButton() { const { dashboard, isFullscreen } = this.props; const folderSymbol = css` margin-right: 0 4px; `; const mainIconClassName = css` margin-right: 8px; margin-bottom: 3px; `; const folderTitle = dashboard.meta.folderTitle; const haveFolder = (dashboard.meta.folderId ?? 0) > 0; return ( <>
{!isFullscreen && } {haveFolder && ( <> {folderTitle} / )} {dashboard.title}
{this.renderLeftActionsButton()}
); } renderBackButton() { return (
); } renderRightActionsButton() { const { dashboard, onAddPanel } = this.props; const { canSave, showSettings } = dashboard.meta; const { snapshot } = dashboard; const snapshotUrl = snapshot && snapshot.originalUrl; const buttons: ReactNode[] = []; if (canSave) { buttons.push( ); buttons.push( {({ showModal, hideModal }) => ( { showModal(SaveDashboardModalProxy, { dashboard, onDismiss: hideModal, }); }} /> )} ); } if (snapshotUrl) { buttons.push( ); } if (showSettings) { buttons.push( ); } this.addCustomContent(customRightActions, buttons); return buttons; } render() { const { dashboard, location, isFullscreen, updateTimeZoneForSession } = this.props; return (
{isFullscreen && this.renderBackButton()} {this.renderDashboardTitleSearchButton()} {this.playlistSrv.isPlaying && (
)}
{this.renderRightActionsButton()}
{!dashboard.timepicker.hidden && (
)}
); } } const mapStateToProps = (state: StoreState) => ({ location: state.location, }); const mapDispatchToProps: MapDispatchToProps = { updateLocation, updateTimeZoneForSession, }; export default connect(mapStateToProps, mapDispatchToProps)(DashNav);