function dataInput({
value = [],
initialValue = undefined,
accept = "",
delimiter = ",",
format = "auto"
} = {}) {
if (initialValue !== undefined && value.length === 0) {
value = initialValue;
}
const processFile = (data) => {
if (format === "auto") {
try {
return loadJSON(data, delimiter);
} catch {
console.log("Failed with json");
}
try {
return loadMongoExport(data);
} catch {
console.log("Failed with MongoExport");
}
try {
return loadCSVAutoType(data, delimiter);
} catch {
console.log("Failed with CSV AutoType");
}
try {
return loadCSV(data);
} catch {
console.log("Failed with CSV");
}
return value;
} else {
switch (format.toUpperCase()) {
case "JSON":
try {
return loadJSON(data, delimiter);
} catch {
console.log("Failed with json");
return initialValue;
}
case "MONGOEXPORT":
try {
return loadMongoExport(data);
} catch {
console.log("Failed with MongoExport");
return value;
}
case "CSV":
try {
return loadCSVAutoType(data, delimiter);
} catch {
console.log("Failed with CSV AutoType");
return value;
}
case "CSVNOAUTO":
try {
return loadCSV(data);
} catch {
console.log("Failed with CSV");
return value;
}
}
}
};
const form = html`<form><input name=i type="file" accept="${accept}">`;
form.i.onchange = async () => {
form.value = await Files.text(
form.i.multiple ? form.i.files : form.i.files[0]
).then(processFile);
form.dispatchEvent(new Event("input"), { bubbles: true });
};
form.value = value;
return form;
}