2019-06-25 17:38:51 +08:00
|
|
|
// Libraries
|
|
|
|
|
import React, { FC, useContext } from 'react';
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
import Prism from 'prismjs';
|
|
|
|
|
// Components
|
|
|
|
|
import { css } from 'emotion';
|
2019-07-07 00:18:23 +08:00
|
|
|
import { DataLink } from '@grafana/data';
|
|
|
|
|
import { ThemeContext } from '../../index';
|
2019-06-25 17:38:51 +08:00
|
|
|
import { Button } from '../index';
|
|
|
|
|
import { DataLinkEditor } from './DataLinkEditor';
|
|
|
|
|
import { VariableSuggestion } from './DataLinkSuggestions';
|
|
|
|
|
|
|
|
|
|
interface DataLinksEditorProps {
|
|
|
|
|
value: DataLink[];
|
|
|
|
|
onChange: (links: DataLink[]) => void;
|
|
|
|
|
suggestions: VariableSuggestion[];
|
|
|
|
|
maxLinks?: number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Prism.languages['links'] = {
|
|
|
|
|
builtInVariable: {
|
2019-09-13 22:38:21 +08:00
|
|
|
pattern: /(\${\S+?})/,
|
2019-06-25 17:38:51 +08:00
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const DataLinksEditor: FC<DataLinksEditorProps> = React.memo(({ value, onChange, suggestions, maxLinks }) => {
|
|
|
|
|
const theme = useContext(ThemeContext);
|
|
|
|
|
|
|
|
|
|
const onAdd = () => {
|
|
|
|
|
onChange(value ? [...value, { url: '', title: '' }] : [{ url: '', title: '' }]);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onLinkChanged = (linkIndex: number, newLink: DataLink) => {
|
|
|
|
|
onChange(
|
|
|
|
|
value.map((item, listIndex) => {
|
|
|
|
|
if (linkIndex === listIndex) {
|
|
|
|
|
return newLink;
|
|
|
|
|
}
|
|
|
|
|
return item;
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onRemove = (link: DataLink) => {
|
|
|
|
|
onChange(value.filter(item => item !== link));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
{value && value.length > 0 && (
|
|
|
|
|
<div
|
|
|
|
|
className={css`
|
|
|
|
|
margin-bottom: ${theme.spacing.sm};
|
|
|
|
|
`}
|
|
|
|
|
>
|
|
|
|
|
{value.map((link, index) => (
|
|
|
|
|
<DataLinkEditor
|
|
|
|
|
key={index.toString()}
|
|
|
|
|
index={index}
|
2019-09-13 22:38:21 +08:00
|
|
|
isLast={index === value.length - 1}
|
2019-06-25 17:38:51 +08:00
|
|
|
value={link}
|
|
|
|
|
onChange={onLinkChanged}
|
|
|
|
|
onRemove={onRemove}
|
|
|
|
|
suggestions={suggestions}
|
|
|
|
|
/>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
|
2019-08-07 20:13:19 +08:00
|
|
|
{(!value || (value && value.length < (maxLinks || Infinity))) && (
|
2019-06-25 17:38:51 +08:00
|
|
|
<Button variant="inverse" icon="fa fa-plus" onClick={() => onAdd()}>
|
2019-08-28 14:50:43 +08:00
|
|
|
Add link
|
2019-06-25 17:38:51 +08:00
|
|
|
</Button>
|
|
|
|
|
)}
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
DataLinksEditor.displayName = 'DataLinksEditor';
|