Skip to content
Snippets Groups Projects
Commit 957a4a65 authored by Reiter, Christoph's avatar Reiter, Christoph :snake:
Browse files

Fix some event handler leaks with person/organization-select

It was using the click event but never removed the event listener
parent 8c457e48
No related branches found
No related tags found
No related merge requests found
...@@ -25,6 +25,8 @@ export class OrganizationSelect extends AdapterLitElement { ...@@ -25,6 +25,8 @@ export class OrganizationSelect extends AdapterLitElement {
this.cache = {}; this.cache = {};
this.value = ''; this.value = '';
this.context = ''; this.context = '';
this._onDocumentClicked = this._onDocumentClicked.bind(this);
} }
static get properties() { static get properties() {
...@@ -48,16 +50,13 @@ export class OrganizationSelect extends AdapterLitElement { ...@@ -48,16 +50,13 @@ export class OrganizationSelect extends AdapterLitElement {
connectedCallback() { connectedCallback() {
super.connectedCallback(); super.connectedCallback();
document.addEventListener('click', this._onDocumentClicked);
this.updateSelect2(); this.updateSelect2();
this.updateComplete.then(()=> {
// Close the popup when clicking outside of select2
document.addEventListener('click', (ev) => {
if (!ev.composedPath().includes(this)) {
this._closeSelect2();
} }
});
}); disconnectedCallback() {
document.removeEventListener('click', this._onDocumentClicked);
super.disconnectedCallback();
} }
async load_organizations() { async load_organizations() {
...@@ -71,13 +70,15 @@ export class OrganizationSelect extends AdapterLitElement { ...@@ -71,13 +70,15 @@ export class OrganizationSelect extends AdapterLitElement {
return this.cache[this.lang] === undefined; return this.cache[this.lang] === undefined;
} }
_closeSelect2() { _onDocumentClicked(ev) {
// Close the popup when clicking outside of select2
if (!ev.composedPath().includes(this)) {
const $select = this.$('#' + this.selectId); const $select = this.$('#' + this.selectId);
console.assert($select.length, "select2 missing"); if ($select.length && this.select2IsInitialized($select)) {
if (this.select2IsInitialized($select)) {
$select.select2('close'); $select.select2('close');
} }
} }
}
_clearSelect2() { _clearSelect2() {
const $select = this.$('#' + this.selectId); const $select = this.$('#' + this.selectId);
......
...@@ -44,6 +44,8 @@ export class PersonSelect extends ScopedElementsMixin(AdapterLitElement) { ...@@ -44,6 +44,8 @@ export class PersonSelect extends ScopedElementsMixin(AdapterLitElement) {
this.showReloadButton = false; this.showReloadButton = false;
this.reloadButtonTitle = ''; this.reloadButtonTitle = '';
this.showDetails = false; this.showDetails = false;
this._onDocumentClicked = this._onDocumentClicked.bind(this);
} }
static get scopedElements() { static get scopedElements() {
...@@ -84,30 +86,29 @@ export class PersonSelect extends ScopedElementsMixin(AdapterLitElement) { ...@@ -84,30 +86,29 @@ export class PersonSelect extends ScopedElementsMixin(AdapterLitElement) {
connectedCallback() { connectedCallback() {
super.connectedCallback(); super.connectedCallback();
const that = this; document.addEventListener('click', this._onDocumentClicked);
this.updateComplete.then(()=>{ this.updateComplete.then(()=>{
that.$select = that.$('#' + that.selectId); this.$select = this.$('#' + this.selectId);
// Close the popup when clicking outside of select2
document.addEventListener('click', (ev) => {
if (!ev.composedPath().includes(this)) {
this._closeSelect2();
}
});
// try an init when user-interface is loaded // try an init when user-interface is loaded
that.initJSONLD(); this.initJSONLD();
}); });
} }
_closeSelect2() { disconnectedCallback() {
document.removeEventListener('click', this._onDocumentClicked);
super.disconnectedCallback();
}
_onDocumentClicked(ev) {
// Close the popup when clicking outside of select2
if (!ev.composedPath().includes(this)) {
const $select = this.$('#' + this.selectId); const $select = this.$('#' + this.selectId);
console.assert($select.length, "select2 missing"); if ($select.length && this.select2IsInitialized($select)) {
if (this.select2IsInitialized($select)) {
$select.select2('close'); $select.select2('close');
} }
} }
}
initJSONLD(ignorePreset = false) { initJSONLD(ignorePreset = false) {
const that = this; const that = this;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment