coffeescript-ui
Version:
Coffeescript User Interface System
245 lines (187 loc) • 6.1 kB
text/coffeescript
attributionHtml = require('./leaflet.attribution.html')
require("leaflet/dist/leaflet.js")
class CUI.LeafletMap extends CUI.Map
=
urlCss: "https://unpkg.com/leaflet@1.2.0/dist/leaflet.css"
tileLayerUrl: 'https://{s}.tile.osm.org/{z}/{x}/{y}.png'
tileLayerOptions:
attribution: attributionHtml
constructor: (opts) ->
super(opts)
if not CUI.LeafletMap.loadCSSPromise
CUI.LeafletMap.loadCSSPromise =
CUI.LeafletMap.defaults.tileLayerOptions.maxZoom = CUI.Map.defaults.maxZoom
= {}
__getMapClassName: ->
"cui-leaflet-map"
__buildMap: ->
map = L.map(,
zoomControl: false
)
tileLayer = L.tileLayer(CUI.LeafletMap.defaults.tileLayerUrl, CUI.LeafletMap.defaults.tileLayerOptions)
CUI.dom.waitForDOMInsert(node: @).done =>
CUI.LeafletMap.loadCSSPromise.done =>
if
else
map.setView(, )
tileLayer.addTo(map)
if
map.on("click", )
if
map.on("zoomend", )
map.on("moveend", => )
map
__buildMarker: (options) ->
if options.iconName and options.iconColor
iconMarker = new CUI.IconMarker(icon: options.iconName, color: options.iconColor)
iconSize = iconMarker.getSize()
iconAnchor = iconMarker.getAnchor()
options.icon = L.divIcon(
html: iconMarker.toHtml()
iconAnchor: [iconAnchor.left, iconAnchor.top]
iconSize: [iconSize, iconSize]
)
delete options.iconName
delete options.iconColor
marker = L.marker(options.position, options)
if options.group?.type and
[options.group.type] = [options.group.type] or []
[options.group.type].push(marker: marker, options: options.group.options)
delete options.group
marker
__addMarkerToMap: (marker) ->
marker.addTo();
__bindOnClickMapEvent: ->
.on('click', (event) =>
if
.setView()
)
__afterMarkerCreated: (marker, options) ->
onClickFunction = options["cui_onClick"]
onDoubleClickFunction = options["cui_onDoubleClick"]
# Workaround to fix a Leaflet non-implemented feature https://github.com/Leaflet/Leaflet/issues/108
if onClickFunction and onDoubleClickFunction
isDoubleClick = false
onClickFunction = (event) ->
CUI.setTimeout( ->
if not isDoubleClick
options["cui_onClick"](event)
, 200)
onDoubleClickFunction = (event) ->
isDoubleClick = true
options["cui_onDoubleClick"](event)
CUI.setTimeout( ->
isDoubleClick = false
, 250)
if onClickFunction
marker.on("click", onClickFunction)
if onDoubleClickFunction
.doubleClickZoom.disable() # If at least 1 marker has 'onDoubleClick' event, the map disables zoom on double click.
marker.on("dblclick", onDoubleClickFunction)
return
__removeMarker: (marker) ->
for _, group of
foundElement = group.filter((element) => element.marker == marker)
if foundElement = foundElement[0]
indexOfElement = group.indexOf(foundElement)
group.splice(indexOfElement, 1)
break
if foundElement && foundElement.polyline
.removeLayer(foundElement.polyline)
marker.off()
.removeLayer(marker)
marker = null
return
__updateGroupsPolylines: ->
for _, group of
group.reduce((elementOne, elementTwo) =>
if not elementTwo
return
if elementOne.polyline
return elementTwo
elementOne.polyline = L.polyline([elementOne.marker.getLatLng(), elementTwo.marker.getLatLng()],
weight: 1.5
color: elementOne.options.color
dashArray: elementOne.options.polyline
)
elementOne.polyline.addTo()
return elementTwo
)
return
getSelectedMarkerPosition: ->
?.getLatLng()
setSelectedMarkerPosition: (position) ->
if not CUI.Map.isValidPosition(position)
return
if
.setLatLng(position)
else
options = or {}
options.position = position
options.draggable =
=
.on('dragstart', =>
=
)
.on('dragend', =>
latLng =
if CUI.Map.isValidPosition(latLng)
?()
else
.setView()
)
?()
removeSelectedMarker: ->
if
delete
hideMarkers: ->
for marker in
marker.setOpacity(0)
return
showMarkers: ->
for marker in
marker.setOpacity(1)
return
zoomToFitAllMarkers: ->
CUI.dom.waitForDOMInsert(node: @).done =>
if .length > 0
group = new L.featureGroup();
.fitBounds(group.getBounds().pad(0.05));
else
.setView(, )
zoomIn: ->
.setZoom(.getZoom() + 1)
zoomOut: ->
.setZoom(.getZoom() - 1)
resize: ->
.invalidateSize()
getZoom: ->
return .getZoom()
setZoom: (zoom) ->
.setZoom(zoom)
setCenter: (position, zoom = ) ->
.setView(position, zoom)
getCenter: ->
return .getCenter()
destroy: ->
for marker in
?.remove()
delete
delete
super()
__loadCSS: ->
leafletDeferred = new CUI.Deferred()
# Load Leaflet CSS to avoid bundle it.
if CUI.LeafletMap.defaults.urlCss
new CUI.CSSLoader().load(url: CUI.LeafletMap.defaults.urlCss).done ->
leafletDeferred.resolve()
else
leafletDeferred.resolve()
leafletDeferred.promise()