2014-01-25 07:19:07 -08:00
|
|
|
# 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/.
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyGetter(this, "FxAccountsCommon", function () {
|
|
|
|
return Cu.import("resource://gre/modules/FxAccountsCommon.js", {});
|
|
|
|
});
|
|
|
|
|
|
|
|
let gFxAccounts = {
|
|
|
|
|
2014-01-25 13:02:18 -08:00
|
|
|
_initialized: false,
|
2014-01-10 10:32:11 -08:00
|
|
|
_originalLabel: null,
|
|
|
|
_inCustomizationMode: false,
|
|
|
|
|
|
|
|
get weave() {
|
|
|
|
delete this.weave;
|
|
|
|
return this.weave = Cc["@mozilla.org/weave/service;1"]
|
|
|
|
.getService(Ci.nsISupports)
|
|
|
|
.wrappedJSObject;
|
|
|
|
},
|
2014-01-25 13:02:18 -08:00
|
|
|
|
2014-01-25 07:19:07 -08:00
|
|
|
get topics() {
|
|
|
|
delete this.topics;
|
|
|
|
return this.topics = [
|
2014-01-10 10:32:11 -08:00
|
|
|
FxAccountsCommon.ONLOGIN_NOTIFICATION,
|
|
|
|
FxAccountsCommon.ONVERIFIED_NOTIFICATION,
|
|
|
|
FxAccountsCommon.ONLOGOUT_NOTIFICATION
|
2014-01-25 07:19:07 -08:00
|
|
|
];
|
|
|
|
},
|
|
|
|
|
2014-01-10 10:32:11 -08:00
|
|
|
get button() {
|
|
|
|
delete this.button;
|
|
|
|
return this.button = document.getElementById("PanelUI-fxa-status");
|
|
|
|
},
|
|
|
|
|
2014-01-25 07:19:07 -08:00
|
|
|
init: function () {
|
2014-01-25 13:02:18 -08:00
|
|
|
if (this._initialized) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-25 07:19:07 -08:00
|
|
|
for (let topic of this.topics) {
|
|
|
|
Services.obs.addObserver(this, topic, false);
|
|
|
|
}
|
2014-01-25 13:02:18 -08:00
|
|
|
|
2014-01-10 10:32:11 -08:00
|
|
|
gNavToolbox.addEventListener("customizationstarting", this);
|
|
|
|
gNavToolbox.addEventListener("customizationending", this);
|
|
|
|
|
|
|
|
// Save the button's original label so that
|
|
|
|
// we can restore it if overridden later.
|
|
|
|
this._originalLabel = this.button.getAttribute("label");
|
2014-01-25 13:02:18 -08:00
|
|
|
this._initialized = true;
|
2014-01-10 10:32:11 -08:00
|
|
|
|
|
|
|
this.updateUI();
|
2014-01-25 07:19:07 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
uninit: function () {
|
2014-01-25 13:02:18 -08:00
|
|
|
if (!this._initialized) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-25 07:19:07 -08:00
|
|
|
for (let topic of this.topics) {
|
|
|
|
Services.obs.removeObserver(this, topic);
|
|
|
|
}
|
2014-01-25 13:02:18 -08:00
|
|
|
|
|
|
|
this._initialized = false;
|
2014-01-25 07:19:07 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
observe: function (subject, topic) {
|
2014-01-10 10:32:11 -08:00
|
|
|
if (topic == FxAccountsCommon.ONVERIFIED_NOTIFICATION) {
|
|
|
|
this.showDoorhanger();
|
|
|
|
} else {
|
|
|
|
this.updateUI();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
handleEvent: function (event) {
|
|
|
|
this._inCustomizationMode = event.type == "customizationstarting";
|
|
|
|
this.updateUI();
|
2014-01-25 07:19:07 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
showDoorhanger: function () {
|
|
|
|
let panel = document.getElementById("sync-popup");
|
|
|
|
let anchor = document.getElementById("PanelUI-menu-button");
|
|
|
|
|
|
|
|
let iconAnchor =
|
|
|
|
document.getAnonymousElementByAttribute(anchor, "class",
|
|
|
|
"toolbarbutton-icon");
|
|
|
|
|
|
|
|
panel.hidden = false;
|
|
|
|
panel.openPopup(iconAnchor || anchor, "bottomcenter topright");
|
2014-01-10 10:32:11 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
updateUI: function () {
|
|
|
|
// Bail out if FxA is disabled.
|
|
|
|
if (!this.weave.fxAccountsEnabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FxA is enabled, show the widget.
|
|
|
|
this.button.removeAttribute("hidden");
|
|
|
|
|
|
|
|
// Make sure the button is disabled in customization mode.
|
|
|
|
if (this._inCustomizationMode) {
|
|
|
|
this.button.setAttribute("disabled", "true");
|
|
|
|
} else {
|
|
|
|
this.button.removeAttribute("disabled");
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the user is signed into their Firefox account and we are not
|
|
|
|
// currently in customization mode, show their email address.
|
|
|
|
fxAccounts.getSignedInUser().then(userData => {
|
|
|
|
if (userData && !this._inCustomizationMode) {
|
|
|
|
this.button.setAttribute("signedin", "true");
|
|
|
|
this.button.setAttribute("label", userData.email);
|
|
|
|
this.button.setAttribute("tooltiptext", userData.email);
|
|
|
|
} else {
|
|
|
|
this.button.removeAttribute("signedin");
|
|
|
|
this.button.setAttribute("label", this._originalLabel);
|
|
|
|
this.button.removeAttribute("tooltiptext");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
toggle: function (event) {
|
|
|
|
if (event.originalTarget.hasAttribute("signedin")) {
|
|
|
|
openPreferences("paneSync");
|
|
|
|
} else {
|
|
|
|
switchToTabHavingURI("about:accounts", true);
|
|
|
|
}
|
|
|
|
|
|
|
|
PanelUI.hide();
|
2014-01-25 07:19:07 -08:00
|
|
|
}
|
|
|
|
};
|