Skip to content

Commit b5ff9eb

Browse files
committed
extract statement from into a separate functions
1 parent 1b5f4c9 commit b5ff9eb

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

layouts/shortcodes/heatmap.html

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@
143143
postsByDate.set(date, data);
144144
{{- end -}}
145145

146+
years = Array.from(years).sort().reverse();
146147
return [postsByDate, years];
147148
}
148149

@@ -162,6 +163,21 @@
162163
myChart.setOption(option);
163164
}
164165

166+
function renderYearSelector(years){
167+
// Populate year selector
168+
var yearSelector = document.getElementById('yearSelector');
169+
years.forEach(year => {
170+
var option = document.createElement('option');
171+
option.value = year;
172+
option.text = year;
173+
yearSelector.appendChild(option);
174+
});
175+
176+
yearSelector.addEventListener('change', function() {
177+
updateHeatmap(this.value);
178+
});
179+
}
180+
165181

166182
// Watch for theme changes
167183
const observer = new MutationObserver((mutations) => {
@@ -179,23 +195,8 @@
179195
});
180196

181197
var option;
182-
//var postsByDate = new Map();
183-
//var years = new Set();
184-
var [postsByDate, years] = populatePostData();
185-
186-
// Populate year selector
187-
years = Array.from(years).sort().reverse();
188-
var yearSelector = document.getElementById('yearSelector');
189-
years.forEach(year => {
190-
var option = document.createElement('option');
191-
option.value = year;
192-
option.text = year;
193-
yearSelector.appendChild(option);
194-
});
195-
196-
yearSelector.addEventListener('change', function() {
197-
updateHeatmap(this.value);
198-
});
198+
const [postsByDate, years] = populatePostData();
199+
const latestYear = years && years.length > 0 ? years[0]: new Date().getFullYear();
199200

200201
option = {
201202
title: { show: false },
@@ -259,10 +260,12 @@
259260
type: 'heatmap',
260261
coordinateSystem: 'calendar',
261262
calendarIndex: 0,
262-
data: getHeatmapData(years[0])
263+
data: getHeatmapData(latestYear)
263264
}
264265
};
265266

267+
renderYearSelector(years);
268+
266269
// Initial chart initialization
267270
initOrUpdateChart();
268271
</script>

0 commit comments

Comments
 (0)