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

If no enterprises have been geocoded yet make sure Open Street Map displays correctly #5680

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Darkswarm.directive 'ofnOpenStreetMap', ($window, Enterprises, EnterpriseModal, availableCountries, openStreetMapConfig) ->
Darkswarm.directive 'ofnOpenStreetMap', ($window, MapCentreCalculator, Enterprises, EnterpriseModal, availableCountries, openStreetMapConfig) ->
restrict: 'E'
replace: true
scope: true
Expand All @@ -11,34 +11,6 @@ Darkswarm.directive 'ofnOpenStreetMap', ($window, Enterprises, EnterpriseModal,
openStreetMapProviderName = openStreetMapConfig.open_street_map_provider_name
openStreetMapProviderOptions = JSON.parse(openStreetMapConfig.open_street_map_provider_options)

average = (values) ->
total = values.reduce (sum, value) ->
sum = sum + value
, 0
total / values.length

averageAngle = (angleName) ->
positiveAngles = []
negativeAngles = []
for enterprise in Enterprises.enterprises
if enterprise.latitude? && enterprise.longitude?
if enterprise[angleName] > 0
positiveAngles.push(enterprise[angleName])
else
negativeAngles.push(enterprise[angleName])

averageNegativeAngle = average(negativeAngles)
averagePositiveAngle = average(positiveAngles)

if negativeAngles.length == 0
averagePositiveAngle
else if positiveAngles.length == 0
averageNegativeAngle
else if averagePositiveAngle > averageNegativeAngle
averagePositiveAngle - averageNegativeAngle
else
averageNegativeAngle - averagePositiveAngle

buildMarker = (enterprise, latlng, title) ->
icon = L.icon
Copy link
Contributor

Choose a reason for hiding this comment

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

Where's this L variable coming from? I can't see where it's set...?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

iconUrl: enterprise.icon
Expand All @@ -61,28 +33,31 @@ Darkswarm.directive 'ofnOpenStreetMap', ($window, Enterprises, EnterpriseModal,

displayMap = ->
setMapDimensions()
averageLatitude = averageAngle("latitude")
averageLongitude = averageAngle("longitude")
zoomLevel = 6
map = L.map('open-street-map')
L.tileLayer.provider(openStreetMapProviderName, openStreetMapProviderOptions).addTo(map)
map.setView([averageLatitude, averageLongitude], zoomLevel)
map.setView([MapCentreCalculator.initialLatitude(), MapCentreCalculator.initialLongitude()], zoomLevel)

displayEnterprises = ->
for enterprise in Enterprises.enterprises
if enterprise.latitude? && enterprise.longitude?
marker = buildMarker(enterprise, { lat: enterprise.latitude, lng: enterprise.longitude }, enterprise.name).addTo(map)
enterpriseNames.push(enterpriseName(enterprise))
markers.push(marker)
for enterprise in Enterprises.geocodedEnterprises()
marker = buildMarker(enterprise, { lat: enterprise.latitude, lng: enterprise.longitude }, enterprise.name).addTo(map)
enterpriseNames.push(enterpriseName(enterprise))
markers.push(marker)

disableSearchField = () =>
$('#open-street-map--search input').prop("disabled", true)

displaySearchField = () ->
new Autocomplete('#open-street-map--search',
onSubmit: goToEnterprise
search: searchEnterprises
)
overwriteInlinePositionRelativeToPositionSearchField = ->
$('#open-street-map--search').css("position", "absolute")
overwriteInlinePositionRelativeToPositionSearchField()
overwriteInlinePositionRelativeToAbsoluteOnSearchField()
if Enterprises.geocodedEnterprises().length == 0
disableSearchField()

overwriteInlinePositionRelativeToAbsoluteOnSearchField = ->
$('#open-street-map--search').css("position", "absolute")

searchEnterprises = (input) ->
if input.length < 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,8 @@ Darkswarm.factory 'Enterprises', (enterprises, ShopsResource, CurrentHub, Taxons

resetDistance: ->
enterprise.distance = null for enterprise in @enterprises

geocodedEnterprises: =>
@enterprises.filter (enterprise) ->
enterprise.latitude? && enterprise.longitude?

4 changes: 1 addition & 3 deletions app/assets/javascripts/darkswarm/services/map.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ Darkswarm.factory "OfnMap", (Enterprises, EnterpriseListModal, MapConfiguration)
new class OfnMap
constructor: ->
@coordinates = {}
@enterprises = Enterprises.enterprises.filter (enterprise) ->
# Remove enterprises w/o lat or long
enterprise.latitude != null || enterprise.longitude != null
@enterprises = Enterprises.geocodedEnterprises()
@enterprises = @enterprise_markers(@enterprises)

enterprise_markers: (enterprises) ->
Expand Down
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)
2 changes: 2 additions & 0 deletions app/models/content_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class ContentConfiguration < Spree::Preferences::FileConfiguration
preference :open_street_map_enabled, :boolean, default: false
preference :open_street_map_provider_name, :string, default: "OpenStreetMap.Mapnik"
preference :open_street_map_provider_options, :text, default: "{}"
preference :open_street_map_default_latitude, :string, default: "-37.4713077"
preference :open_street_map_default_longitude, :string, default: "144.7851531"

# Producer sign-up page
# All the following defaults using I18n don't work.
Expand Down
4 changes: 3 additions & 1 deletion app/models/preference_sections/map_section.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ def preferences
[
:open_street_map_enabled,
:open_street_map_provider_name,
:open_street_map_provider_options
:open_street_map_provider_options,
:open_street_map_default_latitude,
:open_street_map_default_longitude
]
end
end
Expand Down
12 changes: 11 additions & 1 deletion app/serializers/api/open_street_map_config_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ module Api
class OpenStreetMapConfigSerializer < ActiveModel::Serializer
attributes :open_street_map_enabled,
:open_street_map_provider_name,
:open_street_map_provider_options
:open_street_map_provider_options,
:open_street_map_default_latitude,
:open_street_map_default_longitude

def open_street_map_enabled
ContentConfig.open_street_map_enabled
Expand All @@ -17,5 +19,13 @@ def open_street_map_provider_name
def open_street_map_provider_options
ContentConfig.open_street_map_provider_options.to_json
end

def open_street_map_default_latitude
ContentConfig.open_street_map_default_latitude
end

def open_street_map_default_longitude
ContentConfig.open_street_map_default_longitude
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe "Enterprises service", ->
{id: 5, visible: true, name: 'e', category: "producer_hub", hubs: [{id: 1}]},
{id: 6, visible: true, name: 'f', category: "producer_shop", hubs: [{id: 2}]},
{id: 7, visible: true, name: 'g', category: "producer", hubs: [{id: 2}]}
{id: 8, visible: true, name: 'h', category: "producer", hubs: [{id: 2}]}
{id: 8, visible: true, name: 'h', category: "producer", hubs: [{id: 2}], latitude: 76.26, longitude: -42.66 }
]
H1: 0
beforeEach ->
Expand Down Expand Up @@ -142,4 +142,8 @@ describe "Enterprises service", ->
it "resets the distance", ->
Enterprises.resetDistance()
for e in Enterprises.enterprises
expect(e.distance).toBeNull()
expect(e.distance).toBeNull()

describe "geocodedEnterprises", ->
it "only returns enterprises which have a latitude and longitude", ->
expect(Enterprises.geocodedEnterprises()).toEqual [Enterprises.enterprises[7]]
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