/* * Copyright [2021] [wisemapping] * * Licensed under WiseMapping Public License, Version 1.0 (the "License"). * It is basically the Apache License, Version 2.0 (the "License") plus the * "powered by wisemapping" text requirement on every single page; * you may not use this file except in compliance with the License. * You may obtain a copy of the license at * * http://www.wisemapping.org/license * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import React, { useRef, useState } from 'react'; import AppBar from '@mui/material/AppBar'; import Divider from '@mui/material/Divider'; import IconButton from '@mui/material/IconButton'; import Popover, { PopoverOrigin } from '@mui/material/Popover'; import Tooltip from '@mui/material/Tooltip'; import '../app-bar/styles.css'; import Box from '@mui/material/Box'; import ActionConfig from '../../classes/action-config'; import ToolbarPosition, { defaultPosition } from './ToolbarPositionInterface'; /** * Common button * @param props.configuration the configuration * @returns common button menu entry that uses the onClick of the configuration. */ export const ToolbarButtonOption = (props: { configuration: ActionConfig }) => { const selected = props.configuration.selected && props.configuration.selected(); return ( {typeof props.configuration.icon === 'function' ? props.configuration.icon() : props.configuration.icon} ); }; const verticalAligment: { anchorOrigin: PopoverOrigin; transformOrigin: PopoverOrigin } = { anchorOrigin: { vertical: 48, horizontal: 'right', }, transformOrigin: { vertical: 'top', horizontal: 'right', }, }; const horizontalAligment: { anchorOrigin: PopoverOrigin; transformOrigin: PopoverOrigin } = { anchorOrigin: { vertical: 'center', horizontal: -3, }, transformOrigin: { vertical: 'center', horizontal: 'right', }, }; /** * Submenu button and popover * @param props.configuration the configuration * @returns submenu entry that contains one ToolbarMenuItem for each option. Inserts a divider for null options. */ export const ToolbarSubmenu = (props: { configuration: ActionConfig; vertical?: boolean; elevation?: number; }) => { const [open, setOpen] = useState(false); const itemRef = useRef(null); const orientationProps = props.vertical ? verticalAligment : horizontalAligment; return ( !props.configuration.useClickToClose && setOpen(false)} onMouseEnter={() => { if (props.configuration.disabled && props.configuration.disabled()) return; if (!props.configuration.useClickToClose) setOpen(true); }} > setOpen(true) }} /> setOpen(false)} anchorEl={itemRef.current} container={itemRef.current} anchorOrigin={orientationProps.anchorOrigin} transformOrigin={orientationProps.transformOrigin} PaperProps={{ onMouseLeave: () => !props.configuration.useClickToClose && setOpen(false), square: true, }} sx={{ zIndex: props.configuration.useClickToClose ? '1' : '-1', }} elevation={props.elevation} >
e.stopPropagation()}> {props.configuration.options?.map((o, i) => { if (o?.visible === false) return null; if (!o?.render) { return ( ); } else { return {o.render(() => setOpen(false))}; } })}
); }; /** * Wrapper for all menu entries * @param props.configuration the configuration * @returns menu item wich contains a submenu if options is set or a button if onClick is set or null otherwise. */ export const ToolbarMenuItem = (props: { configuration: ActionConfig | null; vertical?: boolean; elevation?: number; }) => { if (props.configuration === null) return ( ); if (props.configuration.visible === false) { return null; } if (props.configuration.render) { return <>{props.configuration.render(null)}; } if (!props.configuration.options && props.configuration.onClick) return ; else { if (props.configuration.options) return ( ); else return null; } }; // const getOrientationProps = (orientation: 'horizontal' | 'vertical'): [top:number, number, ] /** * The entry point for create a Toolbar * @param props.configurations the configurations array * @returns toolbar wich contains a button/submenu for each configuration in the array */ const Toolbar = (props: { configurations: ActionConfig[]; position?: ToolbarPosition; rerender?: number; }) => { const position = props.position || defaultPosition; return ( {props.configurations.map((c, i) => { return ( ); })} ); }; export default Toolbar;