purecounter.js 4.8 KB

12345678
  1. /*
  2. * purecounter.js - A simple yet configurable native javascript counter which you can count on.
  3. * Author: Stig Rex
  4. * Version: 1.1.4
  5. * Url: https://github.com/srexi/purecounterjs
  6. * License: MIT
  7. */
  8. !function(){function a(d,h){var g=Object.keys(d);if(Object.getOwnPropertySymbols){var f=Object.getOwnPropertySymbols(d);h&&(f=f.filter((function(e){return Object.getOwnPropertyDescriptor(d,e).enumerable}))),g.push.apply(g,f)}return g}function c(d,g,f){return g in d?Object.defineProperty(d,g,{value:f,enumerable:!0,configurable:!0,writable:!0}):d[g]=f,d}function b(d,h){for(var g=0;g<h.length;g++){var f=h[g];f.enumerable=f.enumerable||!1,f.configurable=!0,"value" in f&&(f.writable=!0),Object.defineProperty(d,f.key,f)}}new (function(){function f(h){!function(i,j){if(!(i instanceof j)){throw new TypeError("Cannot call a class as a function")}}(this,f),this.defaults={start:0,end:100,duration:2000,delay:10,once:!0,decimals:0,legacy:!0,currency:!1,currencysymbol:!1,separator:!1,separatorsymbol:",",selector:".purecounter"},this.configOptions=Object.assign({},this.defaults,h||{}),this.registerEventListeners()}var d,e,g;return d=f,(e=[{key:"registerEventListeners",value:function(){var h=document.querySelectorAll(this.configOptions.selector);if(this.intersectionListenerSupported()){var i=new IntersectionObserver(this.animateElements.bind(this),{root:null,rootMargin:"20px",threshold:0.5});h.forEach((function(j){i.observe(j)}))}else{window.addEventListener&&(this.animateLegacy(h),window.addEventListener("scroll",(function(j){this.animateLegacy(h)}),{passive:!0}))}}},{key:"animateLegacy",value:function(h){var i=this;h.forEach((function(j){!0===i.parseConfig(j).legacy&&i.elementIsInView(j)&&i.animateElements([j])}))}},{key:"animateElements",value:function(h,j){var i=this;h.forEach((function(l){var o=l.target||l,k=i.parseConfig(o);if(k.duration<=0){return o.innerHTML=i.formatNumber(k.end,k)}if(!j&&!i.elementIsInView(l)||j&&l.intersectionRatio<0.5){var m=k.start>k.end?k.end:k.start;return o.innerHTML=i.formatNumber(m,k)}setTimeout((function(){return i.startCounter(o,k)}),k.delay)}))}},{key:"startCounter",value:function(j,q){var p=this,l=(q.end-q.start)/(q.duration/q.delay),h="inc";q.start>q.end&&(h="dec",l*=-1);var k=this.parseValue(q.start);j.innerHTML=this.formatNumber(k,q),!0===q.once&&j.setAttribute("data-purecounter-duration",0);var m=setInterval((function(){var i=p.nextNumber(k,l,h);j.innerHTML=p.formatNumber(i,q),((k=i)>=q.end&&"inc"==h||k<=q.end&&"dec"==h)&&(j.innerHTML=p.formatNumber(q.end,q),clearInterval(m))}),q.delay)}},{key:"parseConfig",value:function(m){var k=this,h=function(p){for(var o=1;o<arguments.length;o++){var i=null!=arguments[o]?arguments[o]:{};o%2?a(Object(i),!0).forEach((function(n){c(p,n,i[n])})):Object.getOwnPropertyDescriptors?Object.defineProperties(p,Object.getOwnPropertyDescriptors(i)):a(Object(i)).forEach((function(n){Object.defineProperty(p,n,Object.getOwnPropertyDescriptor(i,n))}))}return p}({},this.configOptions),j=[].filter.call(m.attributes,(function(i){return/^data-purecounter-/.test(i.name)})),l={};return j.forEach((function(i){var o=i.name.replace("data-purecounter-","").toLowerCase(),n="duration"==o?parseInt(1000*k.parseValue(i.value)):k.parseValue(i.value);l[o]=n})),Object.assign(h,l)}},{key:"nextNumber",value:function(h,j){var i=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"inc";return h=this.parseValue(h),j=this.parseValue(j),parseFloat("inc"===i?h+j:h-j)}},{key:"convertToCurrencySystem",value:function(h,k){var j=k.currencysymbol||"",i=k.decimals||1;return j+((h=Math.abs(Number(h)))>=1000000000000?"".concat((h/1000000000000).toFixed(i)," T"):h>=1000000000?"".concat((h/1000000000).toFixed(i)," B"):h>=1000000?"".concat((h/1000000).toFixed(i)," M"):h>=1000?"".concat((h/1000000000000).toFixed(i)," K"):h.toFixed(i))}},{key:"applySeparator",value:function(h,i){return i.separator?h.replace(/(\d)(?=(\d{3})+(?!\d))/g,"$1,").replace(new RegExp(/,/gi,"gi"),i.separatorsymbol):h.replace(new RegExp(/,/gi,"gi"),"")}},{key:"formatNumber",value:function(h,j){var i={minimumFractionDigits:j.decimals,maximumFractionDigits:j.decimals};return h=j.currency?this.convertToCurrencySystem(h,j):parseFloat(h),this.applySeparator(h.toLocaleString(void 0,i),j)}},{key:"parseValue",value:function(h){return/^[0-9]+\.[0-9]+$/.test(h)?parseFloat(h):/^[0-9]+$/.test(h)?parseInt(h):/^true|false/i.test(h)?/^true/i.test(h):h}},{key:"elementIsInView",value:function(i){for(var l=i.offsetTop,k=i.offsetLeft,j=i.offsetWidth,h=i.offsetHeight;i.offsetParent;){l+=(i=i.offsetParent).offsetTop,k+=i.offsetLeft}return l>=window.pageYOffset&&k>=window.pageXOffset&&l+h<=window.pageYOffset+window.innerHeight&&k+j<=window.pageXOffset+window.innerWidth}},{key:"intersectionListenerSupported",value:function(){return"IntersectionObserver" in window&&"IntersectionObserverEntry" in window&&"intersectionRatio" in window.IntersectionObserverEntry.prototype}}])&&b(d.prototype,e),g&&b(d,g),f}())}();