-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathHelperFunctions.ts
More file actions
73 lines (64 loc) · 2.59 KB
/
HelperFunctions.ts
File metadata and controls
73 lines (64 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { PipingNetworkSegmentProps } from "../types/diagram/Piping.ts";
import CommissioningPackage from "../types/CommissioningPackage.ts";
export function ensureArray<T>(value: T | T[]): T[] {
return Array.isArray(value) ? value : [value];
}
export const isBoundary = (
id: string,
commissioningPackage: CommissioningPackage,
) => commissioningPackage.boundaryNodes.some((node) => node.id === id);
export const isSelectedInternal = (
id: string,
commissioningPackage: CommissioningPackage,
) => commissioningPackage.selectedInternalNodes?.some((node) => node.id === id);
export const constructClasses = (
id: string,
activePackage: CommissioningPackage,
) => {
return `${isBoundary(id, activePackage) ? "boundary" : ""} ${isSelectedInternal(id, activePackage) ? "selectedInternal" : ""}`;
};
export const findPackageOfElement = (
packages: CommissioningPackage[],
nodeId: string,
) => {
return packages.find(
(pkg) =>
pkg.boundaryNodes?.some((node) => node.id === nodeId) ||
pkg.internalNodes?.some((node) => node.id === nodeId),
);
};
export const isInActivePackage = (
commissioningPackage: CommissioningPackage | undefined,
activePackageId: string,
) => {
return commissioningPackage
? activePackageId === commissioningPackage.id
: true;
};
// IRI CALCULATION
//TODO - remove when new graphical format implemented
export function iriFromSvgNode(id: string) {
return `https://assetid.equinor.com/plantx#${id}`;
}
export function iriFromPiping(segment: PipingNetworkSegmentProps) {
if (
segment.Connection?.ToID &&
(!segment.PipingComponent || !segment.PropertyBreak)
) {
return `https://assetid.equinor.com/plantx#${segment.Connection.ToID}-node${segment.Connection.ToNode}-connector`;
} else if (segment.PipingComponent && segment.PipingComponent[1]) {
return `https://assetid.equinor.com/plantx#${segment.PipingComponent[1].ID}-node2-connector`;
} else if (segment.Connection?.FromID) {
return `https://assetid.equinor.com/plantx#${segment.Connection.FromID}-node${segment.Connection.FromNode}-connector`;
} else if (segment.Connection?.ToID) {
return `https://assetid.equinor.com/plantx#${segment.Connection!.ToID}-node${segment.Connection!.ToNode}-connector`;
} else if (segment.PipingComponent?.ID) {
return `https://assetid.equinor.com/plantx#${segment.PipingComponent.ID}-node2-connector`;
} else {
console.error("Something went wrong with iri creation");
return ``;
}
}
export function setAttributes(el: Element, attrs: { [key: string]: string }) {
Object.keys(attrs).forEach((key) => el.setAttribute(key, attrs[key]));
}