瀏覽代碼

文件上传事件监听功能优化

jqh 4 年之前
父節點
當前提交
f474a7c7a2
共有 3 個文件被更改,包括 44 次插入14 次删除
  1. 10 5
      resources/views/form/file.blade.php
  2. 34 4
      src/Form/Field/File.php
  3. 0 5
      src/Form/Field/WebUploader.php

+ 10 - 5
resources/views/form/file.blade.php

@@ -52,9 +52,9 @@
 <script require="@webuploader" init="{!! $selector !!}">
     var uploader,
         newPage,
-        options = {!! $options !!};
+        options = {!! $options !!},
+        events = options.events;
 
-    var events = {!!$events??'[]'!!};
     init();
 
     function init() {
@@ -77,13 +77,18 @@
         uploader = Dcat.Uploader(opts);
         uploader.build();
         uploader.preview();
-        /* register events */
+
         for (i = 0; i < events.length; i++) {
-            var evt = events[i]
+            var evt = events[i];
             if (evt.event && evt.script) {
-                uploader.uploader.on(evt.event, (new Function('return ' + evt.script)()).bind(uploader))
+                if (evt.once) {
+                    uploader.uploader.once(evt.event, evt.script.bind(uploader))
+                } else {
+                    uploader.uploader.on(evt.event, evt.script.bind(uploader))
+                }
             }
         }
+
         function resize() {
             setTimeout(function () {
                 if (! uploader) return;

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

@@ -14,7 +14,10 @@ class File extends Field implements UploadFieldInterface
     use WebUploader,
         UploadField;
 
-    protected $events = [];
+    /**
+     * @var array
+     */
+    protected $options = ['events' => []];
 
     public function __construct($column, $arguments = [])
     {
@@ -161,7 +164,6 @@ class File extends Field implements UploadFieldInterface
             'fileType'      => $this->options['isImage'] ? '' : 'file',
             'showUploadBtn' => ($this->options['autoUpload'] ?? false) ? false : true,
             'options'       => JavaScript::format($this->options),
-            'events'        => JavaScript::format($this->events),
         ]);
 
         return parent::render();
@@ -179,10 +181,38 @@ class File extends Field implements UploadFieldInterface
         }
     }
 
-    public function on(string $event, string $script)
+    /**
+     * Webuploader 事件监听.
+     *
+     * @see http://fex.baidu.com/webuploader/doc/index.html#WebUploader_Uploader_events
+     *
+     * @param string $event
+     * @param string $script
+     * @param bool   $once
+     *
+     * @return $this
+     */
+    public function on(string $event, string $script, bool $once = false)
     {
-        $this->events[] = compact('event', 'script');
+        $script = JavaScript::make($script);
+
+        $this->options['events'][] = compact('event', 'script', 'once');
 
         return $this;
     }
+
+    /**
+     * Webuploader 事件监听(once).
+     *
+     * @see http://fex.baidu.com/webuploader/doc/index.html#WebUploader_Uploader_events
+     *
+     * @param string $event
+     * @param string $script
+     *
+     * @return $this
+     */
+    public function once(string $event, string $script)
+    {
+        return $this->on($event, $script, true);
+    }
 }

+ 0 - 5
src/Form/Field/WebUploader.php

@@ -12,11 +12,6 @@ use Illuminate\Support\Str;
  */
 trait WebUploader
 {
-    /**
-     * @var array
-     */
-    protected $options = [];
-
     /**
      * @param string      $extensions exp. gif,jpg,jpeg,bmp,png
      * @param string|null $mimeTypes  exp. image/*