whoops don't forget standard format

This commit is contained in:
Devin Howard 2016-04-14 14:16:16 +08:00
parent de62a08307
commit ca5928113d
4 changed files with 480 additions and 489 deletions

View file

@ -25,25 +25,25 @@ Metamaps.Synapse = {
if (Metamaps.Synapses.get(id) == undefined) { if (Metamaps.Synapses.get(id) == undefined) {
if (!callback) { if (!callback) {
var e = $.ajax({ var e = $.ajax({
url: "/synapses/" + id + ".json", url: '/synapses/' + id + '.json',
async: false async: false
}); })
Metamaps.Synapses.add($.parseJSON(e.responseText)); Metamaps.Synapses.add($.parseJSON(e.responseText))
return Metamaps.Synapses.get(id); return Metamaps.Synapses.get(id)
} else { } else {
return $.ajax({ return $.ajax({
url: "/synapses/" + id + ".json", url: '/synapses/' + id + '.json',
success: function (data) { success: function (data) {
Metamaps.Synapses.add(data); Metamaps.Synapses.add(data)
callback(Metamaps.Synapses.get(id)); callback(Metamaps.Synapses.get(id))
} }
}); })
} }
} else { } else {
if (!callback) { if (!callback) {
return Metamaps.Synapses.get(id); return Metamaps.Synapses.get(id)
} else { } else {
return callback(Metamaps.Synapses.get(id)); return callback(Metamaps.Synapses.get(id))
} }
} }
}, },
@ -52,47 +52,47 @@ Metamaps.Synapse = {
* *
*/ */
renderSynapse: function (mapping, synapse, node1, node2, createNewInDB) { renderSynapse: function (mapping, synapse, node1, node2, createNewInDB) {
var self = Metamaps.Synapse; var self = Metamaps.Synapse
var edgeOnViz; var edgeOnViz
var newedge = synapse.createEdge(mapping); var newedge = synapse.createEdge(mapping)
Metamaps.Visualize.mGraph.graph.addAdjacence(node1, node2, newedge.data); Metamaps.Visualize.mGraph.graph.addAdjacence(node1, node2, newedge.data)
edgeOnViz = Metamaps.Visualize.mGraph.graph.getAdjacence(node1.id, node2.id); edgeOnViz = Metamaps.Visualize.mGraph.graph.getAdjacence(node1.id, node2.id)
synapse.set('edge', edgeOnViz); synapse.set('edge', edgeOnViz)
synapse.updateEdge(); // links the synapse and the mapping to the edge synapse.updateEdge() // links the synapse and the mapping to the edge
Metamaps.Control.selectEdge(edgeOnViz); Metamaps.Control.selectEdge(edgeOnViz)
var mappingSuccessCallback = function (mappingModel, response) { var mappingSuccessCallback = function (mappingModel, response) {
var newSynapseData = { var newSynapseData = {
mappingid: mappingModel.id, mappingid: mappingModel.id,
mappableid: mappingModel.get('mappable_id') mappableid: mappingModel.get('mappable_id')
}; }
$(document).trigger(Metamaps.JIT.events.newSynapse, [newSynapseData]); $(document).trigger(Metamaps.JIT.events.newSynapse, [newSynapseData])
}; }
var synapseSuccessCallback = function (synapseModel, response) { var synapseSuccessCallback = function (synapseModel, response) {
if (Metamaps.Active.Map) { if (Metamaps.Active.Map) {
mapping.save({ mappable_id: synapseModel.id }, { mapping.save({ mappable_id: synapseModel.id }, {
success: mappingSuccessCallback success: mappingSuccessCallback
}); })
}
} }
};
if (!Metamaps.Settings.sandbox && createNewInDB) { if (!Metamaps.Settings.sandbox && createNewInDB) {
if (synapse.isNew()) { if (synapse.isNew()) {
synapse.save(null, { synapse.save(null, {
success: synapseSuccessCallback, success: synapseSuccessCallback,
error: function (model, response) { error: function (model, response) {
console.log('error saving synapse to database'); console.log('error saving synapse to database')
} }
}); })
} else if (!synapse.isNew() && Metamaps.Active.Map) { } else if (!synapse.isNew() && Metamaps.Active.Map) {
mapping.save(null, { mapping.save(null, {
success: mappingSuccessCallback success: mappingSuccessCallback
}); })
} }
} }
}, },
@ -103,67 +103,67 @@ Metamaps.Synapse = {
node1, node1,
node2, node2,
synapse, synapse,
mapping; mapping
$(document).trigger(Metamaps.Map.events.editedByActiveMapper); $(document).trigger(Metamaps.Map.events.editedByActiveMapper)
// for each node in this array we will create a synapse going to the position2 node. // for each node in this array we will create a synapse going to the position2 node.
var synapsesToCreate = []; var synapsesToCreate = []
topic2 = Metamaps.Topics.get(Metamaps.Create.newSynapse.topic2id); topic2 = Metamaps.Topics.get(Metamaps.Create.newSynapse.topic2id)
node2 = topic2.get('node'); node2 = topic2.get('node')
var len = Metamaps.Selected.Nodes.length; var len = Metamaps.Selected.Nodes.length
if (len == 0) { if (len == 0) {
topic1 = Metamaps.Topics.get(Metamaps.Create.newSynapse.topic1id); topic1 = Metamaps.Topics.get(Metamaps.Create.newSynapse.topic1id)
synapsesToCreate[0] = topic1.get('node'); synapsesToCreate[0] = topic1.get('node')
} else if (len > 0) { } else if (len > 0) {
synapsesToCreate = Metamaps.Selected.Nodes; synapsesToCreate = Metamaps.Selected.Nodes
} }
for (var i = 0; i < synapsesToCreate.length; i++) { for (var i = 0; i < synapsesToCreate.length; i++) {
node1 = synapsesToCreate[i]; node1 = synapsesToCreate[i]
topic1 = node1.getData('topic'); topic1 = node1.getData('topic')
synapse = new Metamaps.Backbone.Synapse({ synapse = new Metamaps.Backbone.Synapse({
desc: Metamaps.Create.newSynapse.description, desc: Metamaps.Create.newSynapse.description,
node1_id: topic1.isNew() ? topic1.cid : topic1.id, node1_id: topic1.isNew() ? topic1.cid : topic1.id,
node2_id: topic2.isNew() ? topic2.cid : topic2.id, node2_id: topic2.isNew() ? topic2.cid : topic2.id,
}); })
Metamaps.Synapses.add(synapse); Metamaps.Synapses.add(synapse)
mapping = new Metamaps.Backbone.Mapping({ mapping = new Metamaps.Backbone.Mapping({
mappable_type: "Synapse", mappable_type: 'Synapse',
mappable_id: synapse.cid, mappable_id: synapse.cid,
}); })
Metamaps.Mappings.add(mapping); Metamaps.Mappings.add(mapping)
// this function also includes the creation of the synapse in the database // this function also includes the creation of the synapse in the database
self.renderSynapse(mapping, synapse, node1, node2, true); self.renderSynapse(mapping, synapse, node1, node2, true)
} // for each in synapsesToCreate } // for each in synapsesToCreate
Metamaps.Create.newSynapse.hide(); Metamaps.Create.newSynapse.hide()
}, },
getSynapseFromAutocomplete: function (id) { getSynapseFromAutocomplete: function (id) {
var self = Metamaps.Synapse, var self = Metamaps.Synapse,
topic1, topic1,
topic2, topic2,
node1, node1,
node2; node2
var synapse = self.get(id); var synapse = self.get(id)
var mapping = new Metamaps.Backbone.Mapping({ var mapping = new Metamaps.Backbone.Mapping({
mappable_type: "Synapse", mappable_type: 'Synapse',
mappable_id: synapse.id, mappable_id: synapse.id,
}); })
Metamaps.Mappings.add(mapping); Metamaps.Mappings.add(mapping)
topic1 = Metamaps.Topics.get(Metamaps.Create.newSynapse.topic1id); topic1 = Metamaps.Topics.get(Metamaps.Create.newSynapse.topic1id)
node1 = topic1.get('node'); node1 = topic1.get('node')
topic2 = Metamaps.Topics.get(Metamaps.Create.newSynapse.topic2id); topic2 = Metamaps.Topics.get(Metamaps.Create.newSynapse.topic2id)
node2 = topic2.get('node'); node2 = topic2.get('node')
Metamaps.Create.newSynapse.hide(); Metamaps.Create.newSynapse.hide()
self.renderSynapse(mapping, synapse, node1, node2, true); self.renderSynapse(mapping, synapse, node1, node2, true)
} }
}; // end Metamaps.Synapse }; // end Metamaps.Synapse

