import * as React from 'react'; import { MenuItem, Nav, Navbar, NavDropdown, NavItem } from 'react-bootstrap'; import { findDOMNode } from 'react-dom'; import { Icon, IconStack } from 'react-fa'; import { Observable } from 'rxjs'; import { BaseView, BaseViewProps, HeaderAction, HeaderCommandAction, } from '../../React'; import { CommandButton } from '../CommandButton/CommandButton'; import { ProfilePicture } from '../ProfilePicture/ProfilePicture'; import { SearchView } from '../Search/SearchView'; import { SearchViewModel } from '../Search/SearchViewModel'; import { PageHeaderViewModel } from './PageHeaderViewModel'; import { Sidebar } from './Sidebar'; export interface PageHeaderProps { brand?: any; branduri?: string; responsive?: boolean; } export interface PageHeaderViewProps extends BaseViewProps, PageHeaderProps {} export class PageHeaderView extends BaseView< PageHeaderViewProps, PageHeaderViewModel > { public static displayName = 'PageHeaderView'; static defaultProps: Partial = { brand: 'webrx-react Rocks!!!', }; private containerRef = React.createRef(); private navBarRef = React.createRef(); updateOn(viewModel: Readonly) { return [ viewModel.sidebarMenus.changed, viewModel.navbarMenus.changed, viewModel.navbarActions.changed, viewModel.helpMenuItems.changed, viewModel.adminMenuItems.changed, viewModel.userMenuItems.changed, viewModel.isSidebarVisible.changed, viewModel.menuItemChanged.results, ]; } private isSidebarEnabled() { return String.isNullOrEmpty(this.props.branduri); } private isActionDisabled(item: HeaderCommandAction) { let isDisabled = true; if (item.command == null && String.isNullOrEmpty(item.uri) === false) { isDisabled = false; } else if (item.command != null && item.command.canExecute === true) { isDisabled = false; } return isDisabled; } private getOrderedActions(items: T[]) { return (items || []).sort((a, b) => (a.order || 0) - (b.order || 0)); } private getVisibleActions(items: HeaderCommandAction[]) { return this.getOrderedActions( (items || []).filter( x => x.visibleWhenDisabled === true || this.isActionDisabled(x) === false, ), ); } loaded() { super.loaded(); if (!this.props.responsive) { window.onresize = () => { this.layoutNavBar(); }; this.layoutNavBar(); } } render() { const { className, props, rest } = this.restProps(x => { const { brand, branduri, responsive } = x; return { brand, branduri, responsive }; }); return (
{this.renderBrandButton()} {this.renderSearch()} {this.renderRoutedActions()} {this.wxr.renderConditional(this.isSidebarEnabled(), () => this.renderSidebar(), )}
); } private layoutNavBar() { const container = this.containerRef.current; const navBar = this.navBarRef.current && (findDOMNode(this.navBarRef.current) as HTMLDivElement); if (container && navBar) { const rect = container.getBoundingClientRect(); container.style.paddingBottom = `${navBar.clientHeight + 1}px`; } } private renderBrandButton() { const isSidebarEnabled = this.isSidebarEnabled(); const active = isSidebarEnabled && this.viewModel.isSidebarVisible.value; const command = isSidebarEnabled ? this.viewModel.toggleSideBar : undefined; return ( {this.props.brand} ); } private renderHeaderMenu( id: any, header: any, items: HeaderCommandAction[], noCaret = false, className?: string, ) { const visibleItems = this.getVisibleActions(items); return this.wxr.renderConditional(visibleItems.length > 0, () => ( {visibleItems.map(x => ( vm.menuItemSelected, () => x) : null } > {this.renderHeaderCommandActionIcon(x)} {x.header} ))} )); } private renderHeaderCommandActionIcon( item: HeaderCommandAction, className?: string, fixedWidth = true, ) { const stackProps = { className }; const props = { fixedWidth, ...stackProps }; if (Array.isArray(item.iconName)) { const names = item.iconName.filter( x => String.isNullOrEmpty(x) === false, ); if (names.length === 0) { return null; } else if (names.length === 1) { return ; } else { return ( ); } } else { return String.isNullOrEmpty(item.iconName) ? null : ( ); } } private renderRoutedMenus() { return this.wxr.renderConditional( this.viewModel.navbarMenus.value.length > 0, () => this.getOrderedActions(this.viewModel.navbarMenus.value) .map(x => { return this.renderHeaderMenu(x.id, x.header, x.items); }) .filterNull(), ); } private renderHelpMenu() { return this.renderHeaderMenu( 'helpMenu', , this.viewModel.helpMenuItems.value, true, ); } private renderAdminMenu() { return this.renderHeaderMenu( 'adminMenu', , this.viewModel.adminMenuItems.value, true, 'hover-spin', ); } private renderUserMenu() { const profilePicture = ( ); return this.renderHeaderMenu( 'userMenu', profilePicture, this.viewModel.userMenuItems.value, true, ); } private renderSearch() { if (this.viewModel.search instanceof SearchViewModel) { return ( ); } return null; } private renderRoutedActions() { const visibleActions = this.getVisibleActions( this.viewModel.navbarActions.value, ); return ( {this.wxr.renderConditional(visibleActions.length > 0, () => visibleActions.map(x => ( {this.renderHeaderCommandActionIcon( x, 'PageHeader-actionHeaderIcon', false, )} {x.header} )), )} ); } private renderSidebar() { return ( x.toggleSideBar, () => false)} > {this.wxr.renderConditional(this.viewModel.isSidebarVisible, () => this.getOrderedActions(this.viewModel.sidebarMenus.value) .map(menu => { const visibleActions = this.getVisibleActions(menu.items); return this.wxr.renderConditional( visibleActions.length > 0, () => ( ), ); }) .filterNull(), )} ); } }