export default class AddUploadedFile {
constructor(Uploder) {
this.uploader = Uploder;
// 已上传的文件
this.uploadedFiles = [];
this.init = false;
}
// 渲染已上传文件
render(file) {
let _this = this,
parent = _this.uploader,
options = parent.options,
showImg = parent.isImage(),
html = "";
html += "
";
if (! showImg && options.sortable) {
// 文件排序
html += `
`;
}
if (showImg) {
html += `
`
} else if (!options.disabled) {
html += `
`;
}
html += "";
html += file.serverPath;
html += "
";
if (showImg) {
html += "
";
html += "";
if (!options.disabled) {
html += `
`;
}
html += `
`;
if (options.sortable) {
// 文件排序
html += `
`;
}
html += "
";
} else {
}
html += "";
html = $(html);
if (!showImg) {
html.find('.file-type').show();
html.find('.title').show();
parent.$wrapper.css('background', 'transparent');
}
// 删除操作
let deleteFile = function () {
var fileId = $(this).data('id');
// 本地删除
if (options.removable) {
html.remove();
return _this.removeFormFile(fileId);
}
// 发起删除请求
parent.request.delete({serverId: fileId}, function () {
// 移除
html.remove();
_this.removeFormFile(fileId);
});
};
// 删除按钮点击事件
html.find('[data-file-act="deleteurl"]').click(deleteFile);
html.find('[data-file-act="delete"]').click(deleteFile);
// 文件排序
if (options.sortable) {
html.find('[data-file-act="order"').click(function () {
parent.helper.orderFiles($(this));
});
}
// 图片预览
html.find('[data-file-act="preview"]').click(function () {
var url = $(this).data('url');
Dcat.helpers.previewImage(url);
});
parent.formFiles[file.serverId] = file;
parent.input.add(file.serverId);
parent.$files.append(html);
if (showImg) {
setTimeout(function () {
html.css('margin', '5px');
}, _this.init ? 0 : 400);
_this.init = 1;
}
}
// 重新渲染已上传的文件
reRender() {
for (let i in this.uploadedFiles) {
if (this.uploadedFiles[i]) {
this.render(this.uploadedFiles[i])
}
}
}
// 移除已上传文件
removeFormFile(fileId) {
if (!fileId) {
return;
}
let _this = this,
parent = _this.uploader,
uploader = _this.uploader,
file = parent.formFiles[fileId];
parent.input.delete(fileId);
delete parent.formFiles[fileId];
if (uploader && !file.fake) {
uploader.removeFile(file);
}
parent.status.switch('decrOriginalFileNum');
parent.status.switch('incrFileNumLimit');
if (! Dcat.helpers.len(parent.formFiles) && ! Dcat.helpers.len(parent.percentages)) {
parent.status.switch('pending');
}
}
add(file) {
if (!file.serverId || this.uploader.helper.searchUploadedFile(file.serverId) !== -1) {
return;
}
this.uploadedFiles.push(file)
}
}