2024-06-12 22:47:13 +02:00
|
|
|
import {css, html, LitElement, nothing, TemplateResult} from "lit";
|
2024-06-12 19:48:50 +02:00
|
|
|
import {customElement} from "lit/decorators/custom-element.js";
|
|
|
|
import {Et2Widget} from "../Et2Widget/Et2Widget";
|
|
|
|
import {Favorite} from "./Favorite";
|
|
|
|
import {property} from "lit/decorators/property.js";
|
|
|
|
import {until} from "lit/directives/until.js";
|
|
|
|
import {repeat} from "lit/directives/repeat.js";
|
|
|
|
|
2024-06-12 22:47:13 +02:00
|
|
|
/**
|
|
|
|
* @summary A menu listing a user's favorites. Populated from the user's preferences.
|
|
|
|
*
|
|
|
|
* @dependency sl-menu
|
|
|
|
* @dependency sl-menu-item
|
|
|
|
* @dependency sl-menu-label
|
|
|
|
* @dependency et2-image
|
|
|
|
*
|
|
|
|
* @slot - Add additional menu items
|
|
|
|
*/
|
2024-06-12 19:48:50 +02:00
|
|
|
@customElement("et2-favorites-menu")
|
|
|
|
export class Et2FavoritesMenu extends Et2Widget(LitElement)
|
|
|
|
{
|
|
|
|
static get styles()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
super.styles,
|
|
|
|
css`
|
|
|
|
:host {
|
|
|
|
min-width: 15em;
|
|
|
|
}
|
|
|
|
|
|
|
|
et2-image[src="trash"] {
|
|
|
|
display: none;
|
|
|
|
}
|
|
|
|
|
|
|
|
sl-menu-item:hover et2-image[src="trash"] {
|
|
|
|
display: initial;
|
|
|
|
}`
|
|
|
|
]
|
|
|
|
};
|
|
|
|
|
2024-06-12 22:47:13 +02:00
|
|
|
/**
|
|
|
|
* The current application we're showing favorites for.
|
|
|
|
*
|
|
|
|
* @type {string}
|
|
|
|
*/
|
2024-06-12 19:48:50 +02:00
|
|
|
@property()
|
|
|
|
application : string;
|
|
|
|
|
2024-06-14 00:11:07 +02:00
|
|
|
@property()
|
|
|
|
noAdd : boolean = false;
|
|
|
|
|
2024-06-12 22:47:13 +02:00
|
|
|
private favorites : { [name : string] : Favorite } = {
|
|
|
|
'blank': {
|
|
|
|
name: typeof this.egw()?.lang == "function" ? this.egw().lang("No filters") : "No filters",
|
|
|
|
state: {},
|
|
|
|
group: false
|
|
|
|
}
|
|
|
|
};
|
2024-06-12 19:48:50 +02:00
|
|
|
private loadingPromise = Promise.resolve();
|
|
|
|
|
2024-06-14 00:11:07 +02:00
|
|
|
constructor()
|
|
|
|
{
|
|
|
|
super();
|
|
|
|
this.handlePreferenceChange = this.handlePreferenceChange.bind(this);
|
|
|
|
}
|
2024-06-12 19:48:50 +02:00
|
|
|
connectedCallback()
|
|
|
|
{
|
|
|
|
super.connectedCallback();
|
|
|
|
|
|
|
|
if(this.application)
|
|
|
|
{
|
2024-06-14 00:11:07 +02:00
|
|
|
this._load();
|
|
|
|
}
|
|
|
|
document.addEventListener("preferenceChange", this.handlePreferenceChange);
|
|
|
|
}
|
|
|
|
|
|
|
|
disconnectedCallback()
|
|
|
|
{
|
|
|
|
super.disconnectedCallback();
|
|
|
|
document.removeEventListener("preferenceChange", this.handlePreferenceChange);
|
|
|
|
}
|
|
|
|
|
|
|
|
private _load()
|
|
|
|
{
|
|
|
|
this.loadingPromise = Favorite.load(this.egw(), this.application).then((favorites) =>
|
|
|
|
{
|
|
|
|
this.favorites = favorites;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
handlePreferenceChange(e)
|
|
|
|
{
|
|
|
|
if(e && e.detail?.application == this.application)
|
|
|
|
{
|
|
|
|
this._load();
|
|
|
|
this.requestUpdate();
|
2024-06-12 19:48:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSelect(event)
|
|
|
|
{
|
2024-06-14 00:11:07 +02:00
|
|
|
if(event.detail.item.value == Favorite.ADD_VALUE)
|
|
|
|
{
|
|
|
|
return this.handleAdd(event);
|
|
|
|
}
|
2024-06-12 19:48:50 +02:00
|
|
|
Favorite.applyFavorite(this.egw(), this.application, event.detail.item.value);
|
|
|
|
}
|
|
|
|
|
2024-06-14 00:11:07 +02:00
|
|
|
handleAdd(event)
|
|
|
|
{
|
|
|
|
event.stopPropagation();
|
|
|
|
if(this.egw().window && this.egw().window.app[this.application])
|
|
|
|
{
|
|
|
|
this.egw().window.app[this.application].add_favorite({});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-12 19:48:50 +02:00
|
|
|
handleDelete(event)
|
|
|
|
{
|
|
|
|
// Don't trigger click
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
|
|
const menuItem = event.target.closest("sl-menu-item");
|
|
|
|
menuItem.setAttribute("loading", "");
|
|
|
|
|
|
|
|
const favoriteName = menuItem.value;
|
|
|
|
|
|
|
|
// Remove from server
|
|
|
|
Favorite.remove(this.egw(), this.application, favoriteName).then(() =>
|
|
|
|
{
|
|
|
|
// Remove from widget
|
|
|
|
delete this.favorites[favoriteName];
|
|
|
|
this.requestUpdate();
|
2024-06-14 00:11:07 +02:00
|
|
|
|
|
|
|
this.updateComplete.then(() =>
|
|
|
|
{
|
|
|
|
this.dispatchEvent(new CustomEvent("preferenceChange", {
|
|
|
|
bubbles: true,
|
|
|
|
composed: true,
|
|
|
|
detail: {
|
|
|
|
application: this.application,
|
|
|
|
preference: favoriteName
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
});
|
2024-06-12 19:48:50 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
this.requestUpdate();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected menuItemTemplate(name : string, favorite : Favorite) : TemplateResult
|
|
|
|
{
|
2024-06-12 22:47:13 +02:00
|
|
|
let is_admin = (typeof this.egw()?.app == "function") && (typeof this.egw()?.app('admin') != "undefined");
|
2024-06-12 19:48:50 +02:00
|
|
|
|
|
|
|
//@ts-ignore option.group does not exist
|
|
|
|
let icon = (favorite.group !== false && !is_admin || ['blank', '~add~'].includes(name)) ? "" : html`
|
|
|
|
<et2-image slot="suffix" src="trash" icon @click=${this.handleDelete}
|
2024-06-12 22:47:13 +02:00
|
|
|
statustext="${this.egw()?.lang("Delete") ?? "Delete"}"></et2-image>`;
|
2024-06-12 19:48:50 +02:00
|
|
|
|
|
|
|
return html`
|
|
|
|
<sl-menu-item value="${name}">
|
|
|
|
${icon}
|
|
|
|
${favorite.name}
|
|
|
|
</sl-menu-item>`;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected loadingTemplate()
|
|
|
|
{
|
|
|
|
return html`
|
2024-06-12 22:47:13 +02:00
|
|
|
<sl-menu-item loading>${typeof this.egw()?.lang == "function" ? this.egw().lang("Loading") : "Loading"}
|
|
|
|
</sl-menu-item>`;
|
2024-06-12 19:48:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
render()
|
|
|
|
{
|
|
|
|
let content = this.loadingPromise.then(() =>
|
|
|
|
{
|
|
|
|
return html`
|
|
|
|
<sl-menu
|
2024-06-14 00:11:07 +02:00
|
|
|
part="menu"
|
2024-06-12 19:48:50 +02:00
|
|
|
@sl-select=${this.handleSelect}
|
|
|
|
>
|
2024-06-12 22:47:13 +02:00
|
|
|
${this.label ? html`
|
|
|
|
<sl-menu-label>${this.label}</sl-menu-label>` : nothing}
|
2024-06-12 19:48:50 +02:00
|
|
|
${repeat(Object.keys(this.favorites), (i) => this.menuItemTemplate(i, this.favorites[i]))}
|
2024-06-12 22:47:13 +02:00
|
|
|
<slot></slot>
|
2024-06-14 00:11:07 +02:00
|
|
|
${this.noAdd ? nothing : html`
|
|
|
|
<sl-menu-item value=${Favorite.ADD_VALUE}
|
|
|
|
@sl-select=${this.handleAdd}
|
|
|
|
>
|
|
|
|
<sl-icon name="plus" slot="prefix"></sl-icon>
|
|
|
|
${this.egw().lang("Current view as favourite")}
|
|
|
|
</sl-menu-item>`
|
|
|
|
}
|
2024-06-12 19:48:50 +02:00
|
|
|
</sl-menu>
|
|
|
|
`;
|
|
|
|
});
|
|
|
|
return html`
|
|
|
|
${until(content, this.loadingTemplate())}
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
}
|