mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-11-25 05:41:12 +00:00
c90cc004dc
When a snackbar with a button callback is dismissed, this translates to a rejected promise from sendRequestForResult(). We need to catch this in order to avoid a spurious 'JavaScript Error: "uncaught exception: undefined"' message appearing in the console and possibly causing confusion. MozReview-Commit-ID: 7hsAOAMTeDP --HG-- extra : rebase_source : 6c5eb28d2e0dcf39a35b310d1e1c45cfc47f272b
81 lines
2.0 KiB
JavaScript
81 lines
2.0 KiB
JavaScript
/* 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/. */
|
|
|
|
"use strict";
|
|
|
|
const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
|
|
|
|
this.EXPORTED_SYMBOLS = ["Snackbars"];
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "EventDispatcher", "resource://gre/modules/Messaging.jsm");
|
|
|
|
const LENGTH_INDEFINITE = -2;
|
|
const LENGTH_LONG = 0;
|
|
const LENGTH_SHORT = -1;
|
|
|
|
var Snackbars = {
|
|
LENGTH_INDEFINITE: LENGTH_INDEFINITE,
|
|
LENGTH_LONG: LENGTH_LONG,
|
|
LENGTH_SHORT: LENGTH_SHORT,
|
|
|
|
show: function(aMessage, aDuration, aOptions) {
|
|
|
|
// Takes care of the deprecated toast calls
|
|
if (typeof aDuration === "string") {
|
|
[aDuration, aOptions] = migrateToastIfNeeded(aDuration, aOptions);
|
|
}
|
|
|
|
let msg = {
|
|
type: 'Snackbar:Show',
|
|
message: aMessage,
|
|
duration: aDuration,
|
|
};
|
|
|
|
if (aOptions && aOptions.backgroundColor) {
|
|
msg.backgroundColor = aOptions.backgroundColor;
|
|
}
|
|
|
|
if (aOptions && aOptions.action) {
|
|
msg.action = {};
|
|
|
|
if (aOptions.action.label) {
|
|
msg.action.label = aOptions.action.label;
|
|
}
|
|
|
|
EventDispatcher.instance.sendRequestForResult(msg)
|
|
.then(result => aOptions.action.callback())
|
|
.catch(result => {
|
|
if (result === null) {
|
|
/* The snackbar was dismissed without executing the callback, nothing to do here. */
|
|
} else {
|
|
Cu.reportError(result);
|
|
}
|
|
});
|
|
} else {
|
|
EventDispatcher.instance.sendRequest(msg);
|
|
}
|
|
}
|
|
};
|
|
|
|
function migrateToastIfNeeded(aDuration, aOptions) {
|
|
let duration;
|
|
if (aDuration === "long") {
|
|
duration = LENGTH_LONG;
|
|
}
|
|
else {
|
|
duration = LENGTH_SHORT;
|
|
}
|
|
|
|
let options = {};
|
|
if (aOptions && aOptions.button) {
|
|
options.action = {
|
|
label: aOptions.button.label,
|
|
callback: () => aOptions.button.callback(),
|
|
};
|
|
}
|
|
return [duration, options];
|
|
}
|