2023-07-09 23:54:32 +08:00
|
|
|
import {Menu} from "../../../plugin/Menu";
|
|
|
|
|
import {transaction} from "../../wysiwyg/transaction";
|
|
|
|
|
import {hasClosestByClassName} from "../../util/hasClosest";
|
|
|
|
|
import {confirmDialog} from "../../../dialog/confirmDialog";
|
|
|
|
|
import {upDownHint} from "../../../util/upDownHint";
|
2023-07-18 01:38:59 +08:00
|
|
|
import {bindEditEvent, getEditHTML} from "./col";
|
2023-07-09 23:54:32 +08:00
|
|
|
|
|
|
|
|
const filterSelectHTML = (key: string, options: { name: string, color: string }[]) => {
|
|
|
|
|
let html = "";
|
|
|
|
|
let hasMatch = false;
|
|
|
|
|
if (options) {
|
|
|
|
|
options.forEach(item => {
|
|
|
|
|
if (!key ||
|
|
|
|
|
(key.toLowerCase().indexOf(item.name.toLowerCase()) > -1 ||
|
|
|
|
|
item.name.toLowerCase().indexOf(key.toLowerCase()) > -1)) {
|
2023-07-18 01:03:35 +08:00
|
|
|
html += `<button data-type="addColOptionOrCell" class="b3-menu__item${html ? "" : " b3-menu__item--current"}" draggable="true" data-name="${item.name}" data-color="${item.color}">
|
2023-07-09 23:54:32 +08:00
|
|
|
<svg class="b3-menu__icon"><use xlink:href="#iconDrag"></use></svg>
|
|
|
|
|
<div class="fn__flex-1">
|
2023-07-10 10:29:26 +08:00
|
|
|
<span class="b3-chip" style="background-color:var(--b3-font-background${item.color});color:var(--b3-font-color${item.color})">
|
2023-07-09 23:54:32 +08:00
|
|
|
<span class="fn__ellipsis">${item.name}</span>
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
2023-07-18 01:03:35 +08:00
|
|
|
<svg class="b3-menu__action" data-type="setColOption"><use xlink:href="#iconEdit"></use></svg>
|
2023-07-09 23:54:32 +08:00
|
|
|
</button>`;
|
|
|
|
|
}
|
|
|
|
|
if (key === item.name) {
|
|
|
|
|
hasMatch = true;
|
|
|
|
|
}
|
2023-07-09 23:55:29 +08:00
|
|
|
});
|
2023-07-09 23:54:32 +08:00
|
|
|
}
|
|
|
|
|
if (!hasMatch && key) {
|
|
|
|
|
const colorIndex = (options?.length || 0) % 13 + 1;
|
2023-07-18 01:03:35 +08:00
|
|
|
html = `<button data-type="addColOptionOrCell" class="b3-menu__item${html ? "" : " b3-menu__item--current"}" data-name="${key}" data-color="${colorIndex}">
|
2023-07-10 00:17:54 +08:00
|
|
|
<svg class="b3-menu__icon"><use xlink:href="#iconAdd"></use></svg>
|
2023-07-09 23:54:32 +08:00
|
|
|
<div class="fn__flex-1">
|
|
|
|
|
<span class="b3-chip" style="background-color:var(--b3-font-background${colorIndex});color:var(--b3-font-color${colorIndex})">
|
|
|
|
|
<span class="fn__ellipsis">${key}</span>
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
<span class="b3-menu__accelerator">Enter</span>
|
2023-07-10 00:17:54 +08:00
|
|
|
</button>${html}`;
|
2023-07-09 23:54:32 +08:00
|
|
|
}
|
|
|
|
|
return html;
|
2023-07-09 23:55:29 +08:00
|
|
|
};
|
2023-07-09 23:54:32 +08:00
|
|
|
|
2023-07-18 01:03:35 +08:00
|
|
|
export const removeCellOption = (protyle: IProtyle, data: IAV, cellElements: HTMLElement[], target: HTMLElement) => {
|
2023-07-10 10:29:26 +08:00
|
|
|
if (!target) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-07-18 01:03:35 +08:00
|
|
|
const colId = cellElements[0].dataset.colId;
|
2023-07-17 19:56:52 +08:00
|
|
|
const doOperations: IOperation[] = [];
|
|
|
|
|
const undoOperations: IOperation[] = [];
|
2023-07-18 01:03:35 +08:00
|
|
|
cellElements.forEach(item => {
|
2023-07-17 19:55:46 +08:00
|
|
|
const rowID = item.parentElement.dataset.id;
|
|
|
|
|
const cellId = item.dataset.id;
|
|
|
|
|
let cellData: IAVCell;
|
|
|
|
|
data.view.rows.find(row => {
|
|
|
|
|
if (row.id === rowID) {
|
|
|
|
|
row.cells.find(cell => {
|
|
|
|
|
if (cell.id === cellId) {
|
|
|
|
|
cellData = cell;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
const oldValue = Object.assign([], cellData.value.mSelect);
|
|
|
|
|
cellData.value.mSelect?.find((item: { content: string }, index: number) => {
|
|
|
|
|
if (item.content === target.dataset.content) {
|
|
|
|
|
cellData.value.mSelect.splice(index, 1);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-07-10 10:29:26 +08:00
|
|
|
|
2023-07-17 19:55:46 +08:00
|
|
|
doOperations.push({
|
|
|
|
|
action: "updateAttrViewCell",
|
|
|
|
|
id: cellId,
|
|
|
|
|
keyID: colId,
|
|
|
|
|
rowID,
|
|
|
|
|
avID: data.id,
|
|
|
|
|
data: cellData.value
|
2023-07-17 19:56:52 +08:00
|
|
|
});
|
2023-07-17 19:55:46 +08:00
|
|
|
undoOperations.push({
|
|
|
|
|
action: "updateAttrViewCell",
|
|
|
|
|
id: cellId,
|
|
|
|
|
keyID: colId,
|
|
|
|
|
rowID,
|
|
|
|
|
avID: data.id,
|
|
|
|
|
data: {
|
|
|
|
|
mSelect: oldValue
|
|
|
|
|
}
|
2023-07-17 19:56:52 +08:00
|
|
|
});
|
|
|
|
|
});
|
2023-07-17 19:55:46 +08:00
|
|
|
transaction(protyle, doOperations, undoOperations);
|
|
|
|
|
target.remove();
|
2023-07-12 00:08:00 +08:00
|
|
|
};
|
2023-07-10 10:29:26 +08:00
|
|
|
|
2023-07-18 01:03:35 +08:00
|
|
|
export const setColOption = (protyle: IProtyle, data: IAV, target: HTMLElement, cellElements?: HTMLElement[]) => {
|
2023-07-10 19:02:59 +08:00
|
|
|
const menuElement = hasClosestByClassName(target, "b3-menu");
|
|
|
|
|
if (!menuElement) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-07-18 01:03:35 +08:00
|
|
|
const colId = cellElements ? cellElements[0].dataset.colId : menuElement.firstElementChild.getAttribute("data-col-id");
|
2023-07-10 19:02:59 +08:00
|
|
|
let name = target.parentElement.dataset.name;
|
|
|
|
|
let color = target.parentElement.dataset.color;
|
2023-07-18 01:03:35 +08:00
|
|
|
const menu = new Menu("av-col-option", () => {
|
2023-07-10 19:02:59 +08:00
|
|
|
if (name === inputElement.value) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-07-09 23:54:32 +08:00
|
|
|
transaction(protyle, [{
|
|
|
|
|
action: "updateAttrViewColOption",
|
|
|
|
|
id: colId,
|
2023-07-13 23:16:07 +08:00
|
|
|
avID: data.id,
|
2023-07-09 23:54:32 +08:00
|
|
|
data: {
|
2023-07-10 11:18:59 +08:00
|
|
|
newColor: color,
|
2023-07-09 23:54:32 +08:00
|
|
|
oldName: name,
|
|
|
|
|
newName: inputElement.value
|
|
|
|
|
},
|
|
|
|
|
}], [{
|
|
|
|
|
action: "updateAttrViewColOption",
|
|
|
|
|
id: colId,
|
2023-07-13 23:16:07 +08:00
|
|
|
avID: data.id,
|
2023-07-09 23:54:32 +08:00
|
|
|
data: {
|
2023-07-10 11:18:59 +08:00
|
|
|
newColor: color,
|
2023-07-09 23:54:32 +08:00
|
|
|
oldName: inputElement.value,
|
|
|
|
|
newName: name
|
|
|
|
|
},
|
|
|
|
|
}]);
|
2023-07-13 10:21:11 +08:00
|
|
|
data.view.columns.find(column => {
|
2023-07-10 19:02:59 +08:00
|
|
|
if (column.id === colId) {
|
|
|
|
|
column.options.find((item) => {
|
|
|
|
|
if (item.name === name) {
|
|
|
|
|
item.name = inputElement.value;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-07-18 01:03:35 +08:00
|
|
|
if (!cellElements) {
|
|
|
|
|
menuElement.innerHTML = getEditHTML({protyle, data, colId});
|
2023-07-18 11:47:47 +08:00
|
|
|
bindEditEvent({protyle, data, menuElement});
|
2023-07-18 01:03:35 +08:00
|
|
|
} else {
|
|
|
|
|
cellElements.forEach((cellElement: HTMLMediaElement) => {
|
|
|
|
|
data.view.rows.find(row => {
|
|
|
|
|
if (row.id === cellElement.parentElement.dataset.id) {
|
|
|
|
|
row.cells.find(cell => {
|
|
|
|
|
if (cell.id === cellElement.dataset.id) {
|
|
|
|
|
cell.value.mSelect.find((item) => {
|
|
|
|
|
if (item.content === name) {
|
|
|
|
|
item.content = inputElement.value;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-07-10 23:22:04 +08:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-07-10 19:02:59 +08:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-07-18 11:47:47 +08:00
|
|
|
});
|
2023-07-18 01:03:35 +08:00
|
|
|
menuElement.innerHTML = getSelectHTML(data.view, cellElements);
|
|
|
|
|
bindSelectEvent(protyle, data, menuElement, cellElements);
|
|
|
|
|
}
|
2023-07-09 23:55:29 +08:00
|
|
|
});
|
2023-07-09 23:54:32 +08:00
|
|
|
if (menu.isOpen) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
menu.addItem({
|
|
|
|
|
iconHTML: "",
|
|
|
|
|
label: `<input class="b3-text-field" style="margin: 4px 0" value="${name}">`
|
2023-07-09 23:55:29 +08:00
|
|
|
});
|
2023-07-09 23:54:32 +08:00
|
|
|
menu.addItem({
|
|
|
|
|
label: window.siyuan.languages.delete,
|
|
|
|
|
icon: "iconTrashcan",
|
|
|
|
|
click() {
|
|
|
|
|
confirmDialog(window.siyuan.languages.deleteOpConfirm, window.siyuan.languages.confirmDelete, () => {
|
2023-07-09 23:55:29 +08:00
|
|
|
let colOptions: { name: string, color: string }[] = [];
|
2023-07-13 10:21:11 +08:00
|
|
|
data.view.columns.find(column => {
|
2023-07-09 23:54:32 +08:00
|
|
|
if (column.id === colId) {
|
|
|
|
|
colOptions = column.options;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2023-07-09 23:55:29 +08:00
|
|
|
});
|
|
|
|
|
const newName = target.parentElement.dataset.name;
|
2023-07-09 23:54:32 +08:00
|
|
|
transaction(protyle, [{
|
|
|
|
|
action: "removeAttrViewColOption",
|
|
|
|
|
id: colId,
|
2023-07-13 10:21:11 +08:00
|
|
|
avID: data.id,
|
2023-07-09 23:54:32 +08:00
|
|
|
data: newName,
|
|
|
|
|
}], [{
|
|
|
|
|
action: "updateAttrViewColOptions",
|
|
|
|
|
id: colId,
|
2023-07-13 09:43:10 +08:00
|
|
|
avID: data.id,
|
2023-07-09 23:54:32 +08:00
|
|
|
data: colOptions
|
|
|
|
|
}]);
|
|
|
|
|
colOptions.find((item, index) => {
|
|
|
|
|
if (item.name === newName) {
|
|
|
|
|
colOptions.splice(index, 1);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2023-07-09 23:55:29 +08:00
|
|
|
});
|
2023-07-18 01:03:35 +08:00
|
|
|
if (!cellElements) {
|
|
|
|
|
menuElement.innerHTML = getEditHTML({protyle, data, colId});
|
2023-07-18 11:47:47 +08:00
|
|
|
bindEditEvent({protyle, data, menuElement});
|
2023-07-18 01:03:35 +08:00
|
|
|
} else {
|
|
|
|
|
cellElements.forEach((cellElement: HTMLElement) => {
|
|
|
|
|
data.view.rows.find(row => {
|
|
|
|
|
if (row.id === cellElement.parentElement.dataset.id) {
|
|
|
|
|
row.cells.find(cell => {
|
|
|
|
|
if (cell.id === cellElement.dataset.id) {
|
|
|
|
|
cell.value.mSelect.find((item, index) => {
|
|
|
|
|
if (item.content === newName) {
|
|
|
|
|
cell.value.mSelect.splice(index, 1);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-07-10 23:22:04 +08:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-07-10 19:02:59 +08:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-07-18 11:47:47 +08:00
|
|
|
});
|
2023-07-18 01:03:35 +08:00
|
|
|
menuElement.innerHTML = getSelectHTML(data.view, cellElements);
|
|
|
|
|
bindSelectEvent(protyle, data, menuElement, cellElements);
|
|
|
|
|
}
|
2023-07-09 23:55:29 +08:00
|
|
|
});
|
2023-07-09 23:54:32 +08:00
|
|
|
}
|
2023-07-09 23:55:29 +08:00
|
|
|
});
|
|
|
|
|
menu.addSeparator();
|
2023-07-09 23:54:32 +08:00
|
|
|
Array.from(Array(13).keys()).forEach(index => {
|
|
|
|
|
menu.addItem({
|
2023-07-10 10:40:29 +08:00
|
|
|
accelerator: parseInt(color) === index + 1 ? '<svg class="svg" style="height: 30px; float: left;"><use xlink:href="#iconSelect"></use></svg>' : undefined,
|
2023-07-09 23:54:32 +08:00
|
|
|
iconHTML: "",
|
|
|
|
|
label: `<span class="color__square" style="padding: 5px;margin: 2px;color: var(--b3-font-color${index + 1});background-color: var(--b3-font-background${index + 1});">A</span>`,
|
2023-07-10 19:02:59 +08:00
|
|
|
click(element) {
|
|
|
|
|
if (element.lastElementChild.classList.contains("b3-menu__accelerator")) {
|
2023-07-12 00:08:00 +08:00
|
|
|
return;
|
2023-07-10 19:02:59 +08:00
|
|
|
}
|
|
|
|
|
element.parentElement.querySelector(".b3-menu__accelerator")?.remove();
|
|
|
|
|
element.insertAdjacentHTML("beforeend", '<span class="b3-menu__accelerator"><svg class="svg" style="height: 30px; float: left;"><use xlink:href="#iconSelect"></use></svg></span>');
|
2023-07-09 23:54:32 +08:00
|
|
|
transaction(protyle, [{
|
|
|
|
|
action: "updateAttrViewColOption",
|
|
|
|
|
id: colId,
|
2023-07-13 23:16:07 +08:00
|
|
|
avID: data.id,
|
2023-07-09 23:54:32 +08:00
|
|
|
data: {
|
2023-07-10 11:18:59 +08:00
|
|
|
oldName: name,
|
|
|
|
|
newName: inputElement.value,
|
2023-07-09 23:54:32 +08:00
|
|
|
oldColor: color,
|
|
|
|
|
newColor: (index + 1).toString()
|
|
|
|
|
},
|
|
|
|
|
}], [{
|
|
|
|
|
action: "updateAttrViewColOption",
|
|
|
|
|
id: colId,
|
2023-07-13 23:16:07 +08:00
|
|
|
avID: data.id,
|
2023-07-09 23:54:32 +08:00
|
|
|
data: {
|
2023-07-10 11:18:59 +08:00
|
|
|
oldName: inputElement.value,
|
|
|
|
|
newName: name,
|
2023-07-09 23:54:32 +08:00
|
|
|
oldColor: (index + 1).toString(),
|
|
|
|
|
newColor: color
|
|
|
|
|
},
|
|
|
|
|
}]);
|
2023-07-10 19:02:59 +08:00
|
|
|
|
2023-07-13 10:21:11 +08:00
|
|
|
data.view.columns.find(column => {
|
2023-07-10 19:02:59 +08:00
|
|
|
if (column.id === colId) {
|
|
|
|
|
column.options.find((item) => {
|
|
|
|
|
if (item.name === name) {
|
|
|
|
|
item.name = inputElement.value;
|
|
|
|
|
item.color = (index + 1).toString();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-07-18 01:03:35 +08:00
|
|
|
if (!cellElements) {
|
|
|
|
|
menuElement.innerHTML = getEditHTML({protyle, data, colId});
|
2023-07-18 11:47:47 +08:00
|
|
|
bindEditEvent({protyle, data, menuElement});
|
2023-07-18 01:03:35 +08:00
|
|
|
} else {
|
|
|
|
|
cellElements.forEach((cellElement: HTMLElement) => {
|
|
|
|
|
data.view.rows.find(row => {
|
|
|
|
|
if (row.id === cellElement.parentElement.dataset.id) {
|
|
|
|
|
row.cells.find(cell => {
|
|
|
|
|
if (cell.id === cellElement.dataset.id) {
|
|
|
|
|
cell.value.mSelect.find((item) => {
|
|
|
|
|
if (item.content === name) {
|
|
|
|
|
item.content = inputElement.value;
|
|
|
|
|
item.color = (index + 1).toString();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-07-10 23:22:04 +08:00
|
|
|
return true;
|
2023-07-10 19:02:59 +08:00
|
|
|
}
|
2023-07-10 23:22:04 +08:00
|
|
|
});
|
2023-07-10 19:02:59 +08:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-07-18 11:47:47 +08:00
|
|
|
});
|
2023-07-18 01:03:35 +08:00
|
|
|
menuElement.innerHTML = getSelectHTML(data.view, cellElements);
|
|
|
|
|
bindSelectEvent(protyle, data, menuElement, cellElements);
|
|
|
|
|
}
|
2023-07-10 19:02:59 +08:00
|
|
|
name = inputElement.value;
|
|
|
|
|
color = (index + 1).toString();
|
|
|
|
|
return true;
|
2023-07-09 23:54:32 +08:00
|
|
|
}
|
2023-07-09 23:55:29 +08:00
|
|
|
});
|
|
|
|
|
});
|
2023-07-09 23:54:32 +08:00
|
|
|
const rect = target.getBoundingClientRect();
|
|
|
|
|
menu.open({
|
2023-07-10 19:02:59 +08:00
|
|
|
x: rect.right,
|
2023-07-09 23:54:32 +08:00
|
|
|
y: rect.bottom,
|
2023-07-10 11:18:59 +08:00
|
|
|
w: rect.width,
|
2023-07-09 23:54:32 +08:00
|
|
|
h: rect.height,
|
2023-07-09 23:55:29 +08:00
|
|
|
});
|
2023-07-09 23:54:32 +08:00
|
|
|
const inputElement = window.siyuan.menus.menu.element.querySelector("input");
|
|
|
|
|
inputElement.select();
|
2023-07-09 23:55:29 +08:00
|
|
|
};
|
2023-07-09 23:54:32 +08:00
|
|
|
|
2023-07-18 01:03:35 +08:00
|
|
|
export const bindSelectEvent = (protyle: IProtyle, data: IAV, menuElement: HTMLElement, cellElements: HTMLElement[]) => {
|
2023-07-09 23:54:32 +08:00
|
|
|
const inputElement = menuElement.querySelector("input");
|
2023-07-18 01:03:35 +08:00
|
|
|
const colId = cellElements[0].dataset.colId;
|
2023-07-09 23:54:32 +08:00
|
|
|
let colData: IAVColumn;
|
2023-07-13 10:21:11 +08:00
|
|
|
data.view.columns.find((item: IAVColumn) => {
|
2023-07-09 23:54:32 +08:00
|
|
|
if (item.id === colId) {
|
|
|
|
|
colData = item;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-07-09 23:55:29 +08:00
|
|
|
});
|
2023-07-09 23:54:32 +08:00
|
|
|
if (!colData.options) {
|
|
|
|
|
colData.options = [];
|
|
|
|
|
}
|
|
|
|
|
inputElement.addEventListener("input", (event: InputEvent) => {
|
|
|
|
|
if (event.isComposing) {
|
2023-07-09 23:55:29 +08:00
|
|
|
return;
|
2023-07-09 23:54:32 +08:00
|
|
|
}
|
|
|
|
|
menuElement.lastElementChild.innerHTML = filterSelectHTML(inputElement.value, colData.options);
|
2023-07-09 23:55:29 +08:00
|
|
|
});
|
2023-07-09 23:54:32 +08:00
|
|
|
inputElement.addEventListener("compositionend", (event: InputEvent) => {
|
|
|
|
|
if (event.isComposing) {
|
2023-07-09 23:55:29 +08:00
|
|
|
return;
|
2023-07-09 23:54:32 +08:00
|
|
|
}
|
|
|
|
|
menuElement.lastElementChild.innerHTML = filterSelectHTML(inputElement.value, colData.options);
|
2023-07-09 23:55:29 +08:00
|
|
|
});
|
2023-07-09 23:54:32 +08:00
|
|
|
inputElement.addEventListener("keydown", (event: KeyboardEvent) => {
|
|
|
|
|
if (event.isComposing) {
|
2023-07-09 23:55:29 +08:00
|
|
|
return;
|
2023-07-09 23:54:32 +08:00
|
|
|
}
|
|
|
|
|
let currentElement = upDownHint(menuElement.lastElementChild, event, "b3-menu__item--current");
|
|
|
|
|
if (event.key === "Enter") {
|
|
|
|
|
if (!currentElement) {
|
|
|
|
|
currentElement = menuElement.querySelector(".b3-menu__item--current");
|
|
|
|
|
}
|
2023-07-18 01:03:35 +08:00
|
|
|
addColOptionOrCell(protyle, data, cellElements, currentElement, menuElement);
|
2023-07-10 10:29:26 +08:00
|
|
|
} else if (event.key === "Backspace" && inputElement.value === "") {
|
2023-07-18 01:03:35 +08:00
|
|
|
removeCellOption(protyle, data, cellElements, inputElement.previousElementSibling as HTMLElement);
|
2023-07-13 11:06:39 +08:00
|
|
|
} else if (event.key === "Escape") {
|
|
|
|
|
menuElement.parentElement.remove();
|
2023-07-09 23:54:32 +08:00
|
|
|
}
|
2023-07-09 23:55:29 +08:00
|
|
|
});
|
|
|
|
|
};
|
2023-07-09 23:54:32 +08:00
|
|
|
|
2023-07-18 01:03:35 +08:00
|
|
|
export const addColOptionOrCell = (protyle: IProtyle, data: IAV, cellElements: HTMLElement[], currentElement: HTMLElement, menuElement: HTMLElement) => {
|
2023-07-17 19:55:46 +08:00
|
|
|
let hasSelected = false;
|
|
|
|
|
Array.from(menuElement.querySelectorAll(".b3-chips .b3-chip")).find((item: HTMLElement) => {
|
|
|
|
|
if (item.dataset.content === currentElement.dataset.name) {
|
|
|
|
|
hasSelected = true;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2023-07-17 19:56:52 +08:00
|
|
|
});
|
2023-07-17 19:55:46 +08:00
|
|
|
if (hasSelected) {
|
|
|
|
|
menuElement.querySelector("input").focus();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-18 01:03:35 +08:00
|
|
|
const colId = cellElements[0].dataset.colId;
|
2023-07-14 00:41:49 +08:00
|
|
|
let cellIndex = 0;
|
2023-07-18 01:03:35 +08:00
|
|
|
Array.from(cellElements[0].parentElement.querySelectorAll(".av__cell")).find((item: HTMLElement, index) => {
|
|
|
|
|
if (item.dataset.id === cellElements[0].dataset.id) {
|
2023-07-14 00:41:49 +08:00
|
|
|
cellIndex = index;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2023-07-15 23:19:40 +08:00
|
|
|
});
|
2023-07-10 00:17:54 +08:00
|
|
|
let colData: IAVColumn;
|
2023-07-13 10:21:11 +08:00
|
|
|
data.view.columns.find((item: IAVColumn) => {
|
2023-07-10 00:17:54 +08:00
|
|
|
if (item.id === colId) {
|
|
|
|
|
colData = item;
|
2023-07-17 19:55:46 +08:00
|
|
|
if (!colData.options) {
|
|
|
|
|
colData.options = [];
|
|
|
|
|
}
|
2023-07-10 00:17:54 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-07-17 19:55:46 +08:00
|
|
|
|
2023-07-17 19:56:52 +08:00
|
|
|
const cellDoOperations: IOperation[] = [];
|
|
|
|
|
const cellUndoOperations: IOperation[] = [];
|
2023-07-18 01:03:35 +08:00
|
|
|
cellElements.forEach(item => {
|
2023-07-17 19:55:46 +08:00
|
|
|
let cellData: IAVCell;
|
|
|
|
|
const rowID = item.parentElement.dataset.id;
|
|
|
|
|
data.view.rows.find(row => {
|
|
|
|
|
if (row.id === rowID) {
|
|
|
|
|
cellData = row.cells[cellIndex];
|
|
|
|
|
// 为空时 cellId 每次请求都不一致
|
|
|
|
|
cellData.id = item.dataset.id;
|
|
|
|
|
if (!cellData.value || !cellData.value.mSelect) {
|
|
|
|
|
cellData.value = {mSelect: []} as IAVCellValue;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
2023-07-14 00:41:49 +08:00
|
|
|
}
|
2023-07-17 19:55:46 +08:00
|
|
|
});
|
2023-07-14 00:41:49 +08:00
|
|
|
|
2023-07-17 19:55:46 +08:00
|
|
|
const oldValue = Object.assign([], cellData.value.mSelect);
|
|
|
|
|
if (colData.type === "mSelect") {
|
|
|
|
|
let hasOption = false;
|
|
|
|
|
cellData.value.mSelect.find((item) => {
|
|
|
|
|
if (item.content === currentElement.dataset.name) {
|
2023-07-17 19:56:52 +08:00
|
|
|
hasOption = true;
|
|
|
|
|
return true;
|
2023-07-17 19:55:46 +08:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
if (!hasOption) {
|
|
|
|
|
cellData.value.mSelect.push({
|
|
|
|
|
color: currentElement.dataset.color,
|
|
|
|
|
content: currentElement.dataset.name
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
cellData.value.mSelect = [{
|
|
|
|
|
color: currentElement.dataset.color,
|
|
|
|
|
content: currentElement.dataset.name
|
|
|
|
|
}];
|
2023-07-13 23:16:07 +08:00
|
|
|
}
|
2023-07-17 19:55:46 +08:00
|
|
|
cellDoOperations.push({
|
|
|
|
|
action: "updateAttrViewCell",
|
|
|
|
|
id: cellData.id,
|
|
|
|
|
keyID: colId,
|
|
|
|
|
rowID,
|
|
|
|
|
avID: data.id,
|
|
|
|
|
data: cellData.value
|
2023-07-17 19:56:52 +08:00
|
|
|
});
|
2023-07-17 19:55:46 +08:00
|
|
|
cellUndoOperations.push({
|
|
|
|
|
action: "updateAttrViewCell",
|
|
|
|
|
id: cellData.id,
|
|
|
|
|
keyID: colId,
|
|
|
|
|
rowID,
|
|
|
|
|
avID: data.id,
|
|
|
|
|
data: {
|
|
|
|
|
[colData.type]: oldValue
|
|
|
|
|
}
|
2023-07-17 19:56:52 +08:00
|
|
|
});
|
|
|
|
|
});
|
2023-07-10 23:22:04 +08:00
|
|
|
|
2023-07-10 00:17:54 +08:00
|
|
|
if (currentElement.querySelector(".b3-menu__accelerator")) {
|
|
|
|
|
colData.options.push({
|
|
|
|
|
color: currentElement.dataset.color,
|
|
|
|
|
name: currentElement.dataset.name
|
|
|
|
|
});
|
2023-07-17 19:55:46 +08:00
|
|
|
cellDoOperations.splice(0, 0, {
|
2023-07-10 00:17:54 +08:00
|
|
|
action: "updateAttrViewColOptions",
|
|
|
|
|
id: colId,
|
2023-07-13 09:43:10 +08:00
|
|
|
avID: data.id,
|
2023-07-10 00:17:54 +08:00
|
|
|
data: colData.options
|
2023-07-17 19:56:52 +08:00
|
|
|
});
|
2023-07-17 19:55:46 +08:00
|
|
|
transaction(protyle, cellDoOperations, [{
|
2023-07-10 00:17:54 +08:00
|
|
|
action: "removeAttrViewColOption",
|
|
|
|
|
id: colId,
|
2023-07-13 10:21:11 +08:00
|
|
|
avID: data.id,
|
2023-07-10 00:17:54 +08:00
|
|
|
data: currentElement.dataset.name,
|
|
|
|
|
}]);
|
|
|
|
|
} else {
|
2023-07-17 19:55:46 +08:00
|
|
|
transaction(protyle, cellDoOperations, cellUndoOperations);
|
2023-07-10 00:17:54 +08:00
|
|
|
}
|
|
|
|
|
if (colData.type === "select") {
|
|
|
|
|
menuElement.parentElement.remove();
|
2023-07-13 23:16:07 +08:00
|
|
|
} else {
|
2023-07-18 01:03:35 +08:00
|
|
|
menuElement.innerHTML = getSelectHTML(data.view, cellElements);
|
|
|
|
|
bindSelectEvent(protyle, data, menuElement, cellElements);
|
2023-07-13 23:16:07 +08:00
|
|
|
menuElement.querySelector("input").focus();
|
2023-07-10 00:17:54 +08:00
|
|
|
}
|
2023-07-12 00:08:00 +08:00
|
|
|
};
|
2023-07-10 00:17:54 +08:00
|
|
|
|
2023-07-18 01:03:35 +08:00
|
|
|
export const getSelectHTML = (data: IAVTable, cellElements: HTMLElement[]) => {
|
|
|
|
|
const colId = cellElements[0].dataset["colId"];
|
2023-07-09 23:54:32 +08:00
|
|
|
const colData = data.columns.find(item => {
|
|
|
|
|
if (item.id === colId) {
|
2023-07-09 23:55:29 +08:00
|
|
|
return item;
|
2023-07-09 23:54:32 +08:00
|
|
|
}
|
2023-07-09 23:55:29 +08:00
|
|
|
});
|
2023-07-17 19:55:46 +08:00
|
|
|
|
2023-07-17 19:56:52 +08:00
|
|
|
const commonOptions: { content: string, color: string }[][] = [];
|
|
|
|
|
const allUniqueOptions: { content: string, color: string }[] = [];
|
2023-07-18 01:03:35 +08:00
|
|
|
cellElements.forEach((cellElement) => {
|
2023-07-17 19:55:46 +08:00
|
|
|
data.rows.find(row => {
|
|
|
|
|
if (cellElement.parentElement.dataset.id === row.id) {
|
2023-07-17 19:56:52 +08:00
|
|
|
const commonOption: { content: string, color: string }[] = [];
|
2023-07-17 19:55:46 +08:00
|
|
|
row.cells.find(cell => {
|
|
|
|
|
if (cell.id === cellElement.dataset.id) {
|
|
|
|
|
if (cell.value && cell.value.mSelect) {
|
|
|
|
|
cell.value.mSelect.forEach((item: { content: string, color: string }) => {
|
2023-07-17 19:56:52 +08:00
|
|
|
commonOption.push(item);
|
|
|
|
|
allUniqueOptions.push(item);
|
2023-07-17 19:55:46 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-07-17 19:56:52 +08:00
|
|
|
commonOptions.push(commonOption);
|
2023-07-17 19:55:46 +08:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2023-07-09 23:55:29 +08:00
|
|
|
let selectedHTML = "";
|
2023-07-17 19:55:46 +08:00
|
|
|
allUniqueOptions.forEach((unique) => {
|
2023-07-17 19:56:52 +08:00
|
|
|
let everyRowHas = true;
|
2023-07-17 19:55:46 +08:00
|
|
|
commonOptions.find(item => {
|
2023-07-17 19:56:52 +08:00
|
|
|
let hasContent = false;
|
2023-07-17 19:55:46 +08:00
|
|
|
item.find((option) => {
|
|
|
|
|
if (option.content === unique.content) {
|
2023-07-17 19:56:52 +08:00
|
|
|
hasContent = true;
|
2023-07-09 23:54:32 +08:00
|
|
|
return true;
|
|
|
|
|
}
|
2023-07-17 19:56:52 +08:00
|
|
|
});
|
2023-07-17 19:55:46 +08:00
|
|
|
if (!hasContent) {
|
2023-07-17 19:56:52 +08:00
|
|
|
everyRowHas = false;
|
|
|
|
|
return true;
|
2023-07-17 19:55:46 +08:00
|
|
|
}
|
2023-07-17 19:56:52 +08:00
|
|
|
});
|
2023-07-17 19:55:46 +08:00
|
|
|
if (everyRowHas && selectedHTML.indexOf(`data-content="${unique.content}"`) === -1) {
|
2023-07-18 01:03:35 +08:00
|
|
|
selectedHTML += `<div class="b3-chip b3-chip--middle" data-content="${unique.content}" style="background-color:var(--b3-font-background${unique.color});color:var(--b3-font-color${unique.color})">${unique.content}<svg class="b3-chip__close" data-type="removeCellOption"><use xlink:href="#iconCloseRound"></use></svg></div>`;
|
2023-07-09 23:54:32 +08:00
|
|
|
}
|
2023-07-09 23:55:29 +08:00
|
|
|
});
|
2023-07-17 19:55:46 +08:00
|
|
|
|
2023-07-27 13:38:14 +08:00
|
|
|
return `<div class="b3-menu__items">
|
|
|
|
|
<div class="b3-chips">
|
2023-07-09 23:54:32 +08:00
|
|
|
${selectedHTML}
|
2023-07-10 09:44:27 +08:00
|
|
|
<input>
|
2023-07-09 23:54:32 +08:00
|
|
|
</div>
|
2023-07-27 13:38:14 +08:00
|
|
|
<div>${filterSelectHTML("", colData.options)}</div>
|
|
|
|
|
</div>`;
|
2023-07-09 23:55:29 +08:00
|
|
|
};
|