import { connectStore, customElement, html } from '@mantou/gem';
import { theme } from '../../../';
import { bridgeStore, Item, toggleFavorite, updatePath, updateSelection } from '../store';
import { getPathFolder } from '../utils';
import { BridgeBaseElement } from '../base-element';
import './thumbnail';
@connectStore(bridgeStore)
@customElement('bridge-panel-content')
export class BridgePanelContentElement extends BridgeBaseElement {
#clickHandle = (item: Item) => {
if (bridgeStore.selection.has(item)) {
updateSelection([]);
} else {
updateSelection([item]);
}
};
#dbClickHandle = (item: Item) => {
if (item.type === 'folder') {
updatePath([...bridgeStore.path, item.filename]);
}
};
#openContextMenu = (evt: MouseEvent, item: Item) => {
this.openContextMenu({
activeElement: null,
x: evt.x,
y: evt.y,
menu: [
{
text: bridgeStore.favorites.has(item) ? 'remove from favorites' : 'add to favorites',
handle() {
toggleFavorite(item);
},
},
],
});
};
mounted = () => {
this.effect(
() => updatePath(bridgeStore.path),
() => [bridgeStore.path],
);
};
render() {
const folder = getPathFolder((bridgeStore as unknown) as Item, bridgeStore.path);
if (!folder.content) return html``;
const items = Object.values(folder.content).filter((item) => {
return [...bridgeStore.filters].every((filter) => filter(item) === true);
});
return html`
${items.map(
(item) =>
html`
this.#clickHandle(item)}
@dblclick=${() => this.#dbClickHandle(item)}
@contextmenu=${(evt: MouseEvent) => this.#openContextMenu(evt, item)}
.data=${item}
>
`,
)}
`;
}
}