jellyfin-web/dashboard-ui/bower_components/emby-webcomponents/scrollhelper.js

106 lines
3.3 KiB
JavaScript
Raw Normal View History

2016-08-07 12:43:52 -07:00
define(['focusManager', 'dom', 'scrollStyles'], function (focusManager, dom) {
2016-02-29 09:23:30 -07:00
2016-08-08 22:10:29 -07:00
function getBoundingClientRect(elem) {
2016-02-29 09:23:30 -07:00
2016-08-08 22:10:29 -07:00
// Support: BlackBerry 5, iOS 3 (original iPhone)
// If we don't have gBCR, just use 0,0 rather than error
if (elem.getBoundingClientRect) {
return elem.getBoundingClientRect();
} else {
return { top: 0, left: 0 };
2016-03-11 20:29:37 -07:00
}
2016-02-29 09:23:30 -07:00
}
function getPosition(scrollContainer, item, horizontal) {
2016-03-11 20:29:37 -07:00
2016-08-09 10:31:31 -07:00
var slideeOffset = getBoundingClientRect(scrollContainer);
var itemOffset = getBoundingClientRect(item);
2016-02-29 09:23:30 -07:00
var offset = horizontal ? itemOffset.left - slideeOffset.left : itemOffset.top - slideeOffset.top;
2016-08-09 10:31:31 -07:00
var size = horizontal ? itemOffset.width : itemOffset.height;
if (!size) {
size = item[horizontal ? 'offsetWidth' : 'offsetHeight'];
}
2016-02-29 09:23:30 -07:00
if (horizontal) {
offset += scrollContainer.scrollLeft;
} else {
offset += scrollContainer.scrollTop;
}
var frameSize = horizontal ? scrollContainer.offsetWidth : scrollContainer.offsetHeight;
return {
start: offset,
center: (offset - (frameSize / 2) + (size / 2)),
end: offset - frameSize + size,
size: size
};
}
function toCenter(container, elem, horizontal) {
var pos = getPosition(container, elem, horizontal);
if (container.scrollTo) {
if (horizontal) {
container.scrollTo(pos.center, 0);
} else {
container.scrollTo(0, pos.center);
}
} else {
if (horizontal) {
container.scrollLeft = Math.round(pos.center);
} else {
container.scrollTop = Math.round(pos.center);
}
}
}
function centerOnFocus(e, scrollSlider, horizontal) {
var focused = focusManager.focusableParent(e.target);
if (focused) {
toCenter(scrollSlider, focused, horizontal);
}
}
function centerOnFocusHorizontal(e) {
centerOnFocus(e, this, true);
}
function centerOnFocusVertical(e) {
centerOnFocus(e, this, false);
}
return {
getPosition: getPosition,
centerFocus: {
on: function (element, horizontal) {
if (horizontal) {
2016-08-07 12:43:52 -07:00
dom.addEventListener(element, 'focus', centerOnFocusHorizontal, {
capture: true,
passive: true
});
2016-02-29 09:23:30 -07:00
} else {
2016-08-07 12:43:52 -07:00
dom.addEventListener(element, 'focus', centerOnFocusVertical, {
capture: true,
passive: true
});
2016-02-29 09:23:30 -07:00
}
},
off: function (element, horizontal) {
if (horizontal) {
2016-08-07 12:43:52 -07:00
dom.removeEventListener(element, 'focus', centerOnFocusHorizontal, {
capture: true,
passive: true
});
2016-02-29 09:23:30 -07:00
} else {
2016-08-07 12:43:52 -07:00
dom.removeEventListener(element, 'focus', centerOnFocusVertical, {
capture: true,
passive: true
});
2016-02-29 09:23:30 -07:00
}
}
},
toCenter: toCenter
};
});