index.js 3.4 KB

1
  1. "use strict";function _typeof(t){return(_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function _classCallCheck(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function _defineProperties(t,e){for(var i=0;i<e.length;i++){var n=e[i];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,_toPropertyKey(n.key),n)}}function _createClass(t,e,i){return e&&_defineProperties(t.prototype,e),i&&_defineProperties(t,i),Object.defineProperty(t,"prototype",{writable:!1}),t}function _toPropertyKey(t){t=_toPrimitive(t,"string");return"symbol"==_typeof(t)?t:String(t)}function _toPrimitive(t,e){if("object"!=_typeof(t)||!t)return t;var i=t[Symbol.toPrimitive];if(void 0===i)return("string"===e?String:Number)(t);i=i.call(t,e||"default");if("object"!=_typeof(i))return i;throw new TypeError("@@toPrimitive must return a primitive value.")}Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=void 0;var Countdown=function(){function i(){var t=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{},e=1<arguments.length&&void 0!==arguments[1]?arguments[1]:getCurrentPages()[getCurrentPages().length-1];_classCallCheck(this,i),Object.assign(this,{page:e,options:t}),this.__init()}return _createClass(i,[{key:"__init",value:function(){this.setData=this.page.setData.bind(this.page),this.restart(this.options)}},{key:"setDefaults",value:function(){return{date:"June 7, 2087 15:03:25",refresh:1e3,offset:0,onEnd:function(){},render:function(t){}}}},{key:"mergeOptions",value:function(t){var e,i=this.setDefaults();for(e in i)i.hasOwnProperty(e)&&(this.options[e]=(void 0!==t[e]?t:i)[e],"date"===e&&"object"!==_typeof(this.options.date)&&(this.options.date=new Date(this.options.date)),"function"==typeof this.options[e])&&(this.options[e]=this.options[e].bind(this));"object"!==_typeof(this.options.date)&&(this.options.date=new Date(this.options.date))}},{key:"getDiffDate",value:function(){var t=(this.options.date.getTime()-Date.now()+this.options.offset)/1e3,e={years:0,days:0,hours:0,min:0,sec:0,millisec:0};return t<=0?this.interval&&(this.stop(),this.options.onEnd()):(31557600<=t&&(e.years=Math.floor(t/31557600),t-=365.25*e.years*86400),86400<=t&&(e.days=Math.floor(t/86400),t-=86400*e.days),3600<=t&&(e.hours=Math.floor(t/3600),t-=3600*e.hours),60<=t&&(e.min=Math.floor(t/60),t-=60*e.min),e.sec=Math.round(t),e.millisec=t%1*1e3),e}},{key:"leadingZeros",value:function(t){var e=1<arguments.length&&void 0!==arguments[1]?arguments[1]:2;return(t=String(t)).length>e?t:(Array(e+1).join("0")+t).substr(-e)}},{key:"update",value:function(t){return this.options.date="object"!==_typeof(t)?new Date(t):t,this.render(),this}},{key:"stop",value:function(){return this.interval&&(clearInterval(this.interval),this.interval=!1),this}},{key:"render",value:function(){return this.options.render(this.getDiffDate()),this}},{key:"start",value:function(){var t=this;return!this.interval&&(this.render(),this.options.refresh&&(this.interval=setInterval(function(){t.render()},this.options.refresh)),this)}},{key:"updateOffset",value:function(t){return this.options.offset=t,this}},{key:"restart",value:function(){return this.mergeOptions(0<arguments.length&&void 0!==arguments[0]?arguments[0]:{}),this.interval=!1,this.start(),this}}]),i}(),_default=exports.default=Countdown;