Просмотр исходного кода

修复上传表单浏览按钮无法点击bug,调整上传表单代码

jqh 5 лет назад
Родитель
Сommit
e5a4953164

+ 1 - 5
resources/assets/dcat-admin/upload.js

@@ -636,8 +636,6 @@
 
         // 删除表单值
         function deleteInput(id) {
-            console.log(111, id, $input);
-
             if (!id) {
                 return $input.val('');
             }
@@ -648,9 +646,7 @@
 
         // 重新计算按钮定位
         function refreshButton() {
-            setTimeout(function () {
-                uploader.refresh();
-            }, 400);
+            uploader.refresh();
         }
 
         // 添加上传成功文件到表单区域

Разница между файлами не показана из-за своего большого размера
+ 0 - 0
resources/assets/dcat-admin/upload.min.js


+ 19 - 4
resources/views/form/file.blade.php

@@ -33,7 +33,7 @@
 
 <script data-exec-on-popstate>
 LA.ready(function () {
-    var upload, options = {!! $options !!};
+    var upload, options = {!! $options !!}, listenComplete;
 
     init();
 
@@ -52,12 +52,27 @@ LA.ready(function () {
         }, opts);
 
         upload = LA.Uploader(opts);
-
         upload.build();
-
         upload.preview();
-    }
 
+        function resize() {
+            setTimeout(function () {
+                if (! upload) return;
+
+                upload.refreshButton();
+                resize();
 
+                if (! listenComplete) {
+                    listenComplete = 1;
+                    $(document).one('pjax:complete', function () {
+                        upload = null;
+                    });
+                }
+            }, 250);
+        }
+        resize();
+
+
+    }
 });
 </script>

+ 10 - 0
src/Form/Field/BootstrapUploadField.php

@@ -168,6 +168,16 @@ trait BootstrapUploadField
         return $this;
     }
 
+    /**
+     * Get storage instance.
+     *
+     * @return \Illuminate\Filesystem\Filesystem|null
+     */
+    public function getStorage()
+    {
+        return $this->storage;
+    }
+
     /**
      * Specify the directory and name for upload file.
      *

+ 3 - 4
src/Form/Field/File.php

@@ -19,7 +19,7 @@ class File extends Field
      * @var array
      */
     protected static $css = [
-        '/vendor/dcat-admin/webuploader/webuploader.min.css',
+        'vendor/dcat-admin/webuploader/webuploader.min.css',
     ];
 
     /**
@@ -28,8 +28,8 @@ class File extends Field
      * @var array
      */
     protected static $js = [
-        '/vendor/dcat-admin/webuploader/webuploader.min.js',
-        '/vendor/dcat-admin/dcat-admin/upload.min.js',
+        'vendor/dcat-admin/webuploader/webuploader.min.js',
+        'vendor/dcat-admin/dcat-admin/upload.min.js',
     ];
 
     /**
@@ -42,7 +42,6 @@ class File extends Field
     {
         parent::__construct($column, $arguments);
 
-        $this->initStorage();
         $this->setupDefaultOptions();
     }
 

+ 4 - 4
src/Form/Field/ImageField.php

@@ -149,8 +149,8 @@ trait ImageField
             // We merge original name + thumbnail name + extension
             $path = $path.'-'.$name.'.'.$ext;
 
-            if ($this->storage->exists($path)) {
-                $this->storage->delete($path);
+            if ($this->getStorage()->exists($path)) {
+                $this->getStorage()->delete($path);
             }
         }
     }
@@ -184,9 +184,9 @@ trait ImageField
             });
 
             if (!is_null($this->storagePermission)) {
-                $this->storage->put("{$this->getDirectory()}/{$path}", $image->encode(), $this->storagePermission);
+                $this->getStorage()->put("{$this->getDirectory()}/{$path}", $image->encode(), $this->storagePermission);
             } else {
-                $this->storage->put("{$this->getDirectory()}/{$path}", $image->encode());
+                $this->getStorage()->put("{$this->getDirectory()}/{$path}", $image->encode());
             }
         }
 

+ 35 - 10
src/Form/Field/UploadField.php

@@ -74,6 +74,10 @@ trait UploadField
     protected function initStorage()
     {
         $this->disk(config('admin.upload.disk'));
+
+        if (! $this->storage) {
+            $this->storage = false;
+        }
     }
 
     /**
@@ -85,7 +89,7 @@ trait UploadField
      */
     public function renameIfExists(UploadedFile $file)
     {
-        if ($this->storage->exists("{$this->getDirectory()}/$this->name")) {
+        if ($this->getStorage()->exists("{$this->getDirectory()}/$this->name")) {
             $this->name = $this->generateUniqueName($file);
         }
     }
@@ -183,9 +187,9 @@ trait UploadField
             $this->prepareFile($file);
 
             if (!is_null($this->storagePermission)) {
-                $result = $this->storage->putFileAs($this->getDirectory(), $file, $this->name, $this->storagePermission);
+                $result = $this->getStorage()->putFileAs($this->getDirectory(), $file, $this->name, $this->storagePermission);
             } else {
-                $result = $this->storage->putFileAs($this->getDirectory(), $file, $this->name);
+                $result = $this->getStorage()->putFileAs($this->getDirectory(), $file, $this->name);
             }
 
             $this->deleteTempFile();
@@ -446,7 +450,7 @@ trait UploadField
         $originalName = $file->getClientOriginalName();
         $newName = $originalName . '_' . $index . '.' . $extension;
 
-        while ($this->storage->exists("{$this->getDirectory()}/$newName")) {
+        while ($this->getStorage()->exists("{$this->getDirectory()}/$newName")) {
             $index++;
             $newName = $originalName . '_' . $index . '.' . $extension;
         }
@@ -527,18 +531,35 @@ trait UploadField
 
             return;
         }
-        if ($this->storage->exists($path)) {
-            $this->storage->delete($path);
+
+        $storage = $this->getStorage();
+
+        if ($storage->exists($path)) {
+            $storage->delete($path);
         } else {
-            $prefix = $this->storage->url('');
+            $prefix = $storage->url('');
             $path = str_replace($prefix, '', $path);
 
-            if ($this->storage->exists($path)) {
-                $this->storage->delete($path);
+            if ($storage->exists($path)) {
+                $storage->delete($path);
             }
         }
     }
 
+    /**
+     * Get storage instance.
+     *
+     * @return \Illuminate\Filesystem\Filesystem|null
+     */
+    public function getStorage()
+    {
+        if ($this->storage === null) {
+            $this->initStorage();
+        }
+
+        return $this->storage;
+    }
+
     /**
      * Set disk for storage.
      *
@@ -581,9 +602,13 @@ trait UploadField
             return $path;
         }
 
-        return $this->storage->url($path);
+        return $this->getStorage()->url($path);
     }
 
+    /**
+     * @param $permission
+     * @return $this
+     */
     public function storagePermission($permission)
     {
         $this->storagePermission = $permission;

Некоторые файлы не были показаны из-за большого количества измененных файлов