-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
1 lines (1 loc) · 10.7 KB
/
index.js
1
!function(){var e,t,i;function l(e,t){if(e.classList)return t.forEach(function(t){e.classList.add(t)});e.className+=" "+t.join(" ")}function n(e,t){if(e.classList)return t.forEach(function(t){e.classList.remove(t)});e.className=e.className.replace(new RegExp("(^|\\b)"+t.join("|")+"(\\b|$)","gi")," ")}function s(){var e=navigator.userAgent.toLowerCase();return-1!==e.indexOf("msie")||-1!==e.indexOf("trident")||-1!==e.indexOf(" edge/")}function r(i){var l,n;this.element=null,this.autoshow=!1,this.createElements=!0,this.forceGemini=!1,this.onResize=null,this.minThumbSize=20,Object.keys(i||{}).forEach(function(e){this[e]=i[e]},this),(n=document.createElement("div")).style.position="absolute",n.style.top="-9999px",n.style.width="100px",n.style.height="100px",n.style.overflow="scroll",n.style.msOverflowStyle="scrollbar",document.body.appendChild(n),l=n.offsetWidth-n.clientWidth,document.body.removeChild(n),t=0===(e=l)&&!1===this.forceGemini,this._cache={events:{}},this._created=!1,this._cursorDown=!1,this._prevPageX=0,this._prevPageY=0,this._document=null,this._viewElement=this.element,this._scrollbarVerticalElement=null,this._thumbVerticalElement=null,this._scrollbarHorizontalElement=null,this._scrollbarHorizontalElement=null}i={element:"gm-scrollbar-container",verticalScrollbar:"gm-scrollbar -vertical",horizontalScrollbar:"gm-scrollbar -horizontal",thumb:"thumb",view:"gm-scroll-view",autoshow:"gm-autoshow",disable:"gm-scrollbar-disable-selection",prevented:"gm-prevented",resizeTrigger:"gm-resize-trigger"},r.prototype.create=function(){if(t){if(l(this.element,[i.prevented]),this.onResize){if(!0===this.createElements){for(this._viewElement=document.createElement("div");this.element.childNodes.length>0;)this._viewElement.appendChild(this.element.childNodes[0]);this.element.appendChild(this._viewElement)}else this._viewElement=this.element.querySelector("."+i.view);l(this.element,[i.element]),l(this._viewElement,[i.view]),this._createResizeTrigger()}return this}if(!0===this._created)return console.warn("calling on a already-created object"),this;if(this.autoshow&&l(this.element,[i.autoshow]),this._document=document,!0===this.createElements){for(this._viewElement=document.createElement("div"),this._scrollbarVerticalElement=document.createElement("div"),this._thumbVerticalElement=document.createElement("div"),this._scrollbarHorizontalElement=document.createElement("div"),this._thumbHorizontalElement=document.createElement("div");this.element.childNodes.length>0;)this._viewElement.appendChild(this.element.childNodes[0]);this._scrollbarVerticalElement.appendChild(this._thumbVerticalElement),this._scrollbarHorizontalElement.appendChild(this._thumbHorizontalElement),this.element.appendChild(this._scrollbarVerticalElement),this.element.appendChild(this._scrollbarHorizontalElement),this.element.appendChild(this._viewElement)}else this._viewElement=this.element.querySelector("."+i.view),this._scrollbarVerticalElement=this.element.querySelector("."+i.verticalScrollbar.split(" ").join(".")),this._thumbVerticalElement=this._scrollbarVerticalElement.querySelector("."+i.thumb),this._scrollbarHorizontalElement=this.element.querySelector("."+i.horizontalScrollbar.split(" ").join(".")),this._thumbHorizontalElement=this._scrollbarHorizontalElement.querySelector("."+i.thumb);return l(this.element,[i.element]),l(this._viewElement,[i.view]),l(this._scrollbarVerticalElement,i.verticalScrollbar.split(/\s/)),l(this._scrollbarHorizontalElement,i.horizontalScrollbar.split(/\s/)),l(this._thumbVerticalElement,[i.thumb]),l(this._thumbHorizontalElement,[i.thumb]),this._scrollbarVerticalElement.style.display="",this._scrollbarHorizontalElement.style.display="",this._createResizeTrigger(),this._created=!0,this._bindEvents().update()},r.prototype._createResizeTrigger=function(){var e=document.createElement("object");l(e,[i.resizeTrigger]),e.type="text/html",e.setAttribute("tabindex","-1");var t=this._resizeHandler.bind(this);e.onload=function(){e.contentDocument.defaultView.addEventListener("resize",t)},s()||(e.data="about:blank"),this.element.appendChild(e),s()&&(e.data="about:blank"),this._resizeTriggerElement=e},r.prototype.update=function(){return t?this:!1===this._created?(console.warn("calling on a not-yet-created object"),this):(this._viewElement.style.width=(this.element.offsetWidth+e).toString()+"px",this._viewElement.style.height=(this.element.offsetHeight+e).toString()+"px",this._naturalThumbSizeX=this._scrollbarHorizontalElement.clientWidth/this._viewElement.scrollWidth*this._scrollbarHorizontalElement.clientWidth,this._naturalThumbSizeY=this._scrollbarVerticalElement.clientHeight/this._viewElement.scrollHeight*this._scrollbarVerticalElement.clientHeight,this._scrollTopMax=this._viewElement.scrollHeight-this._viewElement.clientHeight,this._scrollLeftMax=this._viewElement.scrollWidth-this._viewElement.clientWidth,this._naturalThumbSizeY<this.minThumbSize?this._thumbVerticalElement.style.height=this.minThumbSize+"px":this._scrollTopMax?this._thumbVerticalElement.style.height=this._naturalThumbSizeY+"px":this._thumbVerticalElement.style.height="0px",this._naturalThumbSizeX<this.minThumbSize?this._thumbHorizontalElement.style.width=this.minThumbSize+"px":this._scrollLeftMax?this._thumbHorizontalElement.style.width=this._naturalThumbSizeX+"px":this._thumbHorizontalElement.style.width="0px",this._thumbSizeY=this._thumbVerticalElement.clientHeight,this._thumbSizeX=this._thumbHorizontalElement.clientWidth,this._trackTopMax=this._scrollbarVerticalElement.clientHeight-this._thumbSizeY,this._trackLeftMax=this._scrollbarHorizontalElement.clientWidth-this._thumbSizeX,this._scrollHandler(),this)},r.prototype.destroy=function(){if(this._resizeTriggerElement&&(this.element.removeChild(this._resizeTriggerElement),this._resizeTriggerElement=null),t)return this;if(!1===this._created)return console.warn("calling on a not-yet-created object"),this;if(this._unbinEvents(),n(this.element,[i.element,i.autoshow]),!0===this.createElements){for(this.element.removeChild(this._scrollbarVerticalElement),this.element.removeChild(this._scrollbarHorizontalElement);this._viewElement.childNodes.length>0;)this.element.appendChild(this._viewElement.childNodes[0]);this.element.removeChild(this._viewElement)}else this._viewElement.style.width="",this._viewElement.style.height="",this._scrollbarVerticalElement.style.display="none",this._scrollbarHorizontalElement.style.display="none";return this._created=!1,this._document=null,null},r.prototype.getViewElement=function(){return this._viewElement},r.prototype._bindEvents=function(){return this._cache.events.scrollHandler=this._scrollHandler.bind(this),this._cache.events.clickVerticalTrackHandler=this._clickVerticalTrackHandler.bind(this),this._cache.events.clickHorizontalTrackHandler=this._clickHorizontalTrackHandler.bind(this),this._cache.events.clickVerticalThumbHandler=this._clickVerticalThumbHandler.bind(this),this._cache.events.clickHorizontalThumbHandler=this._clickHorizontalThumbHandler.bind(this),this._cache.events.mouseUpDocumentHandler=this._mouseUpDocumentHandler.bind(this),this._cache.events.mouseMoveDocumentHandler=this._mouseMoveDocumentHandler.bind(this),this._viewElement.addEventListener("scroll",this._cache.events.scrollHandler),this._scrollbarVerticalElement.addEventListener("mousedown",this._cache.events.clickVerticalTrackHandler),this._scrollbarHorizontalElement.addEventListener("mousedown",this._cache.events.clickHorizontalTrackHandler),this._thumbVerticalElement.addEventListener("mousedown",this._cache.events.clickVerticalThumbHandler),this._thumbHorizontalElement.addEventListener("mousedown",this._cache.events.clickHorizontalThumbHandler),this._document.addEventListener("mouseup",this._cache.events.mouseUpDocumentHandler),this},r.prototype._unbinEvents=function(){return this._viewElement.removeEventListener("scroll",this._cache.events.scrollHandler),this._scrollbarVerticalElement.removeEventListener("mousedown",this._cache.events.clickVerticalTrackHandler),this._scrollbarHorizontalElement.removeEventListener("mousedown",this._cache.events.clickHorizontalTrackHandler),this._thumbVerticalElement.removeEventListener("mousedown",this._cache.events.clickVerticalThumbHandler),this._thumbHorizontalElement.removeEventListener("mousedown",this._cache.events.clickHorizontalThumbHandler),this._document.removeEventListener("mouseup",this._cache.events.mouseUpDocumentHandler),this._document.removeEventListener("mousemove",this._cache.events.mouseMoveDocumentHandler),this},r.prototype._scrollHandler=function(){var e=this._viewElement.scrollLeft*this._trackLeftMax/this._scrollLeftMax||0,t=this._viewElement.scrollTop*this._trackTopMax/this._scrollTopMax||0;this._thumbHorizontalElement.style.msTransform="translateX("+e+"px)",this._thumbHorizontalElement.style.webkitTransform="translate3d("+e+"px, 0, 0)",this._thumbHorizontalElement.style.transform="translate3d("+e+"px, 0, 0)",this._thumbVerticalElement.style.msTransform="translateY("+t+"px)",this._thumbVerticalElement.style.webkitTransform="translate3d(0, "+t+"px, 0)",this._thumbVerticalElement.style.transform="translate3d(0, "+t+"px, 0)"},r.prototype._resizeHandler=function(){this.update(),this.onResize&&this.onResize()},r.prototype._clickVerticalTrackHandler=function(e){if(e.target===e.currentTarget){var t=100*(e.offsetY-.5*this._naturalThumbSizeY)/this._scrollbarVerticalElement.clientHeight;this._viewElement.scrollTop=t*this._viewElement.scrollHeight/100}},r.prototype._clickHorizontalTrackHandler=function(e){if(e.target===e.currentTarget){var t=100*(e.offsetX-.5*this._naturalThumbSizeX)/this._scrollbarHorizontalElement.clientWidth;this._viewElement.scrollLeft=t*this._viewElement.scrollWidth/100}},r.prototype._clickVerticalThumbHandler=function(e){this._startDrag(e),this._prevPageY=this._thumbSizeY-e.offsetY},r.prototype._clickHorizontalThumbHandler=function(e){this._startDrag(e),this._prevPageX=this._thumbSizeX-e.offsetX},r.prototype._startDrag=function(e){this._cursorDown=!0,l(document.body,[i.disable]),this._document.addEventListener("mousemove",this._cache.events.mouseMoveDocumentHandler),this._document.onselectstart=function(){return!1}},r.prototype._mouseUpDocumentHandler=function(){this._cursorDown=!1,this._prevPageX=this._prevPageY=0,n(document.body,[i.disable]),this._document.removeEventListener("mousemove",this._cache.events.mouseMoveDocumentHandler),this._document.onselectstart=null},r.prototype._mouseMoveDocumentHandler=function(e){var t,i;if(!1!==this._cursorDown)return this._prevPageY?(t=e.clientY-this._scrollbarVerticalElement.getBoundingClientRect().top,i=this._thumbSizeY-this._prevPageY,void(this._viewElement.scrollTop=this._scrollTopMax*(t-i)/this._trackTopMax)):void(this._prevPageX&&(t=e.clientX-this._scrollbarHorizontalElement.getBoundingClientRect().left,i=this._thumbSizeX-this._prevPageX,this._viewElement.scrollLeft=this._scrollLeftMax*(t-i)/this._trackLeftMax))},"object"==typeof exports?module.exports=r:window.GeminiScrollbar=r}();