From 387c863222df6d877bee7801ee1f40750f259a9f Mon Sep 17 00:00:00 2001 From: Devin Howard Date: Sun, 21 Feb 2016 17:02:37 +0800 Subject: [PATCH] fix a bug with synapses and use cid to link new topics with synapses Synapses are now created client-side, but still rejected server-side --- .../javascripts/src/Metamaps.Import.js.erb | 26 +++++++++++-------- app/assets/javascripts/src/Metamaps.js.erb | 6 ++--- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/app/assets/javascripts/src/Metamaps.Import.js.erb b/app/assets/javascripts/src/Metamaps.Import.js.erb index 8a366560..8d6b3867 100644 --- a/app/assets/javascripts/src/Metamaps.Import.js.erb +++ b/app/assets/javascripts/src/Metamaps.Import.js.erb @@ -24,11 +24,12 @@ Metamaps.Import = { topicWhitelist: [ - 'name', 'metacode', 'description', 'link', 'permission' + 'id', 'name', 'metacode', 'description', 'link', 'permission' ], synapseWhitelist: [ - 'desc', 'description', 'category', 'topic1', 'topic2', 'permission' + 'id', 'desc', 'description', 'category', 'topic1', 'topic2', 'permission' ], + cidMappings: {}, //to be filled by import_id => cid mappings init: function() { var self = Metamaps.Import; @@ -145,7 +146,7 @@ Metamaps.Import = { var header = topicHeaders[index]; if (self.topicWhitelist.indexOf(header) === -1) return; topic[header] = field; - if (header === 'x' || header === 'y') { + if (['id', 'x', 'y'].indexOf(header) !== -1) { topic[header] = parseInt(topic[header]); }//if }); @@ -166,8 +167,8 @@ Metamaps.Import = { var header = synapseHeaders[index]; if (self.synapseWhitelist.indexOf(header) === -1) return; synapse[header] = field; - if (header === 'topic1' || header === 'topic2') { - synapse[header] = parseInt(header); + if (['id', 'topic1', 'topic2'].indexOf(header) !== -1) { + synapse[header] = parseInt(synapse[header]); }//if }); results.synapses.push(synapse); @@ -235,6 +236,7 @@ Metamaps.Import = { createTopicWithParameters: function(name, metacode_name, permission, desc, link, xloc, yloc, import_id) { + var self = Metamaps.Import; $(document).trigger(Metamaps.Map.events.editedByActiveMapper); var metacode = Metamaps.Metacodes.where({name: metacode_name})[0] || null; if (metacode === null) return console.error("metacode not found"); @@ -245,9 +247,9 @@ Metamaps.Import = { permission: permission || Metamaps.Active.Map.get('permission'), desc: desc, link: link, - import_id: import_id }); Metamaps.Topics.add(topic); + self.cidMappings[import_id] = topic.cid; var mapping = new Metamaps.Backbone.Mapping({ xloc: xloc, @@ -260,13 +262,15 @@ Metamaps.Import = { // this function also includes the creation of the topic in the database Metamaps.Topic.renderTopic(mapping, topic, true, true); + Metamaps.Famous.viz.hideInstructions(); }, createSynapseWithParameters: function(description, category, permission, node1_id, node2_id) { - var topic1 = Metamaps.Topics.where({import_id: node1_id}); - var topic2 = Metamaps.Topics.where({import_id: node2_id}); + var self = Metamaps.Import; + var topic1 = Metamaps.Topics.get(self.cidMappings[node1_id]); + var topic2 = Metamaps.Topics.get(self.cidMappings[node2_id]); var node1 = topic1.get('node'); var node2 = topic2.get('node'); // TODO check if topic1 and topic2 were sucessfully found... @@ -275,13 +279,13 @@ Metamaps.Import = { desc: description, category: category, permission: permission, - node1_id: node1_id, - node2_id: node2_id, + node1_id: node1.id, + node2_id: node2.id, }); var mapping = new Metamaps.Backbone.Mapping({ mappable_type: "Synapse", - mappable_id: synapse.cid, + mappable_id: synapse.id, }); Metamaps.Synapse.renderSynapse(mapping, synapse, node1, node2, true); diff --git a/app/assets/javascripts/src/Metamaps.js.erb b/app/assets/javascripts/src/Metamaps.js.erb index 2eb08b70..85b12d48 100644 --- a/app/assets/javascripts/src/Metamaps.js.erb +++ b/app/assets/javascripts/src/Metamaps.js.erb @@ -377,7 +377,7 @@ Metamaps.Backbone.init = function () { mappable_id: this.isNew() ? this.cid : this.id }); }, - createEdge: function () { + createEdge: function (providedMapping) { var mapping, mappingID; var synapseID = this.isNew() ? this.cid : this.id; @@ -391,7 +391,7 @@ Metamaps.Backbone.init = function () { }; if (Metamaps.Active.Map) { - mapping = this.getMapping(); + mapping = providedMapping || this.getMapping(); mappingID = mapping.isNew() ? mapping.cid : mapping.id; edge.data.$mappings = []; edge.data.$mappingIDs = [mappingID]; @@ -4614,7 +4614,7 @@ Metamaps.Synapse = { var edgeOnViz; - var newedge = synapse.createEdge(); + var newedge = synapse.createEdge(mapping); Metamaps.Visualize.mGraph.graph.addAdjacence(node1, node2, newedge.data); edgeOnViz = Metamaps.Visualize.mGraph.graph.getAdjacence(node1.id, node2.id);