修复 upload 采用拖拽上传时未合并原始元素上的 lay-data 属性配置项的问题

This commit is contained in:
贤心 2022-11-14 00:47:31 +08:00
parent 41d57848f9
commit 441f3dda48

View File

@ -3,12 +3,11 @@
* 上传组件 * 上传组件
*/ */
layui.define('layer' , function(exports){ layui.define(['lay','layer'], function(exports){
"use strict"; "use strict";
var $ = layui.$ var $ = layui.$
,layer = layui.layer ,layer = layui.layer
,hint = layui.hint()
,device = layui.device() ,device = layui.device()
//外部接口 //外部接口
@ -466,20 +465,20 @@ layui.define('layer' , function(exports){
//事件处理 //事件处理
Class.prototype.events = function(){ Class.prototype.events = function(){
var that = this var that = this;
,options = that.config var options = that.config;
//设置当前选择的文件队列 // 设置当前选择的文件队列
,setChooseFile = function(files){ var setChooseFile = function(files){
that.chooseFiles = {}; that.chooseFiles = {};
layui.each(files, function(i, item){ layui.each(files, function(i, item){
var time = new Date().getTime(); var time = new Date().getTime();
that.chooseFiles[time + '-' + i] = item; that.chooseFiles[time + '-' + i] = item;
}); });
} };
//设置选择的文本 // 设置选择的文本
,setChooseText = function(files, filename){ var setChooseText = function(files, filename){
var elemFile = that.elemFile var elemFile = that.elemFile
,item = options.item ? options.item : options.elem ,item = options.item ? options.item : options.elem
,value = files.length > 1 ,value = files.length > 1
@ -494,19 +493,23 @@ layui.define('layer' , function(exports){
elemFile.after('<span class="layui-inline '+ ELEM_CHOOSE +'">'+ value +'</span>'); elemFile.after('<span class="layui-inline '+ ELEM_CHOOSE +'">'+ value +'</span>');
}; };
//点击上传容器 // 合并 lay-options/lay-data 属性配置项
options.elem.off('upload.start').on('upload.start', function(){ var extendAttrs = function(){
var othis = $(this), data = othis.attr('lay-data'); var othis = $(this);
var data = othis.attr('lay-data') || othis.attr('lay-options'); // 优先兼容旧版本
if(data){ if(data){
try{ that.config = $.extend({}, options, lay.options(this, {
data = new Function('return '+ data)(); attr: othis.attr('lay-data') ? 'lay-data' : null
that.config = $.extend({}, options, data); }));
} catch(e){
hint.error('Upload element property lay-data configuration item has a syntax error: ' + data)
}
} }
};
//点击上传容器
options.elem.off('upload.start').on('upload.start', function(){
var othis = $(this);
extendAttrs.call(this);
that.config.item = othis; that.config.item = othis;
that.elemFile[0].click(); that.elemFile[0].click();
}); });
@ -522,9 +525,11 @@ layui.define('layer' , function(exports){
othis.removeAttr('lay-over'); othis.removeAttr('lay-over');
}) })
.off('upload.drop').on('upload.drop', function(e, param){ .off('upload.drop').on('upload.drop', function(e, param){
var othis = $(this), files = param.originalEvent.dataTransfer.files || []; var othis = $(this);
var files = param.originalEvent.dataTransfer.files || [];
othis.removeAttr('lay-over'); othis.removeAttr('lay-over');
extendAttrs.call(this);
setChooseFile(files); setChooseFile(files);
options.auto ? that.upload() : setChooseText(files); //是否自动触发上传 options.auto ? that.upload() : setChooseText(files); //是否自动触发上传
@ -534,6 +539,8 @@ layui.define('layer' , function(exports){
//文件选择 //文件选择
that.elemFile.off('upload.change').on('upload.change', function(){ that.elemFile.off('upload.change').on('upload.change', function(){
var files = this.files || []; var files = this.files || [];
extendAttrs.call(this);
setChooseFile(files); setChooseFile(files);
options.auto ? that.upload() : setChooseText(files); //是否自动触发上传 options.auto ? that.upload() : setChooseText(files); //是否自动触发上传
}); });