weather_status-weather-status.mjs 13 KB

12
  1. /*! third party licenses: dist/vendor.LICENSE.txt */
  2. import{b$ as i,bR as o,v as w,b as m,_ as g,w as _,u as f,l as y,x as v,cd as x,aV as c}from"./core-common.mjs";import"./chunks/index-PaKKd09k.mjs";import{z as s}from"./chunks/_plugin-vue2_normalizer-VrK6B12S-BQkexw0P.mjs";import{m as A}from"./chunks/index-DaZEPb_2.mjs";import{n as L,P as d,X as k}from"./chunks/icons-TElqpmA8.mjs";const C=async(e,a)=>{const r=i("apps/weather_status/api/v1/location");return(await o.put(r,{address:"",lat:e,lon:a})).data.ocs.data},M=async e=>{const a=i("apps/weather_status/api/v1/location");return(await o.put(a,{address:e,lat:null,lon:null})).data.ocs.data},b=async e=>{const a=i("apps/weather_status/api/v1/mode");return(await o.put(a,{mode:e})).data.ocs.data},T=async()=>{const e=i("apps/weather_status/api/v1/use-personal");return(await o.put(e)).data.ocs.data},W=async()=>{const e=i("apps/weather_status/api/v1/location");return(await o.get(e)).data.ocs.data},F=async()=>{const e=i("apps/weather_status/api/v1/forecast");return(await o.get(e)).data.ocs.data},I=async()=>{const e=i("apps/weather_status/api/v1/favorites");return(await o.get(e)).data.ocs.data},l=async e=>{const a=i("apps/weather_status/api/v1/favorites");return(await o.put(a,{favorites:e})).data.ocs.data},u=1,n=2,h={clearsky_day:{icon:"icon-clearsky-day",text:(e,a,r=!1)=>r?t("weather_status","{temperature} {unit} clear sky later today",{temperature:e,unit:a}):t("weather_status","{temperature} {unit} clear sky",{temperature:e,unit:a})},clearsky_night:{icon:"icon-clearsky-night",text:(e,a,r=!1)=>r?t("weather_status","{temperature} {unit} clear sky later today",{temperature:e,unit:a}):t("weather_status","{temperature} {unit} clear sky",{temperature:e,unit:a})},cloudy:{icon:"icon-cloudy",text:(e,a,r=!1)=>r?t("weather_status","{temperature} {unit} cloudy later today",{temperature:e,unit:a}):t("weather_status","{temperature} {unit} cloudy",{temperature:e,unit:a})},fair_day:{icon:"icon-fair-day",text:(e,a,r=!1)=>r?t("weather_status","{temperature} {unit} fair weather later today",{temperature:e,unit:a}):t("weather_status","{temperature} {unit} fair weather",{temperature:e,unit:a})},fair_night:{icon:"icon-fair-night",text:(e,a,r=!1)=>r?t("weather_status","{temperature} {unit} fair weather later today",{temperature:e,unit:a}):t("weather_status","{temperature} {unit} fair weather",{temperature:e,unit:a})},partlycloudy_day:{icon:"icon-partlycloudy-day",text:(e,a,r=!1)=>r?t("weather_status","{temperature} {unit} partly cloudy later today",{temperature:e,unit:a}):t("weather_status","{temperature} {unit} partly cloudy",{temperature:e,unit:a})},partlycloudy_night:{icon:"icon-partlycloudy-night",text:(e,a,r=!1)=>r?t("weather_status","{temperature} {unit} partly cloudy later today",{temperature:e,unit:a}):t("weather_status","{temperature} {unit} partly cloudy",{temperature:e,unit:a})},fog:{icon:"icon-fog",text:(e,a,r=!1)=>r?t("weather_status","{temperature} {unit} foggy later today",{temperature:e,unit:a}):t("weather_status","{temperature} {unit} foggy",{temperature:e,unit:a})},lightrain:{icon:"icon-lightrain",text:(e,a,r=!1)=>r?t("weather_status","{temperature} {unit} light rainfall later today",{temperature:e,unit:a}):t("weather_status","{temperature} {unit} light rainfall",{temperature:e,unit:a})},rain:{icon:"icon-rain",text:(e,a,r=!1)=>r?t("weather_status","{temperature} {unit} rainfall later today",{temperature:e,unit:a}):t("weather_status","{temperature} {unit} rainfall",{temperature:e,unit:a})},heavyrain:{icon:"icon-heavyrain",text:(e,a,r=!1)=>r?t("weather_status","{temperature} {unit} heavy rainfall later today",{temperature:e,unit:a}):t("weather_status","{temperature} {unit} heavy rainfall",{temperature:e,unit:a})},rainshowers_day:{icon:"icon-rainshowers-day",text:(e,a,r=!1)=>r?t("weather_status","{temperature} {unit} rainfall showers later today",{temperature:e,unit:a}):t("weather_status","{temperature} {unit} rainfall showers",{temperature:e,unit:a})},rainshowers_night:{icon:"icon-rainshowers-night",text:(e,a,r=!1)=>r?t("weather_status","{temperature} {unit} rainfall showers later today",{temperature:e,unit:a}):t("weather_status","{temperature} {unit} rainfall showers",{temperature:e,unit:a})},lightrainshowers_day:{icon:"icon-light-rainshowers-day",text:(e,a,r=!1)=>r?t("weather_status","{temperature} {unit} light rainfall showers later today",{temperature:e,unit:a}):t("weather_status","{temperature} {unit} light rainfall showers",{temperature:e,unit:a})},lightrainshowers_night:{icon:"icon-light-rainshowers-night",text:(e,a,r=!1)=>r?t("weather_status","{temperature} {unit} light rainfall showers later today",{temperature:e,unit:a}):t("weather_status","{temperature} {unit} light rainfall showers",{temperature:e,unit:a})},heavyrainshowers_day:{icon:"icon-heavy-rainshowers-day",text:(e,a,r=!1)=>r?t("weather_status","{temperature} {unit} heavy rainfall showers later today",{temperature:e,unit:a}):t("weather_status","{temperature} {unit} heavy rainfall showers",{temperature:e,unit:a})},heavyrainshowers_night:{icon:"icon-heavy-rainshowers-night",text:(e,a,r=!1)=>r?t("weather_status","{temperature} {unit} heavy rainfall showers later today",{temperature:e,unit:a}):t("weather_status","{temperature} {unit} heavy rainfall showers",{temperature:e,unit:a})}},N={name:"App",components:{IconStar:d,NcActions:w,NcActionButton:m,NcActionCaption:g,NcActionInput:_,NcActionLink:f,NcActionSeparator:y,NcActionText:v},data(){return{locale:x(),loading:!0,errorMessage:"",mode:u,address:null,lat:null,lon:null,offset:5,forecasts:[],loop:null,favorites:[]}},computed:{useFahrenheitLocale(){return["en_US","en_MH","en_FM","en_PW","en_KY","en_LR"].includes(this.locale)},temperatureUnit(){return this.useFahrenheitLocale?"°F":"°C"},locationText(){return t("weather_status","More weather for {adr}",{adr:this.address})},temperature(){return this.getTemperature(this.forecasts,0)},futureTemperature(){return this.getTemperature(this.forecasts,this.offset)},weatherCode(){return this.getWeatherCode(this.forecasts,0)},futureWeatherCode(){return this.getWeatherCode(this.forecasts,this.offset)},weatherIcon(){return this.getWeatherIcon(this.weatherCode,this.loading)},futureWeatherIcon(){return this.getWeatherIcon(this.futureWeatherCode,this.loading)},currentWeatherMessage(){return this.loading?t("weather_status","Loading weather"):this.errorMessage?this.errorMessage:this.getWeatherMessage(this.weatherCode,this.temperature)},forecastMessage(){return this.loading?t("weather_status","Loading weather"):this.getWeatherMessage(this.futureWeatherCode,this.futureTemperature,!0)},weatherLinkTarget(){return"https://www.windy.com/-Rain-thunder-rain?rain,"+this.lat+","+this.lon+",11"},gotWeather(){return this.address&&!this.errorMessage},addRemoveFavoriteIcon(){return this.currentAddressIsFavorite?d:k},addRemoveFavoriteText(){return this.currentAddressIsFavorite?t("weather_status","Remove from favorites"):t("weather_status","Add as favorite")},currentAddressIsFavorite(){return this.favorites.find(e=>e===this.address)}},mounted(){this.initWeatherStatus()},methods:{async initWeatherStatus(){try{const e=await W();this.lat=e.lat,this.lon=e.lon,this.address=e.address,this.mode=e.mode,this.mode===u?this.askBrowserLocation():this.mode===n&&this.startLoop();const a=await I();this.favorites=a}catch(e){if((e==null?void 0:e.code)==="ECONNABORTED"){console.info("The weather status request was cancelled because the user navigates.");return}e.response&&e.response.status===401?s(t("weather_status","You are not logged in.")):s(t("weather_status","There was an error getting the weather status information.")),console.error(e)}},startLoop(){clearInterval(this.loop),this.lat&&this.lon?(this.loop=setInterval(()=>this.getForecast(),60*1e3*60),this.getForecast()):this.loading=!1},askBrowserLocation(){this.loading=!0,this.errorMessage="",navigator.geolocation&&window.isSecureContext?navigator.geolocation.getCurrentPosition(e=>{console.debug("browser location success"),this.lat=e.coords.latitude,this.lon=e.coords.longitude,this.saveMode(u),this.mode=u,this.saveLocation(this.lat,this.lon)},e=>{console.debug("location permission refused"),console.debug(e),this.saveMode(n),this.mode=n,this.lat&&this.lon?this.startLoop():this.usePersonalAddress()}):(console.debug("no secure context!"),this.saveMode(n),this.mode=n,this.startLoop())},async getForecast(){try{this.forecasts=await F()}catch(e){this.errorMessage=t("weather_status","No weather information found"),console.debug(e)}this.loading=!1},async setAddress(e){this.loading=!0,this.errorMessage="";try{const a=await M(e);a.success?(this.lat=a.lat,this.lon=a.lon,this.address=a.address,this.mode=n,this.startLoop()):(this.errorMessage=t("weather_status","Location not found"),this.loading=!1)}catch(a){a.response&&a.response.status===401?s(t("weather_status","You are not logged in.")):s(t("weather_status","There was an error setting the location address.")),this.loading=!1}},async saveLocation(e,a){try{const r=await C(e,a);this.address=r.address,this.startLoop()}catch(r){r.response&&r.response.status===401?s(t("weather_status","You are not logged in.")):s(t("weather_status","There was an error setting the location.")),console.debug(r)}},async saveMode(e){try{await b(e)}catch(a){a.response&&a.response.status===401?s(t("weather_status","You are not logged in.")):s(t("weather_status","There was an error saving the mode.")),console.debug(a)}},onBrowserLocationClick(){this.askBrowserLocation()},async usePersonalAddress(){this.loading=!0;try{const e=await T();this.lat=e.lat,this.lon=e.lon,this.address=e.address,this.mode=n,this.startLoop()}catch(e){e.response&&e.response.status===401?s(t("weather_status","You are not logged in.")):s(t("weather_status","There was an error using personal address.")),console.debug(e),this.loading=!1}},onAddressSubmit(){const e=this.$refs.addressInput.$el.querySelector('input[type="text"]').value;this.setAddress(e)},getLocalizedTemperature(e){return this.useFahrenheitLocale?e*(9/5)+32:e},onAddRemoveFavoriteClick(){const e=this.currentAddressIsFavorite;if(e){const a=this.favorites.indexOf(e);a!==-1&&this.favorites.splice(a,1)}else this.favorites.push(this.address);l(this.favorites)},onFavoriteClick(e,a){if(e.target.classList.contains("action-button__icon")){const r=this.favorites.indexOf(a);r!==-1&&this.favorites.splice(r,1),l(this.favorites)}else a!==this.address&&this.setAddress(a)},formatTime(e){return A(e).format("LT")},getTemperature(e,a=0){return e.length>a?e[a].data.instant.details.air_temperature:""},getWeatherCode(e,a=0){return e.length>a?e[a].data.next_1_hours.summary.symbol_code:""},getWeatherIcon(e,a){return a?"icon-loading-small":"icon-weather "+(e&&e in h?h[e].icon:"icon-fair-day")},getWeatherMessage(e,a,r=!1){return e&&e in h?h[e].text(Math.round(this.getLocalizedTemperature(a)),this.temperatureUnit,r):t("weather_status","Set location for weather")}}};var S=function(){var e=this,a=e._self._c;return a("div",{attrs:{id:"weather-status-menu-item"}},[a("NcActions",{staticClass:"weather-status-menu-item__subheader",attrs:{"default-icon":e.weatherIcon,"aria-hidden":!0,"aria-label":e.currentWeatherMessage,"menu-name":e.currentWeatherMessage}},[e.gotWeather?a("NcActionText",{attrs:{"aria-hidden":!0,icon:e.futureWeatherIcon}},[e._v(" "+e._s(e.forecastMessage)+" ")]):e._e(),e.gotWeather?a("NcActionLink",{attrs:{icon:"icon-address",target:"_blank","aria-hidden":!0,href:e.weatherLinkTarget,"close-after-click":!0}},[e._v(" "+e._s(e.locationText)+" ")]):e._e(),e.gotWeather?a("NcActionButton",{attrs:{"aria-hidden":!0},on:{click:e.onAddRemoveFavoriteClick},scopedSlots:e._u([{key:"icon",fn:function(){return[a(e.addRemoveFavoriteIcon,{tag:"component",staticClass:"favorite-color",attrs:{size:20}})]},proxy:!0}],null,!1,1785206719)},[e._v(" "+e._s(e.addRemoveFavoriteText)+" ")]):e._e(),e.address&&!e.errorMessage?a("NcActionSeparator"):e._e(),a("NcActionButton",{attrs:{icon:"icon-crosshair","close-after-click":!0,"aria-hidden":!0},on:{click:e.onBrowserLocationClick}},[e._v(" "+e._s(e.t("weather_status","Detect location"))+" ")]),a("NcActionInput",{ref:"addressInput",attrs:{label:e.t("weather_status","Set custom address"),disabled:!1,icon:"icon-rename","aria-hidden":!0,type:"text",value:""},on:{submit:e.onAddressSubmit}}),e.favorites.length>0?[a("NcActionCaption",{attrs:{name:e.t("weather_status","Favorites")}}),e._l(e.favorites,function(r){return a("NcActionButton",{key:r,attrs:{"aria-hidden":!0},on:{click:function(p){return e.onFavoriteClick(p,r)}},scopedSlots:e._u([{key:"icon",fn:function(){return[a("IconStar",{class:{"favorite-color":e.address===r},attrs:{size:20}})]},proxy:!0}],null,!0)},[e._v(" "+e._s(r)+" ")])})]:e._e()],2)],1)},B=[],R=L(N,S,B,!1,null,null);const O=R.exports;c.prototype.t=t,document.addEventListener("DOMContentLoaded",function(){OCA.Dashboard&&OCA.Dashboard.registerStatus("weather",e=>{const a=c.extend(O);return new a().$mount(e)})});