diff options
author | Matt A. Tobin <mattatobin@localhost.localdomain> | 2018-02-02 04:16:08 -0500 |
---|---|---|
committer | Matt A. Tobin <mattatobin@localhost.localdomain> | 2018-02-02 04:16:08 -0500 |
commit | 5f8de423f190bbb79a62f804151bc24824fa32d8 (patch) | |
tree | 10027f336435511475e392454359edea8e25895d /devtools/client/projecteditor/lib/helpers/file-picker.js | |
parent | 49ee0794b5d912db1f95dce6eb52d781dc210db5 (diff) | |
download | UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.gz UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.lz UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.xz UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.zip |
Add m-esr52 at 52.6.0
Diffstat (limited to 'devtools/client/projecteditor/lib/helpers/file-picker.js')
-rw-r--r-- | devtools/client/projecteditor/lib/helpers/file-picker.js | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/devtools/client/projecteditor/lib/helpers/file-picker.js b/devtools/client/projecteditor/lib/helpers/file-picker.js new file mode 100644 index 000000000..1dab0f001 --- /dev/null +++ b/devtools/client/projecteditor/lib/helpers/file-picker.js @@ -0,0 +1,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; |