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

Fix NAD branch width when zooming in Microsoft Edge #114

Closed
wants to merge 16 commits into from
72 changes: 31 additions & 41 deletions src/components/network-area-diagram-viewer/dynamic-css-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,77 +9,67 @@ export enum THRESHOLD_STATUS {
BELOW = 'BELOW',
ABOVE = 'ABOVE',
}
export type CSS_DECLARATION = Record<string, string>;

export type CSS_DECLARATION = Record<string, (value: number) => string>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export type CSS_DECLARATION = Record<string, (value: number) => string>;
export type CSS_DECLARATION = Record<string, ((value: number) => string) | string>;

then use CSS_DECLARATION instead of Record<string, string> ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For updateSvgCssDisplayValue function, the type should be Record<string, string> or we have to consider both cases of string and (value: number) => string.
Instead, I kept the old CSS_DECLERATION type as Record<string, string> and added a new type DYNAMIC_CSS_DECLARATION = Record<string, ((value: number) => string) | string>; which gives the user the freedom to use a function or a value

export type CSS_RULE = {
cssSelector: string;
belowThresholdCssDeclaration: CSS_DECLARATION;
aboveThresholdCssDeclaration: CSS_DECLARATION;
threshold: number;
thresholdStatus: THRESHOLD_STATUS;
cssDeclaration: CSS_DECLARATION;
currentValue: Record<string, string>;
};

export function getValueFromThreshold(
value: number,
threshold: number,
aboveThreshold: string,
belowThreshold: string
) {
return value > threshold ? aboveThreshold : belowThreshold;
}

export const DEFAULT_DYNAMIC_CSS_RULES: CSS_RULE[] = [
{
cssSelector: '.nad-edge-infos', // data on edges (arrows and values)
belowThresholdCssDeclaration: { display: 'block' },
aboveThresholdCssDeclaration: { display: 'none' },
threshold: 2200,
thresholdStatus: THRESHOLD_STATUS.ABOVE,
cssDeclaration: { display: (value: number) => getValueFromThreshold(value, 2200, 'none', 'block') },
currentValue: { display: 'none' },
},
{
cssSelector: '.nad-label-box', // tooltips linked to nodes
belowThresholdCssDeclaration: { display: 'block' },
aboveThresholdCssDeclaration: { display: 'none' },
threshold: 3000,
thresholdStatus: THRESHOLD_STATUS.ABOVE,
cssDeclaration: { display: (value: number) => getValueFromThreshold(value, 3000, 'none', 'block') },
currentValue: { display: 'none' },
},
{
cssSelector: '.nad-text-edges', // visual link between nodes and their tooltip
belowThresholdCssDeclaration: { display: 'block' },
aboveThresholdCssDeclaration: { display: 'none' },
threshold: 3000,
thresholdStatus: THRESHOLD_STATUS.ABOVE,
cssDeclaration: { display: (value: number) => getValueFromThreshold(value, 3000, 'none', 'block') },
currentValue: { display: 'none' },
},
{
cssSelector: '[class^="nad-vl0to30"], [class*=" nad-vl0to30"]',
belowThresholdCssDeclaration: { display: 'block' },
aboveThresholdCssDeclaration: { display: 'none' },
threshold: 4000,
thresholdStatus: THRESHOLD_STATUS.BELOW,
cssDeclaration: { display: (value: number) => getValueFromThreshold(value, 4000, 'none', 'block') },
currentValue: { display: 'none' },
},
{
cssSelector: '[class^="nad-vl30to50"], [class*=" nad-vl30to50"]',
belowThresholdCssDeclaration: { display: 'block' },
aboveThresholdCssDeclaration: { display: 'none' },
threshold: 4000,
thresholdStatus: THRESHOLD_STATUS.BELOW,
cssDeclaration: { display: (value: number) => getValueFromThreshold(value, 4000, 'none', 'block') },
currentValue: { display: 'none' },
},
{
cssSelector: '[class^="nad-vl50to70"], [class*=" nad-vl50to70"]',
belowThresholdCssDeclaration: { display: 'block' },
aboveThresholdCssDeclaration: { display: 'none' },
threshold: 9000,
thresholdStatus: THRESHOLD_STATUS.BELOW,
cssDeclaration: { display: (value: number) => getValueFromThreshold(value, 9000, 'none', 'block') },
currentValue: { display: 'block' },
},
{
cssSelector: '[class^="nad-vl70to120"], [class*=" nad-vl70to120"]',
belowThresholdCssDeclaration: { display: 'block' },
aboveThresholdCssDeclaration: { display: 'none' },
threshold: 9000,
thresholdStatus: THRESHOLD_STATUS.BELOW,
cssDeclaration: { display: (value: number) => getValueFromThreshold(value, 9000, 'none', 'block') },
currentValue: { display: 'block' },
},
{
cssSelector: '[class^="nad-vl120to180"], [class*=" nad-vl120to180"]',
belowThresholdCssDeclaration: { display: 'block' },
aboveThresholdCssDeclaration: { display: 'none' },
threshold: 12000,
thresholdStatus: THRESHOLD_STATUS.BELOW,
cssDeclaration: { display: (value: number) => getValueFromThreshold(value, 12000, 'none', 'block') },
currentValue: { display: 'block' },
},
{
cssSelector: '[class^="nad-vl180to300"], [class*=" nad-vl180to300"]',
belowThresholdCssDeclaration: { display: 'block' },
aboveThresholdCssDeclaration: { display: 'none' },
threshold: 20000,
thresholdStatus: THRESHOLD_STATUS.BELOW,
cssDeclaration: { display: (value: number) => getValueFromThreshold(value, 20000, 'none', 'block') },
currentValue: { display: 'block' },
},
];
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import { Point, SVG, ViewBoxLike, Svg } from '@svgdotjs/svg.js';
import '@svgdotjs/svg.panzoom.js';
import * as DiagramUtils from './diagram-utils';
import { SvgParameters } from './svg-parameters';
import { CSS_RULE, DEFAULT_DYNAMIC_CSS_RULES } from './dynamic-css-utils';
import { LayoutParameters } from './layout-parameters';
import { DiagramMetadata, EdgeMetadata, BusNodeMetadata, NodeMetadata, TextNodeMetadata } from './diagram-metadata';
import { CSS_DECLARATION, CSS_RULE, THRESHOLD_STATUS, DEFAULT_DYNAMIC_CSS_RULES } from './dynamic-css-utils';

