Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

link variable reserved tokens #73

Merged
merged 1 commit into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion provisioning/dashboardData/links.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions provisioning/dashboardData/links.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,9 @@ cells:
link:
url: "\\\\< > bad link ${animationSiteVar} variable data"
params: null
andrew:
dataRef: "andy"
link:
url: "https://github.com/${cell.dataRef}${urlSiteWebsiteSurname}/${cell.name}b${urlSiteWebsiteSurname}-flow-panel"
params: null

7 changes: 3 additions & 4 deletions provisioning/dashboards/links.json

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion src/components/Config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export type Threshold = {
export type Link = {
url: string;
params: LinkUrlParams;
initialized: boolean | undefined;
};

export type Background = {
Expand Down
5 changes: 3 additions & 2 deletions src/components/FlowPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,10 @@ function clickHandlerFactory(elementAttribs: Map<string, SvgElementAttribs>, lin
return (event: React.MouseEvent<HTMLElement, MouseEvent>) => {
if (event.target) {
const element = event.target as HTMLElement;
const link = elementAttribs.get(element.id)?.link;
const attribs = elementAttribs.get(element.id);
const link = attribs?.link;
if (link) {
const url = constructUrl(link, linkVariables);
const url = constructUrl(link, attribs, linkVariables);
if (url) {
window.open(getTemplateSrv().replace(url));
}
Expand Down
4 changes: 4 additions & 0 deletions src/components/SvgUpdater.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export type SvgCell = {
};

export type SvgElementAttribs = {
name: string;
dataRef: string | null;
link: Link | null;
strokeColor: string | null;
fillColor: string | null;
Expand Down Expand Up @@ -197,6 +199,8 @@ export function svgInit(doc: Document, grafanaTheme: GrafanaTheme2, panelConfig:
[cell.textElements, cell.fillElements].forEach((arr) => {
arr.forEach((el) => {
elementAttribs.set(el.id, {
name: cellIdShort,
dataRef: panelConfigCell?.dataRef || null,
link: link || null,
strokeColor: el.getAttribute('stroke'),
fillColor: el.getAttribute('fill'),
Expand Down
32 changes: 22 additions & 10 deletions src/components/Utils.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GrafanaTheme2, colorManipulator } from '@grafana/data';
import { SvgAttribs, SvgCell } from 'components/SvgUpdater'
import { SvgAttribs, SvgCell, SvgElementAttribs } from 'components/SvgUpdater'
import { Background, HighlightFactors, Link, PanelConfigCellColor, Threshold, VariableThresholdScalars } from 'components/Config';
import { HighlightState } from './Highlighter';

Expand Down Expand Up @@ -52,15 +52,27 @@ export function createUrl(url: string) {
}
}

export function constructUrl(link: Link, linkVariables: Map<string, string>) {
if (!link.initialized) {
link.initialized = true;
linkVariables.forEach((value: string, key: string) => {
const token = '\$\{'.concat(key, '\}');
link.url = link.url.split(token).join(value);
});
link.url = createUrl(link.url) || "";
}
function tokenStr(key: string) {
return '\$\{'.concat(key, '\}');
}

export function constructUrl(link: Link, attribs: SvgElementAttribs, linkVariables: Map<string, string>) {
// Blend reserved tokens with variables
const linkVars = new Map([
['cell.name', attribs.name],
['cell.dataRef', attribs.dataRef || tokenStr('cell.dataRef')],
...linkVariables]);

// Substitute tokens
linkVars.forEach((value: string, key: string) => {
const token = tokenStr(key);
link.url = link.url.split(token).join(value);
});

// Generate url
link.url = createUrl(link.url) || "";

// Append window args
if (link.url.length) {
let url = link.url;
if (link.params === 'time') {
Expand Down
4 changes: 4 additions & 0 deletions yaml_defs/panelConfig.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ zoomPanPinch:
# Version 1.14.0 onwards: Link variables will be substituted in urls prior to passing through
# grafana variable substitution. Variables should be defined in the same format as grafana variables.
# i.e. ${urlBasePlugin}. These variables can be defined in siteConfig or panelConfig.
# Alongside these bespoke variables there are two reserved variables driven from the cell yaml of:
# ${cell.name}
# ${cell.dataRef}
# Substitution order is: reserved-vars -> siteConfig-vars -> panelConfig-vars -> grafana-vars
linkVariables:
urlBasePlugin: "https://github.com/andymchugh/andrewbmchugh-flow-panel/blob/main/"
urlBaseGrafana: "https://app.diagrams.net/"
Expand Down
4 changes: 4 additions & 0 deletions yaml_defs/siteConfig.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ variableThresholdScalars:
# Version 1.14.0 onwards: Link variables will be substituted in urls prior to passing through
# grafana variable substitution. Variables should be defined in the same format as grafana variables.
# i.e. ${urlBasePlugin}. These variables can be defined in siteConfig or panelConfig.
# Alongside these bespoke variables there are two reserved variables driven from the cell yaml of:
# ${cell.name}
# ${cell.dataRef}
# Substitution order is: reserved-vars -> siteConfig-vars -> panelConfig-vars -> grafana-vars
linkVariables:
urlBasePlugin: "https://github.com/andymchugh/andrewbmchugh-flow-panel/blob/main/"
urlBaseGrafana: "https://app.diagrams.net/"
Expand Down
Loading