@ -9,7 +9,6 @@ import {
CanvasElementItem ,
canvasElementRegistry ,
CanvasElementOptions ,
TextConfig ,
CanvasConnection ,
} from 'app/features/canvas' ;
import { notFoundItem } from 'app/features/canvas/elements/notFound' ;
@ -107,20 +106,18 @@ export function onAddItem(sel: SelectableValue<string>, rootLayer: FrameState |
}
}
export function getDataLinks ( ctx : DimensionContext , cfg : TextConfig , textData : string | undefined ) : LinkModel [ ] {
const panelData = ctx . getPanelData ( ) ;
const frames = panelData ? . series ;
const links : Array < LinkModel < Field > > = [ ] ;
const linkLookup = new Set < string > ( ) ;
frames ? . forEach ( ( frame ) = > {
const visibleFields = frame . fields . filter ( ( field ) = > ! Boolean ( field . config . custom ? . hideFrom ? . tooltip ) ) ;
if ( cfg . text ? . field && visibleFields . some ( ( f ) = > getFieldDisplayName ( f , frame ) === cfg . text ? . field ) ) {
const field = visibleFields . filter ( ( field ) = > getFieldDisplayName ( field , frame ) === cfg . text ? . field ) [ 0 ] ;
/ *
* Provided a given field add any matching data links
* Mutates the links object in place which is then returned by the ` getDataLinks ` function downstream
* /
const addDataLinkForField = (
field : Field < unknown > ,
data : string | undefined ,
linkLookup : Set < string > ,
links : Array < LinkModel < Field > >
) : void = > {
if ( field ? . getLinks ) {
const disp = field . display ? field . display ( textD ata) : { text : ` ${ textD ata} ` , numeric : + textD ata! } ;
const disp = field . display ? field . display ( data ) : { text : ` ${ data } ` , numeric : + data ! } ;
field . getLinks ( { calculatedValue : disp } ) . forEach ( ( link ) = > {
const key = ` ${ link . title } / ${ link . href } ` ;
if ( ! linkLookup . has ( key ) ) {
@ -129,6 +126,135 @@ export function getDataLinks(ctx: DimensionContext, cfg: TextConfig, textData: s
}
} ) ;
}
} ;
// TODO: This could be refactored a fair amount, ideally the element specific config code should be owned by each element and not in this shared util file
export function getDataLinks (
dimensionContext : DimensionContext ,
elementOptions : CanvasElementOptions ,
data : string | undefined
) : LinkModel [ ] {
const panelData = dimensionContext . getPanelData ( ) ;
const frames = panelData ? . series ;
const links : Array < LinkModel < Field > > = [ ] ;
const linkLookup = new Set < string > ( ) ;
const elementConfig = elementOptions . config ;
frames ? . forEach ( ( frame ) = > {
const visibleFields = frame . fields . filter ( ( field ) = > ! Boolean ( field . config . custom ? . hideFrom ? . tooltip ) ) ;
// Text config
const isTextTiedToFieldData =
elementConfig . text ? . field &&
visibleFields . some ( ( field ) = > getFieldDisplayName ( field , frame ) === elementConfig . text ? . field ) ;
const isTextColorTiedToFieldData =
elementConfig . color ? . field &&
visibleFields . some ( ( field ) = > getFieldDisplayName ( field , frame ) === elementConfig . color ? . field ) ;
// General element config
const isElementBackgroundColorTiedToFieldData =
elementOptions ? . background ? . color ? . field &&
visibleFields . some ( ( field ) = > getFieldDisplayName ( field , frame ) === elementOptions ? . background ? . color ? . field ) ;
const isElementBackgroundImageTiedToFieldData =
elementOptions ? . background ? . image ? . field &&
visibleFields . some ( ( field ) = > getFieldDisplayName ( field , frame ) === elementOptions ? . background ? . image ? . field ) ;
const isElementBorderColorTiedToFieldData =
elementOptions ? . border ? . color ? . field &&
visibleFields . some ( ( field ) = > getFieldDisplayName ( field , frame ) === elementOptions ? . border ? . color ? . field ) ;
// Icon config
const isIconSVGTiedToFieldData =
elementConfig . path ? . field &&
visibleFields . some ( ( field ) = > getFieldDisplayName ( field , frame ) === elementConfig . path ? . field ) ;
const isIconColorTiedToFieldData =
elementConfig . fill ? . field &&
visibleFields . some ( ( field ) = > getFieldDisplayName ( field , frame ) === elementConfig . fill ? . field ) ;
// Wind turbine config (maybe remove / not support this?)
const isWindTurbineRPMTiedToFieldData =
elementConfig . rpm ? . field &&
visibleFields . some ( ( field ) = > getFieldDisplayName ( field , frame ) === elementConfig . rpm ? . field ) ;
// Server config
const isServerBlinkRateTiedToFieldData =
elementConfig . blinkRate ? . field &&
visibleFields . some ( ( field ) = > getFieldDisplayName ( field , frame ) === elementConfig . blinkRate ? . field ) ;
const isServerStatusColorTiedToFieldData =
elementConfig . statusColor ? . field &&
visibleFields . some ( ( field ) = > getFieldDisplayName ( field , frame ) === elementConfig . statusColor ? . field ) ;
const isServerBulbColorTiedToFieldData =
elementConfig . bulbColor ? . field &&
visibleFields . some ( ( field ) = > getFieldDisplayName ( field , frame ) === elementConfig . bulbColor ? . field ) ;
if ( isTextTiedToFieldData ) {
const field = visibleFields . filter ( ( field ) = > getFieldDisplayName ( field , frame ) === elementConfig . text ? . field ) [ 0 ] ;
addDataLinkForField ( field , data , linkLookup , links ) ;
}
if ( isTextColorTiedToFieldData ) {
const field = visibleFields . filter (
( field ) = > getFieldDisplayName ( field , frame ) === elementConfig . color ? . field
) [ 0 ] ;
addDataLinkForField ( field , data , linkLookup , links ) ;
}
if ( isElementBackgroundColorTiedToFieldData ) {
const field = visibleFields . filter (
( field ) = > getFieldDisplayName ( field , frame ) === elementOptions ? . background ? . color ? . field
) [ 0 ] ;
addDataLinkForField ( field , data , linkLookup , links ) ;
}
if ( isElementBackgroundImageTiedToFieldData ) {
const field = visibleFields . filter (
( field ) = > getFieldDisplayName ( field , frame ) === elementOptions ? . background ? . image ? . field
) [ 0 ] ;
addDataLinkForField ( field , data , linkLookup , links ) ;
}
if ( isElementBorderColorTiedToFieldData ) {
const field = visibleFields . filter (
( field ) = > getFieldDisplayName ( field , frame ) === elementOptions ? . border ? . color ? . field
) [ 0 ] ;
addDataLinkForField ( field , data , linkLookup , links ) ;
}
if ( isIconSVGTiedToFieldData ) {
const field = visibleFields . filter ( ( field ) = > getFieldDisplayName ( field , frame ) === elementConfig . path ? . field ) [ 0 ] ;
addDataLinkForField ( field , data , linkLookup , links ) ;
}
if ( isIconColorTiedToFieldData ) {
const field = visibleFields . filter ( ( field ) = > getFieldDisplayName ( field , frame ) === elementConfig . fill ? . field ) [ 0 ] ;
addDataLinkForField ( field , data , linkLookup , links ) ;
}
if ( isWindTurbineRPMTiedToFieldData ) {
const field = visibleFields . filter ( ( field ) = > getFieldDisplayName ( field , frame ) === elementConfig . rpm ? . field ) [ 0 ] ;
addDataLinkForField ( field , data , linkLookup , links ) ;
}
if ( isServerBlinkRateTiedToFieldData ) {
const field = visibleFields . filter (
( field ) = > getFieldDisplayName ( field , frame ) === elementConfig . blinkRate ? . field
) [ 0 ] ;
addDataLinkForField ( field , data , linkLookup , links ) ;
}
if ( isServerStatusColorTiedToFieldData ) {
const field = visibleFields . filter (
( field ) = > getFieldDisplayName ( field , frame ) === elementConfig . statusColor ? . field
) [ 0 ] ;
addDataLinkForField ( field , data , linkLookup , links ) ;
}
if ( isServerBulbColorTiedToFieldData ) {
const field = visibleFields . filter (
( field ) = > getFieldDisplayName ( field , frame ) === elementConfig . bulbColor ? . field
) [ 0 ] ;
addDataLinkForField ( field , data , linkLookup , links ) ;
}
} ) ;