mapbox-gl
Version:
A WebGL interactive maps library
83 lines (72 loc) • 2.25 kB
JavaScript
;
const Point = require('point-geometry');
const window = require('./window');
exports.create = function (tagName, className, container) {
const el = window.document.createElement(tagName);
if (className) el.className = className;
if (container) container.appendChild(el);
return el;
};
const docStyle = window.document.documentElement.style;
function testProp(props) {
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;
exports.disableDrag = function () {
if (selectProp) {
userSelect = docStyle[selectProp];
docStyle[selectProp] = 'none';
}
};
exports.enableDrag = function () {
if (selectProp) {
docStyle[selectProp] = userSelect;
}
};
const transformProp = testProp(['transform', 'WebkitTransform']);
exports.setTransform = function(el, value) {
el.style[transformProp] = value;
};
// Suppress the next click, but only if it's immediate.
function suppressClick(e) {
e.preventDefault();
e.stopPropagation();
window.removeEventListener('click', suppressClick, true);
}
exports.suppressClick = function() {
window.addEventListener('click', suppressClick, true);
window.setTimeout(() => {
window.removeEventListener('click', suppressClick, true);
}, 0);
};
exports.mousePos = function (el, e) {
const rect = el.getBoundingClientRect();
e = e.touches ? e.touches[0] : e;
return new Point(
e.clientX - rect.left - el.clientLeft,
e.clientY - rect.top - el.clientTop
);
};
exports.touchPos = function (el, e) {
const rect = el.getBoundingClientRect(),
points = [];
const touches = (e.type === 'touchend') ? e.changedTouches : e.touches;
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;
};
exports.remove = function(node) {
if (node.parentNode) {
node.parentNode.removeChild(node);
}
};