2016-03-24 20:15:04 +00:00
|
|
|
'use strict'
|
2016-03-18 18:51:02 +00:00
|
|
|
|
2016-05-15 13:41:30 +00:00
|
|
|
const {app, BrowserWindow} = require('electron')
|
2016-03-24 20:15:04 +00:00
|
|
|
const binding = process.atomBinding('dialog')
|
|
|
|
const v8Util = process.atomBinding('v8_util')
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
var includes = [].includes
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-07-11 04:30:18 +00:00
|
|
|
const fileDialogProperties = {
|
2016-01-12 02:40:23 +00:00
|
|
|
openFile: 1 << 0,
|
|
|
|
openDirectory: 1 << 1,
|
|
|
|
multiSelections: 1 << 2,
|
2016-07-11 04:30:18 +00:00
|
|
|
createDirectory: 1 << 3,
|
|
|
|
showHiddenFiles: 1 << 4
|
2016-03-24 20:15:04 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
var messageBoxTypes = ['none', 'info', 'warning', 'error', 'question']
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 21:16:42 +00:00
|
|
|
var messageBoxOptions = {
|
2016-01-12 02:40:23 +00:00
|
|
|
noLink: 1 << 0
|
2016-03-24 20:15:04 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-31 16:07:56 +00:00
|
|
|
var parseArgs = function (window, options, callback, ...args) {
|
2016-05-07 15:13:40 +00:00
|
|
|
if (window !== null && window.constructor !== BrowserWindow) {
|
2016-01-14 18:35:29 +00:00
|
|
|
// Shift.
|
2016-05-07 14:55:26 +00:00
|
|
|
[callback, options, window] = [options, window, null]
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-31 16:07:56 +00:00
|
|
|
|
2016-01-12 02:40:23 +00:00
|
|
|
if ((callback == null) && typeof options === 'function') {
|
2016-01-14 18:35:29 +00:00
|
|
|
// Shift.
|
2016-05-07 14:55:26 +00:00
|
|
|
[callback, options] = [options, null]
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-31 16:07:56 +00:00
|
|
|
|
|
|
|
// Fallback to using very last argument as the callback function
|
2016-05-07 14:52:52 +00:00
|
|
|
var lastArgument = args[args.length - 1]
|
|
|
|
if ((callback == null) && typeof lastArgument === 'function') {
|
|
|
|
callback = lastArgument
|
2016-03-31 16:07:56 +00:00
|
|
|
}
|
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
return [window, options, callback]
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
var checkAppInitialized = function () {
|
2016-01-12 02:40:23 +00:00
|
|
|
if (!app.isReady()) {
|
2016-03-24 20:15:04 +00:00
|
|
|
throw new Error('dialog module can only be used after app is ready')
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-24 20:15:04 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
|
|
|
module.exports = {
|
2016-03-24 20:15:04 +00:00
|
|
|
showOpenDialog: function (...args) {
|
|
|
|
var prop, properties, value, wrappedCallback
|
|
|
|
checkAppInitialized()
|
2016-12-01 22:37:03 +00:00
|
|
|
let [window, options, callback] = parseArgs(...args)
|
2016-01-12 02:40:23 +00:00
|
|
|
if (options == null) {
|
|
|
|
options = {
|
|
|
|
title: 'Open',
|
|
|
|
properties: ['openFile']
|
2016-03-24 20:15:04 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
if (options.properties == null) {
|
2016-03-24 20:15:04 +00:00
|
|
|
options.properties = ['openFile']
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
if (!Array.isArray(options.properties)) {
|
2016-03-24 20:15:04 +00:00
|
|
|
throw new TypeError('Properties must be an array')
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-24 20:15:04 +00:00
|
|
|
properties = 0
|
2016-01-12 02:40:23 +00:00
|
|
|
for (prop in fileDialogProperties) {
|
2016-03-24 20:15:04 +00:00
|
|
|
value = fileDialogProperties[prop]
|
2016-01-15 17:30:29 +00:00
|
|
|
if (includes.call(options.properties, prop)) {
|
2016-03-24 20:15:04 +00:00
|
|
|
properties |= value
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (options.title == null) {
|
2016-03-24 20:15:04 +00:00
|
|
|
options.title = ''
|
2016-03-23 07:44:11 +00:00
|
|
|
} else if (typeof options.title !== 'string') {
|
2016-03-24 20:15:04 +00:00
|
|
|
throw new TypeError('Title must be a string')
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-05-06 18:10:31 +00:00
|
|
|
if (options.buttonLabel == null) {
|
2016-05-15 06:01:52 +00:00
|
|
|
options.buttonLabel = ''
|
2016-05-06 18:10:31 +00:00
|
|
|
} else if (typeof options.buttonLabel !== 'string') {
|
2016-12-01 22:16:33 +00:00
|
|
|
throw new TypeError('Button label must be a string')
|
2016-05-06 18:10:31 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
if (options.defaultPath == null) {
|
2016-03-24 20:15:04 +00:00
|
|
|
options.defaultPath = ''
|
2016-03-23 07:44:11 +00:00
|
|
|
} else if (typeof options.defaultPath !== 'string') {
|
2016-03-24 20:15:04 +00:00
|
|
|
throw new TypeError('Default path must be a string')
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
if (options.filters == null) {
|
2016-03-24 20:15:04 +00:00
|
|
|
options.filters = []
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-24 20:15:04 +00:00
|
|
|
wrappedCallback = typeof callback === 'function' ? function (success, result) {
|
|
|
|
return callback(success ? result : void 0)
|
|
|
|
} : null
|
2016-05-15 06:01:52 +00:00
|
|
|
return binding.showOpenDialog(options.title, options.buttonLabel, options.defaultPath, options.filters, properties, window, wrappedCallback)
|
2016-01-12 02:40:23 +00:00
|
|
|
},
|
2016-03-18 18:51:02 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
showSaveDialog: function (...args) {
|
|
|
|
var wrappedCallback
|
|
|
|
checkAppInitialized()
|
2016-12-01 22:37:03 +00:00
|
|
|
let [window, options, callback] = parseArgs(...args)
|
2016-01-12 02:40:23 +00:00
|
|
|
if (options == null) {
|
|
|
|
options = {
|
|
|
|
title: 'Save'
|
2016-03-24 20:15:04 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
if (options.title == null) {
|
2016-03-24 20:15:04 +00:00
|
|
|
options.title = ''
|
2016-03-23 07:44:11 +00:00
|
|
|
} else if (typeof options.title !== 'string') {
|
2016-03-24 20:15:04 +00:00
|
|
|
throw new TypeError('Title must be a string')
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-05-06 18:10:31 +00:00
|
|
|
if (options.buttonLabel == null) {
|
2016-05-15 06:01:52 +00:00
|
|
|
options.buttonLabel = ''
|
2016-05-06 18:10:31 +00:00
|
|
|
} else if (typeof options.buttonLabel !== 'string') {
|
2016-12-01 22:16:33 +00:00
|
|
|
throw new TypeError('Button label must be a string')
|
2016-05-06 18:10:31 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
if (options.defaultPath == null) {
|
2016-03-24 20:15:04 +00:00
|
|
|
options.defaultPath = ''
|
2016-03-23 07:44:11 +00:00
|
|
|
} else if (typeof options.defaultPath !== 'string') {
|
2016-03-24 20:15:04 +00:00
|
|
|
throw new TypeError('Default path must be a string')
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
if (options.filters == null) {
|
2016-03-24 20:15:04 +00:00
|
|
|
options.filters = []
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-24 20:15:04 +00:00
|
|
|
wrappedCallback = typeof callback === 'function' ? function (success, result) {
|
|
|
|
return callback(success ? result : void 0)
|
|
|
|
} : null
|
2016-05-15 06:01:52 +00:00
|
|
|
return binding.showSaveDialog(options.title, options.buttonLabel, options.defaultPath, options.filters, window, wrappedCallback)
|
2016-01-12 02:40:23 +00:00
|
|
|
},
|
2016-03-18 18:51:02 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
showMessageBox: function (...args) {
|
|
|
|
var flags, i, j, len, messageBoxType, ref2, ref3, text
|
|
|
|
checkAppInitialized()
|
2016-12-01 22:37:03 +00:00
|
|
|
let [window, options, callback] = parseArgs(...args)
|
2016-01-12 02:40:23 +00:00
|
|
|
if (options == null) {
|
|
|
|
options = {
|
|
|
|
type: 'none'
|
2016-03-24 20:15:04 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
if (options.type == null) {
|
2016-03-24 20:15:04 +00:00
|
|
|
options.type = 'none'
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-24 20:15:04 +00:00
|
|
|
messageBoxType = messageBoxTypes.indexOf(options.type)
|
2016-01-12 02:40:23 +00:00
|
|
|
if (!(messageBoxType > -1)) {
|
2016-03-24 20:15:04 +00:00
|
|
|
throw new TypeError('Invalid message box type')
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
if (!Array.isArray(options.buttons)) {
|
2016-03-24 20:15:04 +00:00
|
|
|
throw new TypeError('Buttons must be an array')
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
if (options.title == null) {
|
2016-03-24 20:15:04 +00:00
|
|
|
options.title = ''
|
2016-03-23 07:44:11 +00:00
|
|
|
} else if (typeof options.title !== 'string') {
|
2016-03-24 20:15:04 +00:00
|
|
|
throw new TypeError('Title must be a string')
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
if (options.message == null) {
|
2016-03-24 20:15:04 +00:00
|
|
|
options.message = ''
|
2016-03-23 07:44:11 +00:00
|
|
|
} else if (typeof options.message !== 'string') {
|
2016-03-24 20:15:04 +00:00
|
|
|
throw new TypeError('Message must be a string')
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
if (options.detail == null) {
|
2016-03-24 20:15:04 +00:00
|
|
|
options.detail = ''
|
2016-03-23 07:44:11 +00:00
|
|
|
} else if (typeof options.detail !== 'string') {
|
2016-03-24 20:15:04 +00:00
|
|
|
throw new TypeError('Detail must be a string')
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
if (options.icon == null) {
|
2016-03-24 20:15:04 +00:00
|
|
|
options.icon = null
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
if (options.defaultId == null) {
|
2016-03-24 20:15:04 +00:00
|
|
|
options.defaultId = -1
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Choose a default button to get selected when dialog is cancelled.
|
2016-01-12 02:40:23 +00:00
|
|
|
if (options.cancelId == null) {
|
2016-03-24 20:15:04 +00:00
|
|
|
options.cancelId = 0
|
|
|
|
ref2 = options.buttons
|
2016-01-12 02:40:23 +00:00
|
|
|
for (i = j = 0, len = ref2.length; j < len; i = ++j) {
|
2016-03-24 20:15:04 +00:00
|
|
|
text = ref2[i]
|
2016-01-12 02:40:23 +00:00
|
|
|
if ((ref3 = text.toLowerCase()) === 'cancel' || ref3 === 'no') {
|
2016-03-24 20:15:04 +00:00
|
|
|
options.cancelId = i
|
|
|
|
break
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-24 20:15:04 +00:00
|
|
|
flags = options.noLink ? messageBoxOptions.noLink : 0
|
|
|
|
return binding.showMessageBox(messageBoxType, options.buttons, options.defaultId, options.cancelId, flags, options.title, options.message, options.detail, options.icon, window, callback)
|
2016-01-12 02:40:23 +00:00
|
|
|
},
|
2016-03-18 18:51:02 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
showErrorBox: function (...args) {
|
2016-12-01 22:37:03 +00:00
|
|
|
return binding.showErrorBox(...args)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-24 20:15:04 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Mark standard asynchronous functions.
|
2016-03-24 20:15:04 +00:00
|
|
|
var ref1 = ['showMessageBox', 'showOpenDialog', 'showSaveDialog']
|
|
|
|
var j, len, api
|
2016-01-12 02:40:23 +00:00
|
|
|
for (j = 0, len = ref1.length; j < len; j++) {
|
2016-03-24 20:15:04 +00:00
|
|
|
api = ref1[j]
|
|
|
|
v8Util.setHiddenValue(module.exports[api], 'asynchronous', true)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|