View file

@ -33,70 +33,70 @@ Metamaps.Topic = {
get: function (id, callback) { get: function (id, callback) {
// if the desired topic is not yet in the local topic repository, fetch it // if the desired topic is not yet in the local topic repository, fetch it
if (Metamaps.Topics.get(id) == undefined) { if (Metamaps.Topics.get(id) == undefined) {
//console.log("Ajax call!"); // console.log("Ajax call!")
if (!callback) { if (!callback) {
var e = $.ajax({ var e = $.ajax({
url: "/topics/" + id + ".json", url: '/topics/' + id + '.json',
async: false async: false
}); })
Metamaps.Topics.add($.parseJSON(e.responseText)); Metamaps.Topics.add($.parseJSON(e.responseText))
return Metamaps.Topics.get(id); return Metamaps.Topics.get(id)
} else { } else {
return $.ajax({ return $.ajax({
url: "/topics/" + id + ".json", url: '/topics/' + id + '.json',
success: function (data) { success: function (data) {
Metamaps.Topics.add(data); Metamaps.Topics.add(data)
callback(Metamaps.Topics.get(id)); callback(Metamaps.Topics.get(id))
} }
}); })
} }
} else { } else {
if (!callback) { if (!callback) {
return Metamaps.Topics.get(id); return Metamaps.Topics.get(id)
} else { } else {
return callback(Metamaps.Topics.get(id)); return callback(Metamaps.Topics.get(id))
} }
} }
}, },
launch: function (id) { launch: function (id) {
var bb = Metamaps.Backbone; var bb = Metamaps.Backbone
var start = function (data) { var start = function (data) {
Metamaps.Active.Topic = new bb.Topic(data.topic); Metamaps.Active.Topic = new bb.Topic(data.topic)
Metamaps.Creators = new bb.MapperCollection(data.creators); Metamaps.Creators = new bb.MapperCollection(data.creators)
Metamaps.Topics = new bb.TopicCollection([data.topic].concat(data.relatives)); Metamaps.Topics = new bb.TopicCollection([data.topic].concat(data.relatives))
Metamaps.Synapses = new bb.SynapseCollection(data.synapses); Metamaps.Synapses = new bb.SynapseCollection(data.synapses)
Metamaps.Backbone.attachCollectionEvents(); Metamaps.Backbone.attachCollectionEvents()
// set filter mapper H3 text // set filter mapper H3 text
$('#filter_by_mapper h3').html('CREATORS'); $('#filter_by_mapper h3').html('CREATORS')
// build and render the visualization // build and render the visualization
Metamaps.Visualize.type = "RGraph"; Metamaps.Visualize.type = 'RGraph'
Metamaps.JIT.prepareVizData(); Metamaps.JIT.prepareVizData()
// update filters // update filters
Metamaps.Filter.reset(); Metamaps.Filter.reset()
// reset selected arrays // reset selected arrays
Metamaps.Selected.reset(); Metamaps.Selected.reset()
// these three update the actual filter box with the right list items // these three update the actual filter box with the right list items
Metamaps.Filter.checkMetacodes(); Metamaps.Filter.checkMetacodes()
Metamaps.Filter.checkSynapses(); Metamaps.Filter.checkSynapses()
Metamaps.Filter.checkMappers(); Metamaps.Filter.checkMappers()
} }
$.ajax({ $.ajax({
url: "/topics/" + id + "/network.json", url: '/topics/' + id + '/network.json',
success: start success: start
}); })
}, },
end: function () { end: function () {
if (Metamaps.Active.Topic) { if (Metamaps.Active.Topic) {
$('.rightclickmenu').remove(); $('.rightclickmenu').remove()
Metamaps.TopicCard.hideCard(); Metamaps.TopicCard.hideCard()
Metamaps.SynapseCard.hideCard(); Metamaps.SynapseCard.hideCard()
Metamaps.Filter.close(); Metamaps.Filter.close()
} }
}, },
centerOn: function (nodeid) { centerOn: function (nodeid) {
@ -104,268 +104,259 @@ Metamaps.Topic = {
Metamaps.Visualize.mGraph.onClick(nodeid, { Metamaps.Visualize.mGraph.onClick(nodeid, {
hideLabels: false, hideLabels: false,
duration: 1000, duration: 1000,
onComplete: function () { onComplete: function () {}
})
}
});
} }
}, },
fetchRelatives: function (node, metacode_id) { fetchRelatives: function (node, metacode_id) {
var topics = Metamaps.Topics.map(function (t) { return t.id })
var topics_string = topics.join()
var topics = Metamaps.Topics.map(function(t){ return t.id }); var creators = Metamaps.Creators.map(function (t) { return t.id })
var topics_string = topics.join(); var creators_string = creators.join()
var creators = Metamaps.Creators.map(function(t){ return t.id }); var topic = node.getData('topic')
var creators_string = creators.join();
var topic = node.getData('topic');
var successCallback = function (data) { var successCallback = function (data) {
if (data.creators.length > 0) Metamaps.Creators.add(data.creators); if (data.creators.length > 0) Metamaps.Creators.add(data.creators)
if (data.topics.length > 0) Metamaps.Topics.add(data.topics); if (data.topics.length > 0) Metamaps.Topics.add(data.topics)
if (data.synapses.length > 0) Metamaps.Synapses.add(data.synapses); if (data.synapses.length > 0) Metamaps.Synapses.add(data.synapses)
var topicColl = new Metamaps.Backbone.TopicCollection(data.topics); var topicColl = new Metamaps.Backbone.TopicCollection(data.topics)
topicColl.add(topic); topicColl.add(topic)
var synapseColl = new Metamaps.Backbone.SynapseCollection(data.synapses); var synapseColl = new Metamaps.Backbone.SynapseCollection(data.synapses)
var graph = Metamaps.JIT.convertModelsToJIT(topicColl, synapseColl)[0]; var graph = Metamaps.JIT.convertModelsToJIT(topicColl, synapseColl)[0]
Metamaps.Visualize.mGraph.op.sum(graph, { Metamaps.Visualize.mGraph.op.sum(graph, {
type: 'fade', type: 'fade',
duration: 500, duration: 500,
hideLabels: false hideLabels: false
}); })
var i, l, t, s; var i, l, t, s
Metamaps.Visualize.mGraph.graph.eachNode(function (n) { Metamaps.Visualize.mGraph.graph.eachNode(function (n) {
t = Metamaps.Topics.get(n.id); t = Metamaps.Topics.get(n.id)
t.set({ node: n }, { silent: true }); t.set({ node: n }, { silent: true })
t.updateNode(); t.updateNode()
n.eachAdjacency(function (edge) { n.eachAdjacency(function (edge) {
if (!edge.getData('init')) { if (!edge.getData('init')) {
edge.setData('init', true); edge.setData('init', true)
l = edge.getData('synapseIDs').length; l = edge.getData('synapseIDs').length
for (i = 0; i < l; i++) { for (i = 0; i < l; i++) {
s = Metamaps.Synapses.get(edge.getData('synapseIDs')[i]); s = Metamaps.Synapses.get(edge.getData('synapseIDs')[i])
s.set({ edge: edge }, { silent: true }); s.set({ edge: edge }, { silent: true })
s.updateEdge(); s.updateEdge()
} }
} }
}); })
}); })
}; }
var paramsString = metacode_id ? "metacode=" + metacode_id + "&" : ""; var paramsString = metacode_id ? 'metacode=' + metacode_id + '&' : ''
paramsString += "network=" + topics_string + "&creators=" + creators_string; paramsString += 'network=' + topics_string + '&creators=' + creators_string
$.ajax({ $.ajax({
type: "Get", type: 'Get',
url: "/topics/" + topic.id + "/relatives.json?" + paramsString, url: '/topics/' + topic.id + '/relatives.json?' + paramsString,
success: successCallback, success: successCallback,
error: function () { error: function () {}
})
}
});
}, },
/* /*
* *
* *
*/ */
renderTopic: function (mapping, topic, createNewInDB, permitCreateSynapseAfter) { renderTopic: function (mapping, topic, createNewInDB, permitCreateSynapseAfter) {
var self = Metamaps.Topic; var self = Metamaps.Topic
var nodeOnViz, tempPos; var nodeOnViz, tempPos
var newnode = topic.createNode(); var newnode = topic.createNode()
var midpoint = {}, pixelPos; var midpoint = {}, pixelPos
if (!$.isEmptyObject(Metamaps.Visualize.mGraph.graph.nodes)) { if (!$.isEmptyObject(Metamaps.Visualize.mGraph.graph.nodes)) {
Metamaps.Visualize.mGraph.graph.addNode(newnode); Metamaps.Visualize.mGraph.graph.addNode(newnode)
nodeOnViz = Metamaps.Visualize.mGraph.graph.getNode(newnode.id); nodeOnViz = Metamaps.Visualize.mGraph.graph.getNode(newnode.id)
topic.set('node', nodeOnViz, {silent: true}); topic.set('node', nodeOnViz, {silent: true})
topic.updateNode(); // links the topic and the mapping to the node topic.updateNode() // links the topic and the mapping to the node
nodeOnViz.setData("dim", 1, "start"); nodeOnViz.setData('dim', 1, 'start')
nodeOnViz.setData("dim", 25, "end"); nodeOnViz.setData('dim', 25, 'end')
if (Metamaps.Visualize.type === "RGraph") { if (Metamaps.Visualize.type === 'RGraph') {
tempPos = new $jit.Complex(mapping.get('xloc'), mapping.get('yloc')); tempPos = new $jit.Complex(mapping.get('xloc'), mapping.get('yloc'))
tempPos = tempPos.toPolar(); tempPos = tempPos.toPolar()
nodeOnViz.setPos(tempPos, "current"); nodeOnViz.setPos(tempPos, 'current')
nodeOnViz.setPos(tempPos, "start"); nodeOnViz.setPos(tempPos, 'start')
nodeOnViz.setPos(tempPos, "end"); nodeOnViz.setPos(tempPos, 'end')
} else if (Metamaps.Visualize.type === "ForceDirected") { } else if (Metamaps.Visualize.type === 'ForceDirected') {
nodeOnViz.setPos(new $jit.Complex(mapping.get('xloc'), mapping.get('yloc')), "current"); nodeOnViz.setPos(new $jit.Complex(mapping.get('xloc'), mapping.get('yloc')), 'current')
nodeOnViz.setPos(new $jit.Complex(mapping.get('xloc'), mapping.get('yloc')), "start"); nodeOnViz.setPos(new $jit.Complex(mapping.get('xloc'), mapping.get('yloc')), 'start')
nodeOnViz.setPos(new $jit.Complex(mapping.get('xloc'), mapping.get('yloc')), "end"); nodeOnViz.setPos(new $jit.Complex(mapping.get('xloc'), mapping.get('yloc')), 'end')
} }
if (Metamaps.Create.newTopic.addSynapse && permitCreateSynapseAfter) { if (Metamaps.Create.newTopic.addSynapse && permitCreateSynapseAfter) {
Metamaps.Create.newSynapse.topic1id = Metamaps.tempNode.getData('topic').id; Metamaps.Create.newSynapse.topic1id = Metamaps.tempNode.getData('topic').id
// position the form // position the form
midpoint.x = Metamaps.tempNode.pos.getc().x + (nodeOnViz.pos.getc().x - Metamaps.tempNode.pos.getc().x) / 2; midpoint.x = Metamaps.tempNode.pos.getc().x + (nodeOnViz.pos.getc().x - Metamaps.tempNode.pos.getc().x) / 2
midpoint.y = Metamaps.tempNode.pos.getc().y + (nodeOnViz.pos.getc().y - Metamaps.tempNode.pos.getc().y) / 2; midpoint.y = Metamaps.tempNode.pos.getc().y + (nodeOnViz.pos.getc().y - Metamaps.tempNode.pos.getc().y) / 2
pixelPos = Metamaps.Util.coordsToPixels(midpoint); pixelPos = Metamaps.Util.coordsToPixels(midpoint)
$('#new_synapse').css('left', pixelPos.x + "px"); $('#new_synapse').css('left', pixelPos.x + 'px')
$('#new_synapse').css('top', pixelPos.y + "px"); $('#new_synapse').css('top', pixelPos.y + 'px')
// show the form // show the form
Metamaps.Create.newSynapse.open(); Metamaps.Create.newSynapse.open()
Metamaps.Visualize.mGraph.fx.animate({ Metamaps.Visualize.mGraph.fx.animate({
modes: ["node-property:dim"], modes: ['node-property:dim'],
duration: 500, duration: 500,
onComplete: function () { onComplete: function () {
Metamaps.tempNode = null; Metamaps.tempNode = null
Metamaps.tempNode2 = null; Metamaps.tempNode2 = null
Metamaps.tempInit = false; Metamaps.tempInit = false
} }
}); })
} else { } else {
Metamaps.Visualize.mGraph.fx.plotNode(nodeOnViz, Metamaps.Visualize.mGraph.canvas); Metamaps.Visualize.mGraph.fx.plotNode(nodeOnViz, Metamaps.Visualize.mGraph.canvas)
Metamaps.Visualize.mGraph.fx.animate({ Metamaps.Visualize.mGraph.fx.animate({
modes: ["node-property:dim"], modes: ['node-property:dim'],
duration: 500, duration: 500,
onComplete: function () { onComplete: function () {}
})
}
});
} }
} else { } else {
Metamaps.Visualize.mGraph.loadJSON(newnode); Metamaps.Visualize.mGraph.loadJSON(newnode)
nodeOnViz = Metamaps.Visualize.mGraph.graph.getNode(newnode.id); nodeOnViz = Metamaps.Visualize.mGraph.graph.getNode(newnode.id)
topic.set('node', nodeOnViz, {silent: true}); topic.set('node', nodeOnViz, {silent: true})
topic.updateNode(); // links the topic and the mapping to the node topic.updateNode() // links the topic and the mapping to the node
nodeOnViz.setData("dim", 1, "start"); nodeOnViz.setData('dim', 1, 'start')
nodeOnViz.setData("dim", 25, "end"); nodeOnViz.setData('dim', 25, 'end')
nodeOnViz.setPos(new $jit.Complex(mapping.get('xloc'), mapping.get('yloc')), "current"); nodeOnViz.setPos(new $jit.Complex(mapping.get('xloc'), mapping.get('yloc')), 'current')
nodeOnViz.setPos(new $jit.Complex(mapping.get('xloc'), mapping.get('yloc')), "start"); nodeOnViz.setPos(new $jit.Complex(mapping.get('xloc'), mapping.get('yloc')), 'start')
nodeOnViz.setPos(new $jit.Complex(mapping.get('xloc'), mapping.get('yloc')), "end"); nodeOnViz.setPos(new $jit.Complex(mapping.get('xloc'), mapping.get('yloc')), 'end')
Metamaps.Visualize.mGraph.fx.plotNode(nodeOnViz, Metamaps.Visualize.mGraph.canvas); Metamaps.Visualize.mGraph.fx.plotNode(nodeOnViz, Metamaps.Visualize.mGraph.canvas)
Metamaps.Visualize.mGraph.fx.animate({ Metamaps.Visualize.mGraph.fx.animate({
modes: ["node-property:dim"], modes: ['node-property:dim'],
duration: 500, duration: 500,
onComplete: function () { onComplete: function () {}
})
}
});
} }
var mappingSuccessCallback = function (mappingModel, response) { var mappingSuccessCallback = function (mappingModel, response) {
var newTopicData = { var newTopicData = {
mappingid: mappingModel.id, mappingid: mappingModel.id,
mappableid: mappingModel.get('mappable_id') mappableid: mappingModel.get('mappable_id')
}; }
$(document).trigger(Metamaps.JIT.events.newTopic, [newTopicData]); $(document).trigger(Metamaps.JIT.events.newTopic, [newTopicData])
}; }
var topicSuccessCallback = function (topicModel, response) { var topicSuccessCallback = function (topicModel, response) {
if (Metamaps.Active.Map) { if (Metamaps.Active.Map) {
mapping.save({ mappable_id: topicModel.id }, { mapping.save({ mappable_id: topicModel.id }, {
success: mappingSuccessCallback, success: mappingSuccessCallback,
error: function (model, response) { error: function (model, response) {
console.log('error saving mapping to database'); console.log('error saving mapping to database')
} }
}); })
} }
if (Metamaps.Create.newTopic.addSynapse) { if (Metamaps.Create.newTopic.addSynapse) {
Metamaps.Create.newSynapse.topic2id = topicModel.id; Metamaps.Create.newSynapse.topic2id = topicModel.id
}
} }
};
if (!Metamaps.Settings.sandbox && createNewInDB) { if (!Metamaps.Settings.sandbox && createNewInDB) {
if (topic.isNew()) { if (topic.isNew()) {
topic.save(null, { topic.save(null, {
success: topicSuccessCallback, success: topicSuccessCallback,
error: function (model, response) { error: function (model, response) {
console.log('error saving topic to database'); console.log('error saving topic to database')
} }
}); })
} else if (!topic.isNew() && Metamaps.Active.Map) { } else if (!topic.isNew() && Metamaps.Active.Map) {
mapping.save(null, { mapping.save(null, {
success: mappingSuccessCallback success: mappingSuccessCallback
}); })
} }
} }
}, },
createTopicLocally: function () { createTopicLocally: function () {
var self = Metamaps.Topic; var self = Metamaps.Topic
if (Metamaps.Create.newTopic.name === "") { if (Metamaps.Create.newTopic.name === '') {
Metamaps.GlobalUI.notifyUser("Please enter a topic title..."); Metamaps.GlobalUI.notifyUser('Please enter a topic title...')
return; return
} }
// hide the 'double-click to add a topic' message // hide the 'double-click to add a topic' message
Metamaps.Famous.viz.hideInstructions(); Metamaps.Famous.viz.hideInstructions()
$(document).trigger(Metamaps.Map.events.editedByActiveMapper); $(document).trigger(Metamaps.Map.events.editedByActiveMapper)
var metacode = Metamaps.Metacodes.get(Metamaps.Create.newTopic.metacode); var metacode = Metamaps.Metacodes.get(Metamaps.Create.newTopic.metacode)
var topic = new Metamaps.Backbone.Topic({ var topic = new Metamaps.Backbone.Topic({
name: Metamaps.Create.newTopic.name, name: Metamaps.Create.newTopic.name,
metacode_id: metacode.id metacode_id: metacode.id
}); })
Metamaps.Topics.add(topic); Metamaps.Topics.add(topic)
var mapping = new Metamaps.Backbone.Mapping({ var mapping = new Metamaps.Backbone.Mapping({
xloc: Metamaps.Create.newTopic.x, xloc: Metamaps.Create.newTopic.x,
yloc: Metamaps.Create.newTopic.y, yloc: Metamaps.Create.newTopic.y,
mappable_id: topic.cid, mappable_id: topic.cid,
mappable_type: "Topic", mappable_type: 'Topic',
}); })
Metamaps.Mappings.add(mapping); Metamaps.Mappings.add(mapping)
// these can't happen until the value is retrieved, which happens in the line above // these can't happen until the value is retrieved, which happens in the line above
Metamaps.Create.newTopic.hide(); Metamaps.Create.newTopic.hide()
self.renderTopic(mapping, topic, true, true); // this function also includes the creation of the topic in the database self.renderTopic(mapping, topic, true, true) // this function also includes the creation of the topic in the database
}, },
getTopicFromAutocomplete: function (id) { getTopicFromAutocomplete: function (id) {
var self = Metamaps.Topic; var self = Metamaps.Topic
$(document).trigger(Metamaps.Map.events.editedByActiveMapper); $(document).trigger(Metamaps.Map.events.editedByActiveMapper)
Metamaps.Create.newTopic.hide(); Metamaps.Create.newTopic.hide()
var topic = self.get(id); var topic = self.get(id)
var mapping = new Metamaps.Backbone.Mapping({ var mapping = new Metamaps.Backbone.Mapping({
xloc: Metamaps.Create.newTopic.x, xloc: Metamaps.Create.newTopic.x,
yloc: Metamaps.Create.newTopic.y, yloc: Metamaps.Create.newTopic.y,
mappable_type: "Topic", mappable_type: 'Topic',
mappable_id: topic.id, mappable_id: topic.id,
}); })
Metamaps.Mappings.add(mapping); Metamaps.Mappings.add(mapping)
self.renderTopic(mapping, topic, true, true); self.renderTopic(mapping, topic, true, true)
}, },
getTopicFromSearch: function (event, id) { getTopicFromSearch: function (event, id) {
var self = Metamaps.Topic; var self = Metamaps.Topic
$(document).trigger(Metamaps.Map.events.editedByActiveMapper); $(document).trigger(Metamaps.Map.events.editedByActiveMapper)
var topic = self.get(id); var topic = self.get(id)
var nextCoords = Metamaps.Map.getNextCoord(); var nextCoords = Metamaps.Map.getNextCoord()
var mapping = new Metamaps.Backbone.Mapping({ var mapping = new Metamaps.Backbone.Mapping({
xloc: nextCoords.x, xloc: nextCoords.x,
yloc: nextCoords.y, yloc: nextCoords.y,
mappable_type: "Topic", mappable_type: 'Topic',
mappable_id: topic.id, mappable_id: topic.id,
}); })
Metamaps.Mappings.add(mapping); Metamaps.Mappings.add(mapping)
self.renderTopic(mapping, topic, true, true); self.renderTopic(mapping, topic, true, true)
Metamaps.GlobalUI.notifyUser('Topic was added to your map!'); Metamaps.GlobalUI.notifyUser('Topic was added to your map!')
event.stopPropagation(); event.stopPropagation()
event.preventDefault(); event.preventDefault()
return false; return false
} }
}; // end Metamaps.Topic }; // end Metamaps.Topic