-
Notifications
You must be signed in to change notification settings - Fork 7
/
nw-fileDialog.js
53 lines (48 loc) · 1.67 KB
/
nw-fileDialog.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
/* https://github.com/DWand/nw-fileDialog
* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* http://www.wtfpl.net/ for more details. */
angular.module('DWand.nw-fileDialog', [])
.factory('fileDialog', [function(){
var callDialog = function(dialog, callback) {
dialog.addEventListener('change', function() {
var result = dialog.value;
callback(result);
}, false);
dialog.click();
};
var dialogs = {};
dialogs.saveAs = function(callback, defaultFilename, acceptTypes) {
var dialog = document.createElement('input');
dialog.type = 'file';
dialog.nwsaveas = defaultFilename || '';
if (angular.isArray(acceptTypes)) {
dialog.accept = acceptTypes.join(',');
} else if (angular.isString(acceptTypes)) {
dialog.accept = acceptTypes;
}
callDialog(dialog, callback);
};
dialogs.openFile = function(callback, multiple, acceptTypes) {
var dialog = document.createElement('input');
dialog.type = 'file';
if (multiple === true) {
dialog.multiple = 'multiple';
}
if (angular.isArray(acceptTypes)) {
dialog.accept = acceptTypes.join(',');
} else if (angular.isString(acceptTypes)) {
dialog.accept = acceptTypes;
}
callDialog(dialog, callback);
};
dialogs.openDir = function(callback) {
var dialog = document.createElement('input');
dialog.type = 'file';
dialog.nwdirectory = 'nwdirectory';
callDialog(dialog, callback);
};
return dialogs;
}]);