From 19b25c0d8330caff14a05c6273f89e3d9786ee3b Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Sat, 6 Sep 2014 18:55:01 +0800 Subject: [PATCH] Make dialog API's parameters more flexible. --- atom/browser/api/lib/dialog.coffee | 32 +++++++++++++++--------------- docs/api/dialog.md | 6 +++--- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/atom/browser/api/lib/dialog.coffee b/atom/browser/api/lib/dialog.coffee index 2bb1e9f721f..f34333690de 100644 --- a/atom/browser/api/lib/dialog.coffee +++ b/atom/browser/api/lib/dialog.coffee @@ -10,13 +10,21 @@ fileDialogProperties = messageBoxTypes = ['none', 'info', 'warning'] +parseArgs = (window, options, callback) -> + unless window is null or window?.constructor is BrowserWindow + # Shift. + callback = options + options = window + window = null + if not callback? and typeof options is 'function' + # Shift. + callback = options + options = null + [window, options, callback] + module.exports = - showOpenDialog: (window, options, callback) -> - unless window?.constructor is BrowserWindow - # Shift. - callback = options - options = window - window = null + showOpenDialog: (args...) -> + [window, options, callback] = parseArgs args... options ?= title: 'Open', properties: ['openFile'] options.properties ?= ['openFile'] @@ -44,11 +52,7 @@ module.exports = wrappedCallback showSaveDialog: (window, options, callback) -> - unless window?.constructor is BrowserWindow - # Shift. - callback = options - options = window - window = null + [window, options, callback] = parseArgs args... options ?= title: 'Save' options.title ?= '' @@ -68,11 +72,7 @@ module.exports = wrappedCallback showMessageBox: (window, options, callback) -> - unless window?.constructor is BrowserWindow - # Shift. - callback = options - options = window - window = null + [window, options, callback] = parseArgs args... options ?= type: 'none' options.type ?= 'none' diff --git a/docs/api/dialog.md b/docs/api/dialog.md index 1778e8fb3a8..9c1f1c8addb 100644 --- a/docs/api/dialog.md +++ b/docs/api/dialog.md @@ -13,7 +13,7 @@ console.log(dialog.showOpenDialog({ properties: [ 'openFile', 'openDirectory', ' **Note for OS X**: If you want to present dialogs as sheets, the only thing you have to do is to provide a `BrowserWindow` reference in the `browserWindow` parameter. -## dialog.showOpenDialog([browserWindow], options, [callback]) +## dialog.showOpenDialog([browserWindow], [options], [callback]) * `browserWindow` BrowserWindow * `options` Object @@ -49,7 +49,7 @@ and directory selector at the same time, so if you set `properties` to `['openFile', 'openDirectory']` on these platforms, a directory selector would be showed. -## dialog.showSaveDialog([browserWindow], options, [callback]) +## dialog.showSaveDialog([browserWindow], [options], [callback]) * `browserWindow` BrowserWindow * `options` Object @@ -67,7 +67,7 @@ The `filters` specifies an array of file types that can be displayed, see If a `callback` is passed, the API call would be asynchronous and the result would be passed via `callback(filename)` -## dialog.showMessageBox([browserWindow], options, [callback]) +## dialog.showMessageBox([browserWindow], [options], [callback]) * `browserWindow` BrowserWindow * `options` Object