Browse Source

上传表单支持webuploader事件

ihipop 4 years ago
parent
commit
e29205098e
2 changed files with 18 additions and 1 deletions
  1. 7 1
      resources/views/form/file.blade.php
  2. 11 0
      src/Form/Field/File.php

+ 7 - 1
resources/views/form/file.blade.php

@@ -76,7 +76,13 @@
         uploader = Dcat.Uploader(opts);
         uploader.build();
         uploader.preview();
-
+        /* register events */
+        for (i = 0; i < events.length; i++) {
+            var evt = events[i]
+            if (evt.event && evt.script) {
+                uploader.uploader.on(evt.event, (new Function('return ' + evt.script)()).bind(uploader))
+            }
+        }
         function resize() {
             setTimeout(function () {
                 if (! uploader) return;

+ 11 - 0
src/Form/Field/File.php

@@ -14,6 +14,8 @@ class File extends Field implements UploadFieldInterface
     use WebUploader,
         UploadField;
 
+    protected $events = [];
+
     public function __construct($column, $arguments = [])
     {
         parent::__construct($column, $arguments);
@@ -159,6 +161,7 @@ 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();
@@ -175,4 +178,12 @@ class File extends Field implements UploadFieldInterface
             $this->default = implode(',', $this->default);
         }
     }
+
+    public function on(string $event, string $script)
+    {
+        $this->events[] = compact('event', 'script');
+
+        return $this;
+    }
+
 }