From e46b30bb0e51a23173d28c7d4854f30129aa2a8f Mon Sep 17 00:00:00 2001
From: Patrizio Bekerle <patrizio@bekerle.com>
Date: Thu, 29 Apr 2021 09:29:04 +0200
Subject: [PATCH] Fix error when Nextcloud file picker is not present and do
 element fetching refactoring

---
 packages/file-handling/package.json       |  2 +-
 packages/file-handling/src/file-sink.js   | 11 +++++++----
 packages/file-handling/src/file-source.js | 22 +++++++++++++++++-----
 3 files changed, 25 insertions(+), 10 deletions(-)

diff --git a/packages/file-handling/package.json b/packages/file-handling/package.json
index 9b6ed623..9975a4a1 100644
--- a/packages/file-handling/package.json
+++ b/packages/file-handling/package.json
@@ -1,7 +1,7 @@
 {
   "name": "@dbp-toolkit/file-handling",
   "homepage": "https://gitlab.tugraz.at/dbp/web-components/toolkit/-/tree/master/packages/file-handling",
-  "version": "0.2.2",
+  "version": "0.2.3",
   "main": "src/index.js",
   "license": "LGPL-2.1-or-later",
   "repository": {
diff --git a/packages/file-handling/src/file-sink.js b/packages/file-handling/src/file-sink.js
index 95598b28..9c83bad2 100644
--- a/packages/file-handling/src/file-sink.js
+++ b/packages/file-handling/src/file-sink.js
@@ -198,9 +198,10 @@ export class FileSink extends ScopedElementsMixin(DBPLitElement) {
     }
 
     loadWebdavDirectory() {
+        const filePicker = this._('#nextcloud-file-picker');
 
-        if (this._('#nextcloud-file-picker') && this._('#nextcloud-file-picker').webDavClient !== null) {
-            this._('#nextcloud-file-picker').loadDirectory(this._('#nextcloud-file-picker').directoryPath);
+        if (filePicker && filePicker.webDavClient !== null) {
+            filePicker.loadDirectory(filePicker.directoryPath);
         }
     }
 
@@ -217,8 +218,10 @@ export class FileSink extends ScopedElementsMixin(DBPLitElement) {
         if (this.initialFileHandlingState.target !== '' && typeof this.initialFileHandlingState.target !== 'undefined'  && this.firstOpen) {
             this.activeTarget = this.initialFileHandlingState.target;
             this.nextcloudPath = this.initialFileHandlingState.path;
-            if (this._('#nextcloud-file-picker').webDavClient !== null) {
-                this._('#nextcloud-file-picker').loadDirectory(this.initialFileHandlingState.path);
+            const filePicker = this._('#nextcloud-file-picker');
+
+            if (filePicker && filePicker.webDavClient !== null) {
+                filePicker.loadDirectory(this.initialFileHandlingState.path);
                 console.log("load default nextcloud sink", this.initialFileHandlingState.path);
             }
             this.firstOpen = false;
diff --git a/packages/file-handling/src/file-source.js b/packages/file-handling/src/file-source.js
index e2c1805b..963fae08 100644
--- a/packages/file-handling/src/file-source.js
+++ b/packages/file-handling/src/file-source.js
@@ -389,8 +389,13 @@ export class FileSource extends ScopedElementsMixin(DBPLitElement) {
         if (filePicker) {
             MicroModal.show(filePicker, {
                 disableScroll: true,
-                onClose: modal => { this.isDialogOpen = false;
-                    this._('#nextcloud-file-picker').selectAllButton = true;}
+                onClose: modal => {
+                    this.isDialogOpen = false;
+                    const filePicker = this._('#nextcloud-file-picker');
+
+                    if (filePicker) {
+                        filePicker.selectAllButton = true;}
+                    }
             });
         }
 
@@ -399,8 +404,10 @@ export class FileSource extends ScopedElementsMixin(DBPLitElement) {
         if (this.initialFileHandlingState.target !== '' && typeof this.initialFileHandlingState.target !== 'undefined' && this.firstOpen) {
             this.activeDestination = this.initialFileHandlingState.target;
             this.nextcloudPath = this.initialFileHandlingState.path;
-            if (this._('#nextcloud-file-picker') && this._('#nextcloud-file-picker').webDavClient !== null) {
-                this._('#nextcloud-file-picker').loadDirectory(this.initialFileHandlingState.path);
+            const filePicker = this._('#nextcloud-file-picker');
+
+            if (filePicker && filePicker.webDavClient !== null) {
+                filePicker.loadDirectory(this.initialFileHandlingState.path);
                 //console.log("load default nextcloud source", this.initialFileHandlingState.target);
             }
             this.firstOpen = false;
@@ -409,7 +416,12 @@ export class FileSource extends ScopedElementsMixin(DBPLitElement) {
 
     closeDialog() {
         this.sendSource();
-        this._('#nextcloud-file-picker').selectAllButton = true;
+        const filePicker = this._('#nextcloud-file-picker');
+
+        if (filePicker) {
+            filePicker.selectAllButton = true;
+        }
+
         MicroModal.close(this._('#modal-picker'));
     }
 
-- 
GitLab