2022-07-14 08:56:11 -07:00
|
|
|
// import Controller from "@ember/controller";
|
|
|
|
import { action } from "@ember/object";
|
|
|
|
import loadScript from "discourse/lib/load-script";
|
|
|
|
import { tableToObj } from "../lib/utilities";
|
|
|
|
import Component from "@ember/component";
|
|
|
|
|
|
|
|
export default Component.extend({
|
|
|
|
tagName: "",
|
|
|
|
|
|
|
|
didInsertElement() {
|
|
|
|
this._super(...arguments);
|
|
|
|
|
2022-07-14 09:21:22 -07:00
|
|
|
this.loadLibraries().then(() => {
|
2022-07-14 08:56:11 -07:00
|
|
|
this.buildTable(this.tableHtml);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2022-07-14 09:21:22 -07:00
|
|
|
loadLibraries() {
|
|
|
|
return loadScript(settings.theme_uploads.jsuites).then(() => {
|
|
|
|
return loadScript(settings.theme_uploads.jspreadsheet);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2022-07-14 08:56:11 -07:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build Table Data
|
|
|
|
tableData.push([...Object.values(object)]);
|
|
|
|
});
|
|
|
|
|
|
|
|
const columns = headings.map((heading) => {
|
|
|
|
return {
|
|
|
|
title: heading,
|
2022-07-14 10:05:27 -07:00
|
|
|
width: heading.length * 15,
|
2022-07-14 08:56:11 -07:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
const spreadsheetContainer = document.querySelector("#spreadsheet");
|
|
|
|
|
2022-07-14 09:21:22 -07:00
|
|
|
// eslint-disable-next-line no-undef
|
2022-07-14 08:56:11 -07:00
|
|
|
this.spreadsheet = jspreadsheet(spreadsheetContainer, {
|
|
|
|
data: tableData,
|
|
|
|
columns,
|
|
|
|
});
|
|
|
|
|
|
|
|
const originalData = this.spreadsheet.getData();
|
|
|
|
console.log("Original Data:", originalData);
|
|
|
|
},
|
|
|
|
|
|
|
|
@action
|
|
|
|
cancelTableEdit() {
|
|
|
|
this.triggerModalClose();
|
|
|
|
},
|
|
|
|
|
|
|
|
@action
|
|
|
|
editTable() {
|
|
|
|
// TODO: insert table edit submission logic
|
|
|
|
console.log("New Data:", this.spreadsheet.getData());
|
|
|
|
this.triggerModalClose();
|
|
|
|
},
|
|
|
|
});
|