2017-02-01 17:47:45 +00:00
|
|
|
//import Matter, { Vector, Sleeping, World, Constraint, Composite, Runner, Common, Body, Bodies, Events } from 'matter-js'
|
2017-01-12 04:00:52 -05:00
|
|
|
import { last, sortBy, values } from 'lodash'
|
2017-01-05 00:05:18 -05:00
|
|
|
|
|
|
|
import $jit from '../patched/JIT'
|
2017-02-01 17:47:45 +00:00
|
|
|
import { getLayoutForData } from '../ConvoAlgo'
|
2017-01-05 00:05:18 -05:00
|
|
|
|
2017-01-12 12:31:08 -05:00
|
|
|
import Active from './Active'
|
2017-01-11 23:17:57 -05:00
|
|
|
import Create from './Create'
|
2017-01-05 00:05:18 -05:00
|
|
|
import DataModel from './DataModel'
|
2017-01-12 04:00:52 -05:00
|
|
|
import Mouse from './Mouse'
|
2017-01-11 23:17:57 -05:00
|
|
|
import JIT from './JIT'
|
2017-01-05 00:05:18 -05:00
|
|
|
import Visualize from './Visualize'
|
|
|
|
|
|
|
|
const Engine = {
|
2017-01-12 12:31:08 -05:00
|
|
|
init: (serverData) => {
|
2017-02-01 17:47:45 +00:00
|
|
|
|
2017-01-05 00:05:18 -05:00
|
|
|
},
|
|
|
|
run: init => {
|
2017-01-11 23:17:57 -05:00
|
|
|
if (init) {
|
2017-01-12 12:31:08 -05:00
|
|
|
if (Active.Mapper && Object.keys(Visualize.mGraph.graph.nodes).length) {
|
2017-01-12 04:00:52 -05:00
|
|
|
Engine.setFocusNode(Engine.findFocusNode(Visualize.mGraph.graph.nodes))
|
2017-02-01 17:47:45 +00:00
|
|
|
Engine.runLayout(true)
|
2017-01-12 04:00:52 -05:00
|
|
|
}
|
2017-01-11 23:17:57 -05:00
|
|
|
}
|
2017-01-05 00:05:18 -05:00
|
|
|
},
|
|
|
|
endActiveMap: () => {
|
2017-02-01 17:47:45 +00:00
|
|
|
|
2017-01-05 00:05:18 -05:00
|
|
|
},
|
2017-02-01 17:47:45 +00:00
|
|
|
runLayout: init => {
|
|
|
|
const synapses = DataModel.Synapses.map(s => s.attributes)
|
|
|
|
const topics = DataModel.Topics.map(t => t.attributes)
|
|
|
|
const focalNodeId = Create.newSynapse.focusNode.getData('topic').id
|
|
|
|
const focalCoords = init ? { x: 0, y: 0 } : Create.newSynapse.focusNode.pos
|
|
|
|
const layout = getLayoutForData(topics, synapses, focalNodeId, focalCoords)
|
|
|
|
Visualize.mGraph.graph.eachNode(n => {
|
|
|
|
let calculatedCoords = layout[n.id]
|
|
|
|
if (!calculatedCoords) {
|
|
|
|
calculatedCoords = {x: 0, y: 0}
|
|
|
|
}
|
|
|
|
const endPos = new $jit.Complex(calculatedCoords.x, calculatedCoords.y)
|
|
|
|
n.setPos(endPos, 'end')
|
|
|
|
})
|
|
|
|
Visualize.mGraph.animate({
|
|
|
|
modes: ['linear'],
|
|
|
|
transition: $jit.Trans.Elastic.easeOut,
|
|
|
|
duration: 200,
|
|
|
|
onComplete: () => {}
|
|
|
|
})
|
2017-01-11 23:17:57 -05:00
|
|
|
},
|
|
|
|
addNode: node => {
|
2017-02-01 17:47:45 +00:00
|
|
|
//Engine.runLayout()
|
2017-01-05 00:05:18 -05:00
|
|
|
},
|
2017-01-11 23:17:57 -05:00
|
|
|
removeNode: node => {
|
2017-02-01 17:47:45 +00:00
|
|
|
//Engine.runLayout()
|
2017-01-12 04:00:52 -05:00
|
|
|
},
|
|
|
|
findFocusNode: nodes => {
|
|
|
|
return last(sortBy(values(nodes), n => new Date(n.getData('topic').get('created_at'))))
|
|
|
|
},
|
|
|
|
setFocusNode: node => {
|
2017-01-12 12:31:08 -05:00
|
|
|
if (!Active.Mapper) return
|
2017-01-12 04:00:52 -05:00
|
|
|
Create.newSynapse.focusNode = node
|
2017-02-01 17:47:45 +00:00
|
|
|
Mouse.focusNodeCoords = node.pos
|
|
|
|
Mouse.newNodeCoords = {
|
|
|
|
x: node.x + 200,
|
|
|
|
y: node.y
|
2017-01-12 04:00:52 -05:00
|
|
|
}
|
2017-02-01 17:47:45 +00:00
|
|
|
Create.newSynapse.updateForm()
|
|
|
|
Create.newTopic.position()
|
2017-01-11 23:17:57 -05:00
|
|
|
},
|
|
|
|
addEdge: edge => {
|
2017-02-01 17:47:45 +00:00
|
|
|
Engine.runLayout()
|
2017-01-05 00:05:18 -05:00
|
|
|
},
|
2017-02-01 17:47:45 +00:00
|
|
|
removeEdge: edge => {
|
|
|
|
//Engine.runLayout()
|
2017-01-05 00:05:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Engine
|