Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

world spec #253

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions app/scripts/components/world-map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import React, {Component, PropTypes} from 'react'
import d3 from 'd3'
import topojson from 'topojson'
import {AutoSizer} from 'react-virtualized'
import ReactFauxDOM from 'react-faux-dom'

import worldData from '../../data/world.json'

export default class WorldMap extends Component {
static propTypes = {
coordinates: PropTypes.array.isRequired
};

_renderMap = ({width, height}) => {
const projection = d3.geo.equirectangular()
.scale(height / Math.PI)
.translate([width / 2, height / 2])
.precision(0.1)

const path = d3.geo.path()
.projection(projection)

const graticule = d3.geo.graticule()

const el = d3.select(ReactFauxDOM.createElement('svg'))
.attr('width', width)
.attr('height', height)

// Fill Pattern
el.append('defs')
.append('pattern')
.attr('id', 'gridpattern')
.attr('x', 0)
.attr('y', 0)
.attr('width', 4)
.attr('height', 4)
.attr('patternUnits', 'userSpaceOnUse')
.append('circle')
.attr('cx', 0)
.attr('cy', 0)
.attr('r', 1)
.attr('style', 'stroke: none; fill: rgba(255, 255, 255, 0.7)')

el.append('path')
.datum(graticule)
.attr('class', 'graticule')
.attr('d', path)

el.insert('path', '.graticule')
.datum(topojson.feature(worldData, worldData.objects.land))
.attr('d', path)
.attr('fill', 'url(#gridpattern)')

el.insert('path', '.graticule')
.datum(topojson.mesh(
worldData,
worldData.objects.countries,
(a, b) => a !== b
))
.attr('d', path)
.attr('fill', 'none')
.attr('stroke', 'none')
.attr('stroke-width', '0.5px')

el.append('path')
.datum({
type: 'MultiPoint',
coordinates: this.props.coordinates
})
.attr('d', path.pointRadius((d) => 8))
.attr('class', 'world-locations-base')

el.append('path')
.datum({
type: 'MultiPoint',
coordinates: this.props.coordinates
})
.attr('d', path.pointRadius((d) => 2))
.attr('class', 'world-locations-center')

return el.node().toReact()
}

render () {
return <AutoSizer>{this._renderMap}</AutoSizer>
}
}
84 changes: 3 additions & 81 deletions app/scripts/components/world.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import React, {Component, PropTypes} from 'react'
import d3 from 'd3'
import ReactFauxDOM from 'react-faux-dom'
import topojson from 'topojson'
import {AutoSizer} from 'react-virtualized'
import {map, isEqual} from 'lodash'
import WorldMap from './world-map'

import worldData from '../../data/world.json'

Expand All @@ -18,93 +15,18 @@ export default class World extends Component {
locations: {}
};

_renderMap = (locations) => {
return ({width, height}) => {
const projection = d3.geo.equirectangular()
.scale(height / Math.PI)
.translate([width / 2, height / 2])
.precision(0.1)

const path = d3.geo.path()
.projection(projection)

const graticule = d3.geo.graticule()

const el = d3.select(ReactFauxDOM.createElement('svg'))
.attr('width', width)
.attr('height', height)

// Fill Pattern
el.append('defs')
.append('pattern')
.attr('id', 'gridpattern')
.attr('x', 0)
.attr('y', 0)
.attr('width', 4)
.attr('height', 4)
.attr('patternUnits', 'userSpaceOnUse')
.append('circle')
.attr('cx', 0)
.attr('cy', 0)
.attr('r', 1)
.attr('style', 'stroke: none; fill: rgba(255, 255, 255, 0.7)')

el.append('path')
.datum(graticule)
.attr('class', 'graticule')
.attr('d', path)

el.insert('path', '.graticule')
.datum(topojson.feature(worldData, worldData.objects.land))
.attr('d', path)
.attr('fill', 'url(#gridpattern)')

el.insert('path', '.graticule')
.datum(topojson.mesh(
worldData,
worldData.objects.countries,
(a, b) => a !== b
))
.attr('d', path)
.attr('fill', 'none')
.attr('stroke', 'none')
.attr('stroke-width', '0.5px')

el.append('path')
.datum({
type: 'MultiPoint',
coordinates: locations
})
.attr('d', path.pointRadius((d) => 8))
.attr('class', 'world-locations-base')

el.append('path')
.datum({
type: 'MultiPoint',
coordinates: locations
})
.attr('d', path.pointRadius((d) => 2))
.attr('class', 'world-locations-center')

return el.node().toReact()
}
};

shouldComponentUpdate (nextProps) {
return !isEqual(nextProps, this.props)
}

render () {
// Draw peer locations
const locations = map(this.props.locations, ({lon, lat}) => {
const coordinates = map(this.props.locations, ({lon, lat}) => {
return [lon, lat]
})

return (
<div className='world'>
<AutoSizer>
{this._renderMap(locations)}
</AutoSizer>
<WorldMap coordinates={coordinates}></WorldMap>
<div className='world-peers-counter'>
<div className='counter'>{this.props.peersCount}</div>
<div className='label'>Peers</div>
Expand Down
15 changes: 15 additions & 0 deletions test/components/map.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {expect} from 'chai'
import {render} from 'enzyme'
import sinon from 'sinon'
import React from 'react'

import WorldMap from '../../app/scripts/components/world-map'

describe('WorldMap', () => {
it('should render an svg', () => {
const c = [[12, 14]]

const el = render(<WorldMap coordinates={c}/>)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided to do a static render here since this component only has one child.

expect(el.find('svg').length).to.equal(1)
})
})
22 changes: 22 additions & 0 deletions test/components/world.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {expect} from 'chai'
import {shallow} from 'enzyme'
import React from 'react'

import World from '../../app/scripts/components/world'

describe('World', () => {
it('should have the correct defaults', () => {
const el = shallow(<World/>)
expect(el.find('WorldMap').length).to.equal(1)

var inst = el.instance()
expect(inst.props.peersCount).to.equal(0)
expect(Object.keys(inst.props.locations).length)
.to.equal(0)
})

it('should set peersCount', () => {
const el = shallow(<World peersCount={1337}/>)
expect(el.find('.counter').node.props.children).to.equal(1337)
})
})