Skip to content

Commit

Permalink
Test on NW.js 0.12.3
Browse files Browse the repository at this point in the history
  • Loading branch information
TheJaredWilcurt committed Aug 2, 2019
1 parent d676481 commit b40dec7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ Programmatically open a native "Folder select" dialog in NW.js.

Similar to `<input type="file" nwdirectory>` but with just JavaScript.

Compatible with all versions of NW.js (even all the way back to 0.12.3).
Compatible with all versions of NW.js.

**Version** | **Tested**
:--: | :--:
v0.12.3 | :heavy_check_mark:
v0.14.7 | :heavy_check_mark:
v0.40.0 | :heavy_check_mark:


## Installation
Expand All @@ -20,7 +26,7 @@ npm install --save nw-programmatic-folder-select

```js
const openFolderExplorer = require('nw-programmatic-folder-select');
openFolderExplorer((selection) => { console.log(selection); });
openFolderExplorer(window, (selection) => { console.log(selection); });
```


Expand All @@ -46,6 +52,7 @@ const callback = function (selection) {
}
}

// All arguments are optional
openFolderExplorer(options, callback);
// Window is required to have access to the browser context
// All other arguments are optional
openFolderExplorer(window, options, callback);
```
15 changes: 10 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
/**
* Opens the native OS's folder selection dialog
* @param {object} window The "window" object from the browser context. Required.
* @param {object} options Optional object for setting the title of the window and the default working directory to start in
* @param {Function} callback This is called when the user selects a folder or cancels the window. it will retun the path to the folder or undefined
*/
function openFolderExplorer (options, callback) {
function openFolderExplorer (window, options, callback) {
// Argument validation
var error = false;
if (!window || typeof(window) !== 'object') {
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 = options;
options = null;
Expand Down Expand Up @@ -34,8 +39,8 @@ function openFolderExplorer (options, callback) {
}

// If element does not exist, create it and append to DOM
if (!document.getElementById('nwdirectory')) {
var inputElement = document.createElement('input');
if (!window.document.getElementById('nwdirectory')) {
var inputElement = window.document.createElement('input');
inputElement.setAttribute('type', 'file');
inputElement.setAttribute('id', 'nwdirectory');
inputElement.setAttribute('nwdirectory', '');
Expand All @@ -45,11 +50,11 @@ function openFolderExplorer (options, callback) {
callback(evt.target.value);
}
});
document.body.append(inputElement);
window.document.body.appendChild(inputElement);
}

// Modify element based on options
var element = document.getElementById('nwdirectory');
var element = window.document.getElementById('nwdirectory');
if (options && options.directory) {
element.setAttribute('nwworkingdir', options.directory);
} else {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "nw-programmatic-folder-select",
"version": "1.0.1",
"description": "Programmatically open a native \"Folder select\" dialog in NW.js",
"version": "1.0.2",
"description": "Programmatically open a native folder select dialog in NW.js",
"main": "index.js",
"scripts": {
"lint": "eslint --config=.eslintrc.js index.js",
Expand Down

0 comments on commit b40dec7

Please sign in to comment.