@alzintani/superevents
Version:
JavaScript library that handling any events in websites.
1 lines • 12.1 kB
JavaScript
function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function _defineProperties(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function _createClass(e,t,n){return t&&_defineProperties(e.prototype,t),n&&_defineProperties(e,n),e}function _defineProperty(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function _typeof(e){return(_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}!function(e,t){"use strict";"function"==typeof define&&define.amd?define([],function(){return e.SuperEvents=t.call(e),e.SuperEvents}):"object"===("undefined"==typeof exports?"undefined":_typeof(exports))?module.exports=t.call(e):e.SuperEvents=t.call(e)}("object"===("undefined"==typeof global?"undefined":_typeof(global))?global:this,function(){"use strict";return function(){function e(t){if(_classCallCheck(this,e),_defineProperty(this,"version","0.2.2"),_defineProperty(this,"name","SuperEvents"),_defineProperty(this,"description","SuperEvents is a JavaScript library that handling any events in websites. Support all events action like:- Scrolling, Clicking, Hovering, Mouse Moving, Tapping."),_defineProperty(this,"license","MIT"),_defineProperty(this,"params",{}),"object"!==_typeof(t)||t.length<1)throw new TypeError("You must use correct HTML elements.");this.initialize(t)}return _createClass(e,[{key:"initialize",value:function(e){try{this.eventSource=void 0!==e.source?e.source:window,this.eventTarget=e instanceof Element||e instanceof HTMLDocument||e[0]instanceof Element?e:e.target,this.actions()}catch(e){console.log("ERROR01:",e)}}},{key:"runEvent",value:function(e){this._sourceEvent(e)}},{key:"helper",value:function(){var e=this,t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null;return this.helper.object=t,this.helper.easing=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},n=e.duration,r=e.cubic,o=e.delay;if(t instanceof Element)t.style.transition="all ".concat(n,"s cubic-bezier(").concat(r,")");else if(t[0]instanceof Element){var i=0;Array.prototype.forEach.call(t,function(e){var t=" ".concat(i.toFixed(1),"s");0!==o&&(i+=o),e.style.transition="all ".concat(n,"s").concat(t," cubic-bezier(").concat(r,")")})}},this.helper.css=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=e.helper.object,o=function(e){if(e){if(t&&"object"===_typeof(t))for(var r in t)e.style[r]=t[r];if(n&&"object"===_typeof(n))for(var o in n)e.style[o]=n[o]}};return r[0]instanceof Element?Array.prototype.forEach.call(r,function(e){o(e)}):r instanceof Element&&o(r),e.helper},this.helper.addClass=function(t){if("string"!=typeof t)return e.helper;var n=e.helper.object;return"function"==typeof n.forEach?Array.prototype.forEach.call(n,function(e){e.classList&&e.classList.add(t)}):n.classList&&n.classList.add(t),e.helper},this.helper.removeClass=function(t){if("string"!=typeof t)return e.helper;var n=e.helper.object;return"function"==typeof n.forEach?Array.prototype.forEach.call(n,function(e){e.classList&&e.classList.remove(t)}):n.classList&&n.classList.remove(t),e.helper},this.helper.text=function(t){var n=document.createTextNode(t);return e.helper.object.appendChild(n),e.helper},this.helper.colorCalculater=function(t,n,r){if(t=t.trim(),n=n.trim(),r/=100,0===t.indexOf("#")&&0===n.indexOf("#")||0===t.indexOf("rgb")&&0===n.indexOf("rgb")){0===t.indexOf("#")&&(t=e.helper.hexToRgb(t)),0===n.indexOf("#")&&(n=e.helper.hexToRgb(n));return function(e,t){if(e&&t&&0!==e.indexOf("rgb")||0!==t.indexOf("rgb"))return e;var n=0===e.indexOf("rgba")?5:4,o=0===t.indexOf("rgba")?5:4,i=[];if(e=e.substr(n).split(")")[0].split(","),t=t.substr(o).split(")")[0].split(","),e.length===t.length){var a=0;e.forEach(function(n){i[a]=3!==a?(e[a]-r*e[a]+r*t[a]).toFixed(0):(e[a]-r*e[a]+r*t[a]).toFixed(2),a++})}var c=4===e.length?"rgba":"rgb";return"".concat(c,"(").concat(i.join(","),")")}(t,n)}},this.helper.hexToRgb=function(e){var t=0,n=0,r=0;return 4==e.length?(t="0x"+e[1]+e[1],n="0x"+e[2]+e[2],r="0x"+e[3]+e[3]):7==e.length&&(t="0x"+e[1]+e[2],n="0x"+e[3]+e[4],r="0x"+e[5]+e[6]),"rgb(".concat(t,", ").concat(n,", ").concat(r,")")},this.helper.rgbToHex=function(e){var t=e.indexOf(",")>-1?",":" ",n=(+(e=e.substr(4).split(")")[0].split(t))[0]).toString(16),r=(+e[1]).toString(16),o=(+e[2]).toString(16);return 1==n.length&&(n="0"+n),1==r.length&&(r="0"+r),1==o.length&&(o="0"+o),"#"+n+r+o},this.helper}},{key:"actions",value:function(){var e=this;return this.actions.call=function(t){var n=arguments.length>1&&void 0!==arguments[1]&&arguments[1];if(e.runEvent(function(){var n=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0;"function"==typeof t&&t(n,e.eventSource,e.eventTarget)}),!0===n)return e},this.actions.fromTo=function(t,n){var r,o,i="",a="",c={},s="",l=["translateY","translateX","scale","rotateY","rotateX"],u=["opacity","backgroundPosition","backgroundSize","backgroundColor","color","borderColor"].concat(l,["rotateY","rotateX"],["translateX","translateY"]),f=function(e,t,n){return e-n/100*e+n/100*t};if(Object.keys(t).length)for(var d=function(d){c[d]={},Array.prototype.forEach.call(Object.keys(t),function(p){var h=t[p];void 0!==_typeof(n[p])&&u.indexOf(p)>-1&&t.hasOwnProperty(p)&&n.hasOwnProperty(p)&&(r=n[p],i=(i="function"==typeof h.match?h.match(/\D+$/):"")&&i.length?i[0]:"",o=d<50?h:r,isNaN(h)&&isNaN(r)||(a=f(h,r,d),o=a.toFixed(2)),"borderColor"===p||"color"===p||"backgroundColor"===p?o=e.helper().colorCalculater(h,r,d):(h=parseFloat(h),r=parseFloat(r),a=f(h,r,d),"backgroundPosition"===p?o="50% ".concat(a.toFixed(2)).concat(i):l.indexOf(p)>-1&&(s+=" ".concat(p,"( ").concat(a.toFixed(2)).concat(i," )"))),void 0!==o&&l.indexOf(p)<0&&(c[d][p]=o))}),""!==s&&(c[d].transform=s,s="")},p=0;p<=100;p++)d(p);return e.runEvent(function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0;t=(100*t).toFixed(0),c[t]&&e.helper(e.eventTarget).css(c[t])}),e},this}},{key:"easing",value:function(){var e,t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},n=t.timing||"linear",r=t.duration||0,o=t.delay||0;if(r=r>10||r<0?.5:r,e=n,!1===(!!t.cubicBezier||!1))switch(n){case"ease":e=".25, .1, .25, 1";break;case"linear":e=".25, .25, .75, .75";break;case"ease-in":e=".42, 0, 1, 1";break;case"ease-out":e="0, 0, .58, 1";break;case"ease-in-out":e=".42, 0, .58, 1";break;case"easeInQuad":e=".55, .085, .68, .53";break;case"easeInCubic":e=".55, .055, .675, .19";break;case"easeInQuart":e=".895, .03, .685, .22";break;case"easeInQuint":e=".755, .05, .855, .06";break;case"easeInSine":e=".47, 0, .745, .715";break;case"easeInExpo":e=".95, .05, .795, .035";break;case"easeInCirc":e=".6, .04, .98, .335";break;case"easeInBack":e=".6, -.280, .735, .045";break;case"easeOutQuad":e=".25, .46, .45, .94";break;case"easeOutCubic":e=".215, .61, .355, 1";break;case"easeOutQuart":e=".165, .84, .44, 1";break;case"easeOutQuint":e=".23, 1, .32, 1";break;case"easeOutSine":e=".39, .575, .565, 1";break;case"easeOutExpo":e=".19, 1, .22, 1";break;case"easeOutCirc":e=".075, .82, .165, 1";break;case"easeOutBack":e=".175, .885, .32, 1.275";break;case"easeInOutQuad":e=".455, .03, .515, .955";break;case"easeInOutCubic":e=".645, .045, .355, 1";break;case"easeInOutQuart":e=".770, 0, .175, 1";break;case"easeInOutQuint":e=".86, 0, .07, 1";break;case"easeInOutSine":e=".445, .05, 0.55, 0.95";break;case"easeInOutExpo":e="1, 0, 0, 1";break;case"easeInOutCirc":e=".785, .135, .15, .86";break;case"easeInOutBack":e=".68, -.55, .265, 1.55";break;default:e=".25, .25, .75, .75"}return e&&this.helper(this.eventTarget).easing({duration:r,cubic:e,delay:o}),this}},{key:"scroll",value:function(){var e=this,t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},n=!1;this._sourceEvent=function(e){document.addEventListener(t.listener&&"load"!==t.listener?t.listener:"DOMContentLoaded",function(){return r(e)}),window.addEventListener("load",a),window.addEventListener("resize",a)};var r=function(e){a(),i(e)},o=function(t){var n=e.params._sourceScrolling,r=n.indicator,o=n.runnigArea,i=n.start,a=(("number"==typeof e.eventSource.scrollY?e.eventSource.scrollY:e.eventSource.scrollTop)+r-i)/o;a>1?a=1:a<0&&(a=0),t(a.toFixed(5))},i=function(t){o(t);!n&&e.eventSource.addEventListener("scroll",function(){var r=e.params._sourceScrolling,i=r.indicator,a=r.start,c=r.end,s=("number"==typeof e.eventSource.scrollY?e.eventSource.scrollY:e.eventSource.scrollTop)+i;s<a?t(0):s>c?t(1):o(t),n=!0},!0)},a=function(){var n,r="number"==typeof e.eventSource.innerHeight?e.eventSource.innerHeight:e.eventSource.offsetHeight,o=e.eventTarget instanceof Element?e.eventTarget:e.eventTarget[0],i=(o.getBoundingClientRect(),c(null!==(n=t.indicator)&&void 0!==n?n:90)),a=c(isNaN(parseInt(t.duration))?0:t.duration),l=o.offsetTop,u=l+o.offsetHeight,f=isNaN(parseInt(t.duration))?u+r:l+a;e.params._sourceScrolling={indicator:i,runnigArea:f-l,start:l,end:f},!0===t.debug&&s()},c=function(t){var n="number"==typeof e.eventSource.innerHeight?e.eventSource.innerHeight:e.eventSource.offsetHeight;return t=(t=Math.abs(parseInt(t)))<=100?t:0,Math.round(t/100*n)},s=function(){var e=document.querySelectorAll(".suprescrolling-debug");e&&Array.prototype.forEach.call(e,function(e){e.parentNode.removeChild(e)}),l(),u()},l=function(){var t=e.params._sourceScrolling,n=t.indicator,r=(t.duration,document.createElement("HR")),o=document.body;e.helper(r).addClass("suprescrolling-debug").css({textAlign:"left",position:"absolute",color:"#FF0000",border:"0",borderBottom:"1px solid #FF0000",zIndex:"99999",width:"95%",opacity:"0.9",left:"0",height:"20px",padding:"0 10px",fontSize:"14px"},{top:n-20+"px"}).text("Indicator"),void 0!==e.eventSource.parentNode?(o=e.eventSource.parentNode,e.helper(o).css({position:"relative"})):e.helper(r).css({position:"fixed"}),o.appendChild(r)},u=function(){var t={textAlign:"left",position:"absolute",width:"50px",border:"none",borderTop:"1px solid #FF0000",zIndex:"99999",opacity:"0.7",margin:"0",padding:"0",color:"#FF0000",left:"50px",top:"0",fontSize:"0.8rem"},n=document.body,r=document.createElement("HR"),o=document.createElement("HR"),i=e.params._sourceScrolling.start,a=e.params._sourceScrolling.end;e.helper(r).addClass("suprescrolling-debug").css(t,{top:"".concat(i,"px")}).text("Start"),e.helper(o).addClass("suprescrolling-debug").css(t,{top:"".concat(a,"px")}).text("End"),(n=e.eventTarget instanceof Element?e.eventTarget.parentNode:e.eventTarget[0].parentNode).appendChild(r),n.appendChild(o)};return this.actions}},{key:"click",value:function(){var e=this,t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};return this._sourceEvent=function(n){var r=void 0===t.toggle||t.toggle;e._click_progress=1,n(0);e.eventSource.addEventListener("click",function(){n(e._click_progress),r&&(e._click_progress=+!e._click_progress)})},this.actions}},{key:"hover",value:function(){var e=this;arguments.length>0&&void 0!==arguments[0]&&arguments[0];return this._sourceEvent=function(t){t(0),e.eventSource.addEventListener("mouseover",function(){return t(1)}),e.eventSource.addEventListener("mouseleave",function(){return t(0)})},this.actions}},{key:"mousemove",value:function(){var e=this,t=(arguments.length>0&&void 0!==arguments[0]&&arguments[0],function(t,n){var r=window.innerWidth/2,o=window.innerHeight/2,i=t.clientX,a=t.clientY;if(e.eventSource&&"function"==typeof e.eventSource.getBoundingClientRect){var c=e.eventSource.getBoundingClientRect();r=c.width/2,o=c.height/2,i=t.clientX-c.left,a=t.clientY-c.top}var s=(r-i)/r,l=(o-a)/o;n((s=s<0?1+s:1-s)<(l=l<0?1+l:1-l)?s:l)});return this._sourceEvent=function(n){e.eventSource.addEventListener("mousemove",function(e){return t(e,n)}),e.eventSource.addEventListener("mouseleave",function(){return n(0)}),n(0)},this.actions}}]),e}()});