diff options
Diffstat (limited to 'client/map')
| -rw-r--r-- | client/map/index.js | 65 |
1 files changed, 41 insertions, 24 deletions
diff --git a/client/map/index.js b/client/map/index.js index b35ffddb..a1c728d9 100644 --- a/client/map/index.js +++ b/client/map/index.js @@ -37,25 +37,31 @@ const redDot = L.icon({ popupAnchor: [0, -5] // point from which the popup should open relative to the iconAnchor }) -function addMarker(map, latlng, title, addresses, year, pdf) { +function addMarker(map, latlng, citations) { const marker = L.marker(latlng, { icon: redDot }).addTo(map) - let message = [ - "<b>", title, "</b>", - ] - if (pdf && pdf.length) { - message.unshift("<a href='" + pdf[0] + "' target='_blank'>") - message.push("</a>") - } + let message = citations.map(citation => { + const { title, addresses, year, pdf, doi } = citation + let rec = [ + "<b>", title, "</b>", + ] + if (pdf && pdf.length) { + rec.unshift("<a href='" + pdf[0] + "' target='_blank'>") + rec.push("</a>") + } + else if (doi && doi.length) { + rec.unshift("<a href='" + doi[0] + "' target='_blank'>") + rec.push("</a>") + } + const addressString = addresses.map(addr => addr.name).join('<br/>') + rec.push("<br>") + rec.push(addressString) + if (year) { + rec.push(" (" + year + ")") + } + return rec.join("") + }) - const addressString = addresses.map(addr => addr.name).join('<br/>') - message = message.concat([ - "<br>", - addressString, - ]) - if (year) { - message.push(" (" + year + ")") - } - marker.bindPopup(message.join('')) + marker.bindPopup(message.join('<br><br>')) return marker } @@ -73,7 +79,7 @@ function addArc(map, src, dest, arcStyle) { export default function append(el, payload) { const { data } = payload if (!data) return - let { paper, addresses, citations } = data + let { paper, citations } = data let source = [0, 0] let map = L.map(el).setView([25, 0], 2) @@ -87,30 +93,41 @@ export default function append(el, payload) { accessToken: 'pk.eyJ1IjoiZmFuc2FsY3kiLCJhIjoiY2pvN3I1czJwMHF5NDNrbWRoMWpteHlrdCJ9.kMpM5syQUhVjKkn1iVx9fg' }).addTo(map) - if (addresses && addresses.length) { - source = [address[0].lat, address[0].lng].map(n => (parseFloat(n) || 0)) + if (paper.addresses && paper.addresses.length) { + source = [paper.addresses[0].lat, paper.addresses[0].lng].map(n => (parseFloat(n) || 0)) } else { console.error("No address found for root paper") // console.log(data) } // group papers by address + let citationsByAddress = {} citations.forEach(citation => { - console.log(citation) if (!citation.addresses) { console.log(citation) return } - const citationAddress = citation.addresses[0] + citation.addresses.forEach(address => { + if (!(address.name in citationsByAddress)) { + citationsByAddress[address.name] = { address, citations: []} + } + citationsByAddress[address.name].citations.push(citation) + }) + }) + + Object.keys(citationsByAddress).map(name => { + const { citations: citationList, address: citationAddress } = citationsByAddress[name] + console.log(name, citationsByAddress[name]) + // console.log(citation) const latlng = [citationAddress.lat, citationAddress.lng].map(n => parseFloat(n)) if (Number.isNaN(latlng[0]) || Number.isNaN(latlng[1])) return - addMarker(map, latlng, citation.title, citation.addresses, citation.year, citation.pdf) + addMarker(map, latlng, citationList) addArc(map, source, latlng, arcStyles[citationAddress.type]) }) console.log(paper) - const rootMarker = addMarker(map, source, paper.title, addresses, paper.year) + const rootMarker = addMarker(map, source, [paper]) rootMarker.openPopup() // a transparent div to cover the map, so normal scroll events will not be eaten by leaflet |
