metamaps--metamaps/frontend/src/Metamaps/Organize.js
Devin Howard 0562134157 low hanging fruit
Here is my TODO list:

already done
==> Account.js <==
==> Admin.js <==
==> AutoLayout.js <==
==> Listeners.js <==
==> Mapper.js <==
==> Organize.js <==
==> PasteInput.js <==
==> ReactComponents.js <==
==> Util.js <==

TODO (I think) simple to make modular
==> Backbone.js <==
==> Control.js <==
==> Create.js <==
==> Filter.js <==
==> Import.js <==
==> Mobile.js <==
==> Synapse.js <==
==> SynapseCard.js <==
==> Topic.js <==
==> TopicCard.js <==
==> Views.js <==
==> Visualize.js <==

TODO hard to make modular
==> Constants.js <==
==> Debug.js <==
==> GlobalUI.js <==
==> JIT.js <==
==> Map.js <==
==> Realtime.js <==
==> Router.js <==
2016-09-22 17:00:36 +08:00

116 lines
4.2 KiB
JavaScript

/* global $ */
import Visualize from './Visualize'
import JIT from './JIT'
const Organize = {
arrange: function (layout, centerNode) {
// first option for layout to implement is 'grid', will do an evenly spaced grid with its center at the 0,0 origin
if (layout == 'grid') {
var numNodes = _.size(Visualize.mGraph.graph.nodes); // this will always be an integer, the # of nodes on your graph visualization
var numColumns = Math.floor(Math.sqrt(numNodes)) // the number of columns to make an even grid
var GRIDSPACE = 400
var row = 0
var column = 0
Visualize.mGraph.graph.eachNode(function (n) {
if (column == numColumns) {
column = 0
row += 1
}
var newPos = new $jit.Complex()
newPos.x = column * GRIDSPACE
newPos.y = row * GRIDSPACE
n.setPos(newPos, 'end')
column += 1
})
Visualize.mGraph.animate(JIT.ForceDirected.animateSavedLayout)
} else if (layout == 'grid_full') {
// this will always be an integer, the # of nodes on your graph visualization
var numNodes = _.size(Visualize.mGraph.graph.nodes)
// var numColumns = Math.floor(Math.sqrt(numNodes)) // the number of columns to make an even grid
// var GRIDSPACE = 400
var height = Visualize.mGraph.canvas.getSize(0).height
var width = Visualize.mGraph.canvas.getSize(0).width
var totalArea = height * width
var cellArea = totalArea / numNodes
var ratio = height / width
var cellWidth = sqrt(cellArea / ratio)
var cellHeight = cellArea / cellWidth
var row = floor(height / cellHeight)
var column = floor(width / cellWidth)
var totalCells = row * column
if (totalCells)
Visualize.mGraph.graph.eachNode(function (n) {
if (column == numColumns) {
column = 0
row += 1
}
var newPos = new $jit.Complex()
newPos.x = column * GRIDSPACE
newPos.y = row * GRIDSPACE
n.setPos(newPos, 'end')
column += 1
})
Visualize.mGraph.animate(JIT.ForceDirected.animateSavedLayout)
} else if (layout == 'radial') {
var centerX = centerNode.getPos().x
var centerY = centerNode.getPos().y
centerNode.setPos(centerNode.getPos(), 'end')
console.log(centerNode.adjacencies)
var lineLength = 200
var usedNodes = {}
usedNodes[centerNode.id] = centerNode
var radial = function (node, level, degree) {
if (level == 1) {
var numLinksTemp = _.size(node.adjacencies)
var angleTemp = 2 * Math.PI / numLinksTemp
} else {
angleTemp = 2 * Math.PI / 20
}
node.eachAdjacency(function (a) {
var isSecondLevelNode = (centerNode.adjacencies[a.nodeTo.id] != undefined && level > 1)
if (usedNodes[a.nodeTo.id] == undefined && !isSecondLevelNode) {
var newPos = new $jit.Complex()
newPos.x = level * lineLength * Math.sin(degree) + centerX
newPos.y = level * lineLength * Math.cos(degree) + centerY
a.nodeTo.setPos(newPos, 'end')
usedNodes[a.nodeTo.id] = a.nodeTo
radial(a.nodeTo, level + 1, degree)
degree += angleTemp
}
})
}
radial(centerNode, 1, 0)
Visualize.mGraph.animate(JIT.ForceDirected.animateSavedLayout)
} else if (layout == 'center_viewport') {
var lowX = 0,
lowY = 0,
highX = 0,
highY = 0
var oldOriginX = Visualize.mGraph.canvas.translateOffsetX
var oldOriginY = Visualize.mGraph.canvas.translateOffsetY
Visualize.mGraph.graph.eachNode(function (n) {
if (n.id === 1) {
lowX = n.getPos().x
lowY = n.getPos().y
highX = n.getPos().x
highY = n.getPos().y
}
if (n.getPos().x < lowX) lowX = n.getPos().x
if (n.getPos().y < lowY) lowY = n.getPos().y
if (n.getPos().x > highX) highX = n.getPos().x
if (n.getPos().y > highY) highY = n.getPos().y
})
console.log(lowX, lowY, highX, highY)
var newOriginX = (lowX + highX) / 2
var newOriginY = (lowY + highY) / 2
} else alert('please call function with a valid layout dammit!')
}
}
export default Organize