/**
* Table Sort Heading
*
* @param list $columns - list of maps/str to generate headings
* @param uri $uri - base uri (default: page.uri)
* @param map $params - query params to preserve
* @param str $default - default field to sort by
* @param bool $enabled - allow sorting (default: true)
*/
var uri = $uri ?? page.uri;
var params = $params ?? {};
var columns = $columns ?? [];
var sortEnabled = $enabled ?? true;
var sortKey = $key ?? "sortby";
var sortField = $current ?? __request.args[sortKey] ?? $default ?? "";
// determine the current sorting information
var sortDesc = string.startswith(sortField, "-");
if (sortDesc) {
let sortField = string.substr(sortField, 1);
}
// generate column widths
<colgroup>
foreach (var column in columns) {
<col class=("col-" .. __count .. " " .. column.class) width=(column.width ?? "") />
}
</colgroup>
<thead>
<tr>
foreach (var column in columns) {
var headingClass = column.class;
// auto-classing
let headingClass ..= " sortable col-" .. __count;
let headingClass ..= (sortEnabled ? "" : " ui-state-disabled");
<th class=(headingClass) title=(column.label)>
var sortClass = column.sortclass;
if (sortEnabled && column.field) {
// sortable column
var text = column.label;
var columnSortField = column.field;
var columnSortType = string.startswith(columnSortField, "-") ? "-" : "";
if (columnSortType == "-") {
let columnSortField = substr(columnSortField, 1);
}
// check if we are sorting by this column
if (columnSortField == sortField) {
let columnSortType = sortDesc ? "" : "-";
let sortClass ..= sortDesc ? " sort-desc" : " sort-asc";
}
// generate the sort heading
var columnParams = params .. {(sortKey): (columnSortType .. columnSortField)};
<a class=(sortClass) href=(uri & columnParams)>text;</a>
} else {
if (!column.field) {
let sortClass ..= " nosort";
}
<span class=(sortClass)><span>column.label</span></span>;
}
</th>
}
</tr>
</thead>