Skip to content

Commit

Permalink
made nodes and edges arrays an observable
Browse files Browse the repository at this point in the history
  • Loading branch information
M-Wicenec committed Oct 16, 2023
1 parent b4d6ad4 commit fab02a7
Showing 1 changed file with 67 additions and 67 deletions.
134 changes: 67 additions & 67 deletions src/LogicalGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ import { Utils } from './Utils';

export class LogicalGraph {
fileInfo : ko.Observable<FileInfo>;
private nodes : Node[];
private edges : Edge[];
private nodes : ko.ObservableArray<Node>;
private edges : ko.ObservableArray<Edge>;

constructor(){
this.fileInfo = ko.observable(new FileInfo());
this.fileInfo().type = Eagle.FileType.Graph;
this.nodes = [];
this.edges = [];
this.nodes = ko.observableArray([]);
this.edges = ko.observableArray([]);
}

static toOJSJson = (graph : LogicalGraph, forTranslation : boolean) : object => {
Expand Down Expand Up @@ -127,7 +127,7 @@ export class LogicalGraph {
const extraUsedKeys: number[] = [];

const newNode = Node.fromOJSJson(nodeData, errorsWarnings, false, (): number => {
const resultKeys: number[] = Utils.getUsedKeys(result.nodes);
const resultKeys: number[] = Utils.getUsedKeys(result.nodes());
const nodeDataKeys: number[] = Utils.getUsedKeysFromNodeData(dataObject.nodeDataArray);
const combinedKeys: number[] = resultKeys.concat(nodeDataKeys.concat(extraUsedKeys));

Expand All @@ -153,7 +153,7 @@ export class LogicalGraph {
const parentIndex = GraphUpdater.findIndexOfNodeDataArrayWithKey(dataObject.nodeDataArray, nodeData.group);

if (parentIndex !== -1){
result.nodes[i].setParentKey(result.nodes[parentIndex].getKey());
result.nodes()[i].setParentKey(result.nodes()[parentIndex].getKey());
}
}

Expand All @@ -178,7 +178,7 @@ export class LogicalGraph {

// add a step here to check that no edges are incident on constructs, and move any edges found to the embedded applications
// add warnings to errorsWarnings
for (const edge of result.edges){
for (const edge of result.edges()){
// get references to actual source and destination nodes (from the keys)
const sourceNode : Node = result.findNodeByKey(edge.getSrcNodeKey());
const destinationNode : Node = result.findNodeByKey(edge.getDestNodeKey());
Expand Down Expand Up @@ -218,12 +218,12 @@ export class LogicalGraph {
}

getNodes = () : Node[] => {
return this.nodes;
return this.nodes();
}

getAllNodes = () : Node[] => {
let nodes : Node[] =[]
this.nodes.forEach(function(node){
this.nodes().forEach(function(node){
nodes.push(node)
if(node.isConstruct()){
if(node.getInputApplication()!= null){
Expand All @@ -238,19 +238,19 @@ export class LogicalGraph {
}

getNumNodes = () : number => {
return this.nodes.length;
return this.nodes().length;
}

addEdgeComplete = (edge : Edge) => {
this.edges.push(edge);
}

getEdges = () : Edge[] => {
return this.edges;
return this.edges();
}

getNumEdges = () : number => {
return this.edges.length;
return this.edges().length;
}

getCommentNodes = () : Node[] => {
Expand All @@ -268,7 +268,7 @@ export class LogicalGraph {
countEdgesIncidentOnNode = (node : Node) : number => {
let result: number = 0;

for (const edge of this.edges){
for (const edge of this.edges()){
if ((edge.getSrcNodeKey() === node.getKey() ) || ( edge.getDestNodeKey() === node.getKey() )){
result += 1;
}
Expand All @@ -280,8 +280,8 @@ export class LogicalGraph {
clear = () : void => {
this.fileInfo().clear();
this.fileInfo().type = Eagle.FileType.Graph;
this.nodes = [];
this.edges = [];
this.nodes([]);
this.edges([]);
}

clone = () : LogicalGraph => {
Expand All @@ -290,12 +290,12 @@ export class LogicalGraph {
result.fileInfo(this.fileInfo().clone());

// copy nodes
for (const node of this.nodes){
for (const node of this.nodes()){
result.nodes.push(node.clone());
}

// copy edges
for (const edge of this.edges){
for (const edge of this.edges()){
result.edges.push(edge.clone());
}

Expand Down Expand Up @@ -355,24 +355,24 @@ export class LogicalGraph {
}

findNodeByKey = (key : number) : Node => {
for (let i = this.nodes.length - 1; i >= 0 ; i--){
for (let i = this.nodes().length - 1; i >= 0 ; i--){

// check if the node itself has a matching key
if (this.nodes[i].getKey() === key){
return this.nodes[i];
if (this.nodes()[i].getKey() === key){
return this.nodes()[i];
}

// check if the node's inputApp has a matching key
if (this.nodes[i].hasInputApplication()){
if (this.nodes[i].getInputApplication().getKey() === key){
return this.nodes[i].getInputApplication();
if (this.nodes()[i].hasInputApplication()){
if (this.nodes()[i].getInputApplication().getKey() === key){
return this.nodes()[i].getInputApplication();
}
}

// check if the node's outputApp has a matching key
if (this.nodes[i].hasOutputApplication()){
if (this.nodes[i].getOutputApplication().getKey() === key){
return this.nodes[i].getOutputApplication();
if (this.nodes()[i].hasOutputApplication()){
if (this.nodes()[i].getOutputApplication().getKey() === key){
return this.nodes()[i].getOutputApplication();
}
}
}
Expand All @@ -382,49 +382,49 @@ export class LogicalGraph {

findNodeByKeyQuiet = (key : number) : Node => {
//used temporarily for the table modals to prevent console spam relating to too many calls when changing selected objects
for (let i = this.nodes.length - 1; i >= 0 ; i--){
for (let i = this.nodes().length - 1; i >= 0 ; i--){

// check if the node itself has a matching key
if (this.nodes[i].getKey() === key){
return this.nodes[i];
if (this.nodes()[i].getKey() === key){
return this.nodes()[i];
}

// check if the node's inputApp has a matching key
if (this.nodes[i].hasInputApplication()){
if (this.nodes[i].getInputApplication().getKey() === key){
return this.nodes[i].getInputApplication();
if (this.nodes()[i].hasInputApplication()){
if (this.nodes()[i].getInputApplication().getKey() === key){
return this.nodes()[i].getInputApplication();
}
}

// check if the node's outputApp has a matching key
if (this.nodes[i].hasOutputApplication()){
if (this.nodes[i].getOutputApplication().getKey() === key){
return this.nodes[i].getOutputApplication();
if (this.nodes()[i].hasOutputApplication()){
if (this.nodes()[i].getOutputApplication().getKey() === key){
return this.nodes()[i].getOutputApplication();
}
}
}
return null;
}

findNodeById = (id : string) : Node => {
for (let i = this.nodes.length - 1; i >= 0 ; i--){
for (let i = this.nodes().length - 1; i >= 0 ; i--){

// check if the node itself has a matching key
if (this.nodes[i].getId() === id){
return this.nodes[i];
if (this.nodes()[i].getId() === id){
return this.nodes()[i];
}

// check if the node's inputApp has a matching key
if (this.nodes[i].hasInputApplication()){
if (this.nodes[i].getInputApplication().getId() === id){
return this.nodes[i].getInputApplication();
if (this.nodes()[i].hasInputApplication()){
if (this.nodes()[i].getInputApplication().getId() === id){
return this.nodes()[i].getInputApplication();
}
}

// check if the node's outputApp has a matching key
if (this.nodes[i].hasOutputApplication()){
if (this.nodes[i].getOutputApplication().getId() === id){
return this.nodes[i].getOutputApplication();
if (this.nodes()[i].hasOutputApplication()){
if (this.nodes()[i].getOutputApplication().getId() === id){
return this.nodes()[i].getOutputApplication();
}
}
}
Expand Down Expand Up @@ -476,42 +476,42 @@ export class LogicalGraph {
}

// search through nodes in graph, looking for one with the correct key
for (let i = this.nodes.length - 1; i >= 0 ; i--){
for (let i = this.nodes().length - 1; i >= 0 ; i--){
// delete the node
if (this.nodes[i].getKey() === key){
if (this.nodes()[i].getKey() === key){
this.nodes.splice(i, 1);
continue;
}

// delete the input application
if (this.nodes[i].hasInputApplication() && this.nodes[i].getInputApplication().getKey() === key){
this.nodes[i].setInputApplication(null);
if (this.nodes()[i].hasInputApplication() && this.nodes()[i].getInputApplication().getKey() === key){
this.nodes()[i].setInputApplication(null);
}

// delete the output application
if (this.nodes[i].hasOutputApplication() && this.nodes[i].getOutputApplication().getKey() === key){
this.nodes[i].setOutputApplication(null);
if (this.nodes()[i].hasOutputApplication() && this.nodes()[i].getOutputApplication().getKey() === key){
this.nodes()[i].setOutputApplication(null);
}
}

// delete children
for (let i = this.nodes.length - 1; i >= 0 ; i--){
for (let i = this.nodes().length - 1; i >= 0 ; i--){
// check that iterator still points to a valid element in the nodes array
// a check like this wouldn't normally be necessary, but we are deleting elements from the array within the loop, so it might be shorter than we expect
if (i >= this.nodes.length){
if (i >= this.nodes().length){
continue;
}

if (this.nodes[i].getParentKey() === key){
this.removeNode(this.nodes[i]);
if (this.nodes()[i].getParentKey() === key){
this.removeNode(this.nodes()[i]);
}
}
}

findEdgeById = (id: string) : Edge => {
for (let i = this.edges.length - 1; i >= 0 ; i--){
if (this.edges[i].getId() === id){
return this.edges[i];
for (let i = this.edges().length - 1; i >= 0 ; i--){
if (this.edges()[i].getId() === id){
return this.edges()[i];
}
}
return null;
Expand All @@ -520,8 +520,8 @@ export class LogicalGraph {
removeEdgeById = (id: string) : void => {
let found = false;

for (let i = this.edges.length - 1; i >= 0 ; i--){
if (this.edges[i].getId() === id){
for (let i = this.edges().length - 1; i >= 0 ; i--){
if (this.edges()[i].getId() === id){
found = true;
this.edges.splice(i, 1);
}
Expand All @@ -534,16 +534,16 @@ export class LogicalGraph {

// delete edges that start from or end at the node with the given key
removeEdgesByKey = (key: number) : void => {
for (let i = this.edges.length - 1 ; i >= 0; i--){
const edge : Edge = this.edges[i];
for (let i = this.edges().length - 1 ; i >= 0; i--){
const edge : Edge = this.edges()[i];
if (edge.getSrcNodeKey() === key || edge.getDestNodeKey() === key){
this.edges.splice(i, 1);
}
}
}

portIsLinked = (nodeKey : number, portId : string) : boolean => {
for (const edge of this.edges){
for (const edge of this.edges()){
if (edge.getSrcNodeKey() === nodeKey && edge.getSrcPortId() === portId ||
edge.getDestNodeKey() === nodeKey && edge.getDestPortId() === portId){
return true;
Expand Down Expand Up @@ -638,7 +638,7 @@ export class LogicalGraph {
const overlaps : Node[] = [];

// find all the overlapping nodes
for (const node of this.nodes){
for (const node of this.nodes()){
// abort if checking for self!
if (node.getKey() === ignoreKey){
continue;
Expand Down Expand Up @@ -698,7 +698,7 @@ export class LogicalGraph {
getChildrenOfNodeByKey = (key: number) : Node[] => {
const result: Node[] = [];

for (const node of this.nodes){
for (const node of this.nodes()){
if (node.getParentKey() === key){
result.push(node);
}
Expand All @@ -707,12 +707,12 @@ export class LogicalGraph {
return result;
}

getNodesDrawOrdered = () : Node[] => {
getNodesDrawOrdered : ko.PureComputed<Node[]> = ko.pureComputed(() => {
const indexPlusDepths : {index:number, depth:number}[] = [];
const result : Node[] = [];

// populate index plus depths
for (let i = 0 ; i < this.getNodes().length ; i++){
for (let i = 0 ; i < this.nodes().length ; i++){
const node = this.getNodes()[i];

const depth = this.findDepthByKey(node.getKey());
Expand All @@ -731,7 +731,7 @@ export class LogicalGraph {
}

return result;
}
}, this);

static normaliseNodes = (nodes: Node[]) : {x: number, y: number} => {
let minX = Number.MAX_SAFE_INTEGER;
Expand Down

0 comments on commit fab02a7

Please sign in to comment.