import fixes
- better abort logic & messaging - handle \r line delim - better example format at top
This commit is contained in:
parent
ea677f8a6b
commit
c77cc32734
1 changed files with 47 additions and 17 deletions
|
@ -2,19 +2,30 @@
|
||||||
* Example tab-separated input:
|
* Example tab-separated input:
|
||||||
* Some fields will be ignored
|
* Some fields will be ignored
|
||||||
*
|
*
|
||||||
* id name metacode desc link user.name permission synapses
|
* Topics
|
||||||
* 1 topic1 Catalyst admin commons 1->7
|
* Id Name Metacode X Y Description Link User Permission
|
||||||
* 2 topic2 Event admin commons
|
* 8 topic8 Action -231 131 admin commons
|
||||||
* 5 topic Action admin commons
|
* 5 topic Action -229 -131 admin commons
|
||||||
* 6 topic6 Action admin commons 6->7
|
* 7 topic7.1 Action -470 -55 hey admin commons
|
||||||
* 7 topic7 Action admin commons 7->8 7<-6 7<-1
|
* 2 topic2 Event -57 -63 admin commons
|
||||||
* 8 topic8 Action admin commons 8<-7
|
* 1 topic1 Catalyst -51 50 admin commons
|
||||||
|
* 6 topic6 Action -425 63 admin commons
|
||||||
|
*
|
||||||
|
* Synapses
|
||||||
|
* Id Description Category Topic1 Topic2 User Permission
|
||||||
|
* 43 from-to 6 2 admin commons
|
||||||
|
* 44 from-to 6 1 admin commons
|
||||||
|
* 45 from-to 6 5 admin commons
|
||||||
|
* 46 from-to 2 7 admin commons
|
||||||
|
* 47 from-to 8 6 admin commons
|
||||||
|
* 48 from-to 8 1 admin commons
|
||||||
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Metamaps.Import = {
|
Metamaps.Import = {
|
||||||
topicWhitelist: [
|
topicWhitelist: [
|
||||||
'name', 'metacode', 'description', 'link', 'permission'
|
'name', 'metacode', 'description', 'link', 'permission'
|
||||||
],
|
],
|
||||||
synapseWhitelist: [
|
synapseWhitelist: [
|
||||||
'desc', 'description', 'category', 'topic1', 'topic2', 'permission'
|
'desc', 'description', 'category', 'topic1', 'topic2', 'permission'
|
||||||
],
|
],
|
||||||
|
@ -25,6 +36,8 @@ Metamaps.Import = {
|
||||||
var text = e.originalEvent.clipboardData.getData('text/plain');
|
var text = e.originalEvent.clipboardData.getData('text/plain');
|
||||||
|
|
||||||
var results = self.parseTabbedString(text);
|
var results = self.parseTabbedString(text);
|
||||||
|
if (results === false) return;
|
||||||
|
|
||||||
var topics = results.topics;
|
var topics = results.topics;
|
||||||
var synapses = results.synapses;
|
var synapses = results.synapses;
|
||||||
|
|
||||||
|
@ -39,9 +52,16 @@ 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) {
|
||||||
|
return string
|
||||||
|
.replace(/(^\s*|\s*$)/g, '')
|
||||||
|
.toLowerCase();
|
||||||
|
},
|
||||||
|
|
||||||
parseTabbedString: function(text) {
|
parseTabbedString: function(text) {
|
||||||
var self = Metamaps.Import;
|
var self = Metamaps.Import;
|
||||||
|
|
||||||
|
@ -49,9 +69,12 @@ Metamaps.Import = {
|
||||||
var delim = "\n";
|
var delim = "\n";
|
||||||
if (text.indexOf("\r\n") !== -1) {
|
if (text.indexOf("\r\n") !== -1) {
|
||||||
delim = "\r\n";
|
delim = "\r\n";
|
||||||
|
} else if (text.indexOf("\r") !== -1) {
|
||||||
|
delim = "\r";
|
||||||
}//if
|
}//if
|
||||||
|
|
||||||
var STATES = {
|
var STATES = {
|
||||||
|
ABORT: -1,
|
||||||
UNKNOWN: 0,
|
UNKNOWN: 0,
|
||||||
TOPICS_NEED_HEADERS: 1,
|
TOPICS_NEED_HEADERS: 1,
|
||||||
SYNAPSES_NEED_HEADERS: 2,
|
SYNAPSES_NEED_HEADERS: 2,
|
||||||
|
@ -67,7 +90,7 @@ Metamaps.Import = {
|
||||||
var synapseHeaders = [];
|
var synapseHeaders = [];
|
||||||
|
|
||||||
lines.forEach(function(line_raw, index) {
|
lines.forEach(function(line_raw, index) {
|
||||||
var line = line_raw.split(' '); // tab character
|
var line = line_raw.split("\t");
|
||||||
var noblanks = line.filter(function(elt) {
|
var noblanks = line.filter(function(elt) {
|
||||||
return elt !== "";
|
return elt !== "";
|
||||||
});
|
});
|
||||||
|
@ -76,10 +99,10 @@ Metamaps.Import = {
|
||||||
if (noblanks.length === 0) {
|
if (noblanks.length === 0) {
|
||||||
state = STATES.UNKNOWN;
|
state = STATES.UNKNOWN;
|
||||||
break;
|
break;
|
||||||
} else if (noblanks.length === 1 && line[0].toLowerCase() === 'topics') {
|
} else if (noblanks.length === 1 && self.simplify(line[0]) === 'topics') {
|
||||||
state = STATES.TOPICS_NEED_HEADERS;
|
state = STATES.TOPICS_NEED_HEADERS;
|
||||||
break;
|
break;
|
||||||
} else if (noblanks.length === 1 && line[0].toLowerCase() === 'synapses') {
|
} else if (noblanks.length === 1 && self.simplify(line[0]) === 'synapses') {
|
||||||
state = STATES.SYNAPSES_NEED_HEADERS;
|
state = STATES.SYNAPSES_NEED_HEADERS;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -89,7 +112,8 @@ Metamaps.Import = {
|
||||||
|
|
||||||
case STATES.TOPICS_NEED_HEADERS:
|
case STATES.TOPICS_NEED_HEADERS:
|
||||||
if (noblanks.length < 2) {
|
if (noblanks.length < 2) {
|
||||||
return self.abort("Not enough topic headers on line " + index);
|
self.abort("Not enough topic headers on line " + index);
|
||||||
|
state = STATES.ABORT;
|
||||||
}
|
}
|
||||||
topicHeaders = line.map(function(header, index) {
|
topicHeaders = line.map(function(header, index) {
|
||||||
return header.toLowerCase().replace('description', 'desc');
|
return header.toLowerCase().replace('description', 'desc');
|
||||||
|
@ -99,7 +123,8 @@ Metamaps.Import = {
|
||||||
|
|
||||||
case STATES.SYNAPSES_NEED_HEADERS:
|
case STATES.SYNAPSES_NEED_HEADERS:
|
||||||
if (noblanks.length < 2) {
|
if (noblanks.length < 2) {
|
||||||
return self.abort("Not enough synapse headers on line " + index);
|
self.abort("Not enough synapse headers on line " + index);
|
||||||
|
state = STATES.ABORT;
|
||||||
}
|
}
|
||||||
synapseHeaders = line.map(function(header, index) {
|
synapseHeaders = line.map(function(header, index) {
|
||||||
return header.toLowerCase().replace('description', 'desc');
|
return header.toLowerCase().replace('description', 'desc');
|
||||||
|
@ -148,14 +173,19 @@ Metamaps.Import = {
|
||||||
results.synapses.push(synapse);
|
results.synapses.push(synapse);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
case STATES.ABORT:
|
||||||
|
;
|
||||||
default:
|
default:
|
||||||
return self.abort("Invalid state while parsing import data. " +
|
self.abort("Invalid state while parsing import data. Check code.");
|
||||||
"Check code.");
|
state = STATES.ABORT;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return results;
|
if (state === STATES.ABORT) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
return results;
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue