2022-10-20 01:27:21 +00:00
|
|
|
(function(global,factory){typeof exports==="object"&&typeof module!=="undefined"?module.exports=factory():typeof define==="function"&&define.amd?define(factory):(global=global||self,global.PullToRefresh=factory())})(this,function(){"use strict";var _shared={pullStartY:null,pullMoveY:null,handlers:[],styleEl:null,events:null,dist:0,state:"pending",timeout:null,distResisted:0,supportsPassive:false,supportsPointerEvents:typeof window!=="undefined"&&!!window.PointerEvent};try{window.addEventListener("test",null,{get passive(){_shared.supportsPassive=true}})}catch(e){}function setupDOM(handler){if(!handler.ptrElement){var ptr=document.createElement("div");if(handler.mainElement!==document.body){handler.mainElement.parentNode.insertBefore(ptr,handler.mainElement)}else{document.body.insertBefore(ptr,document.body.firstChild)}ptr.classList.add(handler.classPrefix+"ptr");ptr.innerHTML=handler.getMarkup().replace(/__PREFIX__/g,handler.classPrefix);handler.ptrElement=ptr;if(typeof handler.onInit==="function"){handler.onInit(handler)}if(!_shared.styleEl){_shared.styleEl=document.createElement("style");_shared.styleEl.setAttribute("id","pull-to-refresh-js-style");document.head.appendChild(_shared.styleEl)}_shared.styleEl.textContent=handler.getStyles().replace(/__PREFIX__/g,handler.classPrefix).replace(/\s+/g," ")}return handler}function onReset(handler){if(!handler.ptrElement){return}handler.ptrElement.classList.remove(handler.classPrefix+"refresh");handler.ptrElement.style[handler.cssProp]="0px";setTimeout(function(){if(handler.ptrElement&&handler.ptrElement.parentNode){handler.ptrElement.parentNode.removeChild(handler.ptrElement);handler.ptrElement=null}_shared.state="pending"},handler.refreshTimeout)}function update(handler){var iconEl=handler.ptrElement.querySelector("."+handler.classPrefix+"icon");var textEl=handler.ptrElement.querySelector("."+handler.classPrefix+"text");if(iconEl){if(_shared.state==="refreshing"){iconEl.innerHTML=handler.iconRefreshing}else{iconEl.innerHTML=handler.iconArrow}}if(textEl){if(_shared.state==="releasing"){textEl.innerHTML=handler.instructionsReleaseToRefresh}if(_shared.state==="pulling"||_shared.state==="pending"){textEl.innerHTML=handler.instructionsPullToRefresh}if(_shared.state==="refreshing"){textEl.innerHTML=handler.instructionsRefreshing}}}var _ptr={setupDOM:setupDOM,onReset:onReset,update:update};var _timeout;var screenY=function screenY(event){if(_shared.pointerEventsEnabled&&_shared.supportsPointerEvents){return event.screenY}return event.touches[0].screenY};var _setupEvents=function(){var _el;function _onTouchStart(e){var target=_shared.handlers.filter(function(h){return h.contains(e.target)})[0];_shared.enable=!!target;if(target&&_shared.state==="pending"){_el=_ptr.setupDOM(target);if(target.shouldPullToRefresh()){_shared.pullStartY=screenY(e)}clearTimeout(_shared.timeout);_ptr.update(target)}}function _onTouchMove(e){if(!(_el&&_el.ptrElement&&_shared.enable)){return}if(!_shared.pullStartY){if(_el.shouldPullToRefresh()){_shared.pullStartY=screenY(e)}}else{_shared.pullMoveY=screenY(e)}if(_shared.state==="refreshing"){if(e.cancelable&&_el.shouldPullToRefresh()&&_shared.pullStartY<_shared.pullMoveY){e.preventDefault()}return}if(_shared.state==="pending"){_el.ptrElement.classList.add(_el.classPrefix+"pull");_shared.state="pulling";_ptr.update(_el)}if(_shared.pullStartY&&_shared.pullMoveY){_shared.dist=_shared.pullMoveY-_shared.pullStartY}_shared.distExtra=_shared.dist-_el.distIgnore;if(_shared.distExtra>0){if(e.cancelable){e.preventDefault()}_el.ptrElement.style[_el.cssProp]=_shared.distResisted+"px";_shared.distResisted=_el.resistanceFunction(_shared.distExtra/_el.distThreshold)*Math.min(_el.distMax,_shared.distExtra);if(_shared.state==="pulling"&&_shared.distResisted>_el.distThreshold){_el.ptrElement.classList.add(_el.classPrefix+"release");_shared.state="releasing";_ptr.update(_el)}if(_shared.state==="releasing"&&_shared.distResisted<_el.distThreshold){_el.ptrElement.classList.remove(_el.classPrefix+"release");_shared.state="pulling";_ptr.update(_el)}}}function _onTouchEnd(){if(!(_el&&
|