uikit
Version:
UIkit is a lightweight and modular front-end framework for developing fast and powerful web interfaces.
2 lines (1 loc) • 1.72 kB
JavaScript
/*! UIkit 3.21.0 | https://www.getuikit.com | (c) 2014 - 2024 YOOtheme | MIT License */(function(t,s){typeof exports=="object"&&typeof module<"u"?module.exports=s(require("uikit-util")):typeof define=="function"&&define.amd?define("uikitcountdown",["uikit-util"],s):(t=typeof globalThis<"u"?globalThis:t||self,t.UIkitCountdown=s(t.UIkit.util))})(this,function(t){"use strict";var s={connected(){t.addClass(this.$el,this.$options.id)}};const d=["days","hours","minutes","seconds"];var r={mixins:[s],props:{date:String,clsWrapper:String,role:String},data:{date:"",clsWrapper:".uk-countdown-%unit%",role:"timer"},connected(){t.attr(this.$el,"role",this.role),this.date=t.toFloat(Date.parse(this.$props.date)),this.end=!1,this.start()},disconnected(){this.stop()},events:{name:"visibilitychange",el:()=>document,handler(){document.hidden?this.stop():this.start()}},methods:{start(){this.stop(),this.update(),this.timer||(t.trigger(this.$el,"countdownstart"),this.timer=setInterval(this.update,1e3))},stop(){this.timer&&(clearInterval(this.timer),t.trigger(this.$el,"countdownstop"),this.timer=null)},update(){const o=a(this.date);o.total||(this.stop(),this.end||(t.trigger(this.$el,"countdownend"),this.end=!0));for(const e of d){const i=t.$(this.clsWrapper.replace("%unit%",e),this.$el);if(!i)continue;let n=Math.trunc(o[e]).toString().padStart(2,"0");i.textContent!==n&&(n=n.split(""),n.length!==i.children.length&&t.html(i,n.map(()=>"<span></span>").join("")),n.forEach((h,p)=>i.children[p].textContent=h))}}}};function a(o){const e=Math.max(0,o-Date.now())/1e3;return{total:e,seconds:e%60,minutes:e/60%60,hours:e/60/60%24,days:e/60/60/24}}return typeof window<"u"&&window.UIkit&&window.UIkit.component("countdown",r),r});