-
Notifications
You must be signed in to change notification settings - Fork 1
/
uploadMore.js
2180 lines (2116 loc) · 85.6 KB
/
uploadMore.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*!
* @Title: uploadMore
* @Version: 1.1
* @Description:文件/图片上传组件,预览
* @Author: GuoZhaoXuan
* @License:Apache License 2.0
*/
layui.define(['upload', 'layer', 'sortable'], function (exports) {
var $ = layui.jquery,
upload = layui.upload,
layer = layui.layer,
Sortable = layui.sortable;
/**
* 绑定事件的模块名
*
* @type {string}
*/
var MOD_NAME = 'uploadMore';
/**
* 构造方法
*
* @param options
*/
var uploadMore = function (options) {
var that = this;
// 初始化配置
that.initOption(options);
that.init(); // 初始化
};
/** 初始化表格 */
uploadMore.prototype.init = function () {
var that = this;
that.initAttrs();
// 先清空
this.delete();
// 嵌入css
$.ajax({
url: layui.cache.modules[MOD_NAME].replace('.js', '.css'),
async: false,
success: function (res) {
$('head').append(
'<style id="' + MOD_NAME + '-css">' + res + '</style>'
);
},
});
// 渲染上传按钮
that.renderUploadBtn();
// 绑定事件
this.bindEvents();
// 初始化成员信息
that.renderInitValue();
return this;
};
/**
*
* 初始化配置
*
* @param options
* @returns {uploadMore}
*/
uploadMore.prototype.initOption = function (options) {
var that = this;
that.options = $.extend(true, {
elem: null, //容器对象
maxNum: 5, // 限制数量 0 无限制
upload: {
// 上传配置,组件 upload
drag: true, // 默认排序
},
// 参考组件 sortable, false 则无排序
sortable: {},
/**
* 上传按钮状态: 1.一直显示(默认) 2.没有成员时显示 3.隐藏
*/
uploadBtnStatus: 1,
// 操作按钮
operation: {
update: true,
preview: true,
delete: true,
},
// 初始化数据
initValue: [],
parseData: function (res) {
return {
code: res.code, // 状态码(此处按0成功)
message: res.msg, // 返回信息
fileInfo: res.data.info, // 文件完整信息
url: res.data.info.url, // 文件地址
mimeType: res.data.info.mimeType,
};
},
on: {
/**
* 添加成员回调
*
* @param itemInfo
* @param obj+
*/
add: function (itemInfo, obj) {
},
/**
* 删除成员回调
*
* @param itemInfo
* @param obj
*/
del: function (itemInfo, obj) {
},
/**
* 上传成功回调
*
* @param itemInfo 成员信息
* @param obj
*/
success: function (itemInfo, obj) {
},
/**
* 上传失败回调
*
* @param errorMsg 错误信息
* @param itemInfo 成员信息
* @param obj
*/
error: function (itemInfo, obj, errorMsg) {
// obj.getItem(index);
},
},
},
options
);
return this;
};
/**
* 初始化属性
*/
uploadMore.prototype.initAttrs = function () {
// 容器对象
this.container = $(this.options.elem);
this.container.addClass('uploadMore-container');
// 上传按钮
this.uploadBtn = null;
this.index = 0; // 当前元素下标
// 排序对象
this.sortable = null;
// 上传对象
this.upload = null;
// 成员信息
this.items = {
// 文件下标:{文件信息}
/*1: {
elem: null, // 成员dom
fileInfo: {},// 文件信息
url: '',// 文件地址
progress: null, // 进度条实例
isShowAction: false, // 是否展示操作按钮
sort:0, // 排序值
isSuccess:false,// 是否上传成功
}*/
};
return this;
};
/**
* 初始化默认成员值
*/
uploadMore.prototype.renderInitValue = function () {
var that = this;
if (that.options.initValue) {
layui.each(that.options.initValue, function (i, v) {
v = layui.type(v) === 'object' ? v : {url: v};
var urlInfo = layui.url(v.url);
var index = that.addItem(null, {
name: urlInfo.pathname[urlInfo.pathname.length - 1],
type: uploadMoreObj.getMimeTypeByFile(v.url),
});
that.successProgress(index, v);
});
that.resetSort();
}
};
/** 绑定各项事件 */
uploadMore.prototype.bindEvents = function () {
var that = this;
// 上传按钮绑定事件
that.bindUpload();
// 拖拽排序按钮
that.bindSortable();
// 上传按钮上传
that.container.delegate('.uploadMore-uploadBtn', 'click', function () {
that.container.find('.uploadMore-uploadInput').trigger('click');
});
// 拖拽上传
if (that.options.upload.drag) {
that.container.delegate(
'.uploadMore-uploadBtn',
'dragover',
function (e) {
e.preventDefault();
that.container.find('.uploadMore-uploadInput').trigger('upload.over');
}
);
that.container.delegate(
'.uploadMore-uploadBtn',
'dragleave',
function (e) {
e.preventDefault();
that.container.find('.uploadMore-uploadInput').trigger('upload.leave');
}
);
that.container.delegate('.uploadMore-uploadBtn', 'drop', function (e) {
e.preventDefault();
that.container.find('.uploadMore-uploadInput').trigger('upload.drop', e);
});
}
// 蒙版展示切换
that.container.delegate(
'.uploadMore-item',
'mouseenter mousemove',
function (e) {
layui.stope(e);
// 显示蒙版
var index = $(this).data('index');
if (that.getItemInfo(index).isShowAction) {
$(this).find('.uploadMore-operation').removeClass('layui-hide').animate({opacity: 0.8}, 200);
}
}
);
that.container.delegate('.uploadMore-item', 'mouseleave', function (e) {
layui.stope(e);
// 隐藏蒙版
$(this).find('.uploadMore-operation').addClass('layui-hide').css({
opacity: 0,
});
});
// 蒙版功能按钮能力
this.container.delegate(
'.uploadMore-operation-action',
'mouseenter',
function (e) {
$(this).css({
color: $(this).hasClass('uploadMore-operation-action-delete') ? 'red' : '#4444d9',
});
}
);
this.container.delegate(
'.uploadMore-operation-action',
'mouseleave',
function (e) {
$(this).css({color: 'white',});
}
);
// 删除按钮事件
this.container.delegate(
'.uploadMore-operation-action-delete',
'click',
function (e) {
layui.stope(e);
that.removeItem(
$(this).parents('.uploadMore-item:eq(0)').data('index')
);
if (that.getCurrentNum(false) < 1) {
// 无数据时显示上传按钮
if (that.options.uploadBtnStatus === 2) {
that.uploadBtn.removeClass('layui-hide');
}
}
}
);
// 文件预览
this.container.delegate(
'.uploadMore-operation-action-preview',
'click',
function (e) {
layui.stope(e);
var item = $(this).parents('.uploadMore-item:eq(0)');
// 图片预览
if (item.find('.uploadMore-file').hasClass('uploadMore-img-preview')) {
var imgIndex = 0;
// 过滤成功成员
that.getAllItem().filter('[data-is-success="true"]').each(function () {
if ($(this).find('.uploadMore-file').hasClass('uploadMore-img-preview')) {
if ($(this).is(item)) {
return false;
} else {
imgIndex++;
}
}
});
that.previewImage(imgIndex);
}
}
);
// 编辑图片
this.container.delegate(
'.uploadMore-operation-action-edit',
'click',
function (e) {
layui.stope(e);
}
);
// 信息提示
this.container.delegate('[uploadMore-tips]', 'mouseenter', function (e) {
layui.layer.tips($(this).attr('uploadMore-tips'), this);
});
return this;
};
/**
* 渲染上传按钮
*/
uploadMore.prototype.renderUploadBtn = function () {
var that = this;
that.uploadBtn = that.getUploadBtnTpl();
that.container.append(
'<div class="layui-hide"><input type="hidden" class="uploadMore-uploadInput"></div>'
);
that.container.append(that.uploadBtn);
// 隐藏上传按钮
if (that.options.uploadBtnStatus === 3) {
that.switchUploadBtnStatus(true);
}
};
/**
* 渲染上传能力
*/
uploadMore.prototype.bindUpload = function () {
var that = this;
// 总上传按钮操作
that.upload = upload.render(
$.extend({}, that.options.upload, that.getUploadCommonConfig(1), {
elem: $(this.container).find('.uploadMore-uploadInput:eq(0)'),
// 同时上传文件数(multiple:true)
number: this.options.maxNum > 0 ? this.options.maxNum : null,
// 是否支持多文件
multiple: true,
unified: false,
})
);
};
/**
* 绑定排序能力
*/
uploadMore.prototype.bindSortable = function () {
var that = this;
if (that.options.sortable !== false) {
that.sortable = new Sortable(
that.container[0],
$.extend(that.options.sortable, {
swapThreshold: 1,
animation: 150,
handle: '.uploadMore-drag', // 设置触发排序区域元素
draggable: '.uploadMore-item', // 允许排序类名
// 列表内元素顺序更新的时候触发
onEnd: function (/**Event*/ evt) {
// same properties as onEnd
that.resetSort();
},
})
);
}
};
/**
* 获取文件下标
*
* @param index
* @returns {null|number}
*/
uploadMore.prototype.getIndex = function (index = null) {
return index ? index : ++this.index;
};
/**
* 添加成员
*
* @param index 成员下标
* @param file 文件对象
* @param sortIndex 排序成员下标
* @param obj 文件操作对象
* @returns {number|null}
*/
uploadMore.prototype.addItem = function (
index = null,
file = null,
sortIndex = null,
obj = null
) {
var that = this;
index = that.getIndex(index);
// 初始化成员信息
that.items[index] = {
fileInfo: null,
index: index,
url: '',
// 是否展示操作按钮
isShowAction: false,
// 文件对象
file: file,
// mime类型
mimeType: file ? file.type : null,
// 排序值
sort: 0,
isUpload: false,
isSuccess: false, // 是否上传成功
};
var itemTpl = that.getItemTpl(index);
if (!sortIndex) {
that.uploadBtn.before(itemTpl);
} else {
// 有成员排序则是编辑操作
that.getItem(sortIndex).before(itemTpl);
that.getItem(sortIndex).remove();
}
// 渲染进度条
var progressFilterName = that.getProgressFilterName(index);
layui.element.render('progress', progressFilterName);
// 初始化成员信息
that.setItemInfo(index, {
elem: itemTpl,
// 进度条
progress: progressFilterName,
// 绑定单文件操作对象
upload: that.bindUploadByItem(index),
});
// 更新当前数量
that.getCurrentNum();
// 重置排序
that.resetSort();
// 切换上传按钮限制
if (!this.isAllowAdd()) {
that.switchUploadStatus(true);
}
// 错误消息
that.getItem(index).find('.uploadMore-error').unbind('mouseenter').bind('mouseenter', function () {
layui.layer.tips($(this).attr('data-tips'), this);
});
// 重新上传
that.getItem(index).find('.uploadMore-retryUpload').unbind('click').bind('click', function () {
// 重置状态信息
that.resetItem(index);
// 重新上传
obj.upload(index, file);
// upload();
// 删除自己
// $(this).parents('.uploadMore-item').remove();
});
// 隐藏上传按钮
if (that.options.uploadBtnStatus === 2) {
that.switchUploadBtnStatus(true);
}
// 执行回调
if (that.options.on.add) {
that.options.on.add(that.getItemInfo(index), that);
}
return index;
};
/**
* 成员中编辑上传操作
*
* @param index
* @returns {*}
*/
uploadMore.prototype.bindUploadByItem = function (index) {
var that = this;
// 单文件编辑
return upload.render(
$.extend({}, that.options.upload, that.getUploadCommonConfig(2, index), {
elem: that.getItem(index).find('.uploadMore-operation-action-edit')[0],
// 是否支持多文件
multiple: false,
})
);
};
/**
* 获取上传公共配置
*
* @param type 1.总上传按钮 2.成员编辑
* @param itemIndex type==2时 的成员下标
* @returns {{before: before, progress: progress, choose: choose, done: done}}
*/
uploadMore.prototype.getUploadCommonConfig = function (
type = 1,
itemIndex = null
) {
var that = this;
return {
auto: false, // 不自动上传
//**************** 跳过内部校验/组件接管校验 start **********
/* accept: 'file',// images 图片类型(默认) file 所有文件类型 video 视频类型 audio 音频类型
acceptMime: '*', // MIME 类型限制
exts: '*', // 允许上传的文件后缀。一般结合 accept 属性来设定。
size: 0, // 不限制文件大小*/
//**************** 跳过内部校验/组件接管校验 end **********
// 选择文件时
choose: function (obj) {
obj.preview(function (currentIndex, file, result) {
if (that.isAllowAdd() || type === 2) {
// 文件校验
if (that.checkFile(file)) {
that.addItem(currentIndex, file, itemIndex, obj);
// 上传
obj.upload(currentIndex, file);
}
}
// 制空文件
that.container.find('input.uploadMore-uploadInput:eq(0)').next().val('');
});
},
// 进度
progress: function (n, elem, res, currentIndex) {
// 改变进度
that.changeProgress(currentIndex, n);
},
// 上传前
before: function (obj) {
},
// 上传完成
done: function (res, currentIndex, upload) {
var d = that.options.parseData(res);
if (d.code === 0) {
// 进度条 完成
that.successProgress(currentIndex, d);
} else {
// 错误
that.error(currentIndex, d.message, upload);
}
},
// 上传失败
error: function (currentIndex, upload) {
that.error(currentIndex, '上传失败', upload);
},
};
};
/**
* 文件校验(单文件)
*/
uploadMore.prototype.checkFile = function (file) {
var that = this;
// 验证 accept
if (!that.checkAccept(file)) {
console.log('类型校验错误');
return false;
}
// 验证文件大小
if (!that.checkSize(file)) {
console.log('大小校验错误');
return false;
}
return true;
};
/**
* 校验文件类型
*
* @param file
* @returns {boolean}
*/
uploadMore.prototype.checkAccept = function (file) {
var that = this;
// 文件类型名称
var typeNames = {
file: '文件',
images: '图片',
video: '视频',
audio: '音频'
};
var typeName = typeNames[that.options.upload.accept] || '文件';
var text = that.options.upload.text || {}; // 错误信息设置
// 根据文件类型校验
var exts = that.options.upload.exts;
var check = true;
switch (that.options.upload.accept) {
case 'file': // 一般文件
if (exts && !RegExp('.\\.(' + exts + ')$', 'i').test(escape(file.name))) {
check = false;
}
break;
case 'video': // 视频文件
if (!RegExp('.\\.(' + (exts || 'avi|mp4|wma|rmvb|rm|flash|3gp|flv') + ')$', 'i').test(escape(file.name))) {
check = false;
}
break;
case 'audio': // 音频文件
if (!RegExp('.\\.(' + (exts || 'mp3|wav|mid') + ')$', 'i').test(escape(file.name))) {
check = false;
}
break;
default: // 图片文件
if (!RegExp('.\\.(' + (exts || 'jpg|png|gif|bmp|jpeg|svg') + ')$', 'i').test(escape(file.name))) {
check = false;
}
break;
}
if (!check) {
that.msg(text['check-error'] || '选择的' + typeName + '中包含不支持的格式');
}
return check;
};
/**
* 校验文件大小
*
* @param file
* @returns {boolean}
*/
uploadMore.prototype.checkSize = function (file) {
var that = this;
var device = layui.device();
var text = that.options.upload.text || {}; // 错误信息设置
var size = that.options.upload.size;
if (size > 0 && !(device.ie && device.ie < 10)) {
if (file.size > 1024 * size) {
var sizeStr = size / 1024;
sizeStr = sizeStr >= 1 ? sizeStr.toFixed(2) + 'MB' : size + 'KB';
limitSize = sizeStr;
if (limitSize) {
that.msg(typeof text['limit-size'] === 'function' ? text['limit-size'](that.upload.config, limitSize) : '文件大小不能超过 ' + limitSize);
}
return false;
}
}
return true;
};
/**
* 切换总上传状态
* @param isDisabled
*/
uploadMore.prototype.switchUploadStatus = function (isDisabled = true) {
var that = this;
that.container.find('.uploadMore-uploadInput').siblings('input[type=file]:eq(0)').attr('disabled', isDisabled);
};
/**
* 删除成员
*
* @param index 下标
*/
uploadMore.prototype.removeItem = function (index) {
var that = this;
that.getItem(index).remove();
var item = that.getItemInfo(index);
// 移除成员信息
that.delItemInfo(index);
// 更新当前数
that.getCurrentNum();
// 重置排序
that.resetSort();
if (this.isAllowAdd()) {
that.switchUploadStatus(false);
}
// 执行回调
if (that.options.on.del) {
that.options.on.del(item, that);
}
};
/**
* 获取成员
*
* @param index
* @returns {*}
*/
uploadMore.prototype.getItem = function (index) {
return this.container.find('.uploadMore-item[data-index=' + index + ']');
};
/**
* 获取所有的item
*
* @returns {*}
*/
uploadMore.prototype.getAllItem = function () {
return this.container.find('.uploadMore-item');
};
/**
* 充值排序值
*/
uploadMore.prototype.resetSort = function () {
var that = this;
var i = 0;
that.getAllItem().each(function () {
i++;
var index = $(this).data('index');
$(this).attr('data-sort-index', i);
that.setItemInfo(index, {
sort: i,
});
});
};
/** 进度对象 **/
uploadMore.prototype.getProgressIns = function (index) {
var that = this;
return that.getItem(index).find('.uploadMore-progress');
};
/**
* 完成进度
*
* @param index
* @param data
* @param isHide
*/
uploadMore.prototype.successProgress = function (index, data, isHide = true) {
var that = this;
var progress = that.getProgressIns(index);
if (isHide) {
progress.addClass('layui-hide');
}
that.setItemInfo(index, {
isShowAction: true,
fileInfo: data.fileInfo,
url: data.url,
isSuccess: true,
});
that.getItem(index).attr('data-is-success', true);
// 渲染数据
that.getItem(index).find('.uploadMore-file').replaceWith(that.getFileTpl(index));
// 初始进度条
that.changeProgress(index, 0);
// 执行回调
if (that.options.on.success) {
that.options.on.success(that.getItemInfo(index), that);
}
};
/**
* 失败进度处理
*
* @param index
*/
uploadMore.prototype.failProgress = function (index) {
var that = this;
var progress = that.getProgressIns(index);
progress.addClass('layui-hide');
that.setItemInfo(index, {
isShowAction: false,
});
// 初始进度条
that.changeProgress(index, 0);
};
/**
* 错误处理
*
* @param index 下标
* @param errMsg 错误信息
* @param upload 上传重试回调
*/
uploadMore.prototype.error = function (index, errMsg, upload) {
var that = this;
that.failProgress(index);
that.getItem(index).find('.uploadMore-message').removeClass('layui-hide');
that.getItem(index).find('.uploadMore-error').attr('data-tips', errMsg);
// 触发回调
if (that.options.on.error) {
that.options.on.error(that.getItemInfo(index), that, errMsg);
}
};
/**
* 重置信息
*
* @param index
*/
uploadMore.prototype.resetItem = function (index) {
var that = this;
// 隐藏错误信息
that.getItem(index).find('.uploadMore-message').addClass('layui-hide');
// 隐藏进度条
that.getProgressIns(index).addClass('layui-hide');
};
/**
* 改变进度
*/
uploadMore.prototype.changeProgress = function (index, percent) {
var that = this;
var progress = that.getProgressIns(index);
if (percent > 0) {
progress.removeClass('layui-hide');
}
layui.element.progress(that.getProgressFilterName(index), percent + '%');
layui.element.render('progress');
return this;
};
/**
* 获取成员信息
*
* @param index
* @returns {*}
*/
uploadMore.prototype.getItemInfo = function (index) {
return this.items[this.getIndex(index)];
};
/**
* 设置成员信息
*
* @param index
* @param info
* @returns {*}
*/
uploadMore.prototype.setItemInfo = function (index, info) {
var that = this;
that.items[this.getIndex(index)] = $.extend(that.getItemInfo(index), info);
return that.getItemInfo(index);
};
/**
* 删除成员信息
*
*
* @param index
*/
uploadMore.prototype.delItemInfo = function (index) {
var that = this;
delete that.items[index];
};
/**
* 获取当前数量
*
* @param isUpdate
* @returns {*}
*/
uploadMore.prototype.getCurrentNum = function (isUpdate = true) {
var that = this;
if (isUpdate) {
that.currentNum = that.container.find('.uploadMore-item').length;
that.container.find('.uploadMore-currentNum').text(that.currentNum);
}
return that.currentNum;
};
/**
* 是否允许添加成员
*
* @returns {boolean}
*/
uploadMore.prototype.isAllowAdd = function (addNum = 0) {
return (
this.options.maxNum <= 0 ||
(this.options.maxNum > 0 &&
this.getCurrentNum() + addNum < this.options.maxNum)
);
};
/**
* 图片预览
*
* @param start
* @param title
*/
uploadMore.prototype.previewImage = function (start = 0, title = '图片') {
var that = this;
var data = [];
that.getAllItem().each(function () {
var index = $(this).data('index');
var itemInfo = that.getItemInfo(index);
if (itemInfo.isSuccess && $(this).find('.uploadMore-file').hasClass('uploadMore-img-preview')) {
data.push({
alt: title,
pid: index,
src: itemInfo.url,
});
}
});
layer.photos({
photos: {
title: title, // title
start: start, // 起始位置
data: data, // 数据
/* "data": [
{
"alt": title,
"pid": 1,
"src": src,
}
]*/
},
});
};
/**
* 消息弹窗
*
* @param content
* @returns {*}
*/
uploadMore.prototype.msg = function (content) {
return layui.layer.msg(content, {
icon: 2,
shift: 6,
});
};
/**
* 获取元素模板
*/
uploadMore.prototype.getItemTpl = function (index) {
var that = this;
var item = $('<div class="uploadMore-item" data-index="' + index + '"></div>');
var itemInfo = that.getItemInfo(index);
// 插入文件展示
item.append(that.getFileTpl(index));
// 排序展示
if (that.options.sortable !== false) {
// 插入拖拽排序按钮
item.append(
'<span class="uploadMore-drag"><span' +
' style="font-size: 16px; display: inline-flex;"><svg xmlns="http://www.w3.org/2000/svg"' +
' xmlns:xlink="http://www.w3.org/1999/xlink"' +
' aria-hidden="true" role="img"' +
' class="iconify iconify--ant-design" width="1em"' +
' height="1em" preserveAspectRatio="xMidYMid meet"' +
' viewBox="0 0 1024 1024"><path' +
' fill="currentColor"' +
' d="M847.9 592H152c-4.4 0-8 3.6-8 8v60c0 4.4 3.6 8 8 8h605.2L612.9 851c-4.1 5.2-.4 13 6.3 13h72.5c4.9 0 9.5-2.2 12.6-6.1l168.8-214.1c16.5-21 1.6-51.8-25.2-51.8zM872 356H266.8l144.3-183c4.1-5.2.4-13-6.3-13h-72.5c-4.9 0-9.5 2.2-12.6 6.1L150.9 380.2c-16.5 21-1.6 51.8 25.1 51.8h696c4.4 0 8-3.6 8-8v-60c0-4.4-3.6-8-8-8z"></path></svg></span></span>'
);
}
// 插入蒙版操作
if (
that.options.operation.update ||
that.options.operation.preview ||
that.options.operation.delete
) {
var operationBox = $('<div class="uploadMore-operation layui-hide"><div class="uploadMore-operation-box"></div></div>');
// 编辑按钮
if (that.options.operation.update) {
operationBox.find('.uploadMore-operation-box').append(
'<div style="cursor:pointer" class="uploadMore-operation-action uploadMore-operation-action-edit" >' +
' <i class="layui-icon layui-icon-edit"></i>' +
'</div>'
);
}
// 预览按钮
if (that.options.operation.preview && that.isAllowPreview(itemInfo.mimeType)) {
operationBox.find('.uploadMore-operation-box').append(
'<div style="cursor: pointer; color: white;" class="uploadMore-operation-action uploadMore-operation-action-preview" >' +
' <i class="layui-icon layui-icon-eye"></i>' +
'</div>'
);
}
// 删除按钮
if (that.options.operation.delete) {
operationBox.find('.uploadMore-operation-box').append(
'<div style="cursor: pointer; color: white;" class="uploadMore-operation-action uploadMore-operation-action-delete">' +
' <i class="layui-icon layui-icon-delete"></i>' +
'</div>'
);
}
item.append(operationBox);
}
// 加上进度条
var filter = that.getProgressFilterName(index);
item.append(
'<div class="uploadMore-progress">' +
' <div class="uploadMore-progress-box" >' +
' <div class="layui-progress layui-progress-big" lay-filter="' +
filter +
'" lay-showpercent="true">' +
' <div class="layui-progress-bar" lay-percent="0%"></div>' +
' </div>' +
' </div>' +
' </div>'
);
// 加上错误信息展示