electron/lib/browser/api/dialog.js

184 lines
5.5 KiB
JavaScript
Raw Normal View History

'use strict'
2016-03-18 18:51:02 +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
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-01-12 02:40:23 +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-01-12 02:40:23 +00:00
var parseArgs = function (window, options, callback) {
2016-01-12 02:40:23 +00:00
if (!(window === null || (window != null ? window.constructor : void 0) === BrowserWindow)) {
2016-01-14 18:35:29 +00:00
// Shift.
callback = options
options = window
window = null
2016-01-12 02:40:23 +00:00
}
if ((callback == null) && typeof options === 'function') {
2016-01-14 18:35:29 +00:00
// Shift.
callback = options
options = null
2016-01-12 02:40:23 +00:00
}
return [window, options, callback]
}
2016-01-12 02:40:23 +00:00
var checkAppInitialized = function () {
2016-01-12 02:40:23 +00:00
if (!app.isReady()) {
throw new Error('dialog module can only be used after app is ready')
2016-01-12 02:40:23 +00:00
}
}
2016-01-12 02:40:23 +00:00
module.exports = {
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-01-12 02:40:23 +00:00
}
if (options.properties == null) {
options.properties = ['openFile']
2016-01-12 02:40:23 +00:00
}
if (!Array.isArray(options.properties)) {
throw new TypeError('Properties must be an array')
2016-01-12 02:40:23 +00:00
}
properties = 0
2016-01-12 02:40:23 +00:00
for (prop in fileDialogProperties) {
value = fileDialogProperties[prop]
2016-01-15 17:30:29 +00:00
if (includes.call(options.properties, prop)) {
properties |= value
2016-01-12 02:40:23 +00:00
}
}
if (options.title == null) {
options.title = ''
} else if (typeof options.title !== 'string') {
throw new TypeError('Title must be a string')
2016-01-12 02:40:23 +00:00
}
if (options.defaultPath == null) {
options.defaultPath = ''
} else if (typeof options.defaultPath !== 'string') {
throw new TypeError('Default path must be a string')
2016-01-12 02:40:23 +00:00
}
if (options.filters == null) {
options.filters = []
2016-01-12 02:40:23 +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
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-01-12 02:40:23 +00:00
}
if (options.title == null) {
options.title = ''
} else if (typeof options.title !== 'string') {
throw new TypeError('Title must be a string')
2016-01-12 02:40:23 +00:00
}
if (options.defaultPath == null) {
options.defaultPath = ''
} else if (typeof options.defaultPath !== 'string') {
throw new TypeError('Default path must be a string')
2016-01-12 02:40:23 +00:00
}
if (options.filters == null) {
options.filters = []
2016-01-12 02:40:23 +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
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-01-12 02:40:23 +00:00
}
if (options.type == null) {
options.type = 'none'
2016-01-12 02:40:23 +00:00
}
messageBoxType = messageBoxTypes.indexOf(options.type)
2016-01-12 02:40:23 +00:00
if (!(messageBoxType > -1)) {
throw new TypeError('Invalid message box type')
2016-01-12 02:40:23 +00:00
}
if (!Array.isArray(options.buttons)) {
throw new TypeError('Buttons must be an array')
2016-01-12 02:40:23 +00:00
}
if (options.title == null) {
options.title = ''
} else if (typeof options.title !== 'string') {
throw new TypeError('Title must be a string')
2016-01-12 02:40:23 +00:00
}
if (options.message == null) {
options.message = ''
} else if (typeof options.message !== 'string') {
throw new TypeError('Message must be a string')
2016-01-12 02:40:23 +00:00
}
if (options.detail == null) {
options.detail = ''
} else if (typeof options.detail !== 'string') {
throw new TypeError('Detail must be a string')
2016-01-12 02:40:23 +00:00
}
if (options.icon == null) {
options.icon = null
2016-01-12 02:40:23 +00:00
}
if (options.defaultId == null) {
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) {
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) {
text = ref2[i]
2016-01-12 02:40:23 +00:00
if ((ref3 = text.toLowerCase()) === 'cancel' || ref3 === 'no') {
options.cancelId = i
break
2016-01-12 02:40:23 +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
showErrorBox: function (...args) {
return binding.showErrorBox.apply(binding, args)
2016-01-12 02:40:23 +00:00
}
}
2016-01-12 02:40:23 +00:00
2016-01-14 18:35:29 +00:00
// Mark standard asynchronous functions.
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++) {
api = ref1[j]
v8Util.setHiddenValue(module.exports[api], 'asynchronous', true)
2016-01-12 02:40:23 +00:00
}