luda
Version:
A library helps to build cross-framework UI components.
103 lines (80 loc) • 2.6 kB
text/coffeescript
import '../kernel/index.coffee'
import '../mixins/resetable.coffee'
luda.component 'fmSelect'
.protect
selector:
select: 'select'
options: 'option'
simulator: 'input'
data:
default: 'data-fm-select_default-selected'
defaultMarked: 'data-fm-select_default-marked'
evt:
changed: 'luda:fmSelect:changed'
.protect
placeholder: -> .attr 'placeholder'
multiple: -> .prop 'multiple'
options: -> Array.from .prop('options')
.protect
tryEmpty: ->
selected = .some (o) -> luda(o).hasAttr 'selected'
not selected and .prop('selectedIndex', -1)
markSelected: (markDefault) ->
markDefault = markDefault is true
return if markDefault and .hasData .defaultMarked
.data .defaultMarked, '' if markDefault
.forEach (o) =>
option = luda o
if markDefault
val = if option.hasAttr 'selected' then '' else null
option.data .default, val
else
val = if o.selected then '' else null
option.attr 'selected', val
toggleSimulator: ->
return .remove() if
return if .length
luda('<input>').prop('tabIndex', -1)
.attr('placeholder', )
.insertAfter( )
updateSimulatorValue: ->
return if
selected = [ .prop('selectedIndex')]
val = if selected then luda(selected).text() else ''
.val val
updateValue: ->
oldVal =
val = .val()
= if luda.isArray(val) then val else [val]
return if not oldVal or luda.arrayEqual( , oldVal)
if
selected = .filter (o) -> o.selected
else
selected = [ .prop('selectedIndex')]
.trigger .changed, {value: val, selected: selected}
reset: ->
.prop 'selectedIndex', -1
.forEach (o) =>
o.selected = luda(o).hasData .default
.help
find: ->
select: .select
simulator: .simulator
create: ->
true
watch: ->
node: [
[ .options, , ]
]
attr: [
['selected', .options, , ]
['multiple', .select, , ]
]
listen: ->
luda.mixin('resetable').get('listen').call this,
[['change', .select, ]]