浏览代码

fix graph time select

marek 1 月之前
父节点
当前提交
9bc89deb59
共有 3 个文件被更改,包括 42 次插入33 次删除
  1. 24 23
      routes/include/history.js
  2. 9 5
      templates/en/history/property.eta
  3. 9 5
      templates/sk/history/property.eta

+ 24 - 23
routes/include/history.js

@@ -13,14 +13,15 @@ export default (langName, lang) => new Elysia({ prefix: `/history` })
     const dateMap = Meteostanica.getDateMap()
     
     const years = Object.keys(dateMap)
-    const months = Object.keys(dateMap[years[years.length - 1]])
-    const days = dateMap[years[years.length - 1]][months[months.length - 1]]
+    const selectedYear = years.find(i => i === year) ?? years[years.length - 1]
 
-    const lastYear = years[years.length - 1]
-    const lastMonth = months[months.length - 1]
-    const lastDay = days[days.length - 1]
+    const months = Object.keys(dateMap[selectedYear])
+    const selectedMonth = months.find(i => i === month) ?? months[months.length - 1]
 
-    const data = Meteostanica.getDataPropertyDaily(property, `${year ?? lastYear}-${month ?? lastMonth}-${day ?? lastDay}`)
+    const days = dateMap[selectedYear][selectedMonth]
+    const selectedDay = days.find(i => i === day) ?? days[days.length - 1]
+
+    const data = Meteostanica.getDataPropertyDaily(property, `${selectedYear}-${selectedMonth}-${selectedDay}`)
 
     if (!data) {
         set.headers['content-type'] = 'text/html; charset=utf8'
@@ -28,20 +29,21 @@ export default (langName, lang) => new Elysia({ prefix: `/history` })
     }
 
     set.headers['content-type'] = 'text/html; charset=utf8'
-    return eta.render(`${langName}/history/property`, { lang, dateMap: { years, months, days, raw: dateMap }, type: `daily`, property, data })
+    return eta.render(`${langName}/history/property`, { lang, selected: { day: selectedDay, month: selectedMonth, year: selectedYear }, dateMap: { years, months, days, raw: dateMap }, type: `daily`, property, data })
   })
   .get(`/:property/daily`, ({ params: { property }, query: { day, month, year }, set }) => {
     const dateMap = Meteostanica.getDateMap()
     
     const years = Object.keys(dateMap)
-    const months = Object.keys(dateMap[years[years.length - 1]])
-    const days = dateMap[years[years.length - 1]][months[months.length - 1]]
+    const selectedYear = years.find(i => i === year) ?? years[years.length - 1]
+
+    const months = Object.keys(dateMap[selectedYear])
+    const selectedMonth = months.find(i => i === month) ?? months[months.length - 1]
 
-    const lastYear = years[years.length - 1]
-    const lastMonth = months[months.length - 1]
-    const lastDay = days[days.length - 1]
+    const days = dateMap[selectedYear][selectedMonth]
+    const selectedDay = days.find(i => i === day) ?? days[days.length - 1]
 
-    const data = Meteostanica.getDataPropertyDaily(property, `${year ?? lastYear}-${month ?? lastMonth}-${day ?? lastDay}`)
+    const data = Meteostanica.getDataPropertyDaily(property, `${selectedYear}-${selectedMonth}-${selectedDay}`)
 
     if (!data) {
         set.headers['content-type'] = 'text/html; charset=utf8'
@@ -49,18 +51,18 @@ export default (langName, lang) => new Elysia({ prefix: `/history` })
     }
 
     set.headers['content-type'] = 'text/html; charset=utf8'
-    return eta.render(`${langName}/history/property`, { lang, dateMap: { years, months, days, raw: dateMap }, type: `daily`, property, data })
+    return eta.render(`${langName}/history/property`, { lang, selected: { day: selectedDay, month: selectedMonth, year: selectedYear }, dateMap: { years, months, days, raw: dateMap }, type: `daily`, property, data })
   })
   .get(`/:property/monthly`, ({ params: { property }, query: { month, year }, set }) => {    
     const dateMap = Meteostanica.getDateMap()
     
     const years = Object.keys(dateMap)
-    const months = Object.keys(dateMap[years[years.length - 1]])
+    const selectedYear = years.find(i => i === year) ?? years[years.length - 1]
 
-    const lastYear = years[years.length - 1]
-    const lastMonth = months[months.length - 1]
+    const months = Object.keys(dateMap[selectedYear])
+    const selectedMonth = months.find(i => i === month) ?? months[months.length - 1]
 
-    const data = Meteostanica.getDataPropertyMonthly(property, `${year ?? lastYear}-${month ?? lastMonth}`)
+    const data = Meteostanica.getDataPropertyMonthly(property, `${selectedYear}-${selectedMonth}`)
 
     if (!data) {
         set.headers['content-type'] = 'text/html; charset=utf8'
@@ -68,16 +70,15 @@ export default (langName, lang) => new Elysia({ prefix: `/history` })
     }
 
     set.headers['content-type'] = 'text/html; charset=utf8'
-    return eta.render(`${langName}/history/property`, { lang, dateMap: { years, months, raw: dateMap }, type: `monthly`, property, data })
+    return eta.render(`${langName}/history/property`, { lang, selected: { month: selectedMonth, year: selectedYear }, dateMap: { years, months, raw: dateMap }, type: `monthly`, property, data })
   })
   .get(`/:property/yearly`, ({ params: { property }, query: { year }, set }) => {
     const dateMap = Meteostanica.getDateMap()
     
     const years = Object.keys(dateMap)
+    const selectedYear = years.find(i => i === year) ?? years[years.length - 1]
 
-    const lastYear = years[years.length - 1]
-
-    const data = Meteostanica.getDataPropertyYearly(property, year ?? lastYear)
+    const data = Meteostanica.getDataPropertyYearly(property, selectedYear)
 
     if (!data) {
         set.headers['content-type'] = 'text/html; charset=utf8'
@@ -85,7 +86,7 @@ export default (langName, lang) => new Elysia({ prefix: `/history` })
     }
 
     set.headers['content-type'] = 'text/html; charset=utf8'
-    return eta.render(`${langName}/history/property`, { lang, dateMap: { years, raw: dateMap }, type: `yearly`, property, data })
+    return eta.render(`${langName}/history/property`, { lang, selected: { year: selectedYear }, dateMap: { years, raw: dateMap }, type: `yearly`, property, data })
   })
   .get(`/:property/allTime`, ({ params: { property }, set }) => {
     const data = Meteostanica.getDataPropertyAllTime(property)

+ 9 - 5
templates/en/history/property.eta

@@ -27,7 +27,7 @@
             <label for="day">day</label>
             <select id="day" name="day">
                 <% for (const item of it?.dateMap?.days) { %>
-                    <option value="<%= item %>" <%= item === it?.dateMap?.days[it?.dateMap?.days.length - 1] ? `selected` : `` %> ><%= item %></option>
+                    <option value="<%= item %>" <%= item === it?.selected?.day ? `selected` : `` %> ><%= item %></option>
                 <% } %>
             </select>
             </div>
@@ -38,7 +38,7 @@
             <label for="month">month</label>
             <select id="month" name="month">
                 <% for (const item of it?.dateMap?.months) { %>
-                    <option value="<%= item %>" <%= item === it?.dateMap?.months[it?.dateMap?.months.length - 1] ? `selected` : `` %> > <%= it?.lang?.history.dateFormats.months?.[item]() %></option>
+                    <option value="<%= item %>" <%= item === it?.selected?.month ? `selected` : `` %> > <%= it?.lang?.history.dateFormats.months?.[item]() %></option>
                 <% } %>
             </select>
             </div>
@@ -48,7 +48,7 @@
             <label for="year">year</label>
             <select id="year" name="year">
                 <% for (const item of it?.dateMap?.years) { %>
-                    <option value="<%= item %>" <%= item === it?.dateMap?.years[it?.dateMap?.years.length - 1] ? `selected` : `` %> > <%= item %></option>
+                    <option value="<%= item %>" <%= item === it?.selected?.year ? `selected` : `` %> > <%= item %></option>
                 <% } %>
             </select>
         </div>
@@ -92,8 +92,12 @@
     if (e.target.id === "year") monthSelect.textContent = ''
 
     const years = Object.keys(dateMap)
-    const months = Object.keys(dateMap[years.find(i => i === yearSelect.value) ?? years[years.length - 1]])
-    const days = dateMap[years.find(i => i === yearSelect.value) ?? years[years.length - 1]][months.find(i => i === monthSelect?.value) ?? months[months.length - 1]]
+    const selectedYear = years.find(i => i === yearSelect.value) ?? years[years.length - 1]
+
+    const months = Object.keys(dateMap[selectedYear])
+    const selectedMonth = months.find(i => i === monthSelect.value) ?? months[months.length - 1]
+
+    const days = dateMap[selectedYear][selectedMonth]
 
     for (const day of days) {
         const option = document.createElement('option')

+ 9 - 5
templates/sk/history/property.eta

@@ -27,7 +27,7 @@
             <label for="day">deň</label>
             <select id="day" name="day">
                 <% for (const item of it?.dateMap?.days) { %>
-                    <option value="<%= item %>" <%= item === it?.dateMap?.days[it?.dateMap?.days.length - 1] ? `selected` : `` %> ><%= item %></option>
+                    <option value="<%= item %>" <%= item === it?.selected?.day ? `selected` : `` %> ><%= item %></option>
                 <% } %>
             </select>
             </div>
@@ -38,7 +38,7 @@
             <label for="month">mesiac</label>
             <select id="month" name="month">
                 <% for (const item of it?.dateMap?.months) { %>
-                    <option value="<%= item %>" <%= item === it?.dateMap?.months[it?.dateMap?.months.length - 1] ? `selected` : `` %> > <%= it?.lang?.history.dateFormats.months?.[item]() %></option>
+                    <option value="<%= item %>" <%= item === it?.selected?.month ? `selected` : `` %> > <%= it?.lang?.history.dateFormats.months?.[item]() %></option>
                 <% } %>
             </select>
             </div>
@@ -48,7 +48,7 @@
             <label for="year">rok</label>
             <select id="year" name="year">
                 <% for (const item of it?.dateMap?.years) { %>
-                    <option value="<%= item %>" <%= item === it?.dateMap?.years[it?.dateMap?.years.length - 1] ? `selected` : `` %> > <%= item %></option>
+                    <option value="<%= item %>" <%= item === it?.selected?.year ? `selected` : `` %> > <%= item %></option>
                 <% } %>
             </select>
         </div>
@@ -92,8 +92,12 @@
     if (e.target.id === "year") monthSelect.textContent = ''
 
     const years = Object.keys(dateMap)
-    const months = Object.keys(dateMap[years.find(i => i === yearSelect.value) ?? years[years.length - 1]])
-    const days = dateMap[years.find(i => i === yearSelect.value) ?? years[years.length - 1]][months.find(i => i === monthSelect?.value) ?? months[months.length - 1]]
+    const selectedYear = years.find(i => i === yearSelect.value) ?? years[years.length - 1]
+
+    const months = Object.keys(dateMap[selectedYear])
+    const selectedMonth = months.find(i => i === monthSelect.value) ?? months[months.length - 1]
+
+    const days = dateMap[selectedYear][selectedMonth]
 
     for (const day of days) {
         const option = document.createElement('option')