From a085322545f1b4909516d47d69247ba9a3ffdf89 Mon Sep 17 00:00:00 2001
From: Christoph Reiter <reiter.christoph@gmail.com>
Date: Thu, 19 Nov 2020 11:44:14 +0100
Subject: [PATCH] check-in-place-select: fix a leak

Properly disconnect from the event
---
 .../src/check-in-place-select.js                | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/packages/check-in-place-select/src/check-in-place-select.js b/packages/check-in-place-select/src/check-in-place-select.js
index cc388322..222b3291 100644
--- a/packages/check-in-place-select/src/check-in-place-select.js
+++ b/packages/check-in-place-select/src/check-in-place-select.js
@@ -38,6 +38,8 @@ export class CheckInPlaceSelect extends ScopedElementsMixin(LitElement) {
         this.showReloadButton = false;
         this.reloadButtonTitle = '';
         this.showCapacity = false;
+
+        this._onDocumentClicked = this._onDocumentClicked.bind(this);
     }
 
     static get scopedElements() {
@@ -87,17 +89,20 @@ export class CheckInPlaceSelect extends ScopedElementsMixin(LitElement) {
         this.$select = this.$('#' + that.selectId);
 
         // Close the popup when clicking outside of select2
-        document.addEventListener('click', (ev) => {
-            if (!ev.composedPath().includes(this)) {
-                this._closeSelect2();
-            }
-        });
+        document.addEventListener('click', this._onDocumentClicked);
 
         // try an init when user-interface is loaded
         this.initJSONLD();
     }
 
-    _closeSelect2() {
+    disconnectedCallback() {
+        document.removeEventListener('click', this._onDocumentClicked);
+        super.disconnectedCallback();
+    }
+
+    _onDocumentClicked(event) {
+        if (event.composedPath().includes(this))
+            return;
         const $select = this.$('#' + this.selectId);
         console.assert($select.length, "select2 missing");
         if (this.select2IsInitialized($select)) {
-- 
GitLab