-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
115 lines (106 loc) · 4.01 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
* @typedef {object} OPTIONS
* @property {string} [directory] Optional string. The working directory to start in
* @property {string} [title] Optional string. A custom title for the OS's folder selection dialog
*/
/**
* Optional asynchronous callback function.
*
* @callback {Function} CALLBACK
* @param {string} selection String to the path, like 'C:\Users\Bob\Desktop', or undefined if no selection made
* @return {void}
*/
/**
* Opens the native OS's folder selection dialog
*
* @param {object} window The "window" object from the browser context. Required.
* @param {OPTIONS} [options] Optional object for setting the title of the window and the default working directory to start in
* @param {CALLBACK} [callback] This is called when the user selects a folder or cancels the window. Returns the path to the folder or undefined
*/
function openFolderExplorer (window, options, callback) {
// Argument validation
var error = false;
if (
!window ||
typeof(window) !== 'object' ||
Array.isArray(window) ||
!window.document ||
!window.document.getElementById ||
!window.document.createElement ||
!window.document.body ||
!window.document.body.appendChild
) {
console.log('You must pass in the window object for this script to have access to the browser context.');
error = true;
}
if (typeof(options) === 'function' && !callback) {
callback = options;
options = null;
}
if (
options &&
(typeof(options) === 'function' && typeof(callback) === 'function') ||
(typeof(options) !== 'object' || Array.isArray(options))
) {
console.log('Optional options argument must be an object');
error = true;
}
if (options && typeof(options) === 'object' && !Array.isArray(options)) {
if (options.directory && typeof(options.directory) !== 'string') {
console.log('Optional options.directory must be a string, like "C:\\"');
error = true;
}
if (options.title && typeof(options.title) !== 'string') {
console.log('Optional options.title must be a string, like "Select path to store settings"');
error = true;
}
}
if (callback && typeof(callback) !== 'function') {
console.log('Optional callback argument must be a function');
error = true;
}
// If there are invalid arguments, return early to prevent errors from being thrown
if (error) {
return;
}
// Constants
var ELEMENT_ID = 'nw-programmatic-folder-select';
var NW_DIRECTORY = 'nwdirectory';
var NW_DIRECTORY_DESCRIPTION = 'nwdirectorydesc';
var NW_WORKING_DIRECTORY = 'nwworkingdir';
// If element does not exist, create it and append to DOM
if (!window.document.getElementById(ELEMENT_ID)) {
var inputElement = window.document.createElement('input');
inputElement.setAttribute('type', 'file');
inputElement.setAttribute('id', ELEMENT_ID);
inputElement.setAttribute(NW_DIRECTORY, '');
inputElement.setAttribute('style', 'display:none');
inputElement.addEventListener('change', function (evt) {
if (callback) {
callback(evt && evt.target && evt.target.value);
}
});
window.document.body.appendChild(inputElement);
}
// Modify element based on options
var element = window.document.getElementById(ELEMENT_ID);
if (options && options.directory) {
element.setAttribute(NW_WORKING_DIRECTORY, options.directory);
} else {
element.removeAttribute(NW_WORKING_DIRECTORY);
}
if (options && options.title) {
element.setAttribute(NW_DIRECTORY_DESCRIPTION, options.title);
} else {
element.removeAttribute(NW_DIRECTORY_DESCRIPTION);
}
// Clear out the previous value before opening the dialog to work around
// a bug where a transformed version of the previous value is shown in the
// dialog like 'C__Users_Bob_Desktop'. See: github.com/nwjs/nw.js/issues/7786
if (element && element.files && element.files.clear) {
element.files.clear();
}
// Trigger a click event to cause the dialog to open
element.click();
}
module.exports = openFolderExplorer;