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
116
|
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set ft=javascript ts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/**
* This file contains helper functions for showing OS-specific
* file and folder pickers.
*/
const { Cu, Cc, Ci } = require("chrome");
const { FileUtils } = Cu.import("resource://gre/modules/FileUtils.jsm", {});
const promise = require("promise");
const { merge } = require("sdk/util/object");
const { getLocalizedString } = require("devtools/client/projecteditor/lib/helpers/l10n");
/**
* Show a file / folder picker.
* https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIFilePicker
*
* @param object options
* Additional options for setting the source. Supported options:
* - directory: string, The path to default opening
* - defaultName: string, The filename including extension that
* should be suggested to the user as a default
* - window: DOMWindow, The filename including extension that
* should be suggested to the user as a default
* - title: string, The filename including extension that
* should be suggested to the user as a default
* - mode: int, The type of picker to open.
*
* @return promise
* A promise that is resolved with the full path
* after the file has been picked.
*/
function showPicker(options) {
let fp = Cc["@mozilla.org/filepicker;1"].createInstance(Ci.nsIFilePicker);
if (options.directory) {
try {
fp.displayDirectory = FileUtils.File(options.directory);
} catch (ex) {
console.warn(ex);
}
}
if (options.defaultName) {
fp.defaultString = options.defaultName;
}
fp.init(options.window, options.title, options.mode);
let deferred = promise.defer();
fp.open({
done: function (res) {
if (res === Ci.nsIFilePicker.returnOK || res === Ci.nsIFilePicker.returnReplace) {
deferred.resolve(fp.file.path);
} else {
deferred.reject();
}
}
});
return deferred.promise;
}
exports.showPicker = showPicker;
/**
* Show a save dialog
* https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIFilePicker
*
* @param object options
* Additional options as specified in showPicker
*
* @return promise
* A promise that is resolved when the save dialog has closed
*/
function showSave(options) {
return showPicker(merge({
title: getLocalizedString("projecteditor.selectFileLabel"),
mode: Ci.nsIFilePicker.modeSave
}, options));
}
exports.showSave = showSave;
/**
* Show a file open dialog
*
* @param object options
* Additional options as specified in showPicker
*
* @return promise
* A promise that is resolved when the file has been opened
*/
function showOpen(options) {
return showPicker(merge({
title: getLocalizedString("projecteditor.openFileLabel"),
mode: Ci.nsIFilePicker.modeOpen
}, options));
}
exports.showOpen = showOpen;
/**
* Show a folder open dialog
*
* @param object options
* Additional options as specified in showPicker
*
* @return promise
* A promise that is resolved when the folder has been opened
*/
function showOpenFolder(options) {
return showPicker(merge({
title: getLocalizedString("projecteditor.openFolderLabel"),
mode: Ci.nsIFilePicker.modeGetFolder
}, options));
}
exports.showOpenFolder = showOpenFolder;
|