2014-06-17 10:23:40 +00: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/. */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
// A module for working with panels with iframes shared across windows.
|
|
|
|
|
|
|
|
this.EXPORTED_SYMBOLS = ["PanelFrame"];
|
|
|
|
|
|
|
|
const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "CustomizableUI", "resource:///modules/CustomizableUI.jsm");
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "DynamicResizeWatcher", "resource:///modules/Social.jsm");
|
|
|
|
|
|
|
|
// The minimum sizes for the auto-resize panel code.
|
|
|
|
const PANEL_MIN_HEIGHT = 100;
|
|
|
|
const PANEL_MIN_WIDTH = 330;
|
|
|
|
|
2015-09-15 18:19:45 +00:00
|
|
|
var PanelFrameInternal = {
|
2014-06-17 10:23:40 +00:00
|
|
|
/**
|
|
|
|
* Helper function to get and hold a single instance of a DynamicResizeWatcher.
|
|
|
|
*/
|
|
|
|
get _dynamicResizer() {
|
|
|
|
delete this._dynamicResizer;
|
|
|
|
this._dynamicResizer = new DynamicResizeWatcher();
|
|
|
|
return this._dynamicResizer;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Status panels are one-per button per-process, we swap the docshells between
|
|
|
|
* windows when necessary.
|
|
|
|
*
|
|
|
|
* @param {DOMWindow} aWindow The window in which to show the popup.
|
|
|
|
* @param {PanelUI} aPanelUI The panel UI object that represents the application menu.
|
|
|
|
* @param {DOMElement} aButton The button element that is pressed to show the popup.
|
|
|
|
* @param {String} aType The type of panel this is, e.g. "social" or "loop".
|
|
|
|
* @param {String} aOrigin Optional, the origin to use for the iframe.
|
|
|
|
* @param {String} aSrc The url to load into the iframe.
|
|
|
|
* @param {String} aSize The initial size of the panel (width and height are the same
|
|
|
|
* if specified).
|
|
|
|
*/
|
|
|
|
_attachNotificatonPanel: function(aWindow, aParent, aButton, aType, aOrigin, aSrc, aSize) {
|
|
|
|
aParent.hidden = false;
|
2015-01-14 20:15:38 +00:00
|
|
|
let notificationFrameId = aOrigin ? aType + "-status-" + aOrigin : aType + "-panel-iframe";
|
2014-11-06 00:21:01 +00:00
|
|
|
let doc = aWindow.document;
|
|
|
|
let frame = doc.getElementById(notificationFrameId);
|
2014-06-17 10:23:40 +00:00
|
|
|
|
2014-10-20 15:49:20 +00:00
|
|
|
// If the button was customized to a new location, destroy the
|
2014-06-17 10:23:40 +00:00
|
|
|
// iframe and start fresh.
|
|
|
|
if (frame && frame.parentNode != aParent) {
|
|
|
|
frame.parentNode.removeChild(frame);
|
|
|
|
frame = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!frame) {
|
|
|
|
let {width, height} = aSize ? aSize : {width: PANEL_MIN_WIDTH, height: PANEL_MIN_HEIGHT};
|
2014-11-06 00:21:01 +00:00
|
|
|
frame = doc.createElement("browser");
|
|
|
|
let attrs = {
|
|
|
|
"type": "content",
|
|
|
|
"mozbrowser": "true",
|
|
|
|
// All frames use social-panel-frame as the class.
|
|
|
|
"class": "social-panel-frame",
|
|
|
|
"id": notificationFrameId,
|
|
|
|
"tooltip": "aHTMLTooltip",
|
|
|
|
"context": "contentAreaContextMenu",
|
|
|
|
"flex": "1",
|
|
|
|
|
|
|
|
// work around bug 793057 - by making the panel roughly the final size
|
|
|
|
// we are more likely to have the anchor in the correct position.
|
|
|
|
"style": "width: " + width + "px; height: " + height + "px;",
|
|
|
|
"dynamicresizer": !aSize,
|
|
|
|
|
|
|
|
"origin": aOrigin,
|
|
|
|
"src": aSrc
|
|
|
|
};
|
2015-05-22 17:33:18 +00:00
|
|
|
if (aType == "social") {
|
|
|
|
attrs["message"] = "true";
|
|
|
|
attrs["messagemanagergroup"] = aType;
|
|
|
|
}
|
2014-11-06 00:21:01 +00:00
|
|
|
for (let [k, v] of Iterator(attrs)) {
|
|
|
|
frame.setAttribute(k, v);
|
|
|
|
}
|
|
|
|
aParent.appendChild(frame);
|
2014-06-17 10:23:40 +00:00
|
|
|
} else {
|
|
|
|
frame.setAttribute("origin", aOrigin);
|
2014-11-06 00:21:01 +00:00
|
|
|
frame.setAttribute("src", aSrc);
|
2014-06-17 10:23:40 +00:00
|
|
|
}
|
|
|
|
aButton.setAttribute("notificationFrameId", notificationFrameId);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The exported PanelFrame object
|
|
|
|
*/
|
2015-09-15 18:19:45 +00:00
|
|
|
var PanelFrame = {
|
2014-06-17 10:23:40 +00:00
|
|
|
/**
|
|
|
|
* Shows a popup in a pop-up panel, or in a sliding panel view in the application menu.
|
|
|
|
* It will move the iframe to different DOM locations depending on where it needs to be
|
|
|
|
* shown, enabling one iframe to be used for the entire session.
|
|
|
|
*
|
|
|
|
* @param {DOMWindow} aWindow The window in which to show the popup.
|
|
|
|
* @param {PanelUI} aPanelUI The panel UI object that represents the application menu.
|
|
|
|
* @param {DOMElement} aToolbarButton The button element that is pressed to show the popup.
|
|
|
|
* @param {String} aType The type of panel this is, e.g. "social" or "loop".
|
|
|
|
* @param {String} aOrigin Optional, the origin to use for the iframe.
|
|
|
|
* @param {String} aSrc The url to load into the iframe.
|
|
|
|
* @param {String} aSize The initial size of the panel (width and height are the same
|
|
|
|
* if specified).
|
|
|
|
* @param {Function} aCallback Optional, callback to be called with the iframe when it is
|
|
|
|
* set up.
|
|
|
|
*/
|
2014-08-21 14:44:17 +00:00
|
|
|
showPopup: function(aWindow, aToolbarButton, aType, aOrigin, aSrc, aSize, aCallback) {
|
2014-06-27 15:58:13 +00:00
|
|
|
// if we're overflowed, our anchor needs to be the overflow button
|
2014-06-17 10:23:40 +00:00
|
|
|
let widgetGroup = CustomizableUI.getWidget(aToolbarButton.getAttribute("id"));
|
|
|
|
let widget = widgetGroup.forWindow(aWindow);
|
2014-08-21 14:44:17 +00:00
|
|
|
// if we're a slice in the hamburger, our anchor will be the menu button,
|
|
|
|
// this panel will replace the menu panel when the button is clicked on
|
2014-06-27 15:58:13 +00:00
|
|
|
let anchorBtn = widget.anchor;
|
|
|
|
|
2014-08-21 14:44:17 +00:00
|
|
|
let panel = aWindow.document.getElementById(aType + "-notification-panel");
|
|
|
|
PanelFrameInternal._attachNotificatonPanel(aWindow, panel, aToolbarButton, aType, aOrigin, aSrc, aSize);
|
|
|
|
|
2014-06-17 10:23:40 +00:00
|
|
|
let notificationFrameId = aToolbarButton.getAttribute("notificationFrameId");
|
|
|
|
let notificationFrame = aWindow.document.getElementById(notificationFrameId);
|
|
|
|
|
|
|
|
|
|
|
|
// Clear dimensions on all browsers so the panel size will
|
|
|
|
// only use the selected browser.
|
|
|
|
let frameIter = panel.firstElementChild;
|
|
|
|
while (frameIter) {
|
|
|
|
frameIter.collapsed = (frameIter != notificationFrame);
|
|
|
|
frameIter = frameIter.nextElementSibling;
|
|
|
|
}
|
|
|
|
|
|
|
|
function dispatchPanelEvent(name) {
|
|
|
|
let evt = notificationFrame.contentDocument.createEvent("CustomEvent");
|
|
|
|
evt.initCustomEvent(name, true, true, {});
|
|
|
|
notificationFrame.contentDocument.documentElement.dispatchEvent(evt);
|
|
|
|
}
|
|
|
|
|
|
|
|
// we only use a dynamic resizer when we're located the toolbar.
|
|
|
|
let dynamicResizer;
|
2014-08-21 14:44:17 +00:00
|
|
|
if (notificationFrame.getAttribute("dynamicresizer") == "true") {
|
2014-06-17 10:23:40 +00:00
|
|
|
dynamicResizer = PanelFrameInternal._dynamicResizer;
|
|
|
|
}
|
2014-08-21 14:44:17 +00:00
|
|
|
panel.addEventListener("popuphidden", function onpopuphiding() {
|
|
|
|
panel.removeEventListener("popuphidden", onpopuphiding);
|
|
|
|
anchorBtn.removeAttribute("open");
|
2014-06-17 10:23:40 +00:00
|
|
|
if (dynamicResizer)
|
|
|
|
dynamicResizer.stop();
|
|
|
|
notificationFrame.docShell.isActive = false;
|
|
|
|
dispatchPanelEvent(aType + "FrameHide");
|
|
|
|
});
|
|
|
|
|
2014-08-21 14:44:17 +00:00
|
|
|
panel.addEventListener("popupshown", function onpopupshown() {
|
|
|
|
panel.removeEventListener("popupshown", onpopupshown);
|
2014-06-17 10:23:40 +00:00
|
|
|
let initFrameShow = () => {
|
|
|
|
notificationFrame.docShell.isActive = true;
|
|
|
|
notificationFrame.docShell.isAppTab = true;
|
|
|
|
if (dynamicResizer)
|
|
|
|
dynamicResizer.start(panel, notificationFrame);
|
|
|
|
dispatchPanelEvent(aType + "FrameShow");
|
|
|
|
};
|
2014-10-20 15:49:20 +00:00
|
|
|
// This attribute is needed on both the button and the
|
|
|
|
// containing toolbaritem since the buttons on OS X have
|
|
|
|
// moz-appearance:none, while their container gets
|
|
|
|
// moz-appearance:toolbarbutton due to the way that toolbar buttons
|
|
|
|
// get combined on OS X.
|
2014-08-21 14:44:17 +00:00
|
|
|
anchorBtn.setAttribute("open", "true");
|
2014-06-17 10:23:40 +00:00
|
|
|
if (notificationFrame.contentDocument &&
|
2014-06-30 20:10:19 +00:00
|
|
|
notificationFrame.contentDocument.readyState == "complete") {
|
2014-06-17 10:23:40 +00:00
|
|
|
initFrameShow();
|
|
|
|
} else {
|
|
|
|
// first time load, wait for load and dispatch after load
|
|
|
|
notificationFrame.addEventListener("load", function panelBrowserOnload(e) {
|
|
|
|
notificationFrame.removeEventListener("load", panelBrowserOnload, true);
|
|
|
|
initFrameShow();
|
|
|
|
}, true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-07-15 20:13:24 +00:00
|
|
|
let anchor = aWindow.document.getAnonymousElementByAttribute(anchorBtn, "class", "toolbarbutton-icon");
|
2014-08-21 14:44:17 +00:00
|
|
|
// Bug 849216 - open the popup asynchronously so we avoid the auto-rollup
|
|
|
|
// handling from preventing it being opened in some cases.
|
|
|
|
Services.tm.mainThread.dispatch(function() {
|
|
|
|
panel.openPopup(anchor, "bottomcenter topright", 0, 0, false, false);
|
|
|
|
}, Ci.nsIThread.DISPATCH_NORMAL);
|
2014-06-17 10:23:40 +00:00
|
|
|
|
|
|
|
if (aCallback)
|
|
|
|
aCallback(notificationFrame);
|
|
|
|
}
|
|
|
|
};
|