2023-06-08 18:21:10 +08:00
|
|
|
import {fetchPost} from "../../../util/fetch";
|
2023-09-21 10:54:41 +08:00
|
|
|
import {getColIconByType} from "./col";
|
2023-07-01 23:15:21 +08:00
|
|
|
import {Constants} from "../../../constants";
|
2023-07-15 23:18:35 +08:00
|
|
|
import {getCalcValue} from "./cell";
|
2023-07-22 22:56:15 +08:00
|
|
|
import * as dayjs from "dayjs";
|
2023-08-01 00:09:14 +08:00
|
|
|
import {hasClosestByAttribute} from "../../util/hasClosest";
|
|
|
|
|
import {Menu} from "../../../plugin/Menu";
|
2023-08-01 20:42:10 +08:00
|
|
|
import {escapeAttr} from "../../../util/escape";
|
2023-06-08 18:21:10 +08:00
|
|
|
|
2023-09-09 23:21:46 +08:00
|
|
|
export const avRender = (element: Element, protyle: IProtyle, cb?: () => void) => {
|
2023-06-07 11:55:45 +08:00
|
|
|
let avElements: Element[] = [];
|
|
|
|
|
if (element.getAttribute("data-type") === "NodeAttributeView") {
|
|
|
|
|
// 编辑器内代码块编辑渲染
|
|
|
|
|
avElements = [element];
|
|
|
|
|
} else {
|
|
|
|
|
avElements = Array.from(element.querySelectorAll('[data-type="NodeAttributeView"]'));
|
|
|
|
|
}
|
|
|
|
|
if (avElements.length === 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (avElements.length > 0) {
|
2023-06-08 22:55:31 +08:00
|
|
|
avElements.forEach((e: HTMLElement) => {
|
2023-06-07 11:55:45 +08:00
|
|
|
if (e.getAttribute("data-render") === "true") {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-08-01 21:29:15 +08:00
|
|
|
const left = e.querySelector(".av__scroll")?.scrollLeft || 0;
|
2023-07-31 23:58:45 +08:00
|
|
|
fetchPost("/api/av/renderAttributeView", {
|
|
|
|
|
id: e.getAttribute("data-av-id"),
|
|
|
|
|
nodeID: e.getAttribute("data-node-id")
|
|
|
|
|
}, (response) => {
|
2023-07-11 21:34:22 +08:00
|
|
|
const data = response.data.view as IAVTable;
|
2023-06-08 19:21:33 +08:00
|
|
|
// header
|
2023-07-15 00:07:27 +08:00
|
|
|
let tableHTML = '<div class="av__row av__row--header"><div class="av__firstcol"><svg style="height: 32px"><use xlink:href="#iconUncheck"></use></svg></div>';
|
2023-07-15 23:19:40 +08:00
|
|
|
let calcHTML = "";
|
2023-06-08 18:25:33 +08:00
|
|
|
data.columns.forEach((column: IAVColumn) => {
|
2023-06-08 22:55:31 +08:00
|
|
|
if (column.hidden) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-07-09 23:54:32 +08:00
|
|
|
tableHTML += `<div class="av__cell" data-col-id="${column.id}" data-dtype="${column.type}"
|
2023-07-02 23:58:00 +08:00
|
|
|
style="width: ${column.width || "200px"};
|
2023-07-02 22:37:17 +08:00
|
|
|
${column.wrap ? "" : "white-space: nowrap;"}">
|
2023-07-02 23:58:00 +08:00
|
|
|
<div draggable="true" class="av__cellheader">
|
|
|
|
|
<svg><use xlink:href="#${column.icon || getColIconByType(column.type)}"></use></svg>
|
|
|
|
|
<span class="av__celltext">${column.name}</span>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="av__widthdrag"></div>
|
2023-06-08 22:56:52 +08:00
|
|
|
</div>`;
|
2023-07-15 23:18:35 +08:00
|
|
|
calcHTML += `<div class="av__calc${calcHTML ? "" : " av__calc--show"}${column.calc && column.calc.operator !== "" ? " av__calc--ashow" : ""}" data-col-id="${column.id}" data-dtype="${column.type}" data-operator="${column.calc?.operator || ""}"
|
|
|
|
|
style="width: ${column.width || "200px"}">${getCalcValue(column) || '<svg><use xlink:href="#iconDown"></use></svg>' + window.siyuan.languages.calc}</div>`;
|
2023-06-08 18:21:10 +08:00
|
|
|
});
|
2023-07-15 00:07:27 +08:00
|
|
|
tableHTML += `<div class="block__icons" style="min-height: auto">
|
2023-06-08 19:21:33 +08:00
|
|
|
<div class="block__icon block__icon--show" data-type="av-header-add"><svg><use xlink:href="#iconAdd"></use></svg></div>
|
2023-06-08 12:03:04 +08:00
|
|
|
<div class="fn__space"></div>
|
2023-06-08 19:21:33 +08:00
|
|
|
<div class="block__icon block__icon--show" data-type="av-header-more"><svg><use xlink:href="#iconMore"></use></svg></div>
|
2023-06-08 12:03:04 +08:00
|
|
|
</div>
|
|
|
|
|
</div>`;
|
2023-06-08 19:21:33 +08:00
|
|
|
// body
|
2023-06-08 18:25:33 +08:00
|
|
|
data.rows.forEach((row: IAVRow) => {
|
2023-06-11 22:01:30 +08:00
|
|
|
tableHTML += `<div class="av__row" data-id="${row.id}">
|
2023-07-12 11:46:58 +08:00
|
|
|
<div class="av__gutters ariaLabel" draggable="true" data-position="right" aria-label="${window.siyuan.languages.rowTip}">
|
2023-06-11 22:01:30 +08:00
|
|
|
<button><svg><use xlink:href="#iconLine"></use></svg></button>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="av__firstcol"><svg><use xlink:href="#iconUncheck"></use></svg></div>`;
|
2023-06-08 18:21:10 +08:00
|
|
|
row.cells.forEach((cell, index) => {
|
2023-07-02 20:52:16 +08:00
|
|
|
if (data.columns[index].hidden) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-07-12 00:08:00 +08:00
|
|
|
let text = "";
|
2023-06-30 16:26:35 +08:00
|
|
|
if (cell.valueType === "text") {
|
2023-07-09 23:54:32 +08:00
|
|
|
text = `<span class="av__celltext">${cell.value?.text.content || ""}</span>`;
|
2023-08-09 22:55:34 +08:00
|
|
|
} else if (["url", "email", "phone"].includes(cell.valueType)) {
|
|
|
|
|
text = `<span class="av__celltext av__celltext--url" data-type="${cell.valueType}">${cell.value ? cell.value[cell.valueType as "url"].content : ""}</span>`;
|
2023-08-03 15:08:07 +08:00
|
|
|
if (cell.value && cell.value[cell.valueType as "url"].content) {
|
|
|
|
|
text += `<span data-type="copy" class="b3-tooltips b3-tooltips__n block__icon" aria-label="${window.siyuan.languages.copy}"><svg><use xlink:href="#iconCopy"></use></svg></span>`;
|
2023-07-30 23:09:37 +08:00
|
|
|
}
|
2023-06-30 16:26:35 +08:00
|
|
|
} else if (cell.valueType === "block") {
|
2023-07-18 11:47:47 +08:00
|
|
|
text = `<span class="av__celltext">${cell.value?.block.content || ""}</span>`;
|
2023-07-17 20:23:27 +08:00
|
|
|
if (cell.value?.block.id) {
|
2023-07-28 15:20:34 +08:00
|
|
|
text += `<span class="b3-chip b3-chip--info b3-chip--small" data-type="block-ref" data-id="${cell.value.block.id}" data-subtype="s">${window.siyuan.languages.openBy}</span>`;
|
2023-07-17 20:23:27 +08:00
|
|
|
}
|
2023-07-01 12:04:23 +08:00
|
|
|
} else if (cell.valueType === "number") {
|
2023-08-03 23:23:28 +08:00
|
|
|
text = `<span class="av__celltext" data-content="${cell.value?.number.content || ""}">${cell.value?.number.formattedContent || ""}</span>`;
|
2023-07-10 23:22:04 +08:00
|
|
|
} else if (cell.valueType === "mSelect" || cell.valueType === "select") {
|
2023-09-21 17:27:48 +08:00
|
|
|
cell.value?.mSelect?.forEach((item) => {
|
2023-07-13 23:16:07 +08:00
|
|
|
text += `<span class="b3-chip b3-chip--middle" style="background-color:var(--b3-font-background${item.color});color:var(--b3-font-color${item.color})">${item.content}</span>`;
|
2023-07-10 00:17:54 +08:00
|
|
|
});
|
2023-07-10 23:22:04 +08:00
|
|
|
if (!text) {
|
2023-07-12 00:08:00 +08:00
|
|
|
text = '<span class="av__celltext"></span>';
|
2023-07-13 23:16:07 +08:00
|
|
|
} else {
|
|
|
|
|
text = `<span class="av__celltext">${text}</span>`;
|
2023-07-10 23:22:04 +08:00
|
|
|
}
|
2023-07-01 12:04:23 +08:00
|
|
|
} else if (cell.valueType === "date") {
|
2023-08-03 23:23:28 +08:00
|
|
|
text = '<span class="av__celltext av__celltext--date">';
|
2023-08-02 00:14:05 +08:00
|
|
|
if (cell.value?.date.isNotEmpty) {
|
2023-07-22 22:56:15 +08:00
|
|
|
text += dayjs(cell.value.date.content).format("YYYY-MM-DD HH:mm");
|
|
|
|
|
}
|
2023-08-02 00:14:05 +08:00
|
|
|
if (cell.value?.date.hasEndDate && cell.value?.date.isNotEmpty && cell.value?.date.isNotEmpty2) {
|
2023-08-03 23:23:28 +08:00
|
|
|
text += `<svg><use xlink:href="#iconForward"></use></svg>${dayjs(cell.value.date.content2).format("YYYY-MM-DD HH:mm")}`;
|
2023-07-22 22:56:15 +08:00
|
|
|
}
|
2023-07-25 20:40:28 +08:00
|
|
|
text += "</span>";
|
2023-09-21 17:27:48 +08:00
|
|
|
} else if (cell.valueType === "mAsset") {
|
|
|
|
|
cell.value?.mAsset?.forEach((item) => {
|
|
|
|
|
if (item.type === "image") {
|
2023-09-22 12:11:56 +08:00
|
|
|
text += `<img class="av__cellassetimg" src="${item.content}">`;
|
2023-09-21 17:27:48 +08:00
|
|
|
} else {
|
|
|
|
|
text += `<span class="b3-chip b3-chip--middle" data-url="${item.content}">${item.name}</span>`;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
if (!text) {
|
|
|
|
|
text = '<span class="av__celltext"></span>';
|
|
|
|
|
} else {
|
|
|
|
|
text = `<span class="av__celltext">${text}</span>`;
|
|
|
|
|
}
|
2023-06-30 16:26:35 +08:00
|
|
|
}
|
2023-07-09 23:54:32 +08:00
|
|
|
tableHTML += `<div class="av__cell" data-id="${cell.id}" data-col-id="${data.columns[index].id}"
|
2023-07-02 22:37:17 +08:00
|
|
|
${cell.valueType === "block" ? 'data-block-id="' + (cell.value.block.id || "") + '"' : ""}
|
2023-07-02 23:58:00 +08:00
|
|
|
style="width: ${data.columns[index].width || "200px"};
|
2023-07-02 22:37:17 +08:00
|
|
|
${cell.bgColor ? `background-color:${cell.bgColor};` : ""}
|
2023-07-02 23:58:00 +08:00
|
|
|
${data.columns[index].wrap ? "" : "white-space: nowrap;"}
|
2023-08-03 23:23:28 +08:00
|
|
|
${cell.valueType !== "number" ? "" : "flex-direction: row-reverse;"}
|
2023-07-09 23:54:32 +08:00
|
|
|
${cell.color ? `color:${cell.color};` : ""}">${text}</div>`;
|
2023-06-08 18:21:10 +08:00
|
|
|
});
|
2023-06-08 22:56:52 +08:00
|
|
|
tableHTML += "<div></div></div>";
|
2023-06-08 10:07:26 +08:00
|
|
|
});
|
2023-07-12 00:08:00 +08:00
|
|
|
let tabHTML = "";
|
2023-07-11 23:13:19 +08:00
|
|
|
response.data.views.forEach((item: IAVView) => {
|
|
|
|
|
tabHTML += `<div data-id="${response.data.viewID}" class="item${item.id === response.data.viewID ? " item--focus" : ""}">
|
2023-07-11 21:34:22 +08:00
|
|
|
<svg class="item__graphic"><use xlink:href="#iconTable"></use></svg>
|
|
|
|
|
<span class="item__text">${item.name}</span>
|
2023-07-12 00:08:00 +08:00
|
|
|
</div>`;
|
|
|
|
|
});
|
2023-06-11 22:01:30 +08:00
|
|
|
const paddingLeft = e.parentElement.style.paddingLeft;
|
|
|
|
|
const paddingRight = e.parentElement.style.paddingRight;
|
|
|
|
|
e.style.width = e.parentElement.clientWidth + "px";
|
2023-06-08 18:21:10 +08:00
|
|
|
e.style.alignSelf = "center";
|
|
|
|
|
e.firstElementChild.outerHTML = `<div>
|
2023-06-30 23:57:11 +08:00
|
|
|
<div class="av__header" style="padding-left: ${paddingLeft};padding-right: ${paddingRight};">
|
|
|
|
|
<div class="layout-tab-bar fn__flex">
|
2023-07-11 21:34:22 +08:00
|
|
|
${tabHTML}
|
2023-07-02 20:52:16 +08:00
|
|
|
<div class="fn__flex-1"></div>
|
2023-07-03 20:54:57 +08:00
|
|
|
<span data-type="av-filter" class="block__icon block__icon--show b3-tooltips b3-tooltips__w${data.filters.length > 0 ? " block__icon--active" : ""}" aria-label="${window.siyuan.languages.filter}">
|
2023-07-02 20:52:16 +08:00
|
|
|
<svg><use xlink:href="#iconFilter"></use></svg>
|
|
|
|
|
</span>
|
|
|
|
|
<div class="fn__space"></div>
|
2023-07-03 20:54:57 +08:00
|
|
|
<span data-type="av-sort" class="block__icon block__icon--show b3-tooltips b3-tooltips__w${data.sorts.length > 0 ? " block__icon--active" : ""}" aria-label="${window.siyuan.languages.sort}">
|
2023-07-02 20:52:16 +08:00
|
|
|
<svg><use xlink:href="#iconSort"></use></svg>
|
|
|
|
|
</span>
|
|
|
|
|
<div class="fn__space"></div>
|
|
|
|
|
<span data-type="av-more" class="block__icon block__icon--show b3-tooltips b3-tooltips__w" aria-label="${window.siyuan.languages.more}">
|
|
|
|
|
<svg><use xlink:href="#iconMore"></use></svg>
|
|
|
|
|
</span>
|
|
|
|
|
<div class="fn__space"></div>
|
2023-06-08 10:07:26 +08:00
|
|
|
</div>
|
2023-09-09 23:21:46 +08:00
|
|
|
<div contenteditable="${protyle.disabled ? "false" : "true"}" spellcheck="${window.siyuan.config.editor.spellcheck.toString()}" class="av__title" data-title="${data.name || ""}" data-tip="${window.siyuan.languages.title}">${response.data.name || ""}</div>
|
2023-06-30 23:57:11 +08:00
|
|
|
<div class="av__counter fn__none"></div>
|
2023-06-08 10:07:26 +08:00
|
|
|
</div>
|
|
|
|
|
<div class="av__scroll">
|
2023-06-11 22:01:30 +08:00
|
|
|
<div style="padding-left: ${paddingLeft};padding-right: ${paddingRight};float: left;">
|
2023-06-08 10:07:26 +08:00
|
|
|
${tableHTML}
|
2023-07-15 00:07:27 +08:00
|
|
|
<div class="av__row--add">
|
|
|
|
|
<svg><use xlink:href="#iconAdd"></use></svg>
|
2023-06-08 12:03:04 +08:00
|
|
|
${window.siyuan.languages.addAttr}
|
|
|
|
|
</div>
|
2023-07-15 22:24:54 +08:00
|
|
|
<div class="av__row--footer"><div style="width: 24px"></div>${calcHTML}</div>
|
2023-06-08 10:07:26 +08:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>`;
|
2023-06-08 18:21:10 +08:00
|
|
|
e.setAttribute("data-render", "true");
|
2023-08-01 21:29:15 +08:00
|
|
|
e.querySelector(".av__scroll").scrollLeft = left;
|
2023-06-11 23:17:51 +08:00
|
|
|
if (cb) {
|
|
|
|
|
cb();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2023-07-02 23:59:01 +08:00
|
|
|
let lastParentID: string;
|
|
|
|
|
let lastElement: HTMLElement;
|
2023-06-11 23:17:51 +08:00
|
|
|
export const refreshAV = (protyle: IProtyle, operation: IOperation) => {
|
2023-07-01 23:15:21 +08:00
|
|
|
if (lastParentID === operation.parentID && protyle.contentElement.isSameNode(lastElement)) {
|
2023-07-02 23:59:01 +08:00
|
|
|
return;
|
2023-07-01 20:48:44 +08:00
|
|
|
}
|
2023-07-01 23:15:21 +08:00
|
|
|
lastElement = protyle.contentElement;
|
|
|
|
|
lastParentID = operation.parentID;
|
2023-07-11 23:13:19 +08:00
|
|
|
const avId = operation.avID;
|
2023-09-21 10:54:17 +08:00
|
|
|
if (operation.action === "setAttrViewColWidth") {
|
2023-07-03 20:54:57 +08:00
|
|
|
Array.from(protyle.wysiwyg.element.querySelectorAll(`[data-av-id="${avId}"]`)).forEach((item: HTMLElement) => {
|
2023-07-09 23:54:32 +08:00
|
|
|
const cellElement = item.querySelector(`.av__cell[data-col-id="${operation.id}"]`) as HTMLElement;
|
2023-07-02 23:58:00 +08:00
|
|
|
if (!cellElement || cellElement.style.width === operation.data) {
|
2023-07-02 23:59:01 +08:00
|
|
|
return;
|
2023-07-02 23:58:00 +08:00
|
|
|
}
|
|
|
|
|
item.querySelectorAll(".av__row").forEach(rowItem => {
|
2023-07-09 23:54:32 +08:00
|
|
|
(rowItem.querySelector(`[data-col-id="${operation.id}"]`) as HTMLElement).style.width = operation.data;
|
2023-07-02 23:59:01 +08:00
|
|
|
});
|
2023-07-02 23:58:00 +08:00
|
|
|
});
|
2023-07-11 22:48:48 +08:00
|
|
|
} else if (operation.action === "setAttrViewName") {
|
2023-07-03 23:02:19 +08:00
|
|
|
Array.from(protyle.wysiwyg.element.querySelectorAll(`[data-av-id="${avId}"]`)).forEach((item: HTMLElement) => {
|
|
|
|
|
const titleElement = item.querySelector(".av__title") as HTMLElement;
|
2023-07-11 22:48:48 +08:00
|
|
|
if (!titleElement || titleElement.textContent.trim() === operation.data) {
|
2023-07-03 23:02:19 +08:00
|
|
|
return;
|
|
|
|
|
}
|
2023-07-11 22:48:48 +08:00
|
|
|
titleElement.textContent = operation.data;
|
|
|
|
|
titleElement.dataset.title = operation.data;
|
2023-07-03 23:02:19 +08:00
|
|
|
});
|
2023-07-01 12:15:31 +08:00
|
|
|
} else {
|
2023-07-03 20:54:57 +08:00
|
|
|
Array.from(protyle.wysiwyg.element.querySelectorAll(`[data-av-id="${avId}"]`)).forEach((item: HTMLElement) => {
|
2023-06-11 23:17:51 +08:00
|
|
|
item.removeAttribute("data-render");
|
2023-09-09 23:21:46 +08:00
|
|
|
avRender(item, protyle);
|
2023-06-11 23:17:51 +08:00
|
|
|
});
|
2023-06-07 11:55:45 +08:00
|
|
|
}
|
2023-07-01 23:15:21 +08:00
|
|
|
setTimeout(() => {
|
|
|
|
|
lastParentID = null;
|
|
|
|
|
}, Constants.TIMEOUT_TRANSITION);
|
2023-06-07 11:56:34 +08:00
|
|
|
};
|
2023-07-30 22:39:37 +08:00
|
|
|
|
|
|
|
|
const genAVValueHTML = (value: IAVCellValue) => {
|
2023-07-31 11:32:36 +08:00
|
|
|
let html = "";
|
2023-07-30 22:39:37 +08:00
|
|
|
switch (value.type) {
|
|
|
|
|
case "text":
|
|
|
|
|
html = `<input value="${value.text.content}" class="b3-text-field b3-text-field--text fn__flex-1">`;
|
|
|
|
|
break;
|
|
|
|
|
case "number":
|
|
|
|
|
html = `<input value="${value.number.content}" type="number" class="b3-text-field b3-text-field--text fn__flex-1">`;
|
|
|
|
|
break;
|
|
|
|
|
case "mSelect":
|
|
|
|
|
case "select":
|
2023-07-31 11:10:38 +08:00
|
|
|
value.mSelect?.forEach(item => {
|
2023-07-30 22:39:37 +08:00
|
|
|
html += `<span class="b3-chip b3-chip--middle" style="background-color:var(--b3-font-background${item.color});color:var(--b3-font-color${item.color})">${item.content}</span>`;
|
2023-07-31 11:32:36 +08:00
|
|
|
});
|
2023-07-30 22:39:37 +08:00
|
|
|
break;
|
|
|
|
|
case "date":
|
2023-08-02 00:14:05 +08:00
|
|
|
if (value.date.isNotEmpty) {
|
2023-08-01 12:27:47 +08:00
|
|
|
html = `<span data-content="${value.date.content}">${dayjs(value.date.content).format("YYYY-MM-DD HH:mm")}</span>`;
|
|
|
|
|
}
|
2023-08-02 00:14:05 +08:00
|
|
|
if (value.date.hasEndDate && value.date.isNotEmpty2 && value.date.isNotEmpty) {
|
2023-08-01 14:42:33 +08:00
|
|
|
html += `<svg class="custom-attr__avarrow"><use xlink:href="#iconForward"></use></svg><span data-content="${value.date.content2}">${dayjs(value.date.content2).format("YYYY-MM-DD HH:mm")}</span>`;
|
2023-07-30 22:39:37 +08:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case "url":
|
2023-08-02 21:04:23 +08:00
|
|
|
html = `<input value="${value.url.content}" class="b3-text-field b3-text-field--text fn__flex-1">
|
|
|
|
|
<span class="fn__space"></span>
|
|
|
|
|
<a href="${value.url.content}" target="_blank" aria-label="${window.siyuan.languages.openBy}" class="block__icon block__icon--show fn__flex-center b3-tooltips__w b3-tooltips"><svg><use xlink:href="#iconLink"></use></svg></a>`;
|
2023-07-30 22:39:37 +08:00
|
|
|
break;
|
2023-08-03 15:08:07 +08:00
|
|
|
case "phone":
|
|
|
|
|
html = `<input value="${value.phone.content}" class="b3-text-field b3-text-field--text fn__flex-1">
|
|
|
|
|
<span class="fn__space"></span>
|
|
|
|
|
<a href="tel:${value.phone.content}" target="_blank" aria-label="${window.siyuan.languages.openBy}" class="block__icon block__icon--show fn__flex-center b3-tooltips__w b3-tooltips"><svg><use xlink:href="#iconPhone"></use></svg></a>`;
|
|
|
|
|
break;
|
|
|
|
|
case "email":
|
|
|
|
|
html = `<input value="${value.email.content}" class="b3-text-field b3-text-field--text fn__flex-1">
|
|
|
|
|
<span class="fn__space"></span>
|
|
|
|
|
<a href="mailto:${value.email.content}" target="_blank" aria-label="${window.siyuan.languages.openBy}" class="block__icon block__icon--show fn__flex-center b3-tooltips__w b3-tooltips"><svg><use xlink:href="#iconEmail"></use></svg></a>`;
|
|
|
|
|
break;
|
2023-07-30 22:39:37 +08:00
|
|
|
}
|
|
|
|
|
return html;
|
2023-07-31 11:32:36 +08:00
|
|
|
};
|
2023-07-30 22:39:37 +08:00
|
|
|
|
|
|
|
|
export const renderAVAttribute = (element: HTMLElement, id: string) => {
|
|
|
|
|
fetchPost("/api/av/getAttributeViewKeys", {id}, (response) => {
|
2023-07-31 11:32:36 +08:00
|
|
|
let html = "";
|
2023-07-30 22:39:37 +08:00
|
|
|
response.data.forEach((table: {
|
|
|
|
|
keyValues: {
|
|
|
|
|
key: {
|
|
|
|
|
type: TAVCol,
|
2023-08-01 20:42:10 +08:00
|
|
|
name: string,
|
|
|
|
|
options?: { name: string, color: string }[]
|
2023-07-30 22:39:37 +08:00
|
|
|
},
|
2023-07-31 23:58:45 +08:00
|
|
|
values: { keyID: string, id: string, blockID: string, type?: TAVCol & IAVCellValue } []
|
2023-07-30 22:39:37 +08:00
|
|
|
}[],
|
2023-07-31 23:58:45 +08:00
|
|
|
avID: string
|
2023-07-30 22:39:37 +08:00
|
|
|
avName: string
|
|
|
|
|
}) => {
|
2023-07-31 11:10:38 +08:00
|
|
|
html += `<div class="block__logo custom-attr__avheader">
|
|
|
|
|
<svg><use xlink:href="#iconDatabase"></use></svg>
|
2023-07-31 11:18:26 +08:00
|
|
|
<span>${table.avName || window.siyuan.languages.database}</span>
|
2023-07-31 11:10:38 +08:00
|
|
|
</div>`;
|
2023-07-30 22:39:37 +08:00
|
|
|
table.keyValues?.forEach(item => {
|
|
|
|
|
html += `<div class="block__icons">
|
|
|
|
|
<div class="block__logo">
|
|
|
|
|
<svg><use xlink:href="#${getColIconByType(item.key.type)}"></use></svg>
|
2023-07-31 11:10:38 +08:00
|
|
|
<span>${item.key.name}</span>
|
2023-07-30 22:39:37 +08:00
|
|
|
</div>
|
2023-08-01 20:42:10 +08:00
|
|
|
<div data-av-id="${table.avID}" data-key-id="${item.values[0].keyID}" data-block-id="${item.values[0].blockID}" data-id="${item.values[0].id}" data-type="${item.values[0].type}"
|
|
|
|
|
data-options="${item.key?.options ? escapeAttr(JSON.stringify(item.key.options)) : "[]"}"
|
2023-08-03 15:08:07 +08:00
|
|
|
class="fn__flex-1 fn__flex${["url", "text", "number", "email", "phone"].includes(item.values[0].type) ? "" : " custom-attr__avvalue"}">
|
2023-07-30 22:39:37 +08:00
|
|
|
${genAVValueHTML(item.values[0])}
|
|
|
|
|
</div>
|
2023-07-31 11:32:36 +08:00
|
|
|
</div>`;
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
element.innerHTML = html;
|
2023-08-01 12:27:47 +08:00
|
|
|
element.addEventListener("click", (event) => {
|
2023-08-01 20:42:30 +08:00
|
|
|
const target = event.target as HTMLElement;
|
|
|
|
|
const dateElement = hasClosestByAttribute(target, "data-type", "date");
|
2023-08-01 12:27:47 +08:00
|
|
|
if (dateElement) {
|
|
|
|
|
const dateMenu = new Menu("custom-attr-av-date", () => {
|
2023-08-01 20:42:30 +08:00
|
|
|
const textElements = window.siyuan.menus.menu.element.querySelectorAll(".b3-text-field") as NodeListOf<HTMLInputElement>;
|
|
|
|
|
const hasEndDate = (window.siyuan.menus.menu.element.querySelector(".b3-switch") as HTMLInputElement).checked;
|
2023-08-01 12:27:47 +08:00
|
|
|
fetchPost("/api/av/setAttributeViewBlockAttr", {
|
|
|
|
|
avID: dateElement.dataset.avId,
|
|
|
|
|
keyID: dateElement.dataset.keyId,
|
|
|
|
|
rowID: dateElement.dataset.blockId,
|
|
|
|
|
cellID: dateElement.dataset.id,
|
|
|
|
|
value: {
|
|
|
|
|
date: {
|
2023-08-02 00:14:05 +08:00
|
|
|
isNotEmpty: textElements[0].value !== "",
|
|
|
|
|
isNotEmpty2: textElements[1].value !== "",
|
2023-08-01 12:27:47 +08:00
|
|
|
content: new Date(textElements[0].value).getTime(),
|
|
|
|
|
content2: new Date(textElements[1].value).getTime(),
|
|
|
|
|
hasEndDate
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-08-01 20:42:30 +08:00
|
|
|
let dataHTML = "";
|
2023-08-02 00:14:05 +08:00
|
|
|
if (textElements[0].value !== "") {
|
2023-08-01 20:42:30 +08:00
|
|
|
dataHTML = `<span data-content="${new Date(textElements[0].value).getTime()}">${dayjs(textElements[0].value).format("YYYY-MM-DD HH:mm")}</span>`;
|
2023-08-01 12:27:47 +08:00
|
|
|
}
|
2023-08-02 00:14:05 +08:00
|
|
|
if (hasEndDate && textElements[0].value !== "" && textElements[1].value !== "") {
|
2023-08-01 20:42:30 +08:00
|
|
|
dataHTML += `<svg class="custom-attr__avarrow"><use xlink:href="#iconForward"></use></svg><span data-content="${new Date(textElements[1].value).getTime()}">${dayjs(textElements[1].value).format("YYYY-MM-DD HH:mm")}</span>`;
|
2023-08-01 12:27:47 +08:00
|
|
|
}
|
|
|
|
|
dateElement.innerHTML = dataHTML;
|
|
|
|
|
});
|
|
|
|
|
if (dateMenu.isOpen) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-08-01 20:42:30 +08:00
|
|
|
const hasEndDate = dateElement.querySelector("svg");
|
|
|
|
|
const timeElements = dateElement.querySelectorAll("span");
|
2023-08-01 12:27:47 +08:00
|
|
|
dateMenu.addItem({
|
|
|
|
|
iconHTML: "",
|
|
|
|
|
label: `<input value="${timeElements[0] ? dayjs(parseInt(timeElements[0].dataset.content)).format("YYYY-MM-DDTHH:mm") : ""}" type="datetime-local" class="b3-text-field fn__size200" style="margin: 4px 0">`
|
2023-08-01 20:42:30 +08:00
|
|
|
});
|
2023-08-01 12:27:47 +08:00
|
|
|
dateMenu.addItem({
|
|
|
|
|
iconHTML: "",
|
|
|
|
|
label: `<input value="${timeElements[1] ? dayjs(parseInt(timeElements[1].dataset.content)).format("YYYY-MM-DDTHH:mm") : ""}" type="datetime-local" class="b3-text-field fn__size200${hasEndDate ? "" : " fn__none"}" style="margin: 4px 0">`
|
2023-08-01 20:42:30 +08:00
|
|
|
});
|
|
|
|
|
dateMenu.addSeparator();
|
2023-08-01 12:27:47 +08:00
|
|
|
dateMenu.addItem({
|
|
|
|
|
iconHTML: "",
|
|
|
|
|
label: `<label class="fn__flex">
|
|
|
|
|
<span>${window.siyuan.languages.endDate}</span>
|
|
|
|
|
<span class="fn__space fn__flex-1"></span>
|
|
|
|
|
<input type="checkbox" class="b3-switch fn__flex-center"${hasEndDate ? " checked" : ""}>
|
|
|
|
|
</label>`,
|
2023-08-01 14:42:33 +08:00
|
|
|
click(element, event) {
|
2023-08-01 20:42:30 +08:00
|
|
|
const switchElement = element.querySelector(".b3-switch") as HTMLInputElement;
|
2023-08-01 14:42:33 +08:00
|
|
|
if ((event.target as HTMLElement).tagName !== "INPUT") {
|
|
|
|
|
switchElement.checked = !switchElement.checked;
|
|
|
|
|
} else {
|
|
|
|
|
switchElement.outerHTML = `<input type="checkbox" class="b3-switch fn__flex-center"${switchElement.checked ? " checked" : ""}>`;
|
|
|
|
|
}
|
2023-08-01 12:27:47 +08:00
|
|
|
window.siyuan.menus.menu.element.querySelectorAll('[type="datetime-local"]')[1].classList.toggle("fn__none");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2023-08-01 20:42:30 +08:00
|
|
|
});
|
|
|
|
|
dateMenu.addSeparator();
|
2023-08-01 12:27:47 +08:00
|
|
|
dateMenu.addItem({
|
|
|
|
|
icon: "iconTrashcan",
|
|
|
|
|
label: window.siyuan.languages.clear,
|
|
|
|
|
click() {
|
2023-08-01 20:42:30 +08:00
|
|
|
const textElements = window.siyuan.menus.menu.element.querySelectorAll(".b3-text-field") as NodeListOf<HTMLInputElement>;
|
2023-08-01 14:42:33 +08:00
|
|
|
textElements[0].value = "";
|
|
|
|
|
textElements[1].value = "";
|
2023-08-01 20:42:30 +08:00
|
|
|
(window.siyuan.menus.menu.element.querySelector(".b3-switch") as HTMLInputElement).checked = false;
|
2023-08-01 12:27:47 +08:00
|
|
|
}
|
2023-08-01 20:42:30 +08:00
|
|
|
});
|
|
|
|
|
const datetRect = dateElement.getBoundingClientRect();
|
2023-08-01 12:27:47 +08:00
|
|
|
dateMenu.open({
|
2023-08-01 20:42:10 +08:00
|
|
|
x: datetRect.left,
|
|
|
|
|
y: datetRect.bottom
|
2023-08-01 20:42:30 +08:00
|
|
|
});
|
|
|
|
|
event.stopPropagation();
|
2023-08-01 12:27:47 +08:00
|
|
|
event.preventDefault();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-08-01 20:42:30 +08:00
|
|
|
const mSelectElement = hasClosestByAttribute(target, "data-type", "select") || hasClosestByAttribute(target, "data-type", "mSelect");
|
2023-08-01 12:27:47 +08:00
|
|
|
if (mSelectElement) {
|
2023-08-01 20:42:10 +08:00
|
|
|
const mSelectMenu = new Menu("custom-attr-av-select", () => {
|
2023-08-01 20:42:30 +08:00
|
|
|
const mSelect: { content: string, color: string }[] = [];
|
2023-08-01 20:42:10 +08:00
|
|
|
let mSelectHTML = "";
|
2023-08-01 20:42:30 +08:00
|
|
|
window.siyuan.menus.menu.element.querySelectorAll(".svg").forEach(item => {
|
|
|
|
|
const chipElement = item.parentElement.previousElementSibling.firstElementChild as HTMLElement;
|
|
|
|
|
const content = chipElement.textContent.trim();
|
|
|
|
|
const color = chipElement.dataset.color;
|
2023-08-01 20:42:10 +08:00
|
|
|
mSelect.push({
|
|
|
|
|
content,
|
|
|
|
|
color
|
2023-08-01 20:42:30 +08:00
|
|
|
});
|
|
|
|
|
mSelectHTML += `<span class="b3-chip b3-chip--middle" style="background-color:var(--b3-font-background${color});color:var(--b3-font-color${color})">${content}</span>`;
|
|
|
|
|
});
|
2023-08-01 20:42:10 +08:00
|
|
|
fetchPost("/api/av/setAttributeViewBlockAttr", {
|
|
|
|
|
avID: mSelectElement.dataset.avId,
|
|
|
|
|
keyID: mSelectElement.dataset.keyId,
|
|
|
|
|
rowID: mSelectElement.dataset.blockId,
|
|
|
|
|
cellID: mSelectElement.dataset.id,
|
|
|
|
|
value: {
|
|
|
|
|
mSelect
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-08-01 20:42:30 +08:00
|
|
|
mSelectElement.innerHTML = mSelectHTML;
|
|
|
|
|
});
|
2023-08-01 20:42:10 +08:00
|
|
|
if (mSelectMenu.isOpen) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-08-01 20:42:30 +08:00
|
|
|
const names: string[] = [];
|
2023-08-01 20:42:10 +08:00
|
|
|
mSelectElement.querySelectorAll(".b3-chip").forEach(item => {
|
2023-08-01 20:42:30 +08:00
|
|
|
names.push(item.textContent.trim());
|
|
|
|
|
});
|
2023-08-01 20:42:10 +08:00
|
|
|
JSON.parse(mSelectElement.dataset.options || "").forEach((item: { name: string, color: string }) => {
|
|
|
|
|
mSelectMenu.addItem({
|
|
|
|
|
iconHTML: "",
|
|
|
|
|
label: `<span class="b3-chip" data-color="${item.color}" style="height:24px;background-color:var(--b3-font-background${item.color});color:var(--b3-font-color${item.color})">
|
|
|
|
|
<span class="fn__ellipsis">${item.name}</span>
|
|
|
|
|
</span>`,
|
|
|
|
|
accelerator: names.includes(item.name) ? '<svg class="svg" style="height: 30px; float: left;"><use xlink:href="#iconSelect"></use></svg>' : Constants.ZWSP,
|
|
|
|
|
click(element) {
|
2023-08-01 20:42:30 +08:00
|
|
|
const acceleratorElement = element.querySelector(".b3-menu__accelerator");
|
2023-08-01 20:42:10 +08:00
|
|
|
if (mSelectElement.dataset.type === "select") {
|
|
|
|
|
window.siyuan.menus.menu.element.querySelectorAll(".b3-menu__accelerator").forEach(itemElement => {
|
|
|
|
|
if (itemElement.isSameNode(acceleratorElement)) {
|
|
|
|
|
if (acceleratorElement.querySelector("svg")) {
|
|
|
|
|
acceleratorElement.innerHTML = "";
|
|
|
|
|
} else {
|
2023-08-01 20:42:30 +08:00
|
|
|
acceleratorElement.innerHTML = '<svg class="svg" style="height: 30px; float: left;"><use xlink:href="#iconSelect"></use></svg>';
|
2023-08-01 20:42:10 +08:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
itemElement.innerHTML = "";
|
|
|
|
|
}
|
2023-08-01 20:42:30 +08:00
|
|
|
});
|
2023-08-01 20:42:10 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (acceleratorElement.querySelector("svg")) {
|
|
|
|
|
acceleratorElement.innerHTML = "";
|
|
|
|
|
} else {
|
2023-08-01 20:42:30 +08:00
|
|
|
acceleratorElement.innerHTML = '<svg class="svg" style="height: 30px; float: left;"><use xlink:href="#iconSelect"></use></svg>';
|
2023-08-01 20:42:10 +08:00
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2023-08-01 20:42:30 +08:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
const mSelecttRect = mSelectElement.getBoundingClientRect();
|
2023-08-01 20:42:10 +08:00
|
|
|
mSelectMenu.open({
|
|
|
|
|
x: mSelecttRect.left,
|
|
|
|
|
y: mSelecttRect.bottom
|
2023-08-01 20:42:30 +08:00
|
|
|
});
|
|
|
|
|
event.stopPropagation();
|
2023-08-01 20:42:10 +08:00
|
|
|
event.preventDefault();
|
|
|
|
|
return;
|
2023-08-01 12:27:47 +08:00
|
|
|
}
|
2023-08-01 20:42:30 +08:00
|
|
|
});
|
2023-07-31 23:58:45 +08:00
|
|
|
element.querySelectorAll(".b3-text-field--text").forEach((item: HTMLInputElement) => {
|
|
|
|
|
item.addEventListener("change", () => {
|
2023-07-31 23:59:02 +08:00
|
|
|
let value;
|
2023-08-03 15:08:07 +08:00
|
|
|
if (["url", "text", "email", "phone"].includes(item.parentElement.dataset.type)) {
|
2023-07-31 23:58:45 +08:00
|
|
|
value = {
|
2023-08-03 15:08:07 +08:00
|
|
|
[item.parentElement.dataset.type]: {
|
2023-07-31 23:58:45 +08:00
|
|
|
content: item.value
|
|
|
|
|
}
|
2023-07-31 23:59:02 +08:00
|
|
|
};
|
2023-07-31 23:58:45 +08:00
|
|
|
} else if (item.parentElement.dataset.type === "number") {
|
|
|
|
|
value = {
|
|
|
|
|
number: {
|
|
|
|
|
content: parseFloat(item.value)
|
|
|
|
|
}
|
2023-07-31 23:59:02 +08:00
|
|
|
};
|
2023-07-31 23:58:45 +08:00
|
|
|
}
|
|
|
|
|
fetchPost("/api/av/setAttributeViewBlockAttr", {
|
|
|
|
|
avID: item.parentElement.dataset.avId,
|
|
|
|
|
keyID: item.parentElement.dataset.keyId,
|
|
|
|
|
rowID: item.parentElement.dataset.blockId,
|
2023-08-01 00:15:55 +08:00
|
|
|
cellID: item.parentElement.dataset.id,
|
2023-07-31 23:58:45 +08:00
|
|
|
value
|
2023-07-31 23:59:02 +08:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
2023-07-31 11:32:36 +08:00
|
|
|
});
|
|
|
|
|
};
|