Files
Jump/jumpapp/assets/js/index.bundle.js
2022-02-08 23:05:56 +00:00

1 line
2.9 KiB
JavaScript

(()=>{"use strict";class t{constructor(t=0){this.utcshift=1e3*t,this.shiftedtimestamp=(new Date).getTime()+this.utcshift,this.shifteddate=new Date(this.shiftedtimestamp)}get_formatted_time(){return String(this.shifteddate.getHours()).padStart(2,"0")+":"+String(this.shifteddate.getMinutes()).padStart(2,"0")}get_hour(){return this.shifteddate.getHours()}}class e{constructor(t){this.hour=t.get_hour(),this.greetings={0:"morning",12:"afternoon",16:"evening",19:"night"}}get_greeting(){let t=Object.keys(this.greetings).reverse();for(let e of t)if(this.hour>=e)return this.greetings[e]}}class i{constructor(t,e){this.owmapiurlbase="https://api.openweathermap.org/data/2.5/weather",this.owmapikey=t,this.latlong=e}async fetch_owm_data(){const t=this.owmapiurlbase+"?lat="+this.latlong[0]+"&lon="+this.latlong[1]+"&appid="+this.owmapikey;return await fetch(t).then((t=>t.json())).then((t=>{401===t.cod&&alert("The OWM API key is invalid, check config.php");var e="night";return t.dt>t.sys.sunrise&&t.dt<t.sys.sunset&&(e="day"),{locationcode:t.id,locationname:t.name,iconclass:"wi-owm-"+e+"-"+t.weather[0].id,timezoneshift:t.timezone}}))}}(new class{constructor(){this.owmapikey=null,this.latlong=[],this.storage=window.localStorage,this.updatefrequency=1e4,this.contentintervalid=null,this.timezoneshift=0,this.greetingelm=document.querySelector(".greeting .chosen"),this.holderelm=document.querySelector(".time-weather"),this.timeelm=this.holderelm.querySelector(".time"),this.weatherelm=this.holderelm.querySelector(".weather"),this.clientlocationelm=document.querySelector(".useclientlocation"),JUMP.latlong&&JUMP.owmapikey&&(this.owmapikey=JUMP.owmapikey,this.latlong=JUMP.latlong.split(",")),(this.lastrequestedlocation=this.storage.getItem("lastrequestedlocation"))&&(this.latlong=JSON.parse(this.lastrequestedlocation))}init(){if(!this.owmapikey)return this.refresh_basic_content(),void this.show_content();new i(this.owmapikey,this.latlong).fetch_owm_data().then((t=>{this.timezoneshift=t.timezoneshift,this.refresh_basic_content(),this.holderelm.href+="city/"+t.locationcode,this.weatherelm.classList.add(t.iconclass),this.clientlocationelm.innerHTML=t.locationname,this.clientlocationelm.addEventListener("click",(t=>{navigator.geolocation.getCurrentPosition((t=>{this.latlong=[t.coords.latitude,t.coords.longitude],this.storage.setItem("lastrequestedlocation",JSON.stringify(this.latlong)),this.init()}),null,{enableHighAccuracy:!0})}),{once:!0}),this.clientlocationelm.classList.add("enable"),this.show_content()}))}show_content(){document.querySelectorAll(".hidden").forEach((function(t){t.classList.remove("hidden")}))}update_basic_content(){let i=new t(this.timezoneshift),s=new e(i);this.timeelm.innerHTML=i.get_formatted_time(),this.greetingelm.innerHTML=s.get_greeting()}refresh_basic_content(){this.contentintervalid&&clearInterval(this.contentintervalid),this.update_basic_content(),this.contentintervalid=setInterval((()=>{this.update_basic_content()}),this.updatefrequency)}}).init()})();