From c845088ebc723d795fdbfddd460c24d58e8b244d Mon Sep 17 00:00:00 2001 From: nathan Date: Thu, 13 Jun 2024 16:11:07 -0600 Subject: [PATCH] Favourites: - dispatch event when adding / removing preference - favourite widgets listen for event to update --- api/js/etemplate/Et2Favorites/Et2Favorites.ts | 21 +++--- .../Et2Favorites/Et2FavoritesMenu.ts | 71 +++++++++++++++++-- api/js/jsapi/egw_app.ts | 9 +++ 3 files changed, 87 insertions(+), 14 deletions(-) diff --git a/api/js/etemplate/Et2Favorites/Et2Favorites.ts b/api/js/etemplate/Et2Favorites/Et2Favorites.ts index 8379fdd21b..09d62e577a 100644 --- a/api/js/etemplate/Et2Favorites/Et2Favorites.ts +++ b/api/js/etemplate/Et2Favorites/Et2Favorites.ts @@ -345,20 +345,21 @@ export class Et2Favorites extends Et2DropdownButton implements et2_INextmatchHea // Hide the trash trash.remove(); - // Delete preference server side - Favorite.remove(this.egw(), this.app, line.value).then(response => + // Delete preference server side, returns boolean + Favorite.remove(this.egw(), this.app, line.value).then(result => { line.classList.remove("loading"); - let result = response.response.find(r => r.type == "data"); + this.dispatchEvent(new CustomEvent("preferenceChange", { + bubbles: true, + composed: true, + detail: { + application: this.application, + preference: line.value + } + })); - // Could not find the result we want - if(!result || result.type !== "data") - { - return; - } - - if(typeof result.data == 'boolean' && result.data) + if(result) { // Remove line from list line.remove(); diff --git a/api/js/etemplate/Et2Favorites/Et2FavoritesMenu.ts b/api/js/etemplate/Et2Favorites/Et2FavoritesMenu.ts index 922e991223..c91b3d0ea1 100644 --- a/api/js/etemplate/Et2Favorites/Et2FavoritesMenu.ts +++ b/api/js/etemplate/Et2Favorites/Et2FavoritesMenu.ts @@ -46,6 +46,9 @@ export class Et2FavoritesMenu extends Et2Widget(LitElement) @property() application : string; + @property() + noAdd : boolean = false; + private favorites : { [name : string] : Favorite } = { 'blank': { name: typeof this.egw()?.lang == "function" ? this.egw().lang("No filters") : "No filters", @@ -55,24 +58,63 @@ export class Et2FavoritesMenu extends Et2Widget(LitElement) }; private loadingPromise = Promise.resolve(); + constructor() + { + super(); + this.handlePreferenceChange = this.handlePreferenceChange.bind(this); + } connectedCallback() { super.connectedCallback(); if(this.application) { - this.loadingPromise = Favorite.load(this.egw(), this.application).then((favorites) => - { - this.favorites = favorites; - }); + 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(); } } handleSelect(event) { + if(event.detail.item.value == Favorite.ADD_VALUE) + { + return this.handleAdd(event); + } Favorite.applyFavorite(this.egw(), this.application, event.detail.item.value); } + handleAdd(event) + { + event.stopPropagation(); + if(this.egw().window && this.egw().window.app[this.application]) + { + this.egw().window.app[this.application].add_favorite({}); + } + } + handleDelete(event) { // Don't trigger click @@ -89,6 +131,18 @@ export class Et2FavoritesMenu extends Et2Widget(LitElement) // Remove from widget delete this.favorites[favoriteName]; this.requestUpdate(); + + this.updateComplete.then(() => + { + this.dispatchEvent(new CustomEvent("preferenceChange", { + bubbles: true, + composed: true, + detail: { + application: this.application, + preference: favoriteName + } + })); + }); }); this.requestUpdate(); @@ -123,12 +177,21 @@ export class Et2FavoritesMenu extends Et2Widget(LitElement) { return html` ${this.label ? html` ${this.label}` : nothing} ${repeat(Object.keys(this.favorites), (i) => this.menuItemTemplate(i, this.favorites[i]))} + ${this.noAdd ? nothing : html` + + + ${this.egw().lang("Current view as favourite")} + ` + } `; }); diff --git a/api/js/jsapi/egw_app.ts b/api/js/jsapi/egw_app.ts index cbb13095cd..a056605004 100644 --- a/api/js/jsapi/egw_app.ts +++ b/api/js/jsapi/egw_app.ts @@ -1230,6 +1230,15 @@ export abstract class EgwApp this.egw.set_preference(this.appname, favorite_pref, favorite); } + // Trigger event so widgets can update + document.dispatchEvent(new CustomEvent("preferenceChange", { + bubbles: true, + detail: { + application: this.appname, + preference: favorite_pref + } + })); + // Add to list immediately if(this.sidebox) {