coffeescript-ui
Version:
Coffeescript User Interface System
148 lines (121 loc) • 3.13 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.ConfirmationChoice extends CUI.ConfirmationDialog
constructor: (opts) ->
super(opts)
.addClass("cui-confirmation-choice")
:
ok: "Ok"
cancel: "Cancel"
initOpts: ->
super()
onChoice:
check: Function
choices:
mandatory: true
default: []
check: (v) ->
if not CUI.util.isArray(v)
return false
for choice, idx in v
if not choice
continue
CUI.Element.readOpts(choice, "new CUI.ConfirmationChoice[choice#"+idx+"]",
CUI.ConfirmationChoice.choiceOpts)
return true
:
text:
mandatory: true
check: String
onClick:
check: Function
icon:
check: CUI.Icon
choice:
check: String
tooltip:
check: "PlainObject"
cancel:
default: false
check: Boolean
primary:
mandatory: true
default: false
check: Boolean
disabled:
mandatory: true
default: false
check: Boolean
readOpts: ->
super()
= "cancel"
init: ->
= []
for choice in
if not choice
continue
btn_opts =
left: true
value: choice
disabled: choice.disabled
tooltip: choice.tooltip
onClick: (ev, btn) =>
= btn.getValue()
CUI.chainedCall(
=>
ret = .onClick?.call(@, ev, btn)
,
=>
if .cancel
# force this in case opts.cancel is not set
# go out of the way
return false
?.call(@, ev, , @, btn)
).done (ret1, ret2) =>
# console.debug "chained call done", ret1, ret2, ev,
if ev.isImmediatePropagationStopped() or
ret1 == false or
ret2 == false
return
# its possible that a previous click caused an error
# so would already be unset
?.resolve(, btn, ev)
return
for key of CUI.ConfirmationChoice.choiceOpts
if key not in ["onClick", "cancel", "choice"]
btn_opts[key] = choice[key]
.push(btn_opts)
super()
return
__getResolveValue: =>
show: ->
CUI.util.assert(false, "ConfirmationChoice.show", "Use .open to open the ConfirmationChoice")
cancel: (ev, ret) ->
.reject(, ret)
destroy: ->
= null
super()
# opens the dialog and returns a promise
# promise fails if the user cancels
open: ->
if
return .promise()
= new CUI.Deferred()
CUI.ConfirmationDialog::show.call(@)
.always =>
= null
return .promise()
CUI.choice = (opts=text: "CUI.ConfirmationChoice") ->
new CUI.ConfirmationChoice(opts).open()
CUI.defaults.class.ConfirmationChoice = CUI.ConfirmationChoice