integrate handleURL into Import

This commit is contained in:
Devin Howard 2016-10-01 12:57:19 +08:00
parent c5564e02fc
commit 20a32afe3b
2 changed files with 64 additions and 53 deletions

View file

@ -59,7 +59,7 @@ const Import = {
console.warn(err)
return topicsPromise.resolve([])
}
topicsPromise.resolve(data.map(row => self.lowercaseKeys(row)))
topicsPromise.resolve(data.map(row => self.normalizeKeys(row)))
})
const synapsesPromise = $.Deferred()
@ -68,7 +68,7 @@ const Import = {
console.warn(err)
return synapsesPromise.resolve([])
}
synapsesPromise.resolve(data.map(row => self.lowercaseKeys(row)))
synapsesPromise.resolve(data.map(row => self.normalizeKeys(row)))
})
$.when(topicsPromise, synapsesPromise).done((topics, synapses) => {
@ -225,23 +225,25 @@ const Import = {
var self = Import
parsedTopics.forEach(function (topic) {
var x, y
if (topic.x && topic.y) {
x = topic.x
y = topic.y
} else {
const coords = AutoLayout.getNextCoord()
x = coords.x
y = coords.y
let coords = { x: topic.x, y: topic.y }
if (!coords.x || !coords.y) {
coords = AutoLayout.getNextCoord()
}
if (topic.name && topic.link && !topic.metacode) {
topic.metacode = 'Reference'
if (!topic.name && topic.link ||
topic.name && topic.link && !topic.metacode) {
self.handleURL(topic.link, {
coords,
name: topic.name,
permission: topic.permission,
import_id: topic.id
})
return // "continue"
}
self.createTopicWithParameters(
topic.name, topic.metacode, topic.permission,
topic.desc, topic.link, x, y, topic.id
topic.desc, topic.link, coords.x, coords.y, topic.id
)
})
},
@ -344,6 +346,47 @@ const Import = {
Synapse.renderSynapse(mapping, synapse, node1, node2, true)
},
handleURL: function (url, opts = {}) {
let coords = opts.coords
if (!coords || coords.x === undefined || coords.y === undefined) {
coords = AutoLayout.getNextCoord()
}
const name = opts.name || 'Link'
const metacode = opts.metacode || 'Reference'
const import_id = opts.import_id || null // don't store a cidMapping
const permission = opts.permission || null // use default
const desc = opts.desc || url
Import.createTopicWithParameters(
name,
metacode,
permission,
desc,
url,
coords.x,
coords.y,
import_id,
{
success: function(topic) {
if (topic.get('name') !== 'Link') return
$.get('/hacks/load_url_title', {
url
}, function success(data, textStatus) {
var selector = '#showcard #topic_' + topic.get('id') + ' .best_in_place'
if ($(selector).find('form').length > 0) {
$(selector).find('textarea, input').val(data.title)
} else {
$(selector).html(data.title)
}
topic.set('name', data.title)
topic.save()
})
}
}
)
},
/*
* helper functions
*/
@ -352,6 +395,7 @@ const Import = {
console.error(message)
},
// TODO investigate replacing with es6 (?) trim()
simplify: function (string) {
return string
.replace(/(^\s*|\s*$)/g, '')
@ -360,9 +404,13 @@ const Import = {
// thanks to http://stackoverflow.com/a/25290114/5332286
lowercaseKeys: function(obj) {
normalizeKeys: function(obj) {
return _.transform(obj, (result, val, key) => {
result[key.toLowerCase()] = val
let newKey = key.toLowerCase()
if (newKey === 'url') key = 'link'
if (newKey === 'title') key = 'name'
if (newKey === 'description') key = 'desc'
result[newKey] = val
})
}
}

View file

@ -1,6 +1,5 @@
/* global $ */
import AutoLayout from './AutoLayout'
import Import from './Import'
import Util from './Util'
@ -57,7 +56,7 @@ const PasteInput = {
var self = PasteInput
if (text.match(self.URL_REGEX)) {
self.handleURL(text, coords)
Import.handleURL(text, coords)
} else if (text[0] === '{') {
Import.handleJSON(text)
} else if (text.match(/\t/)) {
@ -68,42 +67,6 @@ const PasteInput = {
}
},
handleURL: function (text, coords) {
var title = 'Link'
if (!coords || !coords.x || !coords.y) {
coords = AutoLayout.getNextCoord()
}
var import_id = null // don't store a cidMapping
var permission = null // use default
Import.createTopicWithParameters(
title,
'Reference', // metacode - todo fix
permission,
text, // desc - todo load from url?
text, // link - todo fix because this isn't being POSTed
coords.x,
coords.y,
import_id,
{
success: function(topic) {
$.get('/hacks/load_url_title', {
url: text
}, function success(data, textStatus) {
var selector = '#showcard #topic_' + topic.get('id') + ' .best_in_place'
if ($(selector).find('form').length > 0) {
$(selector).find('textarea, input').val(data.title)
} else {
$(selector).html(data.title)
}
topic.set('name', data.title)
topic.save()
})
}
}
)
}
}
export default PasteInput