2021-04-09 23:21:53 +02:00
|
|
|
|
import React, { useRef } from 'react';
|
|
|
|
|
|
import { PopoverController, Popover, ClickOutsideWrapper, Icon, Tooltip, useTheme } from '@grafana/ui';
|
2021-07-21 20:09:00 +02:00
|
|
|
|
import { FunctionEditorControls, FunctionEditorControlsProps } from './FunctionEditorControls';
|
|
|
|
|
|
import { FuncInstance } from './gfunc';
|
2021-04-09 23:21:53 +02:00
|
|
|
|
import { css } from '@emotion/css';
|
2019-02-18 17:55:38 +01:00
|
|
|
|
|
|
|
|
|
|
interface FunctionEditorProps extends FunctionEditorControlsProps {
|
2021-07-21 20:09:00 +02:00
|
|
|
|
func: FuncInstance;
|
2019-02-18 17:55:38 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-04-09 23:21:53 +02:00
|
|
|
|
const FunctionEditor: React.FC<FunctionEditorProps> = ({ onMoveLeft, onMoveRight, func, ...props }) => {
|
|
|
|
|
|
const triggerRef = useRef<HTMLSpanElement>(null);
|
|
|
|
|
|
const theme = useTheme();
|
2019-02-18 17:55:38 +01:00
|
|
|
|
|
2021-04-09 23:21:53 +02:00
|
|
|
|
const renderContent = ({ updatePopperPosition }: any) => (
|
|
|
|
|
|
<FunctionEditorControls
|
|
|
|
|
|
{...props}
|
|
|
|
|
|
func={func}
|
|
|
|
|
|
onMoveLeft={() => {
|
|
|
|
|
|
onMoveLeft(func);
|
|
|
|
|
|
updatePopperPosition();
|
|
|
|
|
|
}}
|
|
|
|
|
|
onMoveRight={() => {
|
|
|
|
|
|
onMoveRight(func);
|
|
|
|
|
|
updatePopperPosition();
|
|
|
|
|
|
}}
|
|
|
|
|
|
/>
|
|
|
|
|
|
);
|
2019-02-18 17:55:38 +01:00
|
|
|
|
|
2021-04-09 23:21:53 +02:00
|
|
|
|
return (
|
|
|
|
|
|
<PopoverController content={renderContent} placement="top" hideAfter={100}>
|
|
|
|
|
|
{(showPopper, hidePopper, popperProps) => {
|
|
|
|
|
|
return (
|
|
|
|
|
|
<>
|
|
|
|
|
|
{triggerRef.current && (
|
|
|
|
|
|
<Popover
|
|
|
|
|
|
{...popperProps}
|
|
|
|
|
|
referenceElement={triggerRef.current}
|
|
|
|
|
|
wrapperClassName="popper"
|
|
|
|
|
|
className="popper__background"
|
|
|
|
|
|
renderArrow={({ arrowProps, placement }) => (
|
|
|
|
|
|
<div className="popper__arrow" data-placement={placement} {...arrowProps} />
|
|
|
|
|
|
)}
|
|
|
|
|
|
/>
|
|
|
|
|
|
)}
|
|
|
|
|
|
<ClickOutsideWrapper
|
|
|
|
|
|
onClick={() => {
|
|
|
|
|
|
if (popperProps.show) {
|
|
|
|
|
|
hidePopper();
|
|
|
|
|
|
}
|
|
|
|
|
|
}}
|
|
|
|
|
|
>
|
|
|
|
|
|
<span ref={triggerRef} onClick={popperProps.show ? hidePopper : showPopper} style={{ cursor: 'pointer' }}>
|
|
|
|
|
|
{func.def.unknown && (
|
|
|
|
|
|
<Tooltip content={<TooltipContent />} placement="bottom">
|
|
|
|
|
|
<Icon
|
|
|
|
|
|
data-testid="warning-icon"
|
|
|
|
|
|
name="exclamation-triangle"
|
|
|
|
|
|
size="xs"
|
|
|
|
|
|
className={css`
|
|
|
|
|
|
margin-right: ${theme.spacing.xxs};
|
|
|
|
|
|
`}
|
|
|
|
|
|
/>
|
|
|
|
|
|
</Tooltip>
|
|
|
|
|
|
)}
|
|
|
|
|
|
{func.def.name}
|
|
|
|
|
|
</span>
|
|
|
|
|
|
</ClickOutsideWrapper>
|
|
|
|
|
|
</>
|
|
|
|
|
|
);
|
|
|
|
|
|
}}
|
|
|
|
|
|
</PopoverController>
|
|
|
|
|
|
);
|
|
|
|
|
|
};
|
2019-02-18 17:55:38 +01:00
|
|
|
|
|
2021-04-09 23:21:53 +02:00
|
|
|
|
const TooltipContent = React.memo(() => {
|
|
|
|
|
|
return (
|
|
|
|
|
|
<span>
|
|
|
|
|
|
This function is not supported. Check your function for typos and{' '}
|
|
|
|
|
|
<a
|
|
|
|
|
|
target="_blank"
|
|
|
|
|
|
className="external-link"
|
|
|
|
|
|
rel="noreferrer noopener"
|
|
|
|
|
|
href="https://graphite.readthedocs.io/en/latest/functions.html"
|
|
|
|
|
|
>
|
|
|
|
|
|
read the docs
|
|
|
|
|
|
</a>{' '}
|
|
|
|
|
|
to see whether you need to upgrade your data source’s version to make this function available.
|
|
|
|
|
|
</span>
|
|
|
|
|
|
);
|
|
|
|
|
|
});
|
|
|
|
|
|
TooltipContent.displayName = 'FunctionEditorTooltipContent';
|
2019-02-18 17:55:38 +01:00
|
|
|
|
|
|
|
|
|
|
export { FunctionEditor };
|