From a35d3d8778dae7929d2fbddb34377b320737985f Mon Sep 17 00:00:00 2001 From: nathan Date: Fri, 3 Mar 2023 14:06:11 -0700 Subject: [PATCH] Et2LinkList: Fire change event when link is removed Avoid error if instance manager is missing --- api/js/etemplate/Et2Link/Et2LinkList.ts | 54 ++++++++++++++++++----- api/js/etemplate/Et2Link/Et2LinkString.ts | 2 +- 2 files changed, 45 insertions(+), 11 deletions(-) diff --git a/api/js/etemplate/Et2Link/Et2LinkList.ts b/api/js/etemplate/Et2Link/Et2LinkList.ts index 371c811d02..4e2667d19e 100644 --- a/api/js/etemplate/Et2Link/Et2LinkList.ts +++ b/api/js/etemplate/Et2Link/Et2LinkList.ts @@ -113,6 +113,7 @@ export class Et2LinkList extends Et2LinkString this._handleRowContext = this._handleRowContext.bind(this); this._handleChange = this._handleChange.bind(this); + this._handleLinkToChange = this._handleLinkToChange.bind(this); } connectedCallback() @@ -121,19 +122,28 @@ export class Et2LinkList extends Et2LinkString this._createContextMenu(); // Look for LinkTo and listen for change so we can update - this.getInstanceManager().DOMContainer.querySelectorAll("et2-link-to").forEach(link => + if(this.getInstanceManager()) { - link.addEventListener("et2-change", this._handleChange); - }) + this.getInstanceManager().DOMContainer.querySelectorAll("et2-link-to").forEach(link => + { + link.addEventListener("et2-change", this._handleLinkToChange); + }) + } + + this.addEventListener("change", this._handleChange); } disconnectedCallback() { super.disconnectedCallback(); - this.getInstanceManager().DOMContainer.querySelectorAll("et2-link-to").forEach(link => + if(this.getInstanceManager()) { - link.removeEventListener("et2-change", this._handleChange); - }) + this.getInstanceManager().DOMContainer.querySelectorAll("et2-link-to").forEach(link => + { + link.removeEventListener("et2-change", this._handleLinkToChange); + }) + } + this.removeEventListener("change", this._handleChange); } protected _listTemplate() @@ -247,13 +257,27 @@ export class Et2LinkList extends Et2LinkString } } + /** + * Handle & pass on an internal change + * @param {ChangeEvent} _event + * @protected + */ + protected _handleChange(_event : Event) + { + if(!this.onchange) + { + return; + } + this.onchange(this, _event.data, _event) + } + /** * We listen to LinkTo widgets so we can update * * @param _ev * @protected */ - protected _handleChange(_ev) + protected _handleLinkToChange(_ev) { if(_ev && typeof _ev.currentTarget) { @@ -301,7 +325,9 @@ export class Et2LinkList extends Et2LinkString return html``; } return html` - { this._delete_link(link); @@ -341,6 +367,9 @@ export class Et2LinkList extends Et2LinkString this._link_list.splice(this._link_list.indexOf(link), 1); } this.dispatchEvent(new CustomEvent("et2-delete", {bubbles: true, detail: link})); + let change = new Event("change", {bubbles: true}); + change['data'] = link; + this.dispatchEvent(change); }; // Unsaved entry, had no ID yet @@ -391,7 +420,12 @@ export class Et2LinkList extends Et2LinkString for(let id in list) { let link = list[id]; - if(link.app) + if(!link.app && typeof list.splice !== "undefined") + { + list.splice(parseInt(id), 1); + continue; + } + else if(link.app) { // Temp IDs can cause problems since the ID includes the file name or : if(link.link_id && typeof link.link_id != 'number') @@ -415,9 +449,9 @@ export class Et2LinkList extends Et2LinkString { link.title = link.id.name || ''; } - (this._link_list).push(link); } } + this._addLinks(list); } else { diff --git a/api/js/etemplate/Et2Link/Et2LinkString.ts b/api/js/etemplate/Et2Link/Et2LinkString.ts index 7a99d9a31b..c96721064d 100644 --- a/api/js/etemplate/Et2Link/Et2LinkString.ts +++ b/api/js/etemplate/Et2Link/Et2LinkString.ts @@ -248,7 +248,7 @@ export class Et2LinkString extends Et2Widget(LitElement) implements et2_IDetache ); */ - + this.dispatchEvent(new Event("change", {bubbles: true})); } /**