dcos-dygraphs
Version:
dygraphs is a fast, flexible open source JavaScript charting library.
70 lines (64 loc) • 2.08 kB
JavaScript
/**
* To create a "drag" interaction, you typically register a mousedown event
* handler on the element where the drag begins. In that handler, you register a
* mouseup handler on the window to determine when the mouse is released,
* wherever that release happens. This works well, except when the user releases
* the mouse over an off-domain iframe. In that case, the mouseup event is
* handled by the iframe and never bubbles up to the window handler.
*
* To deal with this issue, we cover iframes with high z-index divs to make sure
* they don't capture mouseup.
*
* Usage:
* element.addEventListener('mousedown', function() {
* var tarper = new IFrameTarp();
* tarper.cover();
* var mouseUpHandler = function() {
* ...
* window.removeEventListener(mouseUpHandler);
* tarper.uncover();
* };
* window.addEventListener('mouseup', mouseUpHandler);
* };
*
* @constructor
*/
import * as utils from './dygraph-utils';
function IFrameTarp() {
/** @type {Array.<!HTMLDivElement>} */
this.tarps = [];
};
/**
* Find all the iframes in the document and cover them with high z-index
* transparent divs.
*/
IFrameTarp.prototype.cover = function() {
var iframes = document.getElementsByTagName("iframe");
for (var i = 0; i < iframes.length; i++) {
var iframe = iframes[i];
var pos = utils.findPos(iframe),
x = pos.x,
y = pos.y,
width = iframe.offsetWidth,
height = iframe.offsetHeight;
var div = document.createElement("div");
div.style.position = "absolute";
div.style.left = x + 'px';
div.style.top = y + 'px';
div.style.width = width + 'px';
div.style.height = height + 'px';
div.style.zIndex = 999;
document.body.appendChild(div);
this.tarps.push(div);
}
};
/**
* Remove all the iframe covers. You should call this in a mouseup handler.
*/
IFrameTarp.prototype.uncover = function() {
for (var i = 0; i < this.tarps.length; i++) {
this.tarps[i].parentNode.removeChild(this.tarps[i]);
}
this.tarps = [];
};
export default IFrameTarp;