Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
protected filterEdge(edge: SEdgeSchema, index: SModelIndex): boolean {
const source = index.getById(edge.sourceId);
if (!source)
return false;
const sourceType = getBasicType(source);
if (sourceType === 'node' && !this.filterNode(source, index)
|| sourceType === 'port' && !this.filterPort(source, index))
return false;
const target = index.getById(edge.targetId);
if (!target)
return false;
const targetType = getBasicType(target);
if (targetType === 'node' && !this.filterNode(target, index)
|| targetType === 'port' && !this.filterPort(target, index))
return false;
return true;
}
layout(graph: SGraphSchema, index?: SModelIndex): SGraphSchema | Promise {
if (getBasicType(graph) !== 'graph') {
return graph;
}
if (!index) {
index = new SModelIndex();
index.add(graph);
}
const elkGraph = this.transformToElk(graph, index) as ElkNode;
return this.elk.layout(elkGraph).then(result => {
this.applyLayout(result, index!);
return graph;
});
}
for (const child of elkNode.children) {
this.applyLayout(child, index);
}
}
if (elkNode.edges) {
for (const elkEdge of elkNode.edges) {
const sedge = index.getById(elkEdge.id);
if (sedge && getBasicType(sedge) === 'edge') {
this.applyEdge(sedge as SEdgeSchema, elkEdge, index);
}
}
}
if (elkNode.labels) {
for (const elkLabel of elkNode.labels) {
const slabel = index.getById(elkLabel.id);
if (slabel && getBasicType(slabel) === 'label') {
this.applyShape(slabel as SLabelSchema, elkLabel);
}
}
}
if (elkNode.ports) {
for (const elkPort of elkNode.ports) {
const sport = index.getById(elkPort.id);
if (sport && getBasicType(sport) === 'port') {
this.applyShape(sport as SPortSchema, elkPort);
}
}
}
}
.filter(c => getBasicType(c) === 'label' && this.filter.apply(c, index))
.map(c => this.transformToElk(c, index)) as ElkLabel[];
apply(element: SModelElementSchema, index: SModelIndex): boolean {
switch (getBasicType(element)) {
case 'node':
return this.filterNode(element as SNodeSchema, index);
case 'edge':
return this.filterEdge(element as SEdgeSchema, index);
case 'label':
return this.filterLabel(element as SLabelSchema, index);
case 'port':
return this.filterPort(element as SPortSchema, index);
default:
return true;
}
}
protected transformToElk(smodel: SModelElementSchema, index: SModelIndex): ElkGraphElement {
switch (getBasicType(smodel)) {
case 'graph': {
const sgraph = smodel as SGraphSchema;
const elkGraph: ElkNode = {
id: sgraph.id,
layoutOptions: this.configurator.apply(sgraph, index)
};
if (sgraph.children) {
elkGraph.children = sgraph.children
.filter(c => getBasicType(c) === 'node' && this.filter.apply(c, index))
.map(c => this.transformToElk(c, index)) as ElkNode[];
elkGraph.edges = sgraph.children
.filter(c => getBasicType(c) === 'edge' && this.filter.apply(c, index))
.map(c => this.transformToElk(c, index)) as ElkEdge[];
}
return elkGraph;
}
protected filterEdge(edge: SEdgeSchema, index: SModelIndex): boolean {
const source = index.getById(edge.sourceId);
if (!source)
return false;
const sourceType = getBasicType(source);
if (sourceType === 'node' && !this.filterNode(source, index)
|| sourceType === 'port' && !this.filterPort(source, index))
return false;
const target = index.getById(edge.targetId);
if (!target)
return false;
const targetType = getBasicType(target);
if (targetType === 'node' && !this.filterNode(target, index)
|| targetType === 'port' && !this.filterPort(target, index))
return false;
return true;
}
protected applyLayout(elkNode: ElkNode, index: SModelIndex): void {
const snode = index.getById(elkNode.id);
if (snode && getBasicType(snode) === 'node') {
this.applyShape(snode as SNodeSchema, elkNode);
}
if (elkNode.children) {
for (const child of elkNode.children) {
this.applyLayout(child, index);
}
}
if (elkNode.edges) {
for (const elkEdge of elkNode.edges) {
const sedge = index.getById(elkEdge.id);
if (sedge && getBasicType(sedge) === 'edge') {
this.applyEdge(sedge as SEdgeSchema, elkEdge, index);
}
}
}
if (elkNode.labels) {
this.applyEdge(sedge as SEdgeSchema, elkEdge, index);
}
}
}
if (elkNode.labels) {
for (const elkLabel of elkNode.labels) {
const slabel = index.getById(elkLabel.id);
if (slabel && getBasicType(slabel) === 'label') {
this.applyShape(slabel as SLabelSchema, elkLabel);
}
}
}
if (elkNode.ports) {
for (const elkPort of elkNode.ports) {
const sport = index.getById(elkPort.id);
if (sport && getBasicType(sport) === 'port') {
this.applyShape(sport as SPortSchema, elkPort);
}
}
}
}
protected applyLayout(elkNode: ElkNode, index: SModelIndex): void {
const snode = index.getById(elkNode.id);
if (snode && getBasicType(snode) === 'node') {
this.applyShape(snode as SNodeSchema, elkNode);
}
if (elkNode.children) {
for (const child of elkNode.children) {
this.applyLayout(child, index);
}
}
if (elkNode.edges) {
for (const elkEdge of elkNode.edges) {
const sedge = index.getById(elkEdge.id);
if (sedge && getBasicType(sedge) === 'edge') {
this.applyEdge(sedge as SEdgeSchema, elkEdge, index);
}
}
}
if (elkNode.labels) {
for (const elkLabel of elkNode.labels) {
const slabel = index.getById(elkLabel.id);
if (slabel && getBasicType(slabel) === 'label') {
this.applyShape(slabel as SLabelSchema, elkLabel);
}
}
}
if (elkNode.ports) {
for (const elkPort of elkNode.ports) {
const sport = index.getById(elkPort.id);
if (sport && getBasicType(sport) === 'port') {