diff --git a/app/assets/javascripts/src/Metamaps.Import.js.erb b/app/assets/javascripts/src/Metamaps.Import.js.erb index bfce14bd..f30f65e5 100644 --- a/app/assets/javascripts/src/Metamaps.Import.js.erb +++ b/app/assets/javascripts/src/Metamaps.Import.js.erb @@ -34,14 +34,17 @@ Metamaps.Import = { init: function() { var self = Metamaps.Import; + $('body').bind('paste', function(e) { + if (e.target.tagName === "INPUT") return; + var text = e.originalEvent.clipboardData.getData('text/plain'); var results; if (text[0] === '{') { try { results = JSON.parse(text); - } catch (Error e) { + } catch (e) { results = false; } } else { @@ -63,12 +66,11 @@ Metamaps.Import = { }, abort: function(message) { - alert("Sorry, something went wrong!\n\n" + message); console.error(message); }, simplify: function(string) { - return string + return string; .replace(/(^\s*|\s*$)/g, '') .toLowerCase(); }, @@ -237,10 +239,28 @@ Metamaps.Import = { var self = Metamaps.Import; parsedSynapses.forEach(function(synapse) { - self.createSynapseWithParameters( - synapse.desc, synapse.category, synapse.permission, - synapse.topic1, synapse.topic2 - ); + //only createSynapseWithParameters once both topics are persisted + var topic1 = Metamaps.Topics.get(self.cidMappings[node1_id]); + var topic2 = Metamaps.Topics.get(self.cidMappings[node2_id]); + var synapse_created = false + topic1.once('sync', function() { + if (topic1.id && topic2.id && !synapse_created) { + synaprse_created = true + self.createSynapseWithParameters( + synapse.desc, synapse.category, synapse.permission, + topic1, topic2 + ); + }//if + }); + topic2.once('sync', function() { + if (topic1.id && topic2.id && !synapse_created) { + synaprse_created = true + self.createSynapseWithParameters( + synapse.desc, synapse.category, synapse.permission, + topic1, topic2 + ); + }//if + }); }); }, @@ -277,26 +297,30 @@ Metamaps.Import = { }, createSynapseWithParameters: function(description, category, permission, - node1_id, node2_id) { + topic1, topic2) { 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... + + if (!topic1.id || !topic2.id) { + console.error("missing topic id when creating synapse") + return; + }//if var synapse = new Metamaps.Backbone.Synapse({ desc: description, category: category, permission: permission, - node1_id: node1.id, - node2_id: node2.id, + node1_id: topic1.id, + node2_id: topic2.id }); + Metamaps.Synapses.add(synapse); var mapping = new Metamaps.Backbone.Mapping({ mappable_type: "Synapse", - mappable_id: synapse.id, + mappable_id: synapse.cid, }); + Metamaps.Mappings.add(mapping); 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 85b12d48..7a9d803b 100644 --- a/app/assets/javascripts/src/Metamaps.js.erb +++ b/app/assets/javascripts/src/Metamaps.js.erb @@ -4683,7 +4683,7 @@ Metamaps.Synapse = { node1 = synapsesToCreate[i]; topic1 = node1.getData('topic'); synapse = new Metamaps.Backbone.Synapse({ - desc: Metamaps.Create.newSynapse.description,// || "", + desc: Metamaps.Create.newSynapse.description, node1_id: topic1.isNew() ? topic1.cid : topic1.id, node2_id: topic2.isNew() ? topic2.cid : topic2.id, });