metamaps--metamaps/app/assets/javascripts/src/Metamaps.Import.js.erb

323 lines
9.4 KiB
Text
Raw Normal View History

2016-03-27 11:43:30 +00:00
/* global Metamaps, $ */
/*
2016-03-27 11:43:30 +00:00
* Metamaps.Import.js.erb
*
2016-03-27 11:43:30 +00:00
* Dependencies:
* - Metamaps.Active
* - Metamaps.Backbone
* - Metamaps.Famous // TODO remove dependency
* - Metamaps.Map
* - Metamaps.Mappings
* - Metamaps.Metacodes
* - Metamaps.Synapses
* - Metamaps.Topics
*/
Metamaps.Import = {
// note that user is not imported
topicWhitelist: [
'id', 'name', 'metacode', 'x', 'y', 'description', 'link', 'permission'
],
synapseWhitelist: [
'topic1', 'topic2', 'category', 'desc', 'description', 'permission'
],
2016-03-27 11:43:30 +00:00
cidMappings: {}, // to be filled by import_id => cid mappings
2016-03-27 11:43:30 +00:00
init: function () {
var self = Metamaps.Import
2016-03-27 11:43:30 +00:00
$('body').bind('paste', function (e) {
if (e.target.tagName === 'INPUT') return
2016-03-27 11:43:30 +00:00
var text = e.originalEvent.clipboardData.getData('text/plain')
2016-03-27 11:43:30 +00:00
var results
2016-03-31 05:03:14 +00:00
if (text.trimLeft()[0] === '{') {
2016-03-26 06:20:49 +00:00
try {
2016-03-27 11:43:30 +00:00
results = JSON.parse(text)
} catch (e) {
2016-03-27 11:43:30 +00:00
results = false
2016-03-26 06:20:49 +00:00
}
} else {
2016-03-27 11:43:30 +00:00
results = self.parseTabbedString(text)
2016-03-26 06:20:49 +00:00
}
2016-03-27 11:43:30 +00:00
if (results === false) return
2016-03-27 11:43:30 +00:00
var topics = results.topics
var synapses = results.synapses
if (topics.length > 0 || synapses.length > 0) {
2016-03-27 11:43:30 +00:00
if (window.confirm('Are you sure you want to create ' + topics.length +
' new topics and ' + synapses.length + ' new synapses?')) {
self.importTopics(topics)
self.importSynapses(synapses)
} // if
} // if
})
},
2016-03-27 11:43:30 +00:00
abort: function (message) {
console.error(message)
},
2016-03-27 11:43:30 +00:00
simplify: function (string) {
return string
.replace(/(^\s*|\s*$)/g, '')
2016-03-27 11:43:30 +00:00
.toLowerCase()
},
2016-03-27 11:43:30 +00:00
parseTabbedString: function (text) {
var self = Metamaps.Import
// determine line ending and split lines
2016-03-27 11:43:30 +00:00
var delim = '\n'
if (text.indexOf('\r\n') !== -1) {
delim = '\r\n'
} else if (text.indexOf('\r') !== -1) {
delim = '\r'
} // if
var STATES = {
ABORT: -1,
UNKNOWN: 0,
TOPICS_NEED_HEADERS: 1,
SYNAPSES_NEED_HEADERS: 2,
TOPICS: 3,
2016-03-27 11:43:30 +00:00
SYNAPSES: 4
}
// state & lines determine parser behaviour
2016-03-27 11:43:30 +00:00
var state = STATES.UNKNOWN
var lines = text.split(delim)
var results = { topics: [], synapses: [] }
2016-03-27 11:43:30 +00:00
var topicHeaders = []
var synapseHeaders = []
lines.forEach(function (line_raw, index) {
var line = line_raw.split('\t')
var noblanks = line.filter(function (elt) {
return elt !== ''
})
switch (state) {
case STATES.UNKNOWN:
if (noblanks.length === 0) {
2016-03-27 11:43:30 +00:00
state = STATES.UNKNOWN
break
} else if (noblanks.length === 1 && self.simplify(line[0]) === 'topics') {
2016-03-27 11:43:30 +00:00
state = STATES.TOPICS_NEED_HEADERS
break
} else if (noblanks.length === 1 && self.simplify(line[0]) === 'synapses') {
2016-03-27 11:43:30 +00:00
state = STATES.SYNAPSES_NEED_HEADERS
break
}
2016-03-27 11:43:30 +00:00
state = STATES.TOPICS_NEED_HEADERS
// FALL THROUGH - if we're not sure what to do, pretend
// we're on the TOPICS_NEED_HEADERS state and parse some headers
2016-03-27 11:43:30 +00:00
case STATES.TOPICS_NEED_HEADERS: // eslint-disable-line
if (noblanks.length < 2) {
2016-03-27 11:43:30 +00:00
self.abort('Not enough topic headers on line ' + index)
state = STATES.ABORT
}
2016-03-27 11:43:30 +00:00
topicHeaders = line.map(function (header, index) {
return header.toLowerCase().replace('description', 'desc')
})
state = STATES.TOPICS
break
case STATES.SYNAPSES_NEED_HEADERS:
if (noblanks.length < 2) {
2016-03-27 11:43:30 +00:00
self.abort('Not enough synapse headers on line ' + index)
state = STATES.ABORT
}
2016-03-27 11:43:30 +00:00
synapseHeaders = line.map(function (header, index) {
return header.toLowerCase().replace('description', 'desc')
})
state = STATES.SYNAPSES
break
case STATES.TOPICS:
if (noblanks.length === 0) {
2016-03-27 11:43:30 +00:00
state = STATES.UNKNOWN
} else if (noblanks.length === 1 && line[0].toLowerCase() === 'topics') {
2016-03-27 11:43:30 +00:00
state = STATES.TOPICS_NEED_HEADERS
} else if (noblanks.length === 1 && line[0].toLowerCase() === 'synapses') {
2016-03-27 11:43:30 +00:00
state = STATES.SYNAPSES_NEED_HEADERS
} else {
2016-03-27 11:43:30 +00:00
var topic = {}
line.forEach(function (field, index) {
var header = topicHeaders[index]
if (self.topicWhitelist.indexOf(header) === -1) return
topic[header] = field
if (['id', 'x', 'y'].indexOf(header) !== -1) {
2016-03-27 11:43:30 +00:00
topic[header] = parseInt(topic[header])
} // if
})
results.topics.push(topic)
}
2016-03-27 11:43:30 +00:00
break
case STATES.SYNAPSES:
if (noblanks.length === 0) {
2016-03-27 11:43:30 +00:00
state = STATES.UNKNOWN
} else if (noblanks.length === 1 && line[0].toLowerCase() === 'topics') {
2016-03-27 11:43:30 +00:00
state = STATES.TOPICS_NEED_HEADERS
} else if (noblanks.length === 1 && line[0].toLowerCase() === 'synapses') {
2016-03-27 11:43:30 +00:00
state = STATES.SYNAPSES_NEED_HEADERS
} else {
2016-03-27 11:43:30 +00:00
var synapse = {}
line.forEach(function (field, index) {
var header = synapseHeaders[index]
if (self.synapseWhitelist.indexOf(header) === -1) return
synapse[header] = field
if (['id', 'topic1', 'topic2'].indexOf(header) !== -1) {
2016-03-27 11:43:30 +00:00
synapse[header] = parseInt(synapse[header])
} // if
})
results.synapses.push(synapse)
}
2016-03-27 11:43:30 +00:00
break
case STATES.ABORT:
2016-03-27 11:43:30 +00:00
default:
2016-03-27 11:43:30 +00:00
self.abort('Invalid state while parsing import data. Check code.')
state = STATES.ABORT
}
2016-03-27 11:43:30 +00:00
})
if (state === STATES.ABORT) {
2016-03-27 11:43:30 +00:00
return false
} else {
2016-03-27 11:43:30 +00:00
return results
}
},
2016-03-27 11:43:30 +00:00
importTopics: function (parsedTopics) {
var self = Metamaps.Import
// up to 25 topics: scale 100
// up to 81 topics: scale 200
// up to 169 topics: scale 300
2016-03-27 11:43:30 +00:00
var scale = Math.floor((Math.sqrt(parsedTopics.length) - 1) / 4) * 100
if (scale < 100) scale = 100
var autoX = -scale
var autoY = -scale
2016-03-27 11:43:30 +00:00
parsedTopics.forEach(function (topic) {
var x, y
if (topic.x && topic.y) {
2016-03-27 11:43:30 +00:00
x = topic.x
y = topic.y
} else {
2016-03-27 11:43:30 +00:00
x = autoX
y = autoY
autoX += 50
if (autoX > scale) {
2016-03-27 11:43:30 +00:00
autoY += 50
autoX = -scale
}
}
self.createTopicWithParameters(
topic.name, topic.metacode, topic.permission,
topic.desc, topic.link, x, y, topic.id
2016-03-27 11:43:30 +00:00
)
})
},
2016-03-27 11:43:30 +00:00
importSynapses: function (parsedSynapses) {
var self = Metamaps.Import
parsedSynapses.forEach(function(synapse) {
2016-03-27 11:43:30 +00:00
// only createSynapseWithParameters once both topics are persisted
var topic1 = Metamaps.Topics.get(self.cidMappings[synapse.topic1])
var topic2 = Metamaps.Topics.get(self.cidMappings[synapse.topic2])
2016-03-31 01:25:14 +00:00
if (!topic1 || !topic2) {
console.error("One of the two topics doesn't exist!")
console.error(synapse)
2016-03-27 11:43:30 +00:00
return true
2016-03-31 01:25:14 +00:00
}
var synapse_created = false
2016-03-27 11:43:30 +00:00
topic1.once('sync', function () {
if (topic1.id && topic2.id && !synapse_created) {
2016-03-31 01:25:14 +00:00
synapse_created = true
self.createSynapseWithParameters(
synapse.desc, synapse.category, synapse.permission,
topic1, topic2
2016-03-27 11:43:30 +00:00
)
} // if
})
topic2.once('sync', function () {
if (topic1.id && topic2.id && !synapse_created) {
2016-03-31 01:25:14 +00:00
synapse_created = true
self.createSynapseWithParameters(
synapse.desc, synapse.category, synapse.permission,
topic1, topic2
2016-03-27 11:43:30 +00:00
)
} // if
})
})
},
2016-03-27 11:43:30 +00:00
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')
var topic = new Metamaps.Backbone.Topic({
name: name,
metacode_id: metacode.id,
permission: permission || Metamaps.Active.Map.get('permission'),
desc: desc,
2016-03-27 11:43:30 +00:00
link: link
})
Metamaps.Topics.add(topic)
self.cidMappings[import_id] = topic.cid
var mapping = new Metamaps.Backbone.Mapping({
2016-03-27 11:43:30 +00:00
xloc: xloc,
yloc: yloc,
mappable_id: topic.cid,
mappable_type: 'Topic'
})
Metamaps.Mappings.add(mapping)
2016-02-07 09:51:01 +00:00
// this function also includes the creation of the topic in the database
2016-03-27 11:43:30 +00:00
Metamaps.Topic.renderTopic(mapping, topic, true, true)
2016-03-27 11:43:30 +00:00
Metamaps.Famous.viz.hideInstructions()
},
2016-03-27 11:43:30 +00:00
createSynapseWithParameters: function (description, category, permission,
topic1, topic2) {
var node1 = topic1.get('node')
var node2 = topic2.get('node')
if (!topic1.id || !topic2.id) {
2016-03-27 11:43:30 +00:00
console.error('missing topic id when creating synapse')
return
} // if
var synapse = new Metamaps.Backbone.Synapse({
desc: description,
category: category,
permission: permission,
node1_id: topic1.id,
node2_id: topic2.id
2016-03-27 11:43:30 +00:00
})
Metamaps.Synapses.add(synapse)
var mapping = new Metamaps.Backbone.Mapping({
2016-03-27 11:43:30 +00:00
mappable_type: 'Synapse',
mappable_id: synapse.cid
})
Metamaps.Mappings.add(mapping)
Metamaps.Synapse.renderSynapse(mapping, synapse, node1, node2, true)
}
}