2016-03-18 18:51:02 +00:00
|
|
|
'use strict';
|
|
|
|
|
2016-01-14 21:16:42 +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-01-15 17:30:29 +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-14 21:16:42 +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-14 21:16:42 +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.
|
2016-01-12 02:40:23 +00:00
|
|
|
callback = options;
|
|
|
|
options = window;
|
|
|
|
window = null;
|
|
|
|
}
|
|
|
|
if ((callback == null) && typeof options === 'function') {
|
2016-01-14 18:35:29 +00:00
|
|
|
// Shift.
|
2016-01-12 02:40:23 +00:00
|
|
|
callback = options;
|
|
|
|
options = null;
|
|
|
|
}
|
|
|
|
return [window, options, callback];
|
|
|
|
};
|
|
|
|
|
2016-01-14 21:16:42 +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');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
2016-03-18 18:51:02 +00:00
|
|
|
showOpenDialog: function(...args) {
|
2016-03-21 16:50:11 +00:00
|
|
|
var prop, properties, value, wrappedCallback;
|
2016-01-12 02:40:23 +00:00
|
|
|
checkAppInitialized();
|
2016-03-21 16:50:11 +00:00
|
|
|
let [window, options, callback] = parseArgs.apply(null, args);
|
2016-01-12 02:40:23 +00:00
|
|
|
if (options == null) {
|
|
|
|
options = {
|
|
|
|
title: 'Open',
|
|
|
|
properties: ['openFile']
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (options.properties == null) {
|
|
|
|
options.properties = ['openFile'];
|
|
|
|
}
|
|
|
|
if (!Array.isArray(options.properties)) {
|
|
|
|
throw new TypeError('Properties need to be array');
|
|
|
|
}
|
|
|
|
properties = 0;
|
|
|
|
for (prop in fileDialogProperties) {
|
|
|
|
value = fileDialogProperties[prop];
|
2016-01-15 17:30:29 +00:00
|
|
|
if (includes.call(options.properties, prop)) {
|
2016-01-12 02:40:23 +00:00
|
|
|
properties |= value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (options.title == null) {
|
|
|
|
options.title = '';
|
|
|
|
}
|
|
|
|
if (options.defaultPath == null) {
|
|
|
|
options.defaultPath = '';
|
|
|
|
}
|
|
|
|
if (options.filters == null) {
|
|
|
|
options.filters = [];
|
|
|
|
}
|
|
|
|
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-03-18 18:51:02 +00:00
|
|
|
|
|
|
|
showSaveDialog: function(...args) {
|
2016-03-21 16:50:11 +00:00
|
|
|
var wrappedCallback;
|
2016-01-12 02:40:23 +00:00
|
|
|
checkAppInitialized();
|
2016-03-21 16:50:11 +00:00
|
|
|
let [window, options, callback] = parseArgs.apply(null, args);
|
2016-01-12 02:40:23 +00:00
|
|
|
if (options == null) {
|
|
|
|
options = {
|
|
|
|
title: 'Save'
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (options.title == null) {
|
|
|
|
options.title = '';
|
|
|
|
}
|
|
|
|
if (options.defaultPath == null) {
|
|
|
|
options.defaultPath = '';
|
|
|
|
}
|
|
|
|
if (options.filters == null) {
|
|
|
|
options.filters = [];
|
|
|
|
}
|
|
|
|
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-03-18 18:51:02 +00:00
|
|
|
|
|
|
|
showMessageBox: function(...args) {
|
2016-03-21 16:50:11 +00:00
|
|
|
var flags, i, j, len, messageBoxType, ref2, ref3, text;
|
2016-01-12 02:40:23 +00:00
|
|
|
checkAppInitialized();
|
2016-03-21 16:50:11 +00:00
|
|
|
let [window, options, callback] = parseArgs.apply(null, args);
|
2016-01-12 02:40:23 +00:00
|
|
|
if (options == null) {
|
|
|
|
options = {
|
|
|
|
type: 'none'
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (options.type == null) {
|
|
|
|
options.type = 'none';
|
|
|
|
}
|
|
|
|
messageBoxType = messageBoxTypes.indexOf(options.type);
|
|
|
|
if (!(messageBoxType > -1)) {
|
|
|
|
throw new TypeError('Invalid message box type');
|
|
|
|
}
|
|
|
|
if (!Array.isArray(options.buttons)) {
|
|
|
|
throw new TypeError('Buttons need to be array');
|
|
|
|
}
|
|
|
|
if (options.title == null) {
|
|
|
|
options.title = '';
|
|
|
|
}
|
|
|
|
if (options.message == null) {
|
|
|
|
options.message = '';
|
|
|
|
}
|
|
|
|
if (options.detail == null) {
|
|
|
|
options.detail = '';
|
|
|
|
}
|
|
|
|
if (options.icon == null) {
|
|
|
|
options.icon = null;
|
|
|
|
}
|
|
|
|
if (options.defaultId == null) {
|
|
|
|
options.defaultId = -1;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
for (i = j = 0, len = ref2.length; j < len; i = ++j) {
|
|
|
|
text = ref2[i];
|
|
|
|
if ((ref3 = text.toLowerCase()) === 'cancel' || ref3 === 'no') {
|
|
|
|
options.cancelId = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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-03-18 18:51:02 +00:00
|
|
|
|
|
|
|
showErrorBox: function(...args) {
|
2016-01-12 02:40:23 +00:00
|
|
|
return binding.showErrorBox.apply(binding, args);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Mark standard asynchronous functions.
|
2016-01-14 21:16:42 +00:00
|
|
|
var ref1 = ['showMessageBox', 'showOpenDialog', 'showSaveDialog'];
|
2016-01-15 02:07:29 +00:00
|
|
|
var j, len, api;
|
2016-01-12 02:40:23 +00:00
|
|
|
for (j = 0, len = ref1.length; j < len; j++) {
|
2016-01-15 02:07:29 +00:00
|
|
|
api = ref1[j];
|
2016-01-12 02:40:23 +00:00
|
|
|
v8Util.setHiddenValue(module.exports[api], 'asynchronous', true);
|
|
|
|
}
|