kdf
Version:
86 lines (67 loc) • 2.48 kB
text/coffeescript
KDInputView = require './inputview.coffee'
module.exports = class KDSelectBox extends KDInputView
constructor:(options = {})->
options.type = "select"
super options
setDomElement:(cssClass)->
= "name"
name = "name='#{@options.name}'"
= $ """
<div class='kdselectbox #{cssClass}'>
<select #{name}></select>
<span class='title'></span>
<span class='arrows'></span>
</div>"
"""
$select = @$("select").eq(0)
$title = @$("span.title").eq(0)
bindEvents:->
$select.bind "blur change focus",(event)=>
# log "kdselectbox change" if event.type is "change"
()? () if event.type is "change"
event.type, event, ()
event
super
setDefaultValue:(value)->
().val value if value isnt ""
$select.val value
$title.text $select.find("option[value=\"#{value}\"]").text()
= value
getDefaultValue:->
getValue:-> $select.val()
setValue:(value)->
$select.val value
()
makeDisabled:->
"disabled"
$select.attr "disabled","disabled"
makeEnabled:->
"disabled"
$select.removeAttr "disabled"
setSelectOptions:(options)->
firstOption = null
unless options.length
for own optGroup, subOptions of options
$optGroup = $ "<optgroup label='#{optGroup}'/>"
$select.append $optGroup
for option in subOptions
firstOption or= option
$optGroup.append "<option value='#{option.value}'>#{option.title}</option>"
else if options.length
for option in options
$select.append "<option value='#{option.value}'>#{option.title}</option>"
firstOption or= option
else
warn "no valid options specified for the input:", @
value = () or firstOption?.value or ""
$select.val value + "" # casting to number in case, i don't remember why though. SY
# escapedDefault = value.replace /\//g, '\\/'
$title.text $select.find("option[value=\"#{value}\"]").text()
removeSelectOptions:->
$select.find("optgroup").remove()
$select.find("option").remove()
change:->
$title.text $select.find("option[value=\"#{@getValue()}\"]").text()
focus:-> 'focus'
blur:-> 'focus'