-
-
Notifications
You must be signed in to change notification settings - Fork 708
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5680 from cillian/prevent-blank-open-street-map
If no enterprises have been geocoded yet make sure Open Street Map displays correctly
- Loading branch information
Showing
9 changed files
with
159 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
app/assets/javascripts/darkswarm/services/map_centre_calculator.js.coffee
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
Darkswarm.factory 'MapCentreCalculator', (Enterprises, openStreetMapConfig) -> | ||
new class MapCentreCalculator | ||
|
||
initialLatitude: => | ||
if Enterprises.geocodedEnterprises().length > 0 | ||
@_calculate("latitude", Enterprises.geocodedEnterprises()) | ||
else | ||
openStreetMapConfig.open_street_map_default_latitude | ||
|
||
initialLongitude: => | ||
if Enterprises.geocodedEnterprises().length > 0 | ||
@_calculate("longitude", Enterprises.geocodedEnterprises()) | ||
else | ||
openStreetMapConfig.open_street_map_default_longitude | ||
|
||
_calculate: (angleName, coordinates) => | ||
angles = [] | ||
|
||
for coordinate in coordinates | ||
angles.push(coordinate[angleName]) | ||
|
||
minimumAngle = Math.min.apply(null, angles) | ||
maximumAngle = Math.max.apply(null, angles) | ||
|
||
distanceBetweenMinimumAndMaximum = if maximumAngle > minimumAngle | ||
maximumAngle - minimumAngle | ||
else | ||
minimumAngle - maximumAngle | ||
|
||
minimumAngle + (distanceBetweenMinimumAndMaximum / 2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
spec/javascripts/unit/darkswarm/services/map_centre_calculator_spec.js.coffee
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
describe 'MapCentreCalculator service', -> | ||
MapCentreCalculator = null | ||
Enterprises = null | ||
defaultLongitude = null | ||
defaultLatitude = null | ||
|
||
beforeEach -> | ||
module 'Darkswarm' | ||
defaultLongitude = -6 | ||
defaultLatitude = 53 | ||
angular.module('Darkswarm').value 'openStreetMapConfig', { | ||
open_street_map_default_latitude: 76.26, | ||
open_street_map_default_longitude: -42.66 | ||
} | ||
|
||
inject (_MapCentreCalculator_, _Enterprises_)-> | ||
MapCentreCalculator = _MapCentreCalculator_ | ||
Enterprises = _Enterprises_ | ||
|
||
describe "initialLatitude", -> | ||
it "calculates the center latitude of any present geocoded enterprises", -> | ||
Enterprises.geocodedEnterprises = -> [ | ||
{ latitude: 53, longitude: defaultLongitude }, | ||
{ latitude: 54, longitude: defaultLongitude } | ||
] | ||
|
||
expect(MapCentreCalculator.initialLatitude()).toEqual 53.5 | ||
|
||
it "returns the default configured latitude when there are no geocoded enterprises present", -> | ||
Enterprises.geocodedEnterprises = -> [] | ||
|
||
expect(MapCentreCalculator.initialLatitude()).toEqual 76.26 | ||
|
||
describe "initialLongitude", -> | ||
it "calculates the center longitude of any present geocoded enterprises", -> | ||
Enterprises.geocodedEnterprises = -> [ | ||
{ latitude: defaultLatitude, longitude: -6 }, | ||
{ latitude: defaultLatitude, longitude: -7 } | ||
] | ||
|
||
expect(MapCentreCalculator.initialLongitude()).toEqual -6.5 | ||
|
||
it "returns the default configured longitude when there are no geocoded enterprises present", -> | ||
Enterprises.geocodedEnterprises = -> [] | ||
|
||
expect(MapCentreCalculator.initialLongitude()).toEqual -42.66 | ||
|
||
describe "_calculate", -> | ||
it "calculates the average angle correctly when given a single angle", -> | ||
coordinates = [ | ||
{ latitude: defaultLatitude, longitude: -7 } | ||
] | ||
|
||
expect(MapCentreCalculator._calculate("longitude", coordinates)).toEqual -7 | ||
|
||
it "calculates the centre correctly when given a set of positive angles", -> | ||
coordinates = [ | ||
{ latitude: 53, longitude: defaultLongitude }, | ||
{ latitude: 54, longitude: defaultLongitude } | ||
] | ||
|
||
expect(MapCentreCalculator._calculate("latitude", coordinates)).toEqual 53.5 | ||
|
||
it "calculates the centre correctly when given a set of negative angles", -> | ||
coordinates = [ | ||
{ latitude: defaultLatitude, longitude: -6 }, | ||
{ latitude: defaultLatitude, longitude: -7 } | ||
] | ||
|
||
expect(MapCentreCalculator._calculate("longitude", coordinates)).toEqual -6.5 | ||
|
||
it "calculates the centre correctly when given a mixture of positive and negative angles and the centre is positive", -> | ||
coordinates = [ | ||
{ latitude: defaultLatitude, longitude: 7 }, | ||
{ latitude: defaultLatitude, longitude: -4 } | ||
] | ||
|
||
expect(MapCentreCalculator._calculate("longitude", coordinates)).toEqual 1.5 | ||
|
||
it "calculates the centre correctly when given a mixture of positive and negative angles and the centre is negative", -> | ||
coordinates = [ | ||
{ latitude: defaultLatitude, longitude: 4 }, | ||
{ latitude: defaultLatitude, longitude: -7 } | ||
] | ||
|
||
expect(MapCentreCalculator._calculate("longitude", coordinates)).toEqual -1.5 |