summaryrefslogtreecommitdiff
path: root/client/map/index.js
blob: 2a6686beaf598fcc4255599df50edd2cffcd41fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import L from 'leaflet'
import './leaflet.bezier'

const arcStyles = {
  'edu': {
    color: 'rgb(245, 246, 150)',
    fillColor: 'rgb(245, 246, 150)',
    opacity: 0.5,
    weight: '1',
  },
  'company': {
    color: 'rgb(50, 100, 246)',
    fillColor: 'rgb(50, 100, 246)',
    opacity: 1.0,
    weight: '2',
  },
  'gov': {
    color: 'rgb(245, 150, 100)',
    fillColor: 'rgb(245, 150, 150)',
    opacity: 1.0,
    weight: '2',
  },
  'mil': {
    color: 'rgb(245, 0, 0)',
    fillColor: 'rgb(245, 0, 0)',
    opacity: 1.0,
    weight: '2',
  },
}

const sortOrder = ['edu', 'company', 'gov', 'mil']

const redDot = L.icon({
  iconUrl: '/assets/img/reddot.png',
  iconSize: [17, 17], // size of the icon
  iconAnchor: [8, 8], // point of the icon which will correspond to marker's location
  popupAnchor: [0, -5] // point from which the popup should open relative to the iconAnchor
})

function addMarker(map, latlng, title, subtext) {
  const marker = L.marker(latlng, { icon: redDot }).addTo(map)
  marker.bindPopup([
    "<b>", title, "</b>",
    "<br>",
    subtext,
  ].join(''))
  return marker
}

function addArc(map, src, dest, arcStyle) {
  L.bezier({
    path: [
      [
        { lat: src[0], lng: src[1] },
        { lat: dest[0], lng: dest[1] },
      ],
    ]
  }, arcStyle).addTo(map)
}

export default function append(el, payload) {
  const { data } = payload
  let { paper, address, citations } = data
  let source = [0, 0]

  let map = L.map(el).setView([25, 0], 2)
  L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={accessToken}', {
    attribution: 'Map data &copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, ' +
      '<a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
      'Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
    maxZoom: 18,
    id: 'mapbox.dark',
    style: 'mapbox://styles/mapbox/dark-v9',
    accessToken: 'pk.eyJ1IjoiZmFuc2FsY3kiLCJhIjoiY2pvN3I1czJwMHF5NDNrbWRoMWpteHlrdCJ9.kMpM5syQUhVjKkn1iVx9fg'
  }).addTo(map)

  if (address) {
    source = [address.lat, address.lng].map(n => parseFloat(n))
  }

  citations.sort((a,b) => sortOrder.indexOf(a) - sortOrder.indexOf(b))
  .forEach(citation => {
    const address = citation.addresses[0]
    const latlng = [address.lat, address.lng].map(n => parseFloat(n))
    if (Number.isNaN(latlng[0]) || Number.isNaN(latlng[1])) return
    addMarker(map, latlng, citation.title, address.name)
    addArc(map, source, latlng, arcStyles[address.type])
  })

  console.log(paper)

  const rootMarker = addMarker(map, source, paper.title, paper.address)
  rootMarker.openPopup()
}