Fix gallery navigation from interaction demo (#825)
[dygraphs.git] / src / iframe-tarp.js
CommitLineData
2cfded32
DV
1/**
2 * To create a "drag" interaction, you typically register a mousedown event
3 * handler on the element where the drag begins. In that handler, you register a
4 * mouseup handler on the window to determine when the mouse is released,
5 * wherever that release happens. This works well, except when the user releases
6 * the mouse over an off-domain iframe. In that case, the mouseup event is
7 * handled by the iframe and never bubbles up to the window handler.
8 *
9 * To deal with this issue, we cover iframes with high z-index divs to make sure
10 * they don't capture mouseup.
11 *
12 * Usage:
13 * element.addEventListener('mousedown', function() {
14 * var tarper = new IFrameTarp();
15 * tarper.cover();
16 * var mouseUpHandler = function() {
17 * ...
18 * window.removeEventListener(mouseUpHandler);
19 * tarper.uncover();
20 * };
21 * window.addEventListener('mouseup', mouseUpHandler);
22 * };
23 *
24 * @constructor
25 */
0d9ed3ab
ÜS
26import * as utils from './dygraph-utils';
27
2cfded32
DV
28function IFrameTarp() {
29 /** @type {Array.<!HTMLDivElement>} */
30 this.tarps = [];
31};
32
33/**
34 * Find all the iframes in the document and cover them with high z-index
35 * transparent divs.
36 */
37IFrameTarp.prototype.cover = function() {
38 var iframes = document.getElementsByTagName("iframe");
39 for (var i = 0; i < iframes.length; i++) {
40 var iframe = iframes[i];
41 var pos = utils.findPos(iframe),
42 x = pos.x,
43 y = pos.y,
44 width = iframe.offsetWidth,
45 height = iframe.offsetHeight;
46
47 var div = document.createElement("div");
48 div.style.position = "absolute";
49 div.style.left = x + 'px';
50 div.style.top = y + 'px';
51 div.style.width = width + 'px';
52 div.style.height = height + 'px';
53 div.style.zIndex = 999;
54 document.body.appendChild(div);
55 this.tarps.push(div);
56 }
57};
58
59/**
60 * Remove all the iframe covers. You should call this in a mouseup handler.
61 */
62IFrameTarp.prototype.uncover = function() {
63 for (var i = 0; i < this.tarps.length; i++) {
64 this.tarps[i].parentNode.removeChild(this.tarps[i]);
65 }
66 this.tarps = [];
67};
68
69export default IFrameTarp;