2022-07-12 18:27:03 -04:00
|
|
|
import { apiInitializer } from "discourse/lib/api";
|
|
|
|
import showModal from "discourse/lib/show-modal";
|
|
|
|
import { schedule } from "@ember/runloop";
|
|
|
|
import I18n from "I18n";
|
|
|
|
import { iconNode } from "discourse-common/lib/icon-library";
|
|
|
|
import { create } from "virtual-dom";
|
2022-07-16 14:00:05 -04:00
|
|
|
import { ajax } from "discourse/lib/ajax";
|
|
|
|
import { popupAjaxError } from "discourse/lib/ajax-error";
|
2022-07-12 18:27:03 -04:00
|
|
|
export default apiInitializer("0.11.1", (api) => {
|
|
|
|
const site = api.container.lookup("site:main");
|
|
|
|
const currentUser = api.getCurrentUser();
|
|
|
|
|
|
|
|
function createButton() {
|
|
|
|
const openPopupBtn = document.createElement("button");
|
|
|
|
openPopupBtn.classList.add(
|
|
|
|
"open-popup-link",
|
|
|
|
"btn-default",
|
|
|
|
"btn",
|
|
|
|
"btn-icon-text"
|
|
|
|
);
|
2022-07-20 18:15:13 -04:00
|
|
|
const editIcon = create(
|
2022-07-12 18:27:03 -04:00
|
|
|
iconNode("pencil-alt", { class: "edit-table-icon" })
|
|
|
|
);
|
|
|
|
const openPopupText = document.createTextNode(
|
|
|
|
I18n.t(themePrefix("discourse_table_builder.edit.btn_edit"))
|
|
|
|
);
|
2022-07-20 18:15:13 -04:00
|
|
|
openPopupBtn.append(editIcon, openPopupText);
|
2022-07-12 18:27:03 -04:00
|
|
|
return openPopupBtn;
|
|
|
|
}
|
|
|
|
|
|
|
|
function generateModal(event) {
|
2022-07-13 14:38:47 -04:00
|
|
|
const table = event.target.parentNode.lastElementChild;
|
2022-07-12 18:27:03 -04:00
|
|
|
const tempTable = table.cloneNode(true);
|
2022-07-18 14:03:57 -04:00
|
|
|
const tableId = event.target.getAttribute("data-table-id");
|
2022-07-12 18:27:03 -04:00
|
|
|
|
2022-07-16 14:00:05 -04:00
|
|
|
return ajax(`/posts/${this.id}`, { type: "GET" })
|
|
|
|
.then((post) => {
|
2022-07-19 19:33:10 -04:00
|
|
|
showModal("insert-table-modal", {
|
2022-07-16 14:00:05 -04:00
|
|
|
model: post,
|
|
|
|
}).setProperties({
|
|
|
|
tableHtml: tempTable,
|
2022-07-18 14:03:57 -04:00
|
|
|
tableId,
|
2022-07-16 14:00:05 -04:00
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(popupAjaxError);
|
2022-07-12 18:27:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function generatePopups(tables, attrs) {
|
2022-07-18 14:03:57 -04:00
|
|
|
tables.forEach((table, index) => {
|
2022-07-12 18:27:03 -04:00
|
|
|
if (site.isMobileDevice) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const popupBtn = createButton();
|
2022-07-18 14:18:58 -04:00
|
|
|
popupBtn.setAttribute("data-table-id", index); // sets a table id so each table can be distinctly edited
|
2022-07-12 18:27:03 -04:00
|
|
|
table.parentNode.classList.add("fullscreen-table-wrapper");
|
2022-07-20 19:28:43 -04:00
|
|
|
const expandBtn = table.parentNode.querySelector(".open-popup-link");
|
2022-07-13 14:38:47 -04:00
|
|
|
|
|
|
|
if (table.parentNode.contains(expandBtn)) {
|
|
|
|
expandBtn.parentNode.insertBefore(popupBtn, expandBtn);
|
|
|
|
} else {
|
|
|
|
table.parentNode.insertBefore(popupBtn, table);
|
|
|
|
}
|
2022-07-18 14:03:57 -04:00
|
|
|
|
2022-07-12 18:27:03 -04:00
|
|
|
popupBtn.addEventListener("click", generateModal.bind(attrs), false);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
api.decorateCookedElement(
|
|
|
|
(post, helper) => {
|
|
|
|
const postOwner = helper.widget.attrs.username;
|
|
|
|
|
|
|
|
if (postOwner !== currentUser.username) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
schedule("afterRender", () => {
|
|
|
|
const tables = post.querySelectorAll("table");
|
|
|
|
generatePopups(tables, helper.widget.attrs);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
{
|
|
|
|
onlyStream: true,
|
|
|
|
id: "edit-table",
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|