From ded874f82281ef6b0c486de92013c2aa03a69fe9 Mon Sep 17 00:00:00 2001
From: Patrizio Bekerle <patrizio.bekerle@tugraz.at>
Date: Tue, 22 Oct 2019 13:17:47 +0200
Subject: [PATCH] Fix error on not initialized selector

---
 packages/person-select/src/vpu-person-select.js | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/packages/person-select/src/vpu-person-select.js b/packages/person-select/src/vpu-person-select.js
index 28ddb6a8..f64b9506 100644
--- a/packages/person-select/src/vpu-person-select.js
+++ b/packages/person-select/src/vpu-person-select.js
@@ -56,7 +56,11 @@ class PersonSelect extends VPULitElementJQuery {
             $(that).blur(() => {
                 // the 500ms delay is a workaround to actually get an item selected when clicking on it,
                 // because the blur gets also fired when clicking in the selector
-                setTimeout(() => {that.$select.select2('close')}, 500);
+                setTimeout(() => {
+                    if (this.select2IsInitialized()) {
+                        that.$select.select2('close');
+                    }
+                }, 500);
             });
 
             // try an init when user-interface is loaded
@@ -218,13 +222,13 @@ class PersonSelect extends VPULitElementJQuery {
                 case "lang":
                     i18n.changeLanguage(this.lang);
 
-                    if (this.$select !== null && this.$select.hasClass("select2-hidden-accessible")) {
+                    if (this.select2IsInitialized()) {
                         // no other way to set an other language at runtime did work
                         this.initSelect2(true);
                     }
                     break;
                 case "value":
-                    if (!this.ignoreValueUpdate && this.$select !== null && this.$select.hasClass("select2-hidden-accessible")) {
+                    if (!this.ignoreValueUpdate && this.select2IsInitialized()) {
                         this.initSelect2();
                     }
 
@@ -239,6 +243,10 @@ class PersonSelect extends VPULitElementJQuery {
         super.update(changedProperties);
     }
 
+    select2IsInitialized() {
+        return this.$select !== null && this.$select.hasClass("select2-hidden-accessible");
+    }
+
     static get styles() {
         // language=css
         return css`
-- 
GitLab