coffeescript-ui
Version:
Coffeescript User Interface System
291 lines (239 loc) • 6.41 kB
text/coffeescript
###
* coffeescript-ui - Coffeescript User Interface System (CUI)
* Copyright (c) 2013 - 2016 Programmfabrik GmbH
* MIT Licence
* https://github.com/programmfabrik/coffeescript-ui, http://www.coffeescript-ui.org
###
class CUI.Select extends CUI.Checkbox
:
not_found: "- not found -"
initOpts: ->
super()
empty_text:
check: String
not_found_text:
default: CUI.Select.defaults.not_found
check: String
options:
mandatory: true
check: (v) ->
CUI.util.isArray(v) or CUI.util.isFunction(v)
#group can be used for buttonbars to specify a group css style
group:
check: String
onShow:
check: Function
onHide:
check: Function
init: ->
= null
if not CUI.util.isFunction()
# .prop("title", +":"+)
disable: ( = false) ->
super()
isDisabled: ->
if
false
else
super()
enableOption: (value) ->
opt =
if not opt
return
opt.disabled = false
disableOption: (value) ->
opt =
if not opt
return
opt.disabled = true
__getOptionByValue: (value) ->
for opt in
if opt.value == value
return opt
return null
enable: (automatic = false) ->
if automatic
if
super()
else
# the select was disabled automatically but
# during the deferred, disabled manually
# so we ignore the automatic enable
else
super()
__loadOptions: (event) ->
if ?.state() == "pending"
return
ret =
if CUI.util.isPromise(ret)
= ret
btn =
if btn
icon_right = btn.getIconRight()
btn.setIconRight("spinner")
.always =>
btn.setIconRight(icon_right)
else
= CUI.resolvedPromise(ret)
# if not
#
# .always =>
#
.done () =>
first_value_opt = undefined
# adjust options, so we always have a text and value
for opt, idx in
opt._idx = idx
if CUI.util.isUndef(opt.text) and not CUI.util.isUndef(opt.value) and not opt.icon
opt.text = ""+opt.value
if CUI.util.isUndef(opt.value) and not CUI.util.isUndef(opt.text)
opt.value = opt.text
if not CUI.util.isUndef(opt.value) and first_value_opt == undefined
first_value_opt = opt
# auto - select first opt, if value unset
# and no empty text allowed
= undefined
if first_value_opt
= first_value_opt
= .value
else
console.warn("Select #{@getName()}: did not find a value opt and no empty text is defined.", opts: )
# console.debug "load options", , ,
setData: (data) ->
super(data, false) # dont init data, only set
if CUI.util.isFunction()
.done =>
else
@
getCheckboxClass: ->
"cui-button-select"
registerLabel: ->
# ignore
getButtonOpts: ->
group:
role: "select"
left: true # make sure we have a "left" container for the icon
menu:
active_item_idx: ?._idx or -1
allow_null: not CUI.util.isEmpty()
class: "cui-select-menu"
onDeactivate: (btn, item, idx, flags) =>
if flags.prior_activate
return
if
return
@
onClick: =>
?.apply(@, arguments)
onShow: =>
?.apply(@, arguments)
onHide: =>
?.apply(@, arguments)
onActivate: (btn, item, idx, flags) =>
if
return
@
items: (event) =>
.done =>
# in case we have updated options
# we need to adjust the active idx
has_items: true
getDefaultValue: ->
# console.debug "getDefaultValue", ,
if
.value
else
undefined
getValue: ->
if
super()
else
checkValue: (v, flags) ->
true
reload: ->
if CUI.util.isFunction()
super()
storeValue: (value, flags) ->
if
super(value, flags)
else
= value
@
displayValue: ->
CUI.DataFieldInput::displayValue.call(@)
if CUI.util.isFunction() and not
.done =>
@
__displayValue: ->
found_opt = null
max_chars = null
for opt, idx in
if found_opt == null and opt.value ==
found_opt = opt
if opt.text?.length > max_chars
max_chars = opt.text?.length
# console.warn "Select.displayValue", , , ,
if found_opt
if found_opt.icon
if found_opt.icon instanceof CUI.Icon
.setIcon(found_opt.icon.copy())
else
.setIcon(found_opt.icon)
else
.setIcon(null)
txt = found_opt.text_selected or found_opt.text
.menuSetActiveIdx(found_opt._idx)
else
if == null and not CUI.util.isEmpty()
txt =
else
# console.error("Select, option not found:", , , , , "options:", )
txt = .trim()+" "+
.menuSetActiveIdx(-1)
.setText(txt)
if txt?.length > max_chars
max_chars = txt.length
.setTextMaxChars(max_chars)
render: ->
super()
if button =
CUI.Events.listen
type: "keydown"
node:
capture: true
call: (ev) =>
if ev.getKeyboardKey() not in ["Up", "Down"]
return
button.onClickAction(ev)
ev.stopPropagation()
return
getOptions: ->
: (opts) ->
if opts.options.length == 1
out_opts =
form: opts.form
text: opts.options[0].text
new CUI.Output(out_opts)
else
new CUI.Select(opts)
CUI.defaults.class.Select = CUI.Select