;(function(root, factory){
if(typeof define==='function'&&define.amd){
define([], factory);
}else if(typeof exports==='object'){
module.exports=factory();
}else{
root.Pressure=factory();
}}(this, function(){
"use strict";
function _typeof(obj){ "@babel/helpers - typeof"; if(typeof Symbol==="function"&&typeof Symbol.iterator==="symbol"){ _typeof=function _typeof(obj){ return typeof obj; };}else{ _typeof=function _typeof(obj){ return obj&&typeof Symbol==="function"&&obj.constructor===Symbol&&obj!==Symbol.prototype ? "symbol":typeof obj; };} return _typeof(obj); }
function _inherits(subClass, superClass){ if(typeof superClass!=="function"&&superClass!==null){ throw new TypeError("Super expression must either be null or a function"); } subClass.prototype=Object.create(superClass&&superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true }});if(superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p){ _setPrototypeOf=Object.setPrototypeOf||function _setPrototypeOf(o, p){ o.__proto__=p; return o; }; return _setPrototypeOf(o, p); }
function _createSuper(Derived){ var hasNativeReflectConstruct=_isNativeReflectConstruct(); return function _createSuperInternal(){ var Super=_getPrototypeOf(Derived), result; if(hasNativeReflectConstruct){ var NewTarget=_getPrototypeOf(this).constructor; result=Reflect.construct(Super, arguments, NewTarget); }else{ result=Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); };}
function _possibleConstructorReturn(self, call){ if(call&&(_typeof(call)==="object"||typeof call==="function")){ return call; } return _assertThisInitialized(self); }
function _assertThisInitialized(self){ if(self===void 0){ throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct(){ if(typeof Reflect==="undefined"||!Reflect.construct) return false; if(Reflect.construct.sham) return false; if(typeof Proxy==="function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function (){})); return true; } catch (e){ return false; }}
function _getPrototypeOf(o){ _getPrototypeOf=Object.setPrototypeOf ? Object.getPrototypeOf:function _getPrototypeOf(o){ return o.__proto__||Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _classCallCheck(instance, Constructor){ if(!(instance instanceof Constructor)){ throw new TypeError("Cannot call a class as a function"); }}
function _defineProperties(target, props){ for (var i=0; i < props.length; i++){ var descriptor=props[i]; descriptor.enumerable=descriptor.enumerable||false; descriptor.configurable=true; if("value" in descriptor) descriptor.writable=true; Object.defineProperty(target, descriptor.key, descriptor); }}
function _createClass(Constructor, protoProps, staticProps){ if(protoProps) _defineProperties(Constructor.prototype, protoProps); if(staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
var Pressure={
set: function set(selector, closure, options){
loopPressureElements(selector, closure, options);
},
config: function config(options){
Config.set(options);
},
map: function map(x, in_min, in_max, out_min, out_max){
return _map.apply(null, arguments);
}};
var Element=function (){
function Element(el, block, options){
_classCallCheck(this, Element);
this.routeEvents(el, block, options);
this.preventSelect(el, options);
}
_createClass(Element, [{
key: "routeEvents",
value: function routeEvents(el, block, options){
var type=Config.get('only', options);
if(supportsMouse&&(type==='mouse'||type===null)){
this.adapter=new AdapterForceTouch(el, block, options).bindEvents();
}
else if(supportsPointer&&(type==='pointer'||type===null)){
this.adapter=new AdapterPointer(el, block, options).bindEvents();
}
else if(supportsTouch&&(type==='touch'||type===null)){
this.adapter=new Adapter3DTouch(el, block, options).bindEvents();
}else{
this.adapter=new Adapter(el, block).bindUnsupportedEvent();
}}
}, {
key: "preventSelect",
value: function preventSelect(el, options){
if(Config.get('preventSelect', options)){
el.style.webkitTouchCallout="none";
el.style.webkitUserSelect="none";
el.style.khtmlUserSelect="none";
el.style.MozUserSelect="none";
el.style.msUserSelect="none";
el.style.userSelect="none";
}}
}]);
return Element;
}();
var Adapter=function (){
function Adapter(el, block, options){
_classCallCheck(this, Adapter);
this.el=el;
this.block=block;
this.options=options;
this.pressed=false;
this.deepPressed=false;
this.nativeSupport=false;
this.runningPolyfill=false;
this.runKey=Math.random();
}
_createClass(Adapter, [{
key: "setPressed",
value: function setPressed(_boolean){
this.pressed=_boolean;
}}, {
key: "setDeepPressed",
value: function setDeepPressed(_boolean2){
this.deepPressed=_boolean2;
}}, {
key: "isPressed",
value: function isPressed(){
return this.pressed;
}}, {
key: "isDeepPressed",
value: function isDeepPressed(){
return this.deepPressed;
}}, {
key: "add",
value: function add(event, set){
this.el.addEventListener(event, set, false);
}}, {
key: "runClosure",
value: function runClosure(method){
if(method in this.block){
this.block[method].apply(this.el, Array.prototype.slice.call(arguments, 1));
}}
}, {
key: "fail",
value: function fail(event, runKey){
if(Config.get('polyfill', this.options)){
if(this.runKey===runKey){
this.runPolyfill(event);
}}else{
this.runClosure('unsupported', event);
}}
}, {
key: "bindUnsupportedEvent",
value: function bindUnsupportedEvent(){
var _this=this;
this.add(supportsTouch ? 'touchstart':'mousedown', function (event){
return _this.runClosure('unsupported', event);
});
}}, {
key: "_startPress",
value: function _startPress(event){
if(this.isPressed()===false){
this.runningPolyfill=false;
this.setPressed(true);
this.runClosure('start', event);
}}
}, {
key: "_startDeepPress",
value: function _startDeepPress(event){
if(this.isPressed()&&this.isDeepPressed()===false){
this.setDeepPressed(true);
this.runClosure('startDeepPress', event);
}}
}, {
key: "_changePress",
value: function _changePress(force, event){
this.nativeSupport=true;
this.runClosure('change', force, event);
}}, {
key: "_endDeepPress",
value: function _endDeepPress(){
if(this.isPressed()&&this.isDeepPressed()){
this.setDeepPressed(false);
this.runClosure('endDeepPress');
}}
}, {
key: "_endPress",
value: function _endPress(){
if(this.runningPolyfill===false){
if(this.isPressed()){
this._endDeepPress();
this.setPressed(false);
this.runClosure('end');
}
this.runKey=Math.random();
this.nativeSupport=false;
}else{
this.setPressed(false);
}}
}, {
key: "deepPress",
value: function deepPress(force, event){
force >=0.5 ? this._startDeepPress(event):this._endDeepPress();
}}, {
key: "runPolyfill",
value: function runPolyfill(event){
this.increment=Config.get('polyfillSpeedUp', this.options)===0 ? 1:10 / Config.get('polyfillSpeedUp', this.options);
this.decrement=Config.get('polyfillSpeedDown', this.options)===0 ? 1:10 / Config.get('polyfillSpeedDown', this.options);
this.setPressed(true);
this.runClosure('start', event);
if(this.runningPolyfill===false){
this.loopPolyfillForce(0, event);
}}
}, {
key: "loopPolyfillForce",
value: function loopPolyfillForce(force, event){
if(this.nativeSupport===false){
if(this.isPressed()){
this.runningPolyfill=true;
force=force + this.increment > 1 ? 1:force + this.increment;
this.runClosure('change', force, event);
this.deepPress(force, event);
setTimeout(this.loopPolyfillForce.bind(this, force, event), 10);
}else{
force=force - this.decrement < 0 ? 0:force - this.decrement;
if(force < 0.5&&this.isDeepPressed()){
this.setDeepPressed(false);
this.runClosure('endDeepPress');
}
if(force===0){
this.runningPolyfill=false;
this.setPressed(true);
this._endPress();
}else{
this.runClosure('change', force, event);
this.deepPress(force, event);
setTimeout(this.loopPolyfillForce.bind(this, force, event), 10);
}}
}}
}]);
return Adapter;
}();
var AdapterForceTouch=function (_Adapter){
_inherits(AdapterForceTouch, _Adapter);
var _super=_createSuper(AdapterForceTouch);
function AdapterForceTouch(el, block, options){
_classCallCheck(this, AdapterForceTouch);
return _super.call(this, el, block, options);
}
_createClass(AdapterForceTouch, [{
key: "bindEvents",
value: function bindEvents(){
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 support(event){
if(this.isPressed()===false){
this.fail(event, this.runKey);
}}
}, {
key: "change",
value: function change(event){
if(this.isPressed()&&event.webkitForce > 0){
this._changePress(this.normalizeForce(event.webkitForce), event);
}}
}, {
key: "normalizeForce",
value: function normalizeForce(force){
return this.reachOne(_map(force, 1, 3, 0, 1));
}}, {
key: "reachOne",
value: function reachOne(force){
return force > 0.995 ? 1:force;
}}]);
return AdapterForceTouch;
}(Adapter);
var Adapter3DTouch=function (_Adapter2){
_inherits(Adapter3DTouch, _Adapter2);
var _super2=_createSuper(Adapter3DTouch);
function Adapter3DTouch(el, block, options){
_classCallCheck(this, Adapter3DTouch);
return _super2.call(this, el, block, options);
}
_createClass(Adapter3DTouch, [{
key: "bindEvents",
value: function bindEvents(){
if(supportsTouchForceChange){
this.add('touchforcechange', this.start.bind(this));
this.add('touchstart', this.support.bind(this, 0));
this.add('touchend', this._endPress.bind(this));
}else{
this.add('touchstart', this.startLegacy.bind(this));
this.add('touchend', this._endPress.bind(this));
}}
}, {
key: "start",
value: function start(event){
if(event.touches.length > 0){
this._startPress(event);
this.touch=this.selectTouch(event);
if(this.touch){
this._changePress(this.touch.force, event);
}}
}}, {
key: "support",
value: function support(iter, event){
var runKey=arguments.length > 2&&arguments[2]!==undefined ? arguments[2]:this.runKey;
if(this.isPressed()===false){
if(iter <=6){
iter++;
setTimeout(this.support.bind(this, iter, event, runKey), 10);
}else{
this.fail(event, runKey);
}}
}}, {
key: "startLegacy",
value: function startLegacy(event){
this.initialForce=event.touches[0].force;
this.supportLegacy(0, event, this.runKey, this.initialForce);
}}, {
key: "supportLegacy",
value: function supportLegacy(iter, event, runKey, force){
if(force!==this.initialForce){
this._startPress(event);
this.loopForce(event);
}else if(iter <=6){
iter++;
setTimeout(this.supportLegacy.bind(this, iter, event, runKey, force), 10);
}else{
this.fail(event, runKey);
}}
}, {
key: "loopForce",
value: function loopForce(event){
if(this.isPressed()){
this.touch=this.selectTouch(event);
setTimeout(this.loopForce.bind(this, event), 10);
this._changePress(this.touch.force, event);
}}
}, {
key: "selectTouch",
value: function selectTouch(event){
if(event.touches.length===1){
return this.returnTouch(event.touches[0], event);
}else{
for (var i=0; i < event.touches.length; i++){
if(event.touches[i].target===this.el||this.el.contains(event.touches[i].target)){
return this.returnTouch(event.touches[i], event);
}}
}}
}, {
key: "returnTouch",
value: function returnTouch(touch, event){
this.deepPress(touch.force, event);
return touch;
}}]);
return Adapter3DTouch;
}(Adapter);
var AdapterPointer=function (_Adapter3){
_inherits(AdapterPointer, _Adapter3);
var _super3=_createSuper(AdapterPointer);
function AdapterPointer(el, block, options){
_classCallCheck(this, AdapterPointer);
return _super3.call(this, el, block, options);
}
_createClass(AdapterPointer, [{
key: "bindEvents",
value: function bindEvents(){
this.add('pointerdown', this.support.bind(this));
this.add('pointermove', this.change.bind(this));
this.add('pointerup', this._endPress.bind(this));
this.add('pointerleave', this._endPress.bind(this));
}}, {
key: "support",
value: function support(event){
if(this.isPressed()===false){
if(event.pressure===0||event.pressure===0.5||event.pressure > 1){
this.fail(event, this.runKey);
}else{
this._startPress(event);
this._changePress(event.pressure, event);
}}
}}, {
key: "change",
value: function change(event){
if(this.isPressed()&&event.pressure > 0&&event.pressure!==0.5){
this._changePress(event.pressure, event);
this.deepPress(event.pressure, event);
}}
}]);
return AdapterPointer;
}(Adapter);
var Config={
polyfill: true,
polyfillSpeedUp: 1000,
polyfillSpeedDown: 0,
preventSelect: true,
only: null,
get: function get(option, options){
return options.hasOwnProperty(option) ? options[option]:this[option];
},
set: function set(options){
for (var k in options){
if(options.hasOwnProperty(k)&&this.hasOwnProperty(k)&&k!='get'&&k!='set'){
this[k]=options[k];
}}
}};
var loopPressureElements=function loopPressureElements(selector, closure){
var options=arguments.length > 2&&arguments[2]!==undefined ? arguments[2]:{};
if(typeof selector==='string'||selector instanceof String){
var elements=document.querySelectorAll(selector);
for (var i=0; i < elements.length; i++){
new Element(elements[i], closure, options);
}}else if(isElement(selector)){
new Element(selector, closure, options);
}else{
for (var i=0; i < selector.length; i++){
new Element(selector[i], closure, options);
}}
};
var isElement=function isElement(o){
return (typeof HTMLElement==="undefined" ? "undefined":_typeof(HTMLElement))==="object" ? o instanceof HTMLElement :
o&&_typeof(o)==="object"&&o!==null&&o.nodeType===1&&typeof o.nodeName==="string";
};
var _map=function _map(x, in_min, in_max, out_min, out_max){
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
};
var supportsMouse=false;
var supportsTouch=false;
var supportsPointer=false;
var supportsTouchForce=false;
var supportsTouchForceChange=false;
if(typeof window!=='undefined'){
if(typeof Touch!=='undefined'){
try {
if(Touch.prototype.hasOwnProperty('force')||'force' in new Touch()){
supportsTouchForce=true;
}} catch (e){}}
supportsTouch='ontouchstart' in window.document&&supportsTouchForce;
supportsMouse='onmousemove' in window.document&&'onwebkitmouseforcechanged' in window.document&&!supportsTouch;
supportsPointer='onpointermove' in window.document;
supportsTouchForceChange='ontouchforcechange' in window.document;
}
return Pressure;
}));