2022-07-11 18:05:50 -04:00
|
|
|
import Controller from "@ember/controller";
|
|
|
|
import { action } from "@ember/object";
|
2022-07-12 18:27:03 -04:00
|
|
|
import loadScript from "discourse/lib/load-script";
|
|
|
|
import { tableToObj } from "../lib/utilities";
|
2022-07-11 18:05:50 -04:00
|
|
|
|
|
|
|
export default class extends Controller {
|
2022-07-12 18:27:03 -04:00
|
|
|
onShow() {
|
|
|
|
// ? TODO move to component (read about not allowing Controllers to do DOM manipulation)
|
|
|
|
this._super(...arguments);
|
|
|
|
|
|
|
|
loadScript(settings.theme_uploads.importabular).then(() => {
|
|
|
|
this.buildTable(this.tableHtml);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
buildTable(table) {
|
|
|
|
const tableObject = tableToObj(table);
|
|
|
|
const headings = [];
|
|
|
|
const tableData = [];
|
|
|
|
tableObject.forEach((object) => {
|
|
|
|
// Build Headings
|
|
|
|
if (!headings.includes(...Object.keys(object))) {
|
|
|
|
headings.push(...Object.keys(object));
|
|
|
|
}
|
|
|
|
|
2022-07-13 14:38:33 -04:00
|
|
|
// Build Table Data
|
2022-07-12 18:27:03 -04:00
|
|
|
tableData.push([...Object.values(object)]);
|
|
|
|
});
|
|
|
|
|
|
|
|
const columns = headings.map((heading) => {
|
|
|
|
return {
|
|
|
|
label: heading,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-unused-vars, no-undef
|
|
|
|
const sheet = new Importabular({
|
|
|
|
node: document.getElementById("table-editor-spreadsheet"),
|
|
|
|
columns,
|
|
|
|
data: tableData,
|
|
|
|
width: "100vw",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-07-11 18:05:50 -04:00
|
|
|
@action
|
|
|
|
cancelTableEdit() {
|
|
|
|
this.send("closeModal");
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
editTable() {
|
|
|
|
// TODO: insert table edit submission logic
|
|
|
|
this.send("closeModal");
|
|
|
|
}
|
|
|
|
}
|