Spaces:
Sleeping
Sleeping
File size: 4,330 Bytes
b593f0b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | // @flow strict
import Point from '@mapbox/point-geometry';
import window from './window';
import assert from 'assert';
const DOM = {};
export default DOM;
DOM.create = function (tagName: string, className: ?string, container?: HTMLElement) {
const el = window.document.createElement(tagName);
if (className !== undefined) el.className = className;
if (container) container.appendChild(el);
return el;
};
DOM.createNS = function (namespaceURI: string, tagName: string) {
const el = window.document.createElementNS(namespaceURI, tagName);
return el;
};
const docStyle = window.document && window.document.documentElement.style;
function testProp(props) {
if (!docStyle) return props[0];
for (let i = 0; i < props.length; i++) {
if (props[i] in docStyle) {
return props[i];
}
}
return props[0];
}
const selectProp = testProp(['userSelect', 'MozUserSelect', 'WebkitUserSelect', 'msUserSelect']);
let userSelect;
DOM.disableDrag = function () {
if (docStyle && selectProp) {
userSelect = docStyle[selectProp];
docStyle[selectProp] = 'none';
}
};
DOM.enableDrag = function () {
if (docStyle && selectProp) {
docStyle[selectProp] = userSelect;
}
};
const transformProp = testProp(['transform', 'WebkitTransform']);
DOM.setTransform = function(el: HTMLElement, value: string) {
// https://github.com/facebook/flow/issues/7754
// $FlowFixMe
el.style[transformProp] = value;
};
// Feature detection for {passive: false} support in add/removeEventListener.
let passiveSupported = false;
try {
// https://github.com/facebook/flow/issues/285
// $FlowFixMe
const options = Object.defineProperty({}, "passive", {
get() { // eslint-disable-line
passiveSupported = true;
}
});
window.addEventListener("test", options, options);
window.removeEventListener("test", options, options);
} catch (err) {
passiveSupported = false;
}
DOM.addEventListener = function(target: *, type: *, callback: *, options: {passive?: boolean, capture?: boolean} = {}) {
if ('passive' in options && passiveSupported) {
target.addEventListener(type, callback, options);
} else {
target.addEventListener(type, callback, options.capture);
}
};
DOM.removeEventListener = function(target: *, type: *, callback: *, options: {passive?: boolean, capture?: boolean} = {}) {
if ('passive' in options && passiveSupported) {
target.removeEventListener(type, callback, options);
} else {
target.removeEventListener(type, callback, options.capture);
}
};
// Suppress the next click, but only if it's immediate.
const suppressClick: MouseEventListener = function (e) {
e.preventDefault();
e.stopPropagation();
window.removeEventListener('click', suppressClick, true);
};
DOM.suppressClick = function() {
window.addEventListener('click', suppressClick, true);
window.setTimeout(() => {
window.removeEventListener('click', suppressClick, true);
}, 0);
};
DOM.mousePos = function (el: HTMLElement, e: MouseEvent | window.TouchEvent | Touch) {
const rect = el.getBoundingClientRect();
return new Point(
e.clientX - rect.left - el.clientLeft,
e.clientY - rect.top - el.clientTop
);
};
DOM.touchPos = function (el: HTMLElement, touches: TouchList) {
const rect = el.getBoundingClientRect(),
points = [];
for (let i = 0; i < touches.length; i++) {
points.push(new Point(
touches[i].clientX - rect.left - el.clientLeft,
touches[i].clientY - rect.top - el.clientTop
));
}
return points;
};
DOM.mouseButton = function (e: MouseEvent) {
assert(e.type === 'mousedown' || e.type === 'mouseup');
if (typeof window.InstallTrigger !== 'undefined' && e.button === 2 && e.ctrlKey &&
window.navigator.platform.toUpperCase().indexOf('MAC') >= 0) {
// Fix for https://github.com/mapbox/mapbox-gl-js/issues/3131:
// Firefox (detected by InstallTrigger) on Mac determines e.button = 2 when
// using Control + left click
return 0;
}
return e.button;
};
DOM.remove = function(node: HTMLElement) {
if (node.parentNode) {
node.parentNode.removeChild(node);
}
};
|