type DIMENSIONS = { width: number; height: number; viewbox: VIEWBOX };
type VIEWBOX = { x: number; y: number; width: number; height: number };
Expand Down Expand Up @@ -1275,7 +1275,7 @@ export class NetworkAreaDiagramViewer {
// Will explore the SVG's <style> tags to find the css rule associated with "cssSelector" and update the
// rule using "cssDeclaration".
// Will create a style tag or/and new css rule if not found in the SVG.
public updateSvgCssDisplayValue(svg: SVGSVGElement, cssSelector: string, cssDeclaration: CSS_DECLARATION) {
public updateSvgCssDisplayValue(svg: SVGSVGElement, cssSelector: string, cssDeclaration: Record<string, string>) {
const innerSvg = svg.querySelector('svg');
if (!innerSvg) {
console.error('Cannot find the SVG to update!');
Expand Down Expand Up @@ -1325,19 +1325,17 @@ export class NetworkAreaDiagramViewer {

public initializeDynamicCssRules(maxDisplayedSize: number) {
this.getDynamicCssRules().forEach((rule) => {
rule.thresholdStatus = maxDisplayedSize < rule.threshold ? THRESHOLD_STATUS.BELOW : THRESHOLD_STATUS.ABOVE;
for (const [property, getPropertyValue] of Object.entries(rule.cssDeclaration)) {
rule.currentValue[property] = getPropertyValue(maxDisplayedSize);
}
});
}

public injectDynamicCssRules(htmlElementSvg: HTMLElement) {
const rules = this.getDynamicCssRules()
.map((rule) => {
const ruleToInject =
rule.thresholdStatus === THRESHOLD_STATUS.BELOW
? rule.belowThresholdCssDeclaration
: rule.aboveThresholdCssDeclaration;
const key = Object.keys(ruleToInject)[0];
const value = ruleToInject[key];
const key = Object.keys(rule.currentValue)[0];
const value = rule.currentValue[key];
return `${rule.cssSelector} {${key}: ${value};}`;
})
.join('\n');
Expand Down Expand Up @@ -1365,18 +1363,22 @@ export class NetworkAreaDiagramViewer {
// We will check each dynamic css rule to see if we crossed a zoom threshold. If this is the case, we
// update the rule's threshold status and trigger the CSS change in the SVG.
this.getDynamicCssRules().forEach((rule) => {
if (rule.thresholdStatus === THRESHOLD_STATUS.ABOVE && maxDisplayedSize < rule.threshold) {
console.debug(
'CSS Rule ' + rule.cssSelector + ' below threshold ' + maxDisplayedSize + ' < ' + rule.threshold
);
rule.thresholdStatus = THRESHOLD_STATUS.BELOW;
this.updateSvgCssDisplayValue(svg, rule.cssSelector, rule.belowThresholdCssDeclaration);
} else if (rule.thresholdStatus === THRESHOLD_STATUS.BELOW && maxDisplayedSize >= rule.threshold) {
console.debug(
'CSS Rule ' + rule.cssSelector + ' above threshold ' + maxDisplayedSize + ' >= ' + rule.threshold
);
rule.thresholdStatus = THRESHOLD_STATUS.ABOVE;
this.updateSvgCssDisplayValue(svg, rule.cssSelector, rule.aboveThresholdCssDeclaration);
for (const [property, getPropertyValue] of Object.entries(rule.cssDeclaration)) {
const valueToUpdate = getPropertyValue(maxDisplayedSize);
if (valueToUpdate !== rule.currentValue[property]) {
console.debug(
'CSS Rule ' +
rule.cssSelector +
' will be update for ' +
maxDisplayedSize +
' from ' +
rule.currentValue[property] +
' to ' +
valueToUpdate
);
rule.currentValue[property] = valueToUpdate;
this.updateSvgCssDisplayValue(svg, rule.cssSelector, rule.currentValue);
}
}
});
}
Expand Down
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ export type {
} from './components/network-area-diagram-viewer/network-area-diagram-viewer';
export type { DiagramMetadata } from './components/network-area-diagram-viewer/diagram-metadata';
export { THRESHOLD_STATUS } from './components/network-area-diagram-viewer/dynamic-css-utils';
export type { CSS_DECLARATION, CSS_RULE } from './components/network-area-diagram-viewer/dynamic-css-utils';
export type {
CSS_DECLARATION,
CSS_RULE,
getValueFromThreshold,
} from './components/network-area-diagram-viewer/dynamic-css-utils';
export { SingleLineDiagramViewer } from './components/single-line-diagram-viewer/single-line-diagram-viewer';
export type {
OnToggleSldHoverCallbackType,
Expand Down
Loading