mirror of
https://github.com/jellyfin/jellyfin-web
synced 2025-03-30 19:56:21 +00:00
add passive event listener to headroom
This commit is contained in:
parent
85731c7453
commit
da25a9cc2a
1 changed files with 401 additions and 367 deletions
|
@ -4,16 +4,16 @@
|
|||
* License: MIT
|
||||
*/
|
||||
|
||||
(function(window, document) {
|
||||
(function (window, document) {
|
||||
|
||||
'use strict';
|
||||
|
||||
/* exported features */
|
||||
|
||||
var features = {
|
||||
bind : !!(function(){}.bind),
|
||||
classList : 'classList' in document.documentElement,
|
||||
rAF : !!(window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame)
|
||||
bind: !!(function () { }.bind),
|
||||
classList: 'classList' in document.documentElement,
|
||||
rAF: !!(window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame)
|
||||
};
|
||||
window.requestAnimationFrame = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame;
|
||||
|
||||
|
@ -22,18 +22,18 @@
|
|||
* @see http://www.html5rocks.com/en/tutorials/speed/animations/
|
||||
* @param {Function} callback The callback to handle whichever event
|
||||
*/
|
||||
function Debouncer (callback) {
|
||||
function Debouncer(callback) {
|
||||
this.callback = callback;
|
||||
this.ticking = false;
|
||||
}
|
||||
Debouncer.prototype = {
|
||||
constructor : Debouncer,
|
||||
constructor: Debouncer,
|
||||
|
||||
/**
|
||||
* dispatches the event to the supplied callback
|
||||
* @private
|
||||
*/
|
||||
update : function() {
|
||||
update: function () {
|
||||
this.callback && this.callback();
|
||||
this.ticking = false;
|
||||
},
|
||||
|
@ -42,8 +42,8 @@
|
|||
* ensures events don't get stacked
|
||||
* @private
|
||||
*/
|
||||
requestTick : function() {
|
||||
if(!this.ticking) {
|
||||
requestTick: function () {
|
||||
if (!this.ticking) {
|
||||
requestAnimationFrame(this.rafCallback || (this.rafCallback = this.update.bind(this)));
|
||||
this.ticking = true;
|
||||
}
|
||||
|
@ -52,7 +52,7 @@
|
|||
/**
|
||||
* Attach this as the event listeners
|
||||
*/
|
||||
handleEvent : function() {
|
||||
handleEvent: function () {
|
||||
this.requestTick();
|
||||
}
|
||||
};
|
||||
|
@ -68,8 +68,8 @@
|
|||
/**
|
||||
* Helper function for extending objects
|
||||
*/
|
||||
function extend (object /*, objectN ... */) {
|
||||
if(arguments.length <= 0) {
|
||||
function extend(object /*, objectN ... */) {
|
||||
if (arguments.length <= 0) {
|
||||
throw new Error('Missing arguments in extend function');
|
||||
}
|
||||
|
||||
|
@ -82,7 +82,7 @@
|
|||
|
||||
for (key in replacement) {
|
||||
// Recurse into object except if the object is a DOM element
|
||||
if(typeof result[key] === 'object' && ! isDOMElement(result[key])) {
|
||||
if (typeof result[key] === 'object' && !isDOMElement(result[key])) {
|
||||
result[key] = extend(result[key], replacement[key]);
|
||||
}
|
||||
else {
|
||||
|
@ -97,8 +97,34 @@
|
|||
/**
|
||||
* Helper function for normalizing tolerance option to object format
|
||||
*/
|
||||
function normalizeTolerance (t) {
|
||||
return t === Object(t) ? t : { down : t, up : t };
|
||||
function normalizeTolerance(t) {
|
||||
return t === Object(t) ? t : { down: t, up: t };
|
||||
}
|
||||
|
||||
var supportsCaptureOption = false;
|
||||
try {
|
||||
var opts = Object.defineProperty({}, 'capture', {
|
||||
get: function () {
|
||||
supportsCaptureOption = true;
|
||||
}
|
||||
});
|
||||
window.addEventListener("test", null, opts);
|
||||
} catch (e) { }
|
||||
|
||||
function addEventListenerWithOptions(target, type, handler, options) {
|
||||
var optionsOrCapture = options;
|
||||
if (!supportsCaptureOption) {
|
||||
optionsOrCapture = options.capture;
|
||||
}
|
||||
target.addEventListener(type, handler, optionsOrCapture);
|
||||
}
|
||||
|
||||
function removeEventListenerWithOptions(target, type, handler, options) {
|
||||
var optionsOrCapture = options;
|
||||
if (!supportsCaptureOption) {
|
||||
optionsOrCapture = options.capture;
|
||||
}
|
||||
target.removeEventListener(type, handler, optionsOrCapture);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -109,7 +135,7 @@
|
|||
* @param {DOMElement} elem the header element
|
||||
* @param {Object} options options for the widget
|
||||
*/
|
||||
function Headroom (elem, options) {
|
||||
function Headroom(elem, options) {
|
||||
options = extend(options, Headroom.options);
|
||||
|
||||
this.lastKnownScrollY = 0;
|
||||
|
@ -126,21 +152,19 @@
|
|||
this.onNotTop = options.onNotTop;
|
||||
}
|
||||
Headroom.prototype = {
|
||||
constructor : Headroom,
|
||||
constructor: Headroom,
|
||||
|
||||
/**
|
||||
* Initialises the widget
|
||||
*/
|
||||
init : function() {
|
||||
if(!Headroom.cutsTheMustard) {
|
||||
init: function () {
|
||||
if (!Headroom.cutsTheMustard) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.elem.classList.add(this.classes.initial);
|
||||
|
||||
// defer event registration to handle browser
|
||||
// potentially restoring previous scroll position
|
||||
setTimeout(this.attachEvent.bind(this), 100);
|
||||
this.attachEvent();
|
||||
|
||||
return this;
|
||||
},
|
||||
|
@ -148,23 +172,29 @@
|
|||
/**
|
||||
* Unattaches events and removes any classes that were added
|
||||
*/
|
||||
destroy : function() {
|
||||
destroy: function () {
|
||||
var classes = this.classes;
|
||||
|
||||
this.initialised = false;
|
||||
this.elem.classList.remove(classes.unpinned, classes.pinned, classes.top, classes.initial);
|
||||
this.scroller.removeEventListener('scroll', this.debouncer, false);
|
||||
removeEventListenerWithOptions(this.scroller, 'scroll', this.debouncer, {
|
||||
capture: false,
|
||||
passive: true
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Attaches the scroll event
|
||||
* @private
|
||||
*/
|
||||
attachEvent : function() {
|
||||
if(!this.initialised){
|
||||
attachEvent: function () {
|
||||
if (!this.initialised) {
|
||||
this.lastKnownScrollY = this.getScrollY();
|
||||
this.initialised = true;
|
||||
this.scroller.addEventListener('scroll', this.debouncer, false);
|
||||
addEventListenerWithOptions(this.scroller, 'scroll', this.debouncer, {
|
||||
capture: false,
|
||||
passive: true
|
||||
});
|
||||
|
||||
this.debouncer.handleEvent();
|
||||
}
|
||||
|
@ -173,11 +203,11 @@
|
|||
/**
|
||||
* Unpins the header if it's currently pinned
|
||||
*/
|
||||
unpin : function() {
|
||||
unpin: function () {
|
||||
var classList = this.elem.classList,
|
||||
classes = this.classes;
|
||||
|
||||
if(classList.contains(classes.pinned) || !classList.contains(classes.unpinned)) {
|
||||
if (classList.contains(classes.pinned) || !classList.contains(classes.unpinned)) {
|
||||
classList.add(classes.unpinned);
|
||||
classList.remove(classes.pinned);
|
||||
this.onUnpin && this.onUnpin.call(this);
|
||||
|
@ -187,11 +217,11 @@
|
|||
/**
|
||||
* Pins the header if it's currently unpinned
|
||||
*/
|
||||
pin : function() {
|
||||
pin: function () {
|
||||
var classList = this.elem.classList,
|
||||
classes = this.classes;
|
||||
|
||||
if(classList.contains(classes.unpinned)) {
|
||||
if (classList.contains(classes.unpinned)) {
|
||||
classList.remove(classes.unpinned);
|
||||
classList.add(classes.pinned);
|
||||
this.onPin && this.onPin.call(this);
|
||||
|
@ -201,11 +231,11 @@
|
|||
/**
|
||||
* Handles the top states
|
||||
*/
|
||||
top : function() {
|
||||
top: function () {
|
||||
var classList = this.elem.classList,
|
||||
classes = this.classes;
|
||||
|
||||
if(!classList.contains(classes.top)) {
|
||||
if (!classList.contains(classes.top)) {
|
||||
classList.add(classes.top);
|
||||
classList.remove(classes.notTop);
|
||||
this.onTop && this.onTop.call(this);
|
||||
|
@ -215,11 +245,11 @@
|
|||
/**
|
||||
* Handles the not top state
|
||||
*/
|
||||
notTop : function() {
|
||||
notTop: function () {
|
||||
var classList = this.elem.classList,
|
||||
classes = this.classes;
|
||||
|
||||
if(!classList.contains(classes.notTop)) {
|
||||
if (!classList.contains(classes.notTop)) {
|
||||
classList.add(classes.notTop);
|
||||
classList.remove(classes.top);
|
||||
this.onNotTop && this.onNotTop.call(this);
|
||||
|
@ -231,7 +261,7 @@
|
|||
* @see https://developer.mozilla.org/en-US/docs/Web/API/Window.scrollY
|
||||
* @return {Number} pixels the page has scrolled along the Y-axis
|
||||
*/
|
||||
getScrollY : function() {
|
||||
getScrollY: function () {
|
||||
return (this.scroller.pageYOffset !== undefined)
|
||||
? this.scroller.pageYOffset
|
||||
: (this.scroller.scrollTop !== undefined)
|
||||
|
@ -244,7 +274,7 @@
|
|||
* @see http://andylangton.co.uk/blog/development/get-viewport-size-width-and-height-javascript
|
||||
* @return {int} the height of the viewport in pixels
|
||||
*/
|
||||
getViewportHeight : function () {
|
||||
getViewportHeight: function () {
|
||||
return window.innerHeight
|
||||
|| document.documentElement.clientHeight
|
||||
|| document.body.clientHeight;
|
||||
|
@ -255,7 +285,7 @@
|
|||
* @see http://james.padolsey.com/javascript/get-document-height-cross-browser/
|
||||
* @return {int} the height of the document in pixels
|
||||
*/
|
||||
getDocumentHeight : function () {
|
||||
getDocumentHeight: function () {
|
||||
var body = document.body,
|
||||
documentElement = document.documentElement;
|
||||
|
||||
|
@ -271,7 +301,7 @@
|
|||
* @param {Object} elm the element to calculate the height of which
|
||||
* @return {int} the height of the element in pixels
|
||||
*/
|
||||
getElementHeight : function (elm) {
|
||||
getElementHeight: function (elm) {
|
||||
return Math.max(
|
||||
elm.scrollHeight,
|
||||
elm.offsetHeight,
|
||||
|
@ -283,7 +313,7 @@
|
|||
* Gets the height of the scroller element
|
||||
* @return {int} the height of the scroller element in pixels
|
||||
*/
|
||||
getScrollerHeight : function () {
|
||||
getScrollerHeight: function () {
|
||||
return (this.scroller === window || this.scroller === document.body)
|
||||
? this.getDocumentHeight()
|
||||
: this.getElementHeight(this.scroller);
|
||||
|
@ -294,11 +324,15 @@
|
|||
* @param {int} currentScrollY the current y scroll position
|
||||
* @return {bool} true if out of bounds, false otherwise
|
||||
*/
|
||||
isOutOfBounds : function (currentScrollY) {
|
||||
var pastTop = currentScrollY < 0,
|
||||
pastBottom = currentScrollY + this.getViewportHeight() > this.getScrollerHeight();
|
||||
isOutOfBounds: function (currentScrollY) {
|
||||
var pastTop = currentScrollY < 0;
|
||||
|
||||
return pastTop || pastBottom;
|
||||
if (pastTop) {
|
||||
return true;
|
||||
}
|
||||
|
||||
var pastBottom = currentScrollY + this.getViewportHeight() > this.getScrollerHeight();
|
||||
return pastBottom;
|
||||
},
|
||||
|
||||
/**
|
||||
|
@ -306,8 +340,8 @@
|
|||
* @param {int} currentScrollY the current scroll y position
|
||||
* @return {bool} true if tolerance exceeded, false otherwise
|
||||
*/
|
||||
toleranceExceeded : function (currentScrollY, direction) {
|
||||
return Math.abs(currentScrollY-this.lastKnownScrollY) >= this.tolerance[direction];
|
||||
toleranceExceeded: function (currentScrollY, direction) {
|
||||
return Math.abs(currentScrollY - this.lastKnownScrollY) >= this.tolerance[direction];
|
||||
},
|
||||
|
||||
/**
|
||||
|
@ -316,7 +350,7 @@
|
|||
* @param {bool} toleranceExceeded has the tolerance been exceeded?
|
||||
* @return {bool} true if should unpin, false otherwise
|
||||
*/
|
||||
shouldUnpin : function (currentScrollY, toleranceExceeded) {
|
||||
shouldUnpin: function (currentScrollY, toleranceExceeded) {
|
||||
var scrollingDown = currentScrollY > this.lastKnownScrollY,
|
||||
pastOffset = currentScrollY >= this.offset;
|
||||
|
||||
|
@ -329,7 +363,7 @@
|
|||
* @param {bool} toleranceExceeded has the tolerance been exceeded?
|
||||
* @return {bool} true if should pin, false otherwise
|
||||
*/
|
||||
shouldPin : function (currentScrollY, toleranceExceeded) {
|
||||
shouldPin: function (currentScrollY, toleranceExceeded) {
|
||||
var scrollingUp = currentScrollY < this.lastKnownScrollY,
|
||||
pastOffset = currentScrollY <= this.offset;
|
||||
|
||||
|
@ -339,25 +373,25 @@
|
|||
/**
|
||||
* Handles updating the state of the widget
|
||||
*/
|
||||
update : function() {
|
||||
update: function () {
|
||||
var currentScrollY = this.getScrollY(),
|
||||
scrollDirection = currentScrollY > this.lastKnownScrollY ? 'down' : 'up',
|
||||
toleranceExceeded = this.toleranceExceeded(currentScrollY, scrollDirection);
|
||||
|
||||
if(this.isOutOfBounds(currentScrollY)) { // Ignore bouncy scrolling in OSX
|
||||
if (this.isOutOfBounds(currentScrollY)) { // Ignore bouncy scrolling in OSX
|
||||
return;
|
||||
}
|
||||
|
||||
if (currentScrollY <= this.offset ) {
|
||||
if (currentScrollY <= this.offset) {
|
||||
this.top();
|
||||
} else {
|
||||
this.notTop();
|
||||
}
|
||||
|
||||
if(this.shouldUnpin(currentScrollY, toleranceExceeded)) {
|
||||
if (this.shouldUnpin(currentScrollY, toleranceExceeded)) {
|
||||
this.unpin();
|
||||
}
|
||||
else if(this.shouldPin(currentScrollY, toleranceExceeded)) {
|
||||
else if (this.shouldPin(currentScrollY, toleranceExceeded)) {
|
||||
this.pin();
|
||||
}
|
||||
|
||||
|
@ -369,18 +403,18 @@
|
|||
* @type {Object}
|
||||
*/
|
||||
Headroom.options = {
|
||||
tolerance : {
|
||||
up : 0,
|
||||
down : 0
|
||||
tolerance: {
|
||||
up: 0,
|
||||
down: 0
|
||||
},
|
||||
offset : 0,
|
||||
offset: 0,
|
||||
scroller: window,
|
||||
classes : {
|
||||
pinned : 'headroom--pinned',
|
||||
unpinned : 'headroom--unpinned',
|
||||
top : 'headroom--top',
|
||||
notTop : 'headroom--not-top',
|
||||
initial : 'headroom'
|
||||
classes: {
|
||||
pinned: 'headroom--pinned',
|
||||
unpinned: 'headroom--unpinned',
|
||||
top: 'headroom--top',
|
||||
notTop: 'headroom--not-top',
|
||||
initial: 'headroom'
|
||||
}
|
||||
};
|
||||
Headroom.cutsTheMustard = typeof features !== 'undefined' && features.rAF && features.bind && features.classList;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue