diff --git a/src/atom.coffee b/src/atom.coffee index 29c77d864..cc315d138 100644 --- a/src/atom.coffee +++ b/src/atom.coffee @@ -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) -> diff --git a/src/editor-view.coffee b/src/editor-view.coffee index d3b587e6f..c3e85cdf3 100644 --- a/src/editor-view.coffee +++ b/src/editor-view.coffee @@ -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? diff --git a/src/pane.coffee b/src/pane.coffee index e162fb7a1..bf190e2e2 100644 --- a/src/pane.coffee +++ b/src/pane.coffee @@ -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