Skip to content

Commit 7cbe49f

Browse files
committed
Extract helper functions and tidy
1 parent 2d48deb commit 7cbe49f

File tree

1 file changed

+62
-49
lines changed

1 file changed

+62
-49
lines changed

src/views/Graph.vue

Lines changed: 62 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,7 @@ export default {
678678
}
679679
}
680680
},
681+
681682
/**
682683
* Get a nested object of families
683684
* @property {Family} store - nested object of families
@@ -706,6 +707,7 @@ export default {
706707
}
707708
return store
708709
},
710+
709711
/**
710712
* Removes a node from an array of nodes
711713
* @property {String} nodeName - name of node to be removed
@@ -720,6 +722,7 @@ export default {
720722
})
721723
return nodesFiltered
722724
},
725+
723726
/**
724727
* Creates an edge from the name and cycle of source and target nodes
725728
* @property {String} edgeType - config option 'noCollapsed'|'collapsedTarget'|'collapsedSource'|'collapsedSourceAndTarget'
@@ -768,6 +771,7 @@ export default {
768771
},
769772
}
770773
},
774+
771775
/**
772776
* Removes an edge from an array of edges
773777
* @property {String} sourceName - name of source node of edge to be removed
@@ -785,6 +789,7 @@ export default {
785789
(edge) => edge.id.indexOf(edgeSearchTerm) === -1
786790
)
787791
},
792+
788793
/**
789794
* Removes an edges from an array of edges based on source node
790795
* @property {Node[]} edgeCheckSource - array of edges that have been identified as needing removal from the graph
@@ -806,6 +811,7 @@ export default {
806811
}
807812
return [edges, removedEdges]
808813
},
814+
809815
/**
810816
* Removes an edges from an array of edges based on target node
811817
* @property {Node[]} edgeCheckTarget - array of edges that have been identified as needing removal from the graph
@@ -827,6 +833,7 @@ export default {
827833
}
828834
return [edges, removedEdges]
829835
},
836+
830837
/**
831838
* Filters edges array based on source node
832839
* @property {String} sourceName - name of source node of edge to be removed
@@ -843,6 +850,7 @@ export default {
843850
})
844851
return edgeSearch
845852
},
853+
846854
/**
847855
* Filters edges array based on target node
848856
* @property {String} targetName - name of target node of edge to be removed
@@ -859,6 +867,7 @@ export default {
859867
})
860868
return edgeSearch
861869
},
870+
862871
/**
863872
* Check if node is collapsed by family or ancestor
864873
* If not collapsed return null
@@ -889,6 +898,7 @@ export default {
889898
return null
890899
}
891900
},
901+
892902
mountSVGPanZoom () {
893903
// Check the SVG is ready:
894904
// * The SVG document must be rendered with something in it before we can
@@ -938,9 +948,11 @@ export default {
938948
// Center the view after load:
939949
this.reset()
940950
},
951+
941952
setOption (option, value) {
942953
this[option] = value
943954
},
955+
944956
updateTimer () {
945957
// turn the timer on or off depending on the value of autoRefresh
946958
// if initialLoad is true we want to set a refresh interval
@@ -952,14 +964,17 @@ export default {
952964
this.refreshTimer = null
953965
}
954966
},
967+
955968
increaseSpacing () {
956969
// increase graph layout node spacing by 10%
957970
this.spacing = this.spacing * 1.1
958971
},
972+
959973
decreaseSpacing () {
960974
// decrease graph layout node spacing by 10%
961975
this.spacing = this.spacing * (10 / 11)
962976
},
977+
963978
getGraphNodes () {
964979
// list graph nodes from the store (non reactive list)
965980
const ret = []
@@ -972,6 +987,7 @@ export default {
972987
}
973988
return ret
974989
},
990+
975991
getGraphEdges () {
976992
// list graph edges from the store (non reactive list)
977993
const ret = []
@@ -982,6 +998,7 @@ export default {
982998
}
983999
return ret
9841000
},
1001+
9851002
/**
9861003
* Get the dimensions of currently rendered graph nodes
9871004
* (we feed these dimensions into the GraphViz dot code to improve layout).
@@ -1003,6 +1020,7 @@ export default {
10031020
}
10041021
return ret
10051022
},
1023+
10061024
/**
10071025
* Get the nodes binned by cycle point
10081026
*
@@ -1015,6 +1033,7 @@ export default {
10151033
return x
10161034
}, {})
10171035
},
1036+
10181037
/**
10191038
* Recursive function that adds a subgraph to the dot code
10201039
*
@@ -1108,6 +1127,7 @@ export default {
11081127
}
11091128
}
11101129
},
1130+
11111131
getDotCode (nodeDimensions, nodes, edges, cycles) {
11121132
// return GraphViz dot code for the given nodes, edges and dimensions
11131133
const ret = ['digraph {']
@@ -1228,19 +1248,22 @@ export default {
12281248
ret.push('}')
12291249
return ret.join('\n')
12301250
},
1251+
12311252
hashGraph (nodes, edges) {
12321253
// generate a hash for this list of nodes and edges
12331254
return nonCryptoHash(
12341255
nodes.map(n => n.id).reduce((x, y) => { return x + y }) +
12351256
(edges || []).map(n => n.id).reduce((x, y) => { return x + y }, 1)
12361257
)
12371258
},
1259+
12381260
reset () {
12391261
// pan / zoom so that the graph is centered and in frame
12401262
this.panZoomTo(
12411263
this.$refs.graph.getElementsByClassName('svg-pan-zoom_viewport')[0]
12421264
)
12431265
},
1266+
12441267
panZoomTo (ele) {
12451268
// pan / zoom so that the provided SVG element is centered and in frame
12461269
// Acknowledgment: Code adapted from suggestion from @iftahh in
@@ -1264,6 +1287,31 @@ export default {
12641287
const desiredWidth = 50 * Math.sqrt(bbox.width / 25) * 11 * realZoom
12651288
this.panZoomWidget.zoom(relativeZoom * width / desiredWidth)
12661289
},
1290+
1291+
edgeHasCollapsedTargetFamilyOnly (targetFirstFamily, sourceFirstFamily) {
1292+
const target = this.isNodeCollapsedByFamily(targetFirstFamily)
1293+
if (target && !this.isNodeCollapsedByFamily(sourceFirstFamily)) {
1294+
return { target }
1295+
}
1296+
},
1297+
1298+
edgeHasCollapsedSourceFamilyOnly (targetFirstFamily, sourceFirstFamily) {
1299+
const source = this.isNodeCollapsedByFamily(sourceFirstFamily)
1300+
if (source && !this.isNodeCollapsedByFamily(targetFirstFamily)) {
1301+
return { source }
1302+
}
1303+
},
1304+
1305+
edgeHasCollapsedTargetandSourceFamily (targetFirstFamily, sourceFirstFamily) {
1306+
const target = this.isNodeCollapsedByFamily(targetFirstFamily)
1307+
if (target) {
1308+
const source = this.isNodeCollapsedByFamily(sourceFirstFamily)
1309+
if (source) {
1310+
return { target, source }
1311+
}
1312+
}
1313+
},
1314+
12671315
async refresh () {
12681316
// refresh the graph layout if required
12691317
if (this.updating) {
@@ -1322,39 +1370,6 @@ export default {
13221370
}
13231371
}
13241372
1325-
const edgeHasCollapsedTargetFamilyOnly = (targetFirstFamily, sourceFirstFamily) => {
1326-
if (this.isNodeCollapsedByFamily(targetFirstFamily) && !this.isNodeCollapsedByFamily(sourceFirstFamily)) {
1327-
return {
1328-
target: this.isNodeCollapsedByFamily(targetFirstFamily),
1329-
source: undefined
1330-
}
1331-
} else {
1332-
return false
1333-
}
1334-
}
1335-
1336-
const edgeHasCollapsedSourceFamilyOnly = (targetFirstFamily, sourceFirstFamily) => {
1337-
if (!this.isNodeCollapsedByFamily(targetFirstFamily) && this.isNodeCollapsedByFamily(sourceFirstFamily)) {
1338-
return {
1339-
target: undefined,
1340-
source: this.isNodeCollapsedByFamily(sourceFirstFamily)
1341-
}
1342-
} else {
1343-
return false
1344-
}
1345-
}
1346-
1347-
const edgeHasCollapsedTargetandSourceFamily = (targetFirstFamily, sourceFirstFamily) => {
1348-
if (this.isNodeCollapsedByFamily(targetFirstFamily) && this.isNodeCollapsedByFamily(sourceFirstFamily)) {
1349-
return {
1350-
target: this.isNodeCollapsedByFamily(targetFirstFamily),
1351-
source: this.isNodeCollapsedByFamily(sourceFirstFamily)
1352-
}
1353-
} else {
1354-
return false
1355-
}
1356-
}
1357-
13581373
// ...this node is collapsed so need to remove any of its children (nodes and edges) from the graph if it has any...
13591374
for (const config of this.allChildrenLookUp[indexSearch.id]) {
13601375
const edgeCheckSource = this.checkForEdgeBySource(config.name, cycle, removedEdges)
@@ -1366,18 +1381,16 @@ export default {
13661381
const sourceFamilyName = this.cylcTree.$index[edge.node.source].node.firstParent.name
13671382
const targetFamilyName = this.cylcTree.$index[edge.node.target].node.firstParent.name
13681383
1369-
if (edgeHasCollapsedSourceFamilyOnly(targetFamilyName, sourceFamilyName)) {
1370-
if (!this.collapseCycle.includes(sourceCycle)) {
1371-
const familyData = edgeHasCollapsedSourceFamilyOnly(targetFamilyName, sourceFamilyName)
1384+
if (!this.collapseCycle.includes(sourceCycle)) {
1385+
const familyData = this.edgeHasCollapsedSourceFamilyOnly(targetFamilyName, sourceFamilyName)
1386+
if (familyData) {
13721387
this.edgeTemplate = this.createEdge('noCollapsed', familyData.source, targetName, sourceCycle, targetCycle)
13731388
edges.push(this.edgeTemplate)
13741389
}
1375-
}
13761390
1377-
if (edgeHasCollapsedTargetandSourceFamily(targetFamilyName, sourceFamilyName)) {
1378-
if (!this.collapseCycle.includes(sourceCycle) && !this.collapseCycle.includes(targetCycle)) {
1379-
const familyData = edgeHasCollapsedTargetandSourceFamily(targetFamilyName, sourceFamilyName)
1380-
if (familyData.source !== familyData.target || sourceCycle !== targetCycle) {
1391+
if (!this.collapseCycle.includes(targetCycle)) {
1392+
const familyData = this.edgeHasCollapsedTargetandSourceFamily(targetFamilyName, sourceFamilyName)
1393+
if (familyData && (familyData.source !== familyData.target || sourceCycle !== targetCycle)) {
13811394
this.edgeTemplate = this.createEdge('noCollapsed', familyData.source, familyData.target, sourceCycle, targetCycle)
13821395
edges.push(this.edgeTemplate)
13831396
}
@@ -1394,18 +1407,16 @@ export default {
13941407
const sourceFamilyName = this.cylcTree.$index[edge.node.source].node.firstParent.name
13951408
const targetFamilyName = this.cylcTree.$index[edge.node.target].node.firstParent.name
13961409
1397-
if (edgeHasCollapsedTargetFamilyOnly(targetFamilyName, sourceFamilyName)) {
1398-
if (!this.collapseCycle.includes(targetCycle)) {
1399-
const familyData = edgeHasCollapsedTargetFamilyOnly(targetFamilyName, sourceFamilyName)
1410+
if (!this.collapseCycle.includes(targetCycle)) {
1411+
const familyData = this.edgeHasCollapsedTargetFamilyOnly(targetFamilyName, sourceFamilyName)
1412+
if (familyData) {
14001413
this.edgeTemplate = this.createEdge('noCollapsed', sourceName, familyData.target, sourceCycle, targetCycle)
14011414
edges.push(this.edgeTemplate)
14021415
}
1403-
}
14041416
1405-
if (edgeHasCollapsedTargetandSourceFamily(targetFamilyName, sourceFamilyName)) {
1406-
if (!this.collapseCycle.includes(sourceCycle) && !this.collapseCycle.includes(targetCycle)) {
1407-
const familyData = edgeHasCollapsedTargetandSourceFamily(targetFamilyName, sourceFamilyName)
1408-
if (familyData.source !== familyData.target || sourceCycle !== targetCycle) {
1417+
if (!this.collapseCycle.includes(sourceCycle)) {
1418+
const familyData = this.edgeHasCollapsedTargetandSourceFamily(targetFamilyName, sourceFamilyName)
1419+
if (familyData && (familyData.source !== familyData.target || sourceCycle !== targetCycle)) {
14091420
this.edgeTemplate = this.createEdge('noCollapsed', familyData.source, familyData.target, sourceCycle, targetCycle)
14101421
edges.push(this.edgeTemplate)
14111422
}
@@ -1588,6 +1599,7 @@ export default {
15881599
this.graphID = graphID
15891600
this.updating = false
15901601
},
1602+
15911603
async waitFor (callback, retries = 10) {
15921604
// wait for things to render
15931605
// Will return when the callback returns something truthy.
@@ -1599,6 +1611,7 @@ export default {
15991611
await this.$nextTick()
16001612
}
16011613
},
1614+
16021615
/**
16031616
* Re-layout the graph after any new nodes have been rendered.
16041617
*

0 commit comments

Comments
 (0)