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";
|
|
|
|
import { ajax } from "discourse/lib/ajax";
|
|
|
|
import { popupAjaxError } from "discourse/lib/ajax-error";
|
|
|
|
|
|
|
|
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"
|
|
|
|
);
|
|
|
|
const expandIcon = create(
|
|
|
|
iconNode("pencil-alt", { class: "edit-table-icon" })
|
|
|
|
);
|
|
|
|
const openPopupText = document.createTextNode(
|
|
|
|
I18n.t(themePrefix("discourse_table_builder.edit.btn_edit"))
|
|
|
|
);
|
|
|
|
openPopupBtn.append(expandIcon, openPopupText);
|
|
|
|
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);
|
|
|
|
const postId = this.id;
|
|
|
|
|
|
|
|
return ajax(`/posts/${postId}`, {
|
|
|
|
type: "GET",
|
|
|
|
cache: false,
|
|
|
|
})
|
|
|
|
.then((result) => {
|
|
|
|
const attrs = {
|
|
|
|
widget: this,
|
|
|
|
raw: result.raw,
|
|
|
|
};
|
|
|
|
|
|
|
|
showModal("table-editor-modal", {
|
|
|
|
model: attrs,
|
2022-07-13 18:42:13 -04:00
|
|
|
}).setProperties({
|
|
|
|
tableHtml: tempTable,
|
|
|
|
submitOnEnter: false,
|
|
|
|
});
|
2022-07-12 18:27:03 -04:00
|
|
|
return result.raw;
|
|
|
|
})
|
|
|
|
.catch(popupAjaxError);
|
|
|
|
}
|
|
|
|
|
|
|
|
function generatePopups(tables, attrs) {
|
|
|
|
tables.forEach((table) => {
|
|
|
|
if (site.isMobileDevice) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const popupBtn = createButton();
|
|
|
|
table.parentNode.classList.add("fullscreen-table-wrapper");
|
2022-07-13 14:38:47 -04:00
|
|
|
const expandBtn = document.querySelector(".open-popup-link");
|
|
|
|
|
|
|
|
if (table.parentNode.contains(expandBtn)) {
|
|
|
|
expandBtn.parentNode.insertBefore(popupBtn, expandBtn);
|
|
|
|
} else {
|
|
|
|
table.parentNode.insertBefore(popupBtn, table);
|
|
|
|
}
|
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",
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|