crossbrowdy
Version:
A Multimedia JavaScript framework to create real cross-platform and hybrid game engines, games, emulators, multimedia libraries and apps.
2 lines • 9.46 kB
JavaScript
// Pressure v1.0.1 | Created By Stuart Yamartino | MIT License | 2015-Present
!function(e,t){"use strict";function s(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}function n(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}function i(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var o=function(){function e(e,t){for(var s=0;s<t.length;s++){var n=t[s];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(t,s,n){return s&&e(t.prototype,s),n&&e(t,n),t}}(),r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol?"symbol":typeof e},u={set:function(e,t,s){v(e,t,s)},config:function(e){p.set(e)},map:function(e,t,s,n,i){return k(e,t,s,n,i)}};if(e===!1)return void console.warn("Pressure requires a window with a document");if("undefined"!=typeof u)if("function"==typeof define&&define.amd){var c=u;define(["pressure"],function(e){return e})}else if("object"===("undefined"==typeof module?"undefined":r(module))&&module.exports){var c=u;module.exports=c}else e.Pressure=u;var l=function(){function e(t,s,n){i(this,e),this.element=t,this.block=s,this.type=p.get("only",n),this.options=n,this.routeEvents()}return o(e,[{key:"routeEvents",value:function(){y.mobile!==!1||"force"!==this.type&&"3d"===this.type?y.mobile!==!0||"3d"!==this.type&&"force"===this.type?this.instantFail():new a(this):new f(this)}},{key:"instantFail",value:function(){var e=this;this.element.addEventListener(y.mobile?"touchstart":"mousedown",function(){return P(e.block,"unsupported",e.element)},!1)}}]),e}(),h=function(){function e(t){i(this,e),this.element=t,this.el=t.element,this.block=t.block,this.pressed=!1,this.deepPressed=!1}return o(e,[{key:"add",value:function(e,t){this.el.addEventListener(e,t,!1)}},{key:"remove",value:function(e,t){this.el.removeEventListener(e,t)}},{key:"setPressed",value:function(e){this.pressed=e}},{key:"setDeepPressed",value:function(e){this.deepPressed=e}},{key:"failOrPolyfill",value:function(e){y.didFail(),p.get("polyfill",this.element.options)===!0?this.polyfill=new d(this.element,e):P(this.block,"unsupported",this.el)}},{key:"preventDefault",value:function(e){p.get("preventDefault",this.element.options)===!0&&(e.preventDefault(),this.el.style.webkitTouchCallout="none",this.el.style.userSelect="none",this.el.style.webkitUserSelect="none",this.el.style.MozUserSelect="none")}}]),e}(),a=function(e){function t(e){i(this,t);var n=s(this,Object.getPrototypeOf(t).call(this,e));return n.$support(),n.$start(),n.$change(),n.$end(),n}return n(t,e),o(t,[{key:"$support",value:function(){this.supportMethod=this.middleMan.bind(this),this.add("touchstart",this.supportMethod)}},{key:"middleMan",value:function(e){this.setPressed(!0),this.forceValueTest=e.touches[0].force,this.supportCallback(0,e)}},{key:"supportCallback",value:function(e,t){y.hasRun!==!1||this.polyfill instanceof d?y.forPressure||this.polyfill instanceof d?this.remove("touchstart",this.supportMethod):this.failOrPolyfill(t):t.touches[0].force!==this.forceValueTest?(this.preventDefault(t),y.didSucceed("3d"),this.remove("touchstart",this.supportMethod),P(this.block,"start",this.el,t),this.changeLogic(t)):10>=e&&this.pressed?(e+=1,setTimeout(this.supportCallback.bind(this),10,e,t)):this.pressed&&this.failOrPolyfill(t)}},{key:"$start",value:function(){var e=this;this.add("touchstart",function(t){y.forPressure&&(e.setPressed(!0),e.preventDefault(t),P(e.block,"start",e.el,t))})}},{key:"$change",value:function(){this.add("touchstart",this.changeLogic.bind(this))}},{key:"changeLogic",value:function(e){y.forPressure&&this.pressed&&(this.setPressed(!0),this.runForce(e))}},{key:"$end",value:function(){var e=this;this.add("touchend",function(){y.forPressure&&(e.endDeepPress(),e.setPressed(!1),P(e.block,"end",e.el))})}},{key:"startDeepPress",value:function(e){this.deepPressed===!1&&P(this.block,"startDeepPress",this.el,e),this.setDeepPressed(!0)}},{key:"endDeepPress",value:function(){this.deepPressed===!0&&P(this.block,"endDeepPress",this.el),this.setDeepPressed(!1)}},{key:"runForce",value:function(e){this.pressed&&(this.touch=this.selectTouch(e),setTimeout(this.runForce.bind(this),10,e),P(this.block,"change",this.el,this.touch.force,e))}},{key:"selectTouch",value:function(e){if(1===e.touches.length)return this.returnTouch(e.touches[0],e);for(var t=0;t<e.touches.length;t++)if(e.touches[t].target===this.el)return this.returnTouch(e.touches[t],e)}},{key:"returnTouch",value:function(e,t){return e.force>=.5?this.startDeepPress(t):this.endDeepPress(),e}}]),t}(h),f=function(e){function t(e){i(this,t);var n=s(this,Object.getPrototypeOf(t).call(this,e));return n.$support(),n.$start(),n.$change(),n.$startDeepPress(),n.$endDeepPress(),n.$end(),n}return n(t,e),o(t,[{key:"$support",value:function(){this.add("webkitmouseforcewillbegin",this.forceTouchEnabled),this.add("mousedown",this.supportCallback.bind(this))}},{key:"forceTouchEnabled",value:function(e){e.preventDefault(),y.didSucceed("force")}},{key:"supportCallback",value:function(e){y.forPressure===!0||this.polyfill instanceof d?(this.remove("webkitmouseforcewillbegin",this.forceTouchEnabled),this.preventDefault(e)):this.failOrPolyfill(e)}},{key:"$start",value:function(){var e=this;this.add("mousedown",function(t){y.forPressure&&(e.setPressed(!0),P(e.block,"start",e.el,t))})}},{key:"$change",value:function(){var e=this;this.add("webkitmouseforcechanged",function(t){y.forPressure&&0!==t.webkitForce&&e.pressed&&P(e.block,"change",e.el,e.normalizeForce(t.webkitForce),t)})}},{key:"$end",value:function(){var e=this;this.add("mouseup",function(){y.forPressure&&(e.setPressed(!1),P(e.block,"end",e.el))}),this.add("mouseleave",function(){y.forPressure&&(e.pressed&&P(e.block,"end",e.el),e.setPressed(!1))})}},{key:"$startDeepPress",value:function(){var e=this;this.add("webkitmouseforcedown",function(t){y.forPressure&&(e.setDeepPressed(!0),P(e.block,"startDeepPress",e.el,t))})}},{key:"$endDeepPress",value:function(){var e=this;this.add("webkitmouseforceup",function(){y.forPressure&&(e.setDeepPressed(!1),P(e.block,"endDeepPress",e.el))}),this.add("mouseleave",function(){y.forPressure&&(e.deepPressed&&P(e.block,"endDeepPress",e.el),e.setDeepPressed(!1))})}},{key:"normalizeForce",value:function(e){return this.reachOne(k(e,1,3,0,1))}},{key:"reachOne",value:function(e){return e>.999?1:e}}]),t}(h),d=function(e){function t(e,n){i(this,t);var o=s(this,Object.getPrototypeOf(t).call(this,e));return o.$start(),o.$change(),o.$end(),o.force=0,o.increment=.01,o.firstRun(n),o}return n(t,e),o(t,[{key:"firstRun",value:function(e){this.preventDefault(e),this.startLogic(e),this.changeLogic(e)}},{key:"$start",value:function(){var e=this;this.add(y.mobile?"touchstart":"mousedown",function(t){e.startLogic(t)})}},{key:"startLogic",value:function(e){this.setPressed(!0),P(this.block,"start",this.el,e)}},{key:"$change",value:function(){this.add(y.mobile?"touchstart":"mousedown",this.changeLogic.bind(this))}},{key:"changeLogic",value:function(e){this.pressed&&(this.setPressed(!0),this.runForce(e))}},{key:"$end",value:function(){var e=this;this.add(y.mobile?"touchend":"mouseup",function(){e.endDeepPress(),e.setPressed(!1),P(e.block,"end",e.el),e.force=0}),this.add("mouseleave",function(){e.endDeepPress(),e.pressed&&P(e.block,"end",e.el),e.setPressed(!1),e.force=0})}},{key:"startDeepPress",value:function(e){this.deepPressed===!1&&P(this.block,"startDeepPress",this.el,e),this.setDeepPressed(!0)}},{key:"endDeepPress",value:function(){this.deepPressed===!0&&P(this.block,"endDeepPress",this.el),this.setDeepPressed(!1)}},{key:"runForce",value:function(e){this.pressed&&(P(this.block,"change",this.el,this.force,e),this.force>=.5?this.startDeepPress(e):this.endDeepPress(),this.force=this.force+this.increment>1?1:this.force+this.increment,setTimeout(this.runForce.bind(this),10,e))}}]),t}(h),p={preventDefault:!0,only:null,polyfill:!1,get:function(e,t){return t.hasOwnProperty(e)?t[e]:this[e]},set:function(e){for(var t in e)e.hasOwnProperty(t)&&this.hasOwnProperty(t)&&"get"!=t&&"set"!=t&&(this[t]=e[t])}},y={hasRun:!1,forPressure:!1,type:!1,mobile:"ontouchstart"in t,didFail:function(){this.hasRun=!0,this.forPressure=!1},didSucceed:function(e){this.hasRun=!0,this.forPressure=!0,this.type=e}},v=function(e,s){var n=arguments.length<=2||void 0===arguments[2]?{}:arguments[2];if("string"==typeof e||e instanceof String)for(var i=t.querySelectorAll(e),o=0;o<i.length;o++)new l(i[o],s,n);else if(b(e))new l(e,s,n);else for(var o=0;o<e.length;o++)new l(e[o],s,n)},b=function(e){return"object"===("undefined"==typeof HTMLElement?"undefined":r(HTMLElement))?e instanceof HTMLElement:e&&"object"===("undefined"==typeof e?"undefined":r(e))&&null!==e&&1===e.nodeType&&"string"==typeof e.nodeName},P=function(e,t,s){e.hasOwnProperty(t)&&e[t].apply(s||this,Array.prototype.slice.call(arguments,3))},k=function(e,t,s,n,i){return(e-t)*(i-n)/(s-t)+n}}("undefined"!=typeof window?window:!1,"undefined"!=typeof window?window.document:!1);