2016-03-24 20:15:04 +00:00
|
|
|
'use strict'
|
2016-03-18 18:51:02 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
const app = require('electron').app
|
|
|
|
const BrowserWindow = require('electron').BrowserWindow
|
|
|
|
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-01-14 21:16:42 +00:00
|
|
|
var fileDialogProperties = {
|
2016-01-12 02:40:23 +00:00
|
|
|
openFile: 1 << 0,
|
|
|
|
openDirectory: 1 << 1,
|
|
|
|
multiSelections: 1 << 2,
|
|
|
|
createDirectory: 1 << 3
|
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()
|
|
|
|
let [window, options, callback] = parseArgs.apply(null, 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
|
|
|
}
|
|
|
|
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
|
|
|
|
return binding.showOpenDialog(String(options.title), String(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()
|
|
|
|
let [window, options, callback] = parseArgs.apply(null, 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
|
|
|
}
|
|
|
|
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
|
|
|
|
return binding.showSaveDialog(String(options.title), String(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()
|
|
|
|
let [window, options, callback] = parseArgs.apply(null, 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) {
|
|
|
|
return binding.showErrorBox.apply(binding, 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
|
|
|
}
|