property.eta 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <% layout("/sk/history/layout", { title: `história` }) %>
  2. <%~ include("/sk/partials/topbar") %>
  3. <% const backIcon = `
  4. <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
  5. <path fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="m12 19l-7-7l7-7m7 7H5" />
  6. </svg>
  7. ` %>
  8. <div class="container-row">
  9. <a role="button" href="/history"><%~ backIcon %> história</a>
  10. <h2><%~ it?.lang?.history.properties?.[it?.property]() %></h2>
  11. </div>
  12. <div class="historyLinks">
  13. <a role="button" href="/history/<%= it.property %>/daily">deň</a>
  14. <a role="button" href="/history/<%= it.property %>/monthly">mesiac</a>
  15. <a role="button" href="/history/<%= it.property %>/yearly">rok</a>
  16. <a role="button" href="/history/<%= it.property %>/allTime">celý čas</a>
  17. </div>
  18. <% if (it?.dateMap?.years) { %>
  19. <form id="historyForm">
  20. <% if (it?.dateMap?.days) { %>
  21. <div>
  22. <label for="day">deň</label>
  23. <select id="day" name="day">
  24. <% for (const item of it?.dateMap?.days) { %>
  25. <option value="<%= item %>" <%= item === it?.selected?.day ? `selected` : `` %> ><%= item %></option>
  26. <% } %>
  27. </select>
  28. </div>
  29. <% } %>
  30. <% if (it?.dateMap?.months) { %>
  31. <div>
  32. <label for="month">mesiac</label>
  33. <select id="month" name="month">
  34. <% for (const item of it?.dateMap?.months) { %>
  35. <option value="<%= item %>" <%= item === it?.selected?.month ? `selected` : `` %> > <%= it?.lang?.history.dateFormats.months?.[item]() %></option>
  36. <% } %>
  37. </select>
  38. </div>
  39. <% } %>
  40. <div>
  41. <label for="year">rok</label>
  42. <select id="year" name="year">
  43. <% for (const item of it?.dateMap?.years) { %>
  44. <option value="<%= item %>" <%= item === it?.selected?.year ? `selected` : `` %> > <%= item %></option>
  45. <% } %>
  46. </select>
  47. </div>
  48. <button type="submit">načítať</button>
  49. </form>
  50. <% } %>
  51. <% const time = [] %>
  52. <% const data = [] %>
  53. <% for (const item of it.data) { %>
  54. <% time.push(item.timeMark) %>
  55. <% if (it.property === "outdoorConnected") {%>
  56. <% data.push(item.value) %>
  57. <% continue %>
  58. <% } %>
  59. <% data.push(item.value / 100) %>
  60. <% } %>
  61. <div>
  62. <canvas id="historyChart"></canvas>
  63. </div>
  64. <script>
  65. const dateMap = <%~ JSON.stringify(it?.dateMap.raw, null, 2) %>
  66. const monthLang = <%~ serializeToCode(it?.lang?.general.dateFormats.months) %>
  67. const daySelect = document.querySelector("#historyForm select#day")
  68. const monthSelect = document.querySelector("#historyForm select#month")
  69. const yearSelect = document.querySelector("#historyForm select#year")
  70. monthSelect.addEventListener('change', resetHistoryForm)
  71. yearSelect.addEventListener('change', resetHistoryForm)
  72. function resetHistoryForm(e) {
  73. daySelect.textContent = ''
  74. if (e.target.id === "year") monthSelect.textContent = ''
  75. const years = Object.keys(dateMap)
  76. const selectedYear = years.find(i => i === yearSelect.value) ?? years[years.length - 1]
  77. const months = Object.keys(dateMap[selectedYear])
  78. const selectedMonth = months.find(i => i === monthSelect.value) ?? months[months.length - 1]
  79. const days = dateMap[selectedYear][selectedMonth]
  80. for (const day of days) {
  81. const option = document.createElement('option')
  82. option.textContent = day
  83. option.value = day
  84. daySelect.append(option)
  85. }
  86. if (e.target.id === "year") {
  87. for (const [key, value] of Object.entries(months)) {
  88. const option = document.createElement('option')
  89. option.textContent = monthLang[key]()
  90. option.value = key
  91. monthSelect.append(option)
  92. }
  93. }
  94. }
  95. const ctx = document.querySelector('#historyChart');
  96. new Chart(ctx, {
  97. type: 'bar',
  98. data: {
  99. labels: <%~ JSON.stringify(time, null, 2) %>,
  100. datasets: [{
  101. label: `<%= it.property %>`,
  102. data: <%~ JSON.stringify(data, null, 2) %>,
  103. borderWidth: 1
  104. }]
  105. },
  106. options: {
  107. scales: {
  108. y: {
  109. beginAtZero: true
  110. }
  111. }
  112. }
  113. });
  114. </script>
  115. <% function serializeToCode(obj) { %>
  116. <% // Handle null/primitive types %>
  117. <% if (obj === null) return 'null'; %>
  118. <% if (typeof obj === 'string') return `'${obj.replace(/'/g, "\\'")}'`; %>
  119. <% if (typeof obj !== 'object') return obj.toString(); %>
  120. <% // Handle Arrays %>
  121. <% if (Array.isArray(obj)) { %>
  122. <% return `[${obj.map(item => serializeToCode(item)).join(',')}]`; %>
  123. <% } %>
  124. <% // Handle Objects %>
  125. <% const entries = Object.entries(obj).map(([key, value]) => { %>
  126. <% // Ensure keys are safe (quoted if they contain special characters) %>
  127. <% const safeKey = /^[a-z$_][a-z0-9$_]*$/i.test(key) ? key : `'${key}'`; %>
  128. <% return `${safeKey}: ${serializeToCode(value)}`; %>
  129. <% }); %>
  130. <% return `{${entries.join(',')}}`; %>
  131. <% } %>