import {transaction} from "../../wysiwyg/transaction";
import {fetchPost} from "../../../util/fetch";
import {addCol} from "./addCol";
import {getColIconByType} from "./col";
export const openMenuPanel = (protyle: IProtyle, blockElement: HTMLElement, type: "properties" | "config" = "config") => {
let avMenuPanel = document.querySelector(".av__panel");
if (avMenuPanel) {
avMenuPanel.remove();
return;
}
window.siyuan.menus.menu.remove();
const avId = blockElement.getAttribute("data-av-id")
fetchPost("/api/av/renderAttributeView", {id: avId}, (response) => {
const data = response.data.av as IAV;
const tabRect = blockElement.querySelector(".layout-tab-bar").getBoundingClientRect()
let html
if (type === "config") {
html = getConfigHTML(data, tabRect);
} else if (type === "properties") {
html = getPropertiesHTML(data, tabRect);
}
document.body.insertAdjacentHTML("beforeend", `
${html}
`);
avMenuPanel = document.querySelector(".av__panel");
avMenuPanel.addEventListener("click", (event) => {
event.preventDefault();
let target = event.target as HTMLElement;
while (target && !target.isSameNode(avMenuPanel)) {
const type = target.dataset.type;
if (type === "close") {
avMenuPanel.remove();
event.stopPropagation();
break;
} else if (type === "goConfig") {
avMenuPanel.innerHTML = getConfigHTML(data, tabRect);
event.stopPropagation();
break;
} else if (type === "goProperties") {
avMenuPanel.innerHTML = getPropertiesHTML(data, tabRect);
event.stopPropagation();
break;
} else if (type === "newCol") {
avMenuPanel.remove();
const addMenu = addCol(protyle, blockElement);
addMenu.open({
x: tabRect.right,
y: tabRect.bottom,
h: tabRect.height,
isLeft: true
});
event.stopPropagation();
break;
} else if (type === "showAllCol") {
// showAllCol(data);
avMenuPanel.innerHTML = getPropertiesHTML(data, tabRect);
event.stopPropagation();
break;
} else if (type === "hideAllCol") {
event.stopPropagation();
break;
} else if (type === "hideCol") {
const colId = target.getAttribute("data-id");
transaction(protyle, [{
action: "setAttrViewColHidden",
id: colId,
parentID: avId,
data: true
}], [{
action: "setAttrViewColHidden",
id: colId,
parentID: avId,
data: false
}]);
data.columns.find((item: IAVColumn) => item.id === colId).hidden = true;
avMenuPanel.innerHTML = getPropertiesHTML(data, tabRect);
event.stopPropagation();
break;
} else if (type === "showCol") {
const colId = target.getAttribute("data-id");
transaction(protyle, [{
action: "setAttrViewColHidden",
id: colId,
parentID: avId,
data: false
}], [{
action: "setAttrViewColHidden",
id: colId,
parentID: avId,
data: true
}]);
data.columns.find((item: IAVColumn) => item.id === colId).hidden = false;
avMenuPanel.innerHTML = getPropertiesHTML(data, tabRect);
event.stopPropagation();
break;
}
target = target.parentElement;
}
});
});
}
const getConfigHTML = (data: IAV, tabRect: DOMRect) => {
return `
`
}
const getPropertiesHTML = (data: IAV, tabRect: DOMRect) => {
let showHTML = "";
let hideHTML = "";
data.columns.forEach((item: IAVColumn) => {
if (item.hidden) {
hideHTML += ``
} else {
showHTML += ``
}
});
if (hideHTML) {
hideHTML = `
${hideHTML}`;
}
return `
`
}