goodtap
Version:
Tap, Press, Swipe events without all the event handlers
1 lines • 10.2 kB
JavaScript
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e(require("goodcore"),require("goodcore/Dom"),require("goodcore/Arr"),require("goodcore/Test"),require("goodcore/Util")):"function"==typeof define&&define.amd?define(["goodcore","goodcore/Dom","goodcore/Arr","goodcore/Test","goodcore/Util"],e):"object"==typeof exports?exports.goodtap=e(require("goodcore"),require("goodcore/Dom"),require("goodcore/Arr"),require("goodcore/Test"),require("goodcore/Util")):t.goodtap=e(t.goodcore,t.goodcore.Dom,t.goodcore.Arr,t.goodcore.Test,t.goodcore.Util)}(window,function(t,e,n,o,r){return function(t){var e={};function n(o){if(e[o])return e[o].exports;var r=e[o]={i:o,l:!1,exports:{}};return t[o].call(r.exports,r,r.exports,n),r.l=!0,r.exports}return n.m=t,n.c=e,n.d=function(t,e,o){n.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:o})},n.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},n.t=function(t,e){if(1&e&&(t=n(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var o=Object.create(null);if(n.r(o),Object.defineProperty(o,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var r in t)n.d(o,r,function(e){return t[e]}.bind(null,r));return o},n.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(e,"a",e),e},n.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},n.p="",n(n.s=5)}([function(e,n){e.exports=t},function(t,n){t.exports=e},function(t,e){t.exports=n},function(t,e){t.exports=o},function(t,e){t.exports=r},function(t,e,n){"use strict";n.r(e),n.d(e,"ALL_EVENTS",function(){return d}),n.d(e,"GoodTap",function(){return f}),n.d(e,"init",function(){return h});var o=n(0),r=n(2),i=n(4),s=n(1),a=n(3),u=Object.assign||function(t){for(var e,n=1,o=arguments.length;n<o;n++)for(var r in e=arguments[n])Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r]);return t},c="0.2.1",d=["down","drag","up","press","tap","swipe","outside","dragstart","dragend","drag"],f=function(){function t(t,e){this.version=c,this.minSwipeDistance=100,this.maxTapDuration=400,this.defaultLongPressDuration=400,this.defaultDragResistance=0,this.dragResistanceSquared=0,this.downEvents=["down"],this.upEvents=["up","tap","swipe"],this.longPressIntervals=new o.Dictionary,this.eventAttr="",this.upEventsAndPress=[],this.index=0,this.lastInsides=new o.List,this.dragging=new o.List,this.isListeningToMovement=!1,this.config={map:{}},this.upEventsAndPress=this.upEvents.concat(["press"]),this.init(t||document.body,e)}return t.prototype.init=function(t,e){var n=this;this.config=u({},this.config,e),this.eventAttr=d.map(function(t){return"["+(n.config.map[t]||t)+"]"}).join(","),this.downEvents=this.downEvents.map(function(t){return n.config.map[t]||t}),this.upEventsAndPress=this.upEventsAndPress.map(function(t){return n.config.map[t]||t}),this.hasTouchEvent()&&(t.addEventListener("touchstart",function(e){n.start(e,t)}),t.addEventListener("touchend",function(e){n.end(e,t)})),t.addEventListener("mousedown",function(e){n.start(e,t)}),t.addEventListener("mouseup",function(e){n.end(e,t)}),t.addEventListener("focus",function(e){return n.triggerOutside(e.target,e,t)},!0),this.root=t},t.prototype.findTarget=function(t){for(var e=null;t&&t.parentElement!==document&&null===e;)Object(s.is)(this.eventAttr,t)&&(e=t),t=t.parentElement;return e},t.prototype.findTargets=function(t){for(var e=[];t&&t.parentElement!==document;)t.id||(t.id=Object(i.newUUID)()),Object(s.is)(this.eventAttr,t)&&e.push(t),t=t.parentElement;return e},t.prototype.getTouchPos=function(t,e){return e=e||new o.Vec2(0,0),t instanceof TouchEvent?(e.x=t.changedTouches[0].pageX,e.y=t.changedTouches[0].pageY):t instanceof MouseEvent&&(e.x=t.pageX,e.y=t.pageY),e},t.prototype.longPress=function(t,e){var n=e.touchInfo;(!1===this.executeAction(t,e,this.mapEvent("press"),n)||e.hasAttribute("once"))&&(clearInterval(this.longPressIntervals.get(n.index)),this.longPressIntervals.delete(n.index))},t.prototype.triggerOutside=function(t,e,n){var r=this,i=new o.List(Object(s.findAll)("["+this.mapEvent("outside")+"]",this.root));if(i.length>0){var a=new o.List(this.findTargets(t));a.contains(function(t){return t.hasAttribute("preventDefault")})||(i.filter(function(t){return r.lastInsides.contains(t)&&!a.contains(t)}).forEach(function(t){return r.handleEvent(r.mapEvent("outside"),e,t)}),this.lastInsides=a)}},t.prototype.move=function(t,e,n,o){var r=!0,i=n.touchInfo;if(void 0!==i){if(!0!==t.cancelBubble){if(this.getTouchPos(t,i.pos),0===i.dragResistance||this.getTouchPos(t).subtract(i.pos).lengthSq()<i.dragResistance){var s=n.touchInfo.dragTarget||n,a=n!==s;i.dragResistance=0;try{o&&("[fn]"===o&&"drag-fn"in n?r=n["drag-fn"](t,s,i):(n["drag-fn"]=new Function("event","target","touch",o).bind(n),r=n["drag-fn"](t,s,i)))}catch(t){throw name+" event function error on element '"+n.id+"'\n"+t.toString()}if(!1===r&&this.end(t,e),s.hasAttribute("draggable")){s.getBoundingClientRect();s.style.left=(a?n.touchInfo.dragTargetOrigin.x:n.touchInfo.origin.x)+(n.touchInfo.pos.x-n.touchInfo.startPos.x)+"px",s.style.top=(a?n.touchInfo.dragTargetOrigin.y:n.touchInfo.origin.y)+(n.touchInfo.pos.y-n.touchInfo.startPos.y)+"px"}}(n.hasAttribute("stopPropagation")||n.hasAttribute("gt-false"))&&(t.stopPropagation(),n.touchInfo.prevented.drag=!0),(n.hasAttribute("preventDefault")||n.hasAttribute("gt-false")||n.hasAttribute("noTouchScroll"))&&t.preventDefault()}}else this.end(t,e)},t.prototype.start=function(t,e){var n=this;this.longPressIntervals.values.forEach(function(t){return clearInterval(t)}),this.longPressIntervals.clear();var i=!1,a=t.target,u=0;for(this.triggerOutside(a,t,e);u<100&&(a=this.findTarget(a))&&!i;){++u;var c=null;a.hasAttribute(this.mapEvent("press"))&&(c=setInterval(function(e){return n.longPress(t,e)}.bind(this,a),parseInt(a.getAttribute("pressInterval"))||this.defaultLongPressDuration));var d=void 0,f=0,h=!1;if(a.hasAttribute(this.mapEvent("drag"))){h=!!a.getAttribute("dragTarget"),f=parseInt(a.getAttribute("dragResistance")),isNaN(f)&&(f=this.defaultDragResistance),f*=f;var p=a.getAttribute(this.mapEvent("drag"));d=function(t,o,r){n.move(r,e,t,o)}.bind(this,a,p),this.hasTouchEvent()&&e.addEventListener("touchmove",d),e.addEventListener("mousemove",d)}a.classList.add("gt-active");var g=h?Object(s.get)(a.getAttribute("dragTarget")):a;a.touchInfo={index:this.index++,time:o.Timer.now(),pos:this.getTouchPos(t),startPos:this.getTouchPos(t),origin:new o.Vec2(parseInt(a.style.left||"0"),parseInt(a.style.top||"0")),long:c,moveHandler:d,dragResistance:f,prevented:{},dragTarget:h?g:void 0,dragTargetOrigin:h?new o.Vec2(parseInt(g.style.left||"0"),parseInt(g.style.top||"0")):void 0},a.hasAttribute(this.mapEvent("dragstart"))&&this.handleEvent(this.mapEvent("dragstart"),t,a),c&&this.longPressIntervals.set(a.touchInfo.index,a.touchInfo.long),Object(r.until)(this.downEvents,function(e){return a.hasAttribute(e)&&((!(i=!1===n.handleEvent(e,t,a))&&a.hasAttribute("stopPropagation")||a.hasAttribute("gt-false"))&&(i=!0,t.stopPropagation(),a.touchInfo.prevented[e]=!0),(a.hasAttribute("preventDefault")||a.hasAttribute("gt-false"))&&(t.preventDefault(),!0)),i}),a=a.parentElement}},t.prototype.isSwipe=function(t,e){var n=this.getTouchPos(t),r=!1;if("touchInfo"in e){var i=n.x-e.touchInfo.pos.x,s=n.y-e.touchInfo.pos.y,a=Math.abs(i),u=Math.abs(s),c=a>u,d=c?a:u;(r=d>=this.minSwipeDistance)&&(e.touchInfo.swipeInfo={direction:c?i<0?"left":"right":s<0?"up":"down",distance:d,delta:new o.Vec2(i,s)})}return r},t.prototype.end=function(t,e){var n=this,i=o.Timer.now(),a=!1,u=t.target,c=0;new o.List;this.dragging.clear();for(var d=function(){++c;var e=u.touchInfo;if(void 0!==e){var o=i-e.time;Object(r.until)(f.upEventsAndPress,function(e){if(u.hasAttribute(e)){var r=n.isSwipe(t,u);(e===n.mapEvent("swipe")&&r||e===n.mapEvent("tap")&&!r&&o<n.maxTapDuration||e===n.mapEvent("up"))&&(a=!1===n.handleEvent(e,t,u)),(a||u.hasAttribute("stopPropagation")||u.hasAttribute("gt-false"))&&(a=!0,t.stopPropagation(),delete u.touchInfo),(u.hasAttribute("preventDefault")||u.hasAttribute("gt-false"))&&t.preventDefault()}return a})}u=u.parentElement},f=this;c<100&&(u=this.findTarget(u))&&!a;)d();Object(s.findAll)(".gt-active").forEach(function(o){o.classList.remove("gt-active"),void 0!==o.touchInfo&&void 0!==o.touchInfo.moveHandler&&(e.removeEventListener("touchmove",o.touchInfo.moveHandler),e.removeEventListener("mousemove",o.touchInfo.moveHandler),n.handleEvent(n.mapEvent("dragend"),t,o)),delete o.touchInfo}),this.longPressIntervals.values.forEach(function(t){return clearInterval(t)}),this.longPressIntervals.clear()},t.prototype.mapEvent=function(t){var e=t;return Object(a.isNotUndefined)(this.config.map[t])&&(e=this.config.map[t]),e},t.prototype.executeAction=function(t,e,n,o){var r=!0;if(n===this.mapEvent("outside")||!o.prevented[n]){var i=this.mapEvent(n),s=e.getAttribute(i);try{r="[fn]"===s&&i+"-fn"in e?e[i+"-fn"](t,e,o):new Function("event","target","touch",s).bind(e)(t,e,o)}catch(t){throw name+" event function error on element '"+e.id+"'\n"+t.toString()}}return r},t.prototype.handleEvent=function(t,e,n){var o=!0;return n&&(o=this.executeAction(e,n,t,n.touchInfo),t in this.upEvents&&(n.classList.remove("gt-active"),this.longPressIntervals.has(n.touchInfo.index)&&clearInterval(this.longPressIntervals.get(n.touchInfo.index)),delete n.touchInfo)),o},t.prototype.on=function(t,e,n){t.setAttribute(e,"[fn]"),t[e+"-fn"]=n},t.prototype.off=function(t,e){t.removeAttribute(e+"-action"),delete t[e+"-fn"]},t.prototype.hideKeyboard=function(){var t=document.createElement("input");t.setAttribute("type","text"),document.body.appendChild(t),setTimeout(function(){t.focus(),setTimeout(function(){t.setAttribute("style","display:none;"),t.parentElement.removeChild(t)},50)},50)},t.prototype.hasTouchEvent=function(){return Object(a.isNotUndefined)(document.documentElement)&&"ontouchstart"in document.documentElement},t.prototype.outside=function(){this.triggerOutside(this.root,new FocusEvent("")),this.hideKeyboard()},t}();function h(t,e){return new f(t,e)}}])});