2016-10-02 22:28:00 +08:00
|
|
|
/* global $ */
|
2016-04-14 14:05:50 +08:00
|
|
|
|
2016-09-22 18:31:56 +08:00
|
|
|
import Active from './Active'
|
|
|
|
import Control from './Control'
|
|
|
|
import Create from './Create'
|
2016-10-03 08:32:37 +08:00
|
|
|
import DataModel from './DataModel'
|
2017-01-11 23:17:57 -05:00
|
|
|
import Engine from './Engine'
|
2016-09-22 18:31:56 +08:00
|
|
|
import JIT from './JIT'
|
|
|
|
import Map from './Map'
|
|
|
|
import Selected from './Selected'
|
|
|
|
import Settings from './Settings'
|
|
|
|
import Visualize from './Visualize'
|
|
|
|
|
2016-10-06 09:07:46 -04:00
|
|
|
const noOp = () => {}
|
2016-10-02 22:28:00 +08:00
|
|
|
|
2016-09-22 15:21:59 +08:00
|
|
|
const Synapse = {
|
2016-04-14 14:16:16 +08:00
|
|
|
// this function is to retrieve a synapse JSON object from the database
|
|
|
|
// @param id = the id of the synapse to retrieve
|
2016-11-07 15:25:08 -05:00
|
|
|
get: function(id, callback = noOp) {
|
2016-04-14 14:16:16 +08:00
|
|
|
// if the desired topic is not yet in the local topic repository, fetch it
|
2016-11-07 15:25:08 -05:00
|
|
|
if (DataModel.Synapses.get(id) === undefined) {
|
2016-10-06 09:07:46 -04:00
|
|
|
$.ajax({
|
|
|
|
url: '/synapses/' + id + '.json',
|
2016-11-07 15:25:08 -05:00
|
|
|
success: function(data) {
|
2016-10-02 22:28:00 +08:00
|
|
|
DataModel.Synapses.add(data)
|
|
|
|
callback(DataModel.Synapses.get(id))
|
2016-10-06 09:07:46 -04:00
|
|
|
}
|
|
|
|
})
|
2016-10-02 22:28:00 +08:00
|
|
|
} else callback(DataModel.Synapses.get(id))
|
2016-04-14 14:16:16 +08:00
|
|
|
},
|
2016-10-02 22:28:00 +08:00
|
|
|
|
2017-01-11 23:17:57 -05:00
|
|
|
renderSynapse: function(mapping, synapse, node1, node2, createNewInDB, alreadyAdded) {
|
2016-04-14 14:16:16 +08:00
|
|
|
var edgeOnViz
|
2017-01-11 23:17:57 -05:00
|
|
|
var newedge
|
2016-04-14 14:16:16 +08:00
|
|
|
|
2017-01-11 23:17:57 -05:00
|
|
|
if (!alreadyAdded) {
|
|
|
|
newedge = synapse.createEdge(mapping)
|
|
|
|
Visualize.mGraph.graph.addAdjacence(node1, node2, newedge.data)
|
|
|
|
}
|
2016-09-22 18:31:56 +08:00
|
|
|
edgeOnViz = Visualize.mGraph.graph.getAdjacence(node1.id, node2.id)
|
2017-01-11 23:17:57 -05:00
|
|
|
if (!alreadyAdded) {
|
|
|
|
Engine.addEdge(edgeOnViz)
|
|
|
|
}
|
2016-04-14 14:16:16 +08:00
|
|
|
synapse.set('edge', edgeOnViz)
|
|
|
|
synapse.updateEdge() // links the synapse and the mapping to the edge
|
|
|
|
|
2016-09-22 18:31:56 +08:00
|
|
|
Control.selectEdge(edgeOnViz)
|
2016-04-14 14:16:16 +08:00
|
|
|
|
2016-11-07 15:25:08 -05:00
|
|
|
var synapseSuccessCallback = function(synapseModel, response) {
|
2016-09-22 18:31:56 +08:00
|
|
|
if (Active.Map) {
|
2016-04-14 14:16:16 +08:00
|
|
|
mapping.save({ mappable_id: synapseModel.id }, {
|
2017-01-03 16:12:58 -05:00
|
|
|
error: function(model, response) {
|
|
|
|
console.log('error saving mapping to database')
|
|
|
|
}
|
2016-04-14 14:16:16 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-22 18:31:56 +08:00
|
|
|
if (!Settings.sandbox && createNewInDB) {
|
2016-04-14 14:16:16 +08:00
|
|
|
if (synapse.isNew()) {
|
|
|
|
synapse.save(null, {
|
|
|
|
success: synapseSuccessCallback,
|
2016-11-07 15:25:08 -05:00
|
|
|
error: function(model, response) {
|
2016-04-14 14:16:16 +08:00
|
|
|
console.log('error saving synapse to database')
|
|
|
|
}
|
|
|
|
})
|
2016-09-22 18:31:56 +08:00
|
|
|
} else if (!synapse.isNew() && Active.Map) {
|
2016-04-14 14:16:16 +08:00
|
|
|
mapping.save(null, {
|
2017-01-03 16:12:58 -05:00
|
|
|
error: function(model, response) {
|
|
|
|
console.log('error saving mapping to database')
|
|
|
|
}
|
2016-04-14 14:16:16 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2016-11-07 15:25:08 -05:00
|
|
|
createSynapseLocally: function() {
|
|
|
|
var self = Synapse
|
|
|
|
let topic1
|
|
|
|
let topic2
|
|
|
|
let node1
|
|
|
|
let node2
|
|
|
|
let synapse
|
|
|
|
let mapping
|
2016-04-14 14:16:16 +08:00
|
|
|
|
2016-09-22 18:31:56 +08:00
|
|
|
$(document).trigger(Map.events.editedByActiveMapper)
|
2016-04-14 14:16:16 +08:00
|
|
|
|
|
|
|
// for each node in this array we will create a synapse going to the position2 node.
|
|
|
|
var synapsesToCreate = []
|
|
|
|
|
2016-10-02 22:28:00 +08:00
|
|
|
topic2 = DataModel.Topics.get(Create.newSynapse.topic2id)
|
2016-04-14 14:16:16 +08:00
|
|
|
node2 = topic2.get('node')
|
|
|
|
|
2016-09-22 18:31:56 +08:00
|
|
|
var len = Selected.Nodes.length
|
2016-11-07 15:25:08 -05:00
|
|
|
if (len === 0) {
|
2016-10-02 22:28:00 +08:00
|
|
|
topic1 = DataModel.Topics.get(Create.newSynapse.topic1id)
|
2016-04-14 14:16:16 +08:00
|
|
|
synapsesToCreate[0] = topic1.get('node')
|
|
|
|
} else if (len > 0) {
|
2016-09-22 18:31:56 +08:00
|
|
|
synapsesToCreate = Selected.Nodes
|
2016-04-14 14:16:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (var i = 0; i < synapsesToCreate.length; i++) {
|
|
|
|
node1 = synapsesToCreate[i]
|
|
|
|
topic1 = node1.getData('topic')
|
2016-10-02 22:28:00 +08:00
|
|
|
synapse = new DataModel.Synapse({
|
2016-09-22 18:31:56 +08:00
|
|
|
desc: Create.newSynapse.description,
|
2016-09-28 10:32:28 +08:00
|
|
|
topic1_id: topic1.isNew() ? topic1.cid : topic1.id,
|
2016-11-07 15:25:08 -05:00
|
|
|
topic2_id: topic2.isNew() ? topic2.cid : topic2.id
|
2016-04-14 14:16:16 +08:00
|
|
|
})
|
2016-10-02 22:28:00 +08:00
|
|
|
DataModel.Synapses.add(synapse)
|
2016-04-14 14:16:16 +08:00
|
|
|
|
2016-10-02 22:28:00 +08:00
|
|
|
mapping = new DataModel.Mapping({
|
2016-04-14 14:16:16 +08:00
|
|
|
mappable_type: 'Synapse',
|
2016-11-07 15:25:08 -05:00
|
|
|
mappable_id: synapse.cid
|
2016-04-14 14:16:16 +08:00
|
|
|
})
|
2016-10-02 22:28:00 +08:00
|
|
|
DataModel.Mappings.add(mapping)
|
2016-04-14 14:16:16 +08:00
|
|
|
|
|
|
|
// this function also includes the creation of the synapse in the database
|
2017-01-11 23:17:57 -05:00
|
|
|
self.renderSynapse(mapping, synapse, node1, node2, true, Create.newSynapse.alreadyAdded)
|
2016-04-14 14:16:16 +08:00
|
|
|
} // for each in synapsesToCreate
|
|
|
|
|
2016-09-22 18:31:56 +08:00
|
|
|
Create.newSynapse.hide()
|
2016-04-14 14:16:16 +08:00
|
|
|
},
|
2016-11-07 15:25:08 -05:00
|
|
|
getSynapseFromAutocomplete: function(id) {
|
|
|
|
var self = Synapse
|
2016-04-14 14:16:16 +08:00
|
|
|
|
2016-10-06 09:07:46 -04:00
|
|
|
self.get(id, synapse => {
|
2016-11-07 15:25:08 -05:00
|
|
|
const mapping = new DataModel.Mapping({
|
2016-10-06 09:07:46 -04:00
|
|
|
mappable_type: 'Synapse',
|
2016-11-07 15:25:08 -05:00
|
|
|
mappable_id: synapse.id
|
2016-10-06 09:07:46 -04:00
|
|
|
})
|
2016-10-02 22:28:00 +08:00
|
|
|
DataModel.Mappings.add(mapping)
|
2016-11-07 15:25:08 -05:00
|
|
|
const topic1 = DataModel.Topics.get(Create.newSynapse.topic1id)
|
|
|
|
const node1 = topic1.get('node')
|
|
|
|
const topic2 = DataModel.Topics.get(Create.newSynapse.topic2id)
|
|
|
|
const node2 = topic2.get('node')
|
2016-10-06 09:07:46 -04:00
|
|
|
Create.newSynapse.hide()
|
|
|
|
self.renderSynapse(mapping, synapse, node1, node2, true)
|
2016-04-14 14:16:16 +08:00
|
|
|
})
|
|
|
|
}
|
2016-09-22 15:21:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export default Synapse
|