coffeescript-ui
Version:
Coffeescript User Interface System
240 lines (194 loc) • 6.26 kB
text/coffeescript
CUI.Template.loadTemplateText(require('./map-input.html'));
class CUI.MapInput extends CUI.Input
:
labels:
mapButtonTooltip: "Show map"
iconButtonTooltip: "Show icon"
placeholder: "Insert or paste coordinates"
iconLabel: "Icon"
colorLabel: "Color"
displayFormat: "ll"
mapClass: CUI.LeafletMap
iconColors: ["#b8bfc4", "#80d76a", "#f95b53", "#ffaf0f", "#57a8ff"]
icons: ["fa-map-marker", "fa-envelope", "fa-automobile", "fa-home", "fa-bicycle", "fa-graduation-cap"]
:
dms: "FFf" # Degrees, minutes and seconds: 27° 43′ 31.796″ N 18° 1′ 27.484″ W
ddm: "Ff" # Degrees and decimal minutes: 27° 43.529933333333′ N -18° 1.4580666666667′ W
dd: "f" # Decimal degrees: 27.725499° N 18.024301° W
ll: (position) -> "#{position.lat.toFixed(5)}, #{position.lng.toFixed(5)}" # Latitude and Longitude comma separated: 27.19234, 28.48822
getTemplateKeyForRender: ->
"center"
initOpts: ->
super()
mapOptions:
check: "PlainObject"
default: {}
displayFormat:
check: (value) =>
not CUI.util.isNull(CUI.MapInput.displayFormats[value])
default: CUI.MapInput.defaults.displayFormat
readOpts: ->
super()
=
= {}
initValue: ->
super()
currentValue =
.iconName = currentValue.iconName or CUI.MapInput.defaults.icons[0]
.iconColor = currentValue.iconColor or CUI.MapInput.defaults.iconColors[0]
position =
if position and CUI.Map.isValidPosition(position)
formattedPosition =
else
getTemplate: ->
new CUI.Template
name: "map-input"
map:
left: true
center: true
right: true
getValueForDisplay: ->
getValueForInput: ->
__getPositionForDisplay: ->
position =
if not position
return ""
return
getValueForStore: (stringCoordinates) ->
objectValue =
iconName: .iconName
iconColor: .iconColor
parsedPosition = CUI.util.parseCoordinates(stringCoordinates)
objectValue.position = if parsedPosition then parsedPosition else ""
return objectValue
checkValue: (v) ->
if CUI.util.isEmpty(v)
return true
if
return true
throw new Error("#{@__cls}.setValue(value): Value needs to be valid coordinates.")
render: ->
super()
if CUI.util.isEmpty()
.setAttribute("placeholder", CUI.MapInput.defaults.labels.placeholder)
openMapPopoverButton = new CUI.defaults.class.Button
icon: "fa-map-o"
tooltip:
text: CUI.MapInput.defaults.labels.mapButtonTooltip
onClick: =>
= new CUI.defaults.class.Button
class: "cui-map-icon-popover-button"
icon: .iconName
tooltip:
text: CUI.MapInput.defaults.labels.iconButtonTooltip
onClick: =>
__openMapPopover: (button) ->
mapPopover = new CUI.Popover
element: button
handle_focus: false
placement: "se"
class: "cui-map-popover"
pane:
onHide: ->
mapPopover.destroy()
mapPopover.show()
return
__updateIconOptions: ->
.setIcon(.iconName)
CUI.dom.setStyleOne(.getIcon(), "color", .iconColor)
__openIconPopover: (button) ->
iconPopover = new CUI.Popover
element: button
handle_focus: false
placement: "se"
pane:
padded: true
content:
onHide: ->
iconPopover.destroy()
iconPopover.show()
return
__buildIconPopoverContent: ->
iconColorSelect = new CUI.Select(
data:
name: "iconColor"
form:
label: CUI.MapInput.defaults.labels.colorLabel
onDataChanged: =>
options: =>
options = []
for color in CUI.MapInput.defaults.iconColors
icon = new CUI.Icon(class: "css-swatch")
CUI.dom.setStyle(icon, background: color)
options.push(icon: icon, value: color)
options
)
iconSelect = new CUI.Select(
data:
name: "iconName"
form:
label: CUI.MapInput.defaults.labels.iconLabel
onDataChanged: =>
options: =>
options = []
for icon in CUI.MapInput.defaults.icons
options.push(icon: icon, value: icon)
options
)
form = new CUI.Form
fields: [iconSelect, iconColorSelect]
form.start()
form
__initMap: ->
.showPolylines = false
onMarkerSelected = .onMarkerSelected
.onMarkerSelected = (marker) =>
formattedPosition =
onMarkerSelected?()
return
__buildMap: ->
.selectedMarkerOptions =
currentPosition =
if currentPosition
.selectedMarkerPosition = currentPosition
.centerPosition = currentPosition
return new CUI.MapInput.defaults.mapClass()
__getPosition: ->
.position
__getFormattedPosition: (position) ->
displayFormat = CUI.MapInput.displayFormats[]
if not CUI.Map.isValidPosition(position)
return ""
return CUI.util.formatCoordinates(position, displayFormat)
__checkInput: (value) ->
parsedCoordinates = CUI.util.parseCoordinates(value)
if not parsedCoordinates
return false
return true
: ->
CUI.MapInput.displayFormats[CUI.MapInput.defaults.displayFormat]