DataLinks: support 'onClick' from table panels (#36682)

This commit is contained in:
Ryan McKinley 2021-07-13 09:43:39 -07:00 committed by GitHub
parent dcc6663801
commit db5597ab9a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 48 additions and 34 deletions

View File

@ -393,6 +393,22 @@ export const getLinksSupplier = (
},
};
if (link.onClick) {
return {
href: link.url,
title: replaceVariables(link.title || '', variables),
target: link.targetBlank ? '_blank' : undefined,
onClick: (evt, origin) => {
link.onClick!({
origin: origin ?? field,
e: evt,
replaceVariables: (v) => replaceVariables(v, variables),
});
},
origin: field,
};
}
if (link.internal) {
// For internal links at the moment only destination is Explore.
return mapInternalLinkToExplore({
@ -403,20 +419,19 @@ export const getLinksSupplier = (
range: {} as any,
replaceVariables,
});
} else {
let href = locationUtil.assureBaseUrl(link.url.replace(/\n/g, ''));
href = replaceVariables(href, variables);
href = locationUtil.processUrl(href);
const info: LinkModel<Field> = {
href,
title: replaceVariables(link.title || '', variables),
target: link.targetBlank ? '_blank' : undefined,
origin: field,
};
return info;
}
let href = locationUtil.assureBaseUrl(link.url.replace(/\n/g, ''));
href = replaceVariables(href, variables);
href = locationUtil.processUrl(href);
const info: LinkModel<Field> = {
href,
title: replaceVariables(link.title || '', variables),
target: link.targetBlank ? '_blank' : undefined,
origin: field,
};
return info;
});
};

View File

@ -58,7 +58,7 @@ export interface LinkModel<T = any> {
origin: T;
// When a click callback exists, this is passed the raw mouse|react event
onClick?: (e: any) => void;
onClick?: (e: any, origin?: any) => void;
}
/**

View File

@ -14,12 +14,16 @@ export const getCellLinks = (field: Field, row: Row<any>) => {
})[0];
}
if (link && link.onClick) {
//const fieldLink = link?.onClick;
if (link?.onClick) {
onClick = (event) => {
// Allow opening in new tab
if (!(event.ctrlKey || event.metaKey || event.shiftKey) && link!.onClick) {
if (!(event.ctrlKey || event.metaKey || event.shiftKey)) {
event.preventDefault();
link!.onClick(event);
link!.onClick!(event, {
field,
rowIndex: row.index,
});
}
};
}

View File

@ -7,7 +7,6 @@ import {
DataFrame,
DataLink,
DataLinkBuiltInVars,
DataLinkClickEvent,
deprecationWarning,
Field,
FieldType,
@ -313,30 +312,26 @@ export class LinkSrv implements LinkService {
});
}
let onClick: ((event: DataLinkClickEvent) => void) | undefined = undefined;
if (link.onClick) {
onClick = (e: DataLinkClickEvent) => {
if (link.onClick) {
link.onClick({
origin,
replaceVariables,
e,
});
}
};
}
const info: LinkModel<T> = {
href: locationUtil.assureBaseUrl(href.replace(/\n/g, '')),
title: replaceVariables ? replaceVariables(link.title || '') : link.title,
title: link.title ?? '',
target: link.targetBlank ? '_blank' : undefined,
origin,
onClick,
};
if (replaceVariables) {
info.href = replaceVariables(info.href);
info.title = replaceVariables(link.title);
}
if (link.onClick) {
info.onClick = (e) => {
link.onClick!({
origin,
replaceVariables,
e,
});
};
}
info.href = getConfig().disableSanitizeHtml ? info.href : textUtil.sanitizeUrl(info.href);