Skip to content

Commit

Permalink
Mde sourcesLayer a layer group
Browse files Browse the repository at this point in the history
  • Loading branch information
grassick committed Aug 8, 2013
1 parent e1b7b87 commit 0e97cd6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
21 changes: 16 additions & 5 deletions app/js/map/SourcesLayer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ SourcePage = require "../pages/SourcePage"

# Displays water sources on map. Call updateMarkers to refresh. Creates popups
# with source details.
module.exports = class SourceDisplay
constructor: (map, db, pager) ->
@map = map
module.exports = class SourcesLayer extends L.LayerGroup
constructor: (db, pager) ->
super()
@db = db
@pager = pager
@itemTracker = new ItemTracker()
Expand All @@ -20,6 +20,17 @@ module.exports = class SourceDisplay
iconAnchor: [13, 41]
popupAnchor: [-3, -41]

onAdd: (map) ->
super(map)
@map = map
@map.on 'moveend', @updateMarkers

console.log "Added!"

onRemove: (map) ->
super(map)
@map.off 'moveend', @updateMarkers

updateMarkers: =>
# Get bounds padded
bounds = @map.getBounds()
Expand Down Expand Up @@ -82,9 +93,9 @@ module.exports = class SourceDisplay
marker.bindPopup(content.get(0))

@sourceMarkers[source._id] = marker
marker.addTo(@map)
@addLayer(marker)

removeSourceMarker: (source) ->
if _.has(@sourceMarkers, source._id)
@map.removeLayer(@sourceMarkers[source._id])
@removeLayer(@sourceMarkers[source._id])

12 changes: 6 additions & 6 deletions app/js/pages/SourceMapPage.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@ class SourceMapPage extends Page

# Setup marker display when map is loaded
@map.whenReady =>
@sourceDisplay = new SourceDisplay(@map, @db, @pager)

# Update on map movement
@map.on 'moveend', =>
@sourceDisplay.updateMarkers()
@sourceDisplay = new SourceDisplay(@db, @pager).addTo(@map)

# Setup context menu
contextMenu = new ContextMenu(@map, @ctx)
Expand All @@ -45,8 +41,12 @@ class SourceMapPage extends Page

activate: ->
# Update markers
if @sourceDisplay
if @sourceDisplay and @needsRefresh
@sourceDisplay.updateMarkers()
needsRefresh = false

deactivate: ->
@needsRefresh = true

destroy: ->
$(window).off('resize', @resizeMap)
Expand Down

0 comments on commit 0e97cd6

Please sign in to comment.