jellyfin-web/dashboard-ui/bower_components/emby-webcomponents/headroom/headroom.js
2017-08-22 01:41:02 -04:00

1 line
4.0 KiB
JavaScript

define(["dom","layoutManager","browser","css!./headroom"],function(dom,layoutManager,browser){"use strict";function Debouncer(callback){this.callback=callback,this.ticking=!1}function Headroom(elems,options){options=Object.assign(Headroom.options,options||{}),this.lastKnownScrollY=0,this.elems=elems,this.scroller=options.scroller,this.scroller.getScrollPosition?this.debouncer=this.update.bind(this):this.debouncer=new Debouncer(this.update.bind(this)),this.offset=options.offset,this.initialised=!1,this.initialClass=options.initialClass,this.unPinnedClass=options.unPinnedClass,this.pinnedClass=options.pinnedClass,this.state="clear"}var requestAnimationFrame=window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame;return Debouncer.prototype={constructor:Debouncer,update:function(){this.callback&&this.callback(),this.ticking=!1},handleEvent:function(){this.ticking||(requestAnimationFrame(this.rafCallback||(this.rafCallback=this.update.bind(this))),this.ticking=!0)}},Headroom.prototype={constructor:Headroom,init:function(){if(browser.supportsCssAnimation()){for(var i=0,length=this.elems.length;i<length;i++)this.elems[i].classList.add(this.initialClass);this.attachEvent()}return this},add:function(elem){browser.supportsCssAnimation()&&(elem.classList.add(this.initialClass),this.elems.push(elem))},remove:function(elem){elem.classList.remove(this.unPinnedClass),elem.classList.remove(this.initialClass),elem.classList.remove(this.pinnedClass);var i=this.elems.indexOf(elem);i!==-1&&this.elems.splice(i,1)},destroy:function(){this.initialised=!1;for(var i=0,length=this.elems.length;i<length;i++){var classList=this.elems[i].classList;classList.remove(this.unPinnedClass),classList.remove(this.initialClass),classList.remove(this.pinnedClass)}var scrollEventName=this.scroller.getScrollEventName?this.scroller.getScrollEventName():"scroll";dom.removeEventListener(this.scroller,scrollEventName,this.debouncer,{capture:!1,passive:!0})},attachEvent:function(){if(!this.initialised){this.lastKnownScrollY=this.getScrollY(),this.initialised=!0;var scrollEventName=this.scroller.getScrollEventName?this.scroller.getScrollEventName():"scroll";dom.addEventListener(this.scroller,scrollEventName,this.debouncer,{capture:!1,passive:!0}),this.update()}},clear:function(){if("clear"!==this.state){this.state="clear";for(var unpinnedClass=this.unPinnedClass,i=(this.pinnedClass,0),length=this.elems.length;i<length;i++){var classList=this.elems[i].classList;classList.remove(unpinnedClass)}}},pin:function(){if("pin"!==this.state){this.state="pin";for(var unpinnedClass=this.unPinnedClass,pinnedClass=this.pinnedClass,i=0,length=this.elems.length;i<length;i++){var classList=this.elems[i].classList;classList.remove(unpinnedClass),classList.add(pinnedClass)}}},unpin:function(){if("unpin"!==this.state){this.state="unpin";for(var unpinnedClass=this.unPinnedClass,i=(this.pinnedClass,0),length=this.elems.length;i<length;i++){var classList=this.elems[i].classList;classList.add(unpinnedClass)}}},getScrollY:function(){var scroller=this.scroller;if(scroller.getScrollPosition)return scroller.getScrollPosition();var pageYOffset=scroller.pageYOffset;if(void 0!==pageYOffset)return pageYOffset;var scrollTop=scroller.scrollTop;return void 0!==scrollTop?scrollTop:(document.documentElement||document.body).scrollTop},shouldUnpin:function(currentScrollY){var scrollingDown=currentScrollY>this.lastKnownScrollY,pastOffset=currentScrollY>=this.offset;return scrollingDown&&pastOffset},shouldPin:function(currentScrollY){var scrollingUp=currentScrollY<this.lastKnownScrollY,pastOffset=currentScrollY<=this.offset;return scrollingUp||pastOffset},update:function(){var currentScrollY=this.getScrollY();currentScrollY<=30?this.clear():this.shouldUnpin(currentScrollY)?this.unpin():this.shouldPin(currentScrollY)?currentScrollY&&layoutManager.tv?this.unpin():this.clear():layoutManager.tv&&this.clear(),this.lastKnownScrollY=currentScrollY}},Headroom.options={offset:0,scroller:window,initialClass:"headroom",unPinnedClass:"headroom--unpinned",pinnedClass:"headroom--pinned"},Headroom});