Files
discourse-table-builder/javascripts/discourse/controllers/table-editor-modal.js
T

58 lines
1.5 KiB
JavaScript
Raw Normal View History

2022-07-11 15:05:50 -07:00
import Controller from "@ember/controller";
import { action } from "@ember/object";
2022-07-12 15:27:03 -07:00
import loadScript from "discourse/lib/load-script";
import { tableToObj } from "../lib/utilities";
2022-07-11 15:05:50 -07:00
export default class extends Controller {
2022-07-12 15:27:03 -07:00
onShow() {
// ? TODO move to component (read about not allowing Controllers to do DOM manipulation)
this._super(...arguments);
loadScript(settings.theme_uploads.jspreadsheet).then(() => {
2022-07-12 15:27:03 -07:00
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 11:38:33 -07:00
// Build Table Data
2022-07-12 15:27:03 -07:00
tableData.push([...Object.values(object)]);
});
const columns = headings.map((heading) => {
return {
title: heading,
width: "100", // TODO make based on string length?
2022-07-12 15:27:03 -07:00
};
});
this.spreadsheet = jspreadsheet(document.getElementById("spreadsheet"), {
2022-07-12 15:27:03 -07:00
data: tableData,
columns,
2022-07-12 15:27:03 -07:00
});
const originalData = this.spreadsheet.getData();
console.log("Original Data:", originalData);
2022-07-12 15:27:03 -07:00
}
2022-07-11 15:05:50 -07:00
@action
cancelTableEdit() {
this.send("closeModal");
}
@action
editTable() {
// TODO: insert table edit submission logic
console.log("New Data:", this.spreadsheet.getData());
2022-07-11 15:05:50 -07:00
this.send("closeModal");
}
}