Consolidate Atom::confirm/confirmSync

Support both an array and hash of buttons to allow callbacks
optionally.
This commit is contained in:
Kevin Sawicki
2013-11-22 15:00:32 -08:00
parent c754b73b71
commit ef9ce1bf70
3 changed files with 39 additions and 33 deletions

View File

@@ -239,33 +239,40 @@ class Atom
# Public: Open a confirm dialog.
#
# The callback for the registered button will be called when clicked.
# Example:
# atom.confirm
# message: 'How you feeling?'
# detailedMessage: 'Be honest.'
# buttons:
# Good: -> window.alert('good to hear')
# Bad: -> window.alert('bummer')
#
# FIXME This API could be improved regarding parameters and shift off the
# given array.
# * options:
# + message: The string message to display.
# + detailedMessage: The string detailed message to display.
# + buttons: Either an array of strings or an object where the the values
# are callbacks to invoke when clicked.
#
# * message: The string message to display.
# * detailedMessage: The string detailed message to display.
# * buttonLabelsAndCallbacks: An array of alternating button labels and
# callbacks.
confirm: (message, detailedMessage, buttonLabelsAndCallbacks...) ->
buttons = []
callbacks = []
while buttonLabelsAndCallbacks.length
do =>
buttons.push buttonLabelsAndCallbacks.shift()
callbacks.push buttonLabelsAndCallbacks.shift()
# Returns the chosen index if buttons was an array or the return of the
# callback if buttons was an object.
confirm: ({message, detailedMessage, buttons}={}) ->
buttons ?= {}
if _.isArray(buttons)
buttonLabels = buttons
else
buttonLabels = Object.keys(buttons)
chosen = @confirmSync(message, detailedMessage, buttons)
callbacks[chosen]?()
# Private:
confirmSync: (message, detailedMessage, buttons, browserWindow=@getCurrentWindow()) ->
dialog.showMessageBox browserWindow,
chosen = dialog.showMessageBox @getCurrentWindow(),
type: 'info'
message: message
detail: detailedMessage
buttons: buttons
buttons: buttonLabels
if _.isArray(buttons)
chosen
else
callback = buttons[buttonLabels[chosen]]
callback?()
# Private:
showSaveDialog: (callback) ->

View File

@@ -842,12 +842,12 @@ class EditorView extends View
@edit(editor)
showBufferConflictAlert: (editor) ->
atom.confirm(
editor.getPath(),
"Has changed on disk. Do you want to reload it?",
"Reload", (=> editor.buffer.reload()),
"Cancel"
)
atom.confirm
message: editor.getPath()
detailedMessage: "Has changed on disk. Do you want to reload it?"
buttons:
Reload: -> editor.getBuffer().reload()
Cancel: null
scrollTop: (scrollTop, options={}) ->
return @cachedScrollTop or 0 unless scrollTop?

View File

@@ -220,12 +220,11 @@ class Pane extends View
return true unless item.shouldPromptToSave?()
uri = item.getUri()
chosen = atom.confirmSync(
"'#{item.getTitle?() ? item.getUri()}' has changes, do you want to save them?"
"Your changes will be lost if you close this item without saving."
["Save", "Cancel", "Don't Save"]
atom.getCurrentWindow()
)
chosen = atom.confirm
message: "'#{item.getTitle?() ? item.getUri()}' has changes, do you want to save them?"
detailedMessage: "Your changes will be lost if you close this item without saving."
buttons: ["Save", "Cancel", "Don't Save"]
switch chosen
when 0 then @saveItem(item, -> true)
when 1 then false