You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

184 lines
8.0 KiB
Go

package pages
import (
"bytes"
"html/template"
"faculty_media_report/dbi"
)
var dashboardTmpl = template.Must(template.New("dashboard").Parse(`<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dashboard — Maurer School of Law</title>
<link rel="stylesheet" href="https://unpkg.com/rivet-core@2/css/rivet.min.css">
</head>
<body>
<header class="rvt-header-wrapper">
<a class="rvt-header-wrapper__skip-link" href="#main-content">Skip to main content</a>
<div class="rvt-header-global">
<div class="rvt-container-xl">
<div class="rvt-header-global__inner">
<div class="rvt-header-global__logo-slot">
<a class="rvt-lockup" href="/">
<div class="rvt-lockup__tab">
<svg xmlns="http://www.w3.org/2000/svg" class="rvt-lockup__trident" viewBox="0 0 28 34">
<path d="M-3.34344e-05 4.70897H8.83308V7.174H7.1897V21.1426H10.6134V2.72321H8.83308V0.121224H18.214V2.65476H16.2283V21.1426H19.7889V7.174H18.214V4.64047H27.0471V7.174H25.0614V23.6761L21.7746 26.8944H16.2967V30.455H18.214V33.8787H8.76463V30.592H10.6819V26.8259H5.20403L1.91726 23.6077V7.174H-3.34344e-05V4.70897Z" fill="currentColor"></path>
</svg>
</div>
<div class="rvt-lockup__body">
<span class="rvt-lockup__title">Maurer School of Law</span>
<span class="rvt-lockup__subtitle">Faculty Public Activity Report</span>
</div>
</a>
</div>
</div>
</div>
</div>
</header>
<main id="main-content" class="rvt-container-xl rvt-p-top-xxl rvt-p-bottom-xxl">
<h1 class="rvt-ts-xxl rvt-m-bottom-xxl">Activity Report Dashboard</h1>
{{if .}}
<div class="rvt-m-bottom-lg">
<button class="rvt-button rvt-button--secondary" type="button" id="download-csv">Download CSV Report</button>
</div>
<div class="rvt-m-bottom-md">
<label class="rvt-label" for="table-search">Search</label>
<input class="rvt-input" type="search" id="table-search" placeholder="Filter results...">
</div>
<div style="overflow-x:auto;">
<table class="rvt-table rvt-table--cells" id="report-table">
<thead>
<tr>
<th scope="col" data-col="0" style="cursor:pointer;white-space:nowrap;">Date Submitted <span class="sort-ind">&#8597;</span></th>
<th scope="col" data-col="1" style="cursor:pointer;white-space:nowrap;">Name <span class="sort-ind">&#8597;</span></th>
<th scope="col">Title</th>
<th scope="col">Description</th>
<th scope="col">Hyperlink</th>
<th scope="col" data-col="5" style="cursor:pointer;white-space:nowrap;">Type <span class="sort-ind">&#8597;</span></th>
</tr>
</thead>
<tbody>
{{range .}}
<tr>
<td>{{.Created}}</td>
<td>{{.LastName}}, {{.FirstName}}</td>
<td>{{.Title}}</td>
<td>{{.Description}}</td>
<td>{{if .Hyperlink}}<a href="{{.Hyperlink}}" target="_blank" rel="noopener">Link</a>{{end}}</td>
<td>{{.ItemType}}</td>
</tr>
{{end}}
</tbody>
</table>
</div>
{{else}}
<p class="rvt-ts-lg">No reported items.</p>
{{end}}
</main>
<script>
(function() {
var table = document.getElementById('report-table');
var allRows = table ? Array.prototype.slice.call(table.querySelectorAll('tbody tr')) : [];
var sortState = {col: -1, dir: 1};
function getCellText(row, col) {
var cells = row.querySelectorAll('td');
return cells[col] ? cells[col].textContent.trim() : '';
}
function apply() {
var term = (document.getElementById('table-search').value || '').toLowerCase();
var rows = allRows.filter(function(row) {
if (!term) return true;
return Array.prototype.some.call(row.querySelectorAll('td'), function(cell) {
return cell.textContent.toLowerCase().indexOf(term) !== -1;
});
});
if (sortState.col >= 0) {
rows.sort(function(a, b) {
var av = getCellText(a, sortState.col).toLowerCase();
var bv = getCellText(b, sortState.col).toLowerCase();
return av < bv ? -sortState.dir : av > bv ? sortState.dir : 0;
});
}
var tbody = table.querySelector('tbody');
while (tbody.firstChild) tbody.removeChild(tbody.firstChild);
rows.forEach(function(r) { tbody.appendChild(r); });
}
var searchInput = document.getElementById('table-search');
if (searchInput) searchInput.addEventListener('input', apply);
if (table) {
Array.prototype.forEach.call(table.querySelectorAll('th[data-col]'), function(th) {
th.addEventListener('click', function() {
var col = parseInt(th.getAttribute('data-col'), 10);
sortState.dir = sortState.col === col ? sortState.dir * -1 : 1;
sortState.col = col;
Array.prototype.forEach.call(table.querySelectorAll('.sort-ind'), function(ind) { ind.textContent = '⇕'; });
th.querySelector('.sort-ind').textContent = sortState.dir === 1 ? '↑' : '↓';
apply();
});
});
}
var dlBtn = document.getElementById('download-csv');
if (dlBtn) {
dlBtn.addEventListener('click', function() {
var now = new Date();
var p = function(n) { return n < 10 ? '0' + n : '' + n; };
var stamp = '' + now.getFullYear() + p(now.getMonth()+1) + p(now.getDate()) + p(now.getHours()) + p(now.getMinutes()) + p(now.getSeconds());
function esc(val) {
val = val || '';
if (val.indexOf('|') !== -1 || val.indexOf('"') !== -1 || val.indexOf('\n') !== -1) {
return '"' + val.replace(/"/g, '""') + '"';
}
return val;
}
var lines = ['sep=|'];
lines.push(['Date Submitted', 'Name', 'Title', 'Description', 'Hyperlink', 'Type'].map(esc).join('|'));
Array.prototype.forEach.call(table.querySelectorAll('tbody tr'), function(row) {
var cells = row.querySelectorAll('td');
var fields = [];
Array.prototype.forEach.call(cells, function(cell, i) {
if (i === 4) {
var a = cell.querySelector('a');
fields.push(esc(a ? a.href : ''));
} else {
fields.push(esc(cell.textContent.trim()));
}
});
lines.push(fields.join('|'));
});
var csv = lines.join('\r\n');
var blob = new Blob([csv], {type: 'text/csv;charset=utf-8;'});
var url = URL.createObjectURL(blob);
var a = document.createElement('a');
a.href = url;
a.download = 'Faculty_Activity_Report-' + stamp + '.csv';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
});
}
})();
</script>
</body>
</html>`))
func DashboardPage(items []dbi.DashboardItem) (string, error) {
var buf bytes.Buffer
if err := dashboardTmpl.Execute(&buf, items); err != nil {
return "", err
}
return buf.String(), nil
}