2013-05-03 13:03:26 +00:00
|
|
|
binding = process.atomBinding 'dialog'
|
2013-09-24 10:01:12 +00:00
|
|
|
v8Util = process.atomBinding 'v8_util'
|
2014-11-05 11:50:24 +00:00
|
|
|
app = require 'app'
|
2013-05-30 12:16:54 +00:00
|
|
|
BrowserWindow = require 'browser-window'
|
2013-05-03 11:31:24 +00:00
|
|
|
|
2013-05-20 13:46:43 +00:00
|
|
|
fileDialogProperties =
|
2014-08-06 05:47:44 +00:00
|
|
|
openFile: 1 << 0
|
|
|
|
openDirectory: 1 << 1
|
|
|
|
multiSelections: 1 << 2
|
|
|
|
createDirectory: 1 << 3
|
2013-05-03 11:31:24 +00:00
|
|
|
|
2015-07-07 08:52:53 +00:00
|
|
|
messageBoxTypes = ['none', 'info', 'warning', 'error', 'question']
|
2013-05-03 11:31:24 +00:00
|
|
|
|
2014-09-06 10:55:01 +00:00
|
|
|
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]
|
|
|
|
|
2014-11-05 11:50:24 +00:00
|
|
|
checkAppInitialized = ->
|
|
|
|
throw new Error('dialog module can only be used after app is ready') unless app.isReady()
|
|
|
|
|
2013-05-20 13:46:43 +00:00
|
|
|
module.exports =
|
2014-09-06 10:55:01 +00:00
|
|
|
showOpenDialog: (args...) ->
|
2014-11-05 11:50:24 +00:00
|
|
|
checkAppInitialized()
|
2014-09-06 10:55:01 +00:00
|
|
|
[window, options, callback] = parseArgs args...
|
2013-09-23 08:36:33 +00:00
|
|
|
|
2013-09-23 08:51:00 +00:00
|
|
|
options ?= title: 'Open', properties: ['openFile']
|
|
|
|
options.properties ?= ['openFile']
|
2013-05-20 13:46:43 +00:00
|
|
|
throw new TypeError('Properties need to be array') unless Array.isArray options.properties
|
2013-05-03 11:31:24 +00:00
|
|
|
|
2013-05-20 13:46:43 +00:00
|
|
|
properties = 0
|
|
|
|
for prop, value of fileDialogProperties
|
|
|
|
properties |= value if prop in options.properties
|
2013-05-05 12:24:20 +00:00
|
|
|
|
2013-09-23 08:51:00 +00:00
|
|
|
options.title ?= ''
|
|
|
|
options.defaultPath ?= ''
|
2014-08-06 04:44:02 +00:00
|
|
|
options.filters ?= []
|
2013-05-18 02:41:43 +00:00
|
|
|
|
2014-06-15 04:43:28 +00:00
|
|
|
wrappedCallback =
|
|
|
|
if typeof callback is 'function'
|
|
|
|
(success, result) -> callback(if success then result)
|
|
|
|
else
|
|
|
|
null
|
|
|
|
|
2013-09-23 08:51:00 +00:00
|
|
|
binding.showOpenDialog String(options.title),
|
|
|
|
String(options.defaultPath),
|
2014-08-06 04:44:02 +00:00
|
|
|
options.filters
|
2013-09-23 08:36:33 +00:00
|
|
|
properties,
|
2013-09-23 11:23:49 +00:00
|
|
|
window,
|
2014-06-15 04:43:28 +00:00
|
|
|
wrappedCallback
|
2013-05-05 12:24:20 +00:00
|
|
|
|
2014-09-07 22:14:43 +00:00
|
|
|
showSaveDialog: (args...) ->
|
2014-11-05 11:50:24 +00:00
|
|
|
checkAppInitialized()
|
2014-09-06 10:55:01 +00:00
|
|
|
[window, options, callback] = parseArgs args...
|
2013-05-05 12:24:20 +00:00
|
|
|
|
2013-09-23 08:51:00 +00:00
|
|
|
options ?= title: 'Save'
|
|
|
|
options.title ?= ''
|
|
|
|
options.defaultPath ?= ''
|
2014-08-06 13:51:36 +00:00
|
|
|
options.filters ?= []
|
2013-05-05 12:24:20 +00:00
|
|
|
|
2014-06-15 04:43:28 +00:00
|
|
|
wrappedCallback =
|
|
|
|
if typeof callback is 'function'
|
|
|
|
(success, result) -> callback(if success then result)
|
|
|
|
else
|
|
|
|
null
|
|
|
|
|
2013-09-23 11:59:00 +00:00
|
|
|
binding.showSaveDialog String(options.title),
|
|
|
|
String(options.defaultPath),
|
2014-08-06 04:44:02 +00:00
|
|
|
options.filters
|
2013-09-23 11:59:00 +00:00
|
|
|
window,
|
2014-06-15 04:43:28 +00:00
|
|
|
wrappedCallback
|
2013-05-10 13:19:13 +00:00
|
|
|
|
2014-09-07 22:14:43 +00:00
|
|
|
showMessageBox: (args...) ->
|
2014-11-05 11:50:24 +00:00
|
|
|
checkAppInitialized()
|
2014-09-06 10:55:01 +00:00
|
|
|
[window, options, callback] = parseArgs args...
|
2013-06-07 07:59:12 +00:00
|
|
|
|
2013-09-23 08:51:00 +00:00
|
|
|
options ?= type: 'none'
|
|
|
|
options.type ?= 'none'
|
2015-03-06 12:26:26 +00:00
|
|
|
messageBoxType = messageBoxTypes.indexOf options.type
|
|
|
|
throw new TypeError('Invalid message box type') unless messageBoxType > -1
|
2013-05-10 13:19:13 +00:00
|
|
|
|
|
|
|
throw new TypeError('Buttons need to be array') unless Array.isArray options.buttons
|
|
|
|
|
2013-09-23 08:51:00 +00:00
|
|
|
options.title ?= ''
|
|
|
|
options.message ?= ''
|
|
|
|
options.detail ?= ''
|
2015-01-05 23:08:42 +00:00
|
|
|
options.icon ?= null
|
2013-05-10 13:19:13 +00:00
|
|
|
|
2015-03-06 12:26:26 +00:00
|
|
|
binding.showMessageBox messageBoxType,
|
2013-06-07 07:59:12 +00:00
|
|
|
options.buttons,
|
2015-01-05 23:08:42 +00:00
|
|
|
[options.title, options.message, options.detail],
|
|
|
|
options.icon,
|
2013-09-23 06:29:55 +00:00
|
|
|
window,
|
|
|
|
callback
|
2013-09-24 10:01:12 +00:00
|
|
|
|
2014-11-05 08:04:39 +00:00
|
|
|
showErrorBox: (args...) ->
|
|
|
|
binding.showErrorBox args...
|
|
|
|
|
2013-09-24 10:01:12 +00:00
|
|
|
# Mark standard asynchronous functions.
|
|
|
|
v8Util.setHiddenValue f, 'asynchronous', true for k, f of module.exports
|