wotnot-video-react
Version:
Video-React is a web video player built from the ground up for an HTML5 world using React library.
107 lines (89 loc) • 2.76 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.findElPosition = findElPosition;
exports.getPointerPosition = getPointerPosition;
exports.blurNode = blurNode;
exports.focusNode = focusNode;
exports.hasClass = hasClass;
/**
* Offset Left
* getBoundingClientRect technique from
* John Resig http://ejohn.org/blog/getboundingclientrect-is-awesome/
*
* @function findElPosition
* @param {ReactNodeRef} el React Node ref from which to get offset
* @return {Object}
*/
function findElPosition(el) {
var box;
if (el.getBoundingClientRect && el.parentNode) {
box = el.getBoundingClientRect();
}
if (!box) {
return {
left: 0,
top: 0
};
}
var _document = document,
body = _document.body,
docEl = _document.documentElement;
var clientLeft = docEl.clientLeft || body.clientLeft || 0;
var scrollLeft = window.pageXOffset || body.scrollLeft;
var left = box.left + scrollLeft - clientLeft;
var clientTop = docEl.clientTop || body.clientTop || 0;
var scrollTop = window.pageYOffset || body.scrollTop;
var top = box.top + scrollTop - clientTop; // Android sometimes returns slightly off decimal values, so need to round
return {
left: Math.round(left),
top: Math.round(top)
};
}
/**
* Get pointer position in a React Node ref
* Returns an object with x and y coordinates.
* The base on the coordinates are the bottom left of the element.
*
* @function getPointerPosition
* @param {ReactNodeRef} el React Node ref on which to get the pointer position on
* @param {Event} event Event object
* @return {Object} This object will have x and y coordinates corresponding to the mouse position
*/
function getPointerPosition(el, event) {
var position = {};
var box = findElPosition(el);
var boxW = el.offsetWidth;
var boxH = el.offsetHeight;
var boxY = box.top;
var boxX = box.left;
var evtPageY = event.pageY;
var evtPageX = event.pageX;
if (event.changedTouches) {
evtPageX = event.changedTouches[0].pageX;
evtPageY = event.changedTouches[0].pageY;
}
position.y = Math.max(0, Math.min(1, (boxY - evtPageY + boxH) / boxH));
position.x = Math.max(0, Math.min(1, (evtPageX - boxX) / boxW));
return position;
} // blur an element
function blurNode(reactNode) {
if (reactNode && reactNode.blur) {
reactNode.blur();
}
} // focus an element
function focusNode(reactNode) {
if (reactNode && reactNode.focus) {
reactNode.focus();
}
} // check if an element has a class name
function hasClass(elm, cls) {
var classes = elm.className.split(' ');
for (var i = 0; i < classes.length; i++) {
if (classes[i].toLowerCase() === cls.toLowerCase()) {
return true;
}
}
return false;
}