Skip to content

Instantly share code, notes, and snippets.

@kjk
Created February 11, 2020 09:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kjk/e933c2e9cb52a6b5eed7226cff031b88 to your computer and use it in GitHub Desktop.
Save kjk/e933c2e9cb52a6b5eed7226cff031b88 to your computer and use it in GitHub Desktop.
sumatra crash preview with alpine.js (made with https://codeeval.dev)
<!doctype html>
<httml>
<head>
<style>
html, body {
font-family: monospace;
font-size: 10pt;
}
</style>
<script>
const allCrashes = {{.Crashes}};
function len(o) {
return o.length;
}
let crashesData = null;
/*
{
days: [],
crashesByDay: {},
};
*/
// returns [[CrahsInfo], [CrashInfo]]
function groupCrashesByDay() {
let crashesByDay = {};
for (let crash of allCrashes) {
const day = crash.Day;
const a = crashesByDay[day] || [];
a.push(crash);
crashesByDay[day] = a;
}
let days = Object.keys(crashesByDay);
days.sort();
days = days.reverse();
let res = {
days: days,
crashesByDay: crashesByDay,
}
return res;
}
function getCrashesData() {
crashesData = crashesData || groupCrashesByDay();
return crashesData;
}
function getCrashesByDay(day) {
return crashesData.crashesByDay[day];
}
</script>
<script src="https://cdn.jsdelivr.net/gh/alpinejs/alpine@v1.10.1/dist/alpine.js" defer></script>
</head>
<body>
<div x-data="getCrashesData()">
<template x-for="day in days">
<div>
<span x-text="day"></span>
<span x-text="len(crashesByDay[day])"></span>
<div x-data="{crashes: getCrashesByDay(day)}">
</div>
</div>
</template>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment