/* jshint esversion: 6 */
let $select = $(".multi-select");
$select.on("change-files", (e, files) => {
$(".multi-files-value").val(JSON.stringify(files.map((f) => f.name)));
if (files.length == 0) {
$(".multi-files").html(
`
No files selected`
);
return;
}
$(".multi-files").html(
files
.map((f) => {
const badge = `
${filesize(f.size)}
`;
return `
${htmlEscape(f.name)}
${f.type == "directory" ? `` : badge}
`;
})
.join("")
);
const hasDirectory = files.reduce(
(a, f) => a || f.type == "directory",
false
);
const totalSize = files.map((f) => f.size).reduce((a, b) => a + b);
if (hasDirectory) {
$(".multi-files-total").val("");
} else {
$(".multi-files-total").val(filesize(totalSize));
}
});
const updateSelected = () => {
let $selected = $(".multi-select:checked");
let files = [];
$selected.each((i, ele) => {
files.push({
name: $(ele).data("select"),
type: $(ele).data("select-type"),
size: $(ele).data("select-size"),
});
});
$select.trigger("change-files", [files]);
};
$select.on("change", updateSelected);
updateSelected();