angular-pressure
Version:
pressure.js support for Angular.js applications
2 lines • 7.42 kB
JavaScript
// Pressure v2.0.0 | Created By Stuart Yamartino | MIT License | 2015 - 2016
!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 r=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}}(),o="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){p(e,t,s)},config:function(e){f.set(e)},map:function(e,t,s,n,i){return b.apply(null,arguments)}};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 h=u;define(["pressure"],function(e){return e})}else if("object"===("undefined"==typeof module?"undefined":o(module))&&module.exports){var h=u;module.exports=h}else e.Pressure=u;var l=function(){function e(t,s,n){i(this,e),this.routeEvents(t,s,n),this.preventSelect(t,n)}return r(e,[{key:"routeEvents",value:function(e,t,s){var n=f.get("only",s);!v||"desktop"!==n&&"mobile"===n?!P||"mobile"!==n&&"desktop"===n?this.adapter=new c(e,t).bindUnsupportedEvent():this.adapter=new d(e,t,s).bindEvents():this.adapter=new a(e,t,s).bindEvents()}},{key:"preventSelect",value:function(e,t){f.get("preventSelect",t)&&(e.style.webkitTouchCallout="none",e.style.webkitUserSelect="none",e.style.khtmlUserSelect="none",e.style.MozUserSelect="none",e.style.msUserSelect="none",e.style.userSelect="none")}}]),e}(),c=function(){function e(t,s,n){i(this,e),this.el=t,this.block=s,this.options=n,this.pressed=!1,this.deepPressed=!1,this.runKey=Math.random()}return r(e,[{key:"setPressed",value:function(e){this.pressed=e}},{key:"setDeepPressed",value:function(e){this.deepPressed=e}},{key:"isPressed",value:function(){return this.pressed}},{key:"isDeepPressed",value:function(){return this.deepPressed}},{key:"add",value:function(e,t){this.el.addEventListener(e,t,!1)}},{key:"runClosure",value:function(e){e in this.block&&this.block[e].apply(this.el,Array.prototype.slice.call(arguments,1))}},{key:"fail",value:function(e,t){f.get("polyfill",this.options)?this.runKey===t&&this.runPolyfill(e):this.runClosure("unsupported",e)}},{key:"bindUnsupportedEvent",value:function(){var e=this;this.add(P?"touchstart":"mousedown",function(t){return e.runClosure("unsupported",t)})}},{key:"_startPress",value:function(e){this.isPressed()===!1&&(this.setPressed(!0),this.runClosure("start",e))}},{key:"_startDeepPress",value:function(e){this.isPressed()&&this.isDeepPressed()===!1&&(this.setDeepPressed(!0),this.runClosure("startDeepPress",e))}},{key:"_endDeepPress",value:function(){this.isPressed()&&this.isDeepPressed()&&(this.setDeepPressed(!1),this.runClosure("endDeepPress"))}},{key:"_endPress",value:function(){this.isPressed()&&(this._endDeepPress(),this.setPressed(!1),this.runClosure("end")),this.runKey=Math.random()}},{key:"runPolyfill",value:function(e){this.increment=10/f.get("polyfillSpeed",this.options),this.setPressed(!0),this.runClosure("start",e),this.loopPolyfillForce(0,e)}},{key:"loopPolyfillForce",value:function(e,t){this.isPressed()&&(this.runClosure("change",e,t),e>=.5?this._startDeepPress(t):this._endDeepPress(),e=e+this.increment>1?1:e+this.increment,setTimeout(this.loopPolyfillForce.bind(this,e,t),10))}}]),e}(),a=function(e){function t(e,n,r){return i(this,t),s(this,Object.getPrototypeOf(t).call(this,e,n,r))}return n(t,e),r(t,[{key:"bindEvents",value:function(){this.add("webkitmouseforcewillbegin",this._startPress.bind(this)),this.add("mousedown",this.support.bind(this)),this.add("webkitmouseforcechanged",this.change.bind(this)),this.add("webkitmouseforcedown",this._startDeepPress.bind(this)),this.add("webkitmouseforceup",this._endDeepPress.bind(this)),this.add("mouseleave",this._endPress.bind(this)),this.add("mouseup",this._endPress.bind(this))}},{key:"support",value:function(e){this.isPressed()===!1&&this.fail(e,this.runKey)}},{key:"change",value:function(e){this.isPressed()&&e.webkitForce>0&&this.runClosure("change",this.normalizeForce(e.webkitForce),e)}},{key:"normalizeForce",value:function(e){return this.reachOne(b(e,1,3,0,1))}},{key:"reachOne",value:function(e){return e>.995?1:e}}]),t}(c),d=function(e){function t(e,n,r){return i(this,t),s(this,Object.getPrototypeOf(t).call(this,e,n,r))}return n(t,e),r(t,[{key:"bindEvents",value:function(){m?(this.add("touchforcechange",this.start.bind(this)),this.add("touchstart",this.supportTest.bind(this,0)),this.add("touchend",this._endPress.bind(this))):(this.add("touchstart",this.startLegacyTest.bind(this)),this.add("touchend",this._endPress.bind(this)))}},{key:"start",value:function(e){e.touches.length>0&&(this._startPress(e),this.runClosure("change",this.selectTouch(e).force,e))}},{key:"supportTest",value:function(e,t){var s=arguments.length<=2||void 0===arguments[2]?this.runKey:arguments[2];this.isPressed()===!1&&(6>=e?(e++,setTimeout(this.supportTest.bind(this,e,t,s),10)):this.fail(t,s))}},{key:"startLegacyTest",value:function(e){this.initialForce=e.touches[0].force,this.supportLegacyTest(0,e,this.runKey,this.initialForce)}},{key:"supportLegacyTest",value:function(e,t,s,n){n!==this.initialForce?(this._startPress(t),this.loopForce(t)):6>=e?(e++,setTimeout(this.supportLegacyTest.bind(this,e,t,s,n),10)):this.fail(t,s)}},{key:"loopForce",value:function(e){this.isPressed()&&(this.touch=this.selectTouch(e),setTimeout(this.loopForce.bind(this,e),10),this.runClosure("change",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}(c),f={polyfill:!0,polyfillSpeed:1e3,preventSelect:!0,only:null,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])}},p=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),r=0;r<i.length;r++)new l(i[r],s,n);else if(y(e))new l(e,s,n);else for(var r=0;r<e.length;r++)new l(e[r],s,n)},y=function(e){return"object"===("undefined"==typeof HTMLElement?"undefined":o(HTMLElement))?e instanceof HTMLElement:e&&"object"===("undefined"==typeof e?"undefined":o(e))&&null!==e&&1===e.nodeType&&"string"==typeof e.nodeName},b=function(e,t,s,n,i){return(e-t)*(i-n)/(s-t)+n},v="ontouchstart"in t==!1,P="ontouchstart"in t==!0,m="ontouchforcechange"in t}("undefined"!=typeof window?window:!1,"undefined"!=typeof window?window.document:!1);