Merge pull request #509 from metamaps/feature/import.export

import/export tweaks
This commit is contained in:
Devin Howard 2016-03-27 15:22:17 +08:00
commit dae3106b34
2 changed files with 39 additions and 15 deletions

View file

@ -34,14 +34,17 @@ Metamaps.Import = {
init: function() { init: function() {
var self = Metamaps.Import; var self = Metamaps.Import;
$('body').bind('paste', function(e) { $('body').bind('paste', function(e) {
if (e.target.tagName === "INPUT") return;
var text = e.originalEvent.clipboardData.getData('text/plain'); var text = e.originalEvent.clipboardData.getData('text/plain');
var results; var results;
if (text[0] === '{') { if (text[0] === '{') {
try { try {
results = JSON.parse(text); results = JSON.parse(text);
} catch (Error e) { } catch (e) {
results = false; results = false;
} }
} else { } else {
@ -63,12 +66,11 @@ Metamaps.Import = {
}, },
abort: function(message) { abort: function(message) {
alert("Sorry, something went wrong!\n\n" + message);
console.error(message); console.error(message);
}, },
simplify: function(string) { simplify: function(string) {
return string return string;
.replace(/(^\s*|\s*$)/g, '') .replace(/(^\s*|\s*$)/g, '')
.toLowerCase(); .toLowerCase();
}, },
@ -237,10 +239,28 @@ Metamaps.Import = {
var self = Metamaps.Import; var self = Metamaps.Import;
parsedSynapses.forEach(function(synapse) { parsedSynapses.forEach(function(synapse) {
self.createSynapseWithParameters( //only createSynapseWithParameters once both topics are persisted
synapse.desc, synapse.category, synapse.permission, var topic1 = Metamaps.Topics.get(self.cidMappings[node1_id]);
synapse.topic1, synapse.topic2 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, createSynapseWithParameters: function(description, category, permission,
node1_id, node2_id) { topic1, topic2) {
var self = Metamaps.Import; 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 node1 = topic1.get('node');
var node2 = topic2.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({ var synapse = new Metamaps.Backbone.Synapse({
desc: description, desc: description,
category: category, category: category,
permission: permission, permission: permission,
node1_id: node1.id, node1_id: topic1.id,
node2_id: node2.id, node2_id: topic2.id
}); });
Metamaps.Synapses.add(synapse);
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.cid,
}); });
Metamaps.Mappings.add(mapping);
Metamaps.Synapse.renderSynapse(mapping, synapse, node1, node2, true); Metamaps.Synapse.renderSynapse(mapping, synapse, node1, node2, true);
}, },

View file

@ -4683,7 +4683,7 @@ Metamaps.Synapse = {
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,
}); });