AddUploadedFile.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. export default class AddUploadedFile {
  2. constructor(Uploder) {
  3. this.uploader = Uploder;
  4. // 已上传的文件
  5. this.uploadedFiles = [];
  6. this.init = false;
  7. }
  8. // 渲染已上传文件
  9. render(file) {
  10. let _this = this,
  11. parent = _this.uploader,
  12. options = parent.options,
  13. showImg = parent.isImage(),
  14. html = "";
  15. html += "<li title='" + file.serverPath + "'>";
  16. if (! showImg && options.sortable) {
  17. // 文件排序
  18. html += `
  19. <p style="right: 65px" class="file-action" data-file-act='order' data-order="1" data-id='${file.serverId}'><i class='feather icon-arrow-up'></i></p>
  20. <p style="right: 45px" class="file-action" data-file-act='order' data-order="0" data-id='${file.serverId}'><i class='feather icon-arrow-down'></i></p>
  21. `;
  22. }
  23. // 下载
  24. if (! showImg && options.downloadable) {
  25. html += `
  26. <p style="right: 25px" class="file-action" data-file-act='download' data-id='${file.serverUrl}'><i class='feather icon-download-cloud'></i></p>
  27. `;
  28. }
  29. if (showImg) {
  30. html += `<p class='imgWrap'><img src='${file.serverUrl}'></p>`
  31. } else if (!options.disabled) {
  32. html += `<p class="file-action" data-file-act="delete" data-id="${file.serverId}"><i class="feather icon-trash red-dark"></i></p>`;
  33. }
  34. html += "<p class='title' style=''><i class='feather icon-check text-white icon-success text-white'></i>";
  35. html += file.serverPath;
  36. html += "</p>";
  37. if (showImg) {
  38. html += "<p class='title' style='margin-bottom:20px;'>&nbsp;</p>";
  39. html += "<div class='file-panel' >";
  40. if (!options.disabled) {
  41. html += `<a class='btn btn-sm btn-white' data-file-act='deleteurl' data-id='${file.serverId}'><i class='feather icon-trash red-dark' style='font-size:13px'></i></a>`;
  42. }
  43. html += `<a class='btn btn-sm btn-white' data-file-act='preview' data-url='${file.serverUrl}' ><i class='feather icon-zoom-in'></i></a>`;
  44. if (options.sortable) {
  45. // 文件排序
  46. html += `
  47. <a class='btn btn-sm btn-white' data-file-act='order' data-order="1" data-id='${file.serverId}'><i class='feather icon-arrow-up'></i></a>
  48. <a class='btn btn-sm btn-white' data-file-act='order' data-order="0" data-id='${file.serverId}'><i class='feather icon-arrow-down'></i></a>
  49. `;
  50. }
  51. html += "</div>";
  52. } else {
  53. }
  54. html += "</li>";
  55. html = $(html);
  56. if (!showImg) {
  57. html.find('.file-type').show();
  58. html.find('.title').show();
  59. parent.$wrapper.css('background', 'transparent');
  60. }
  61. // 删除操作
  62. let deleteFile = function () {
  63. var fileId = $(this).data('id');
  64. // 本地删除
  65. if (options.removable) {
  66. html.remove();
  67. return _this.removeFormFile(fileId);
  68. }
  69. // 发起删除请求
  70. parent.request.delete({serverId: fileId}, function () {
  71. // 移除
  72. html.remove();
  73. _this.removeFormFile(fileId);
  74. });
  75. };
  76. // 删除按钮点击事件
  77. html.find('[data-file-act="deleteurl"]').click(deleteFile);
  78. html.find('[data-file-act="delete"]').click(deleteFile);
  79. // 文件排序
  80. if (options.sortable) {
  81. html.find('[data-file-act="order"]').click(function () {
  82. parent.helper.orderFiles($(this));
  83. });
  84. }
  85. if (options.downloadable) {
  86. html.find('[data-file-act="download"]').click(function () {
  87. window.open($(this).attr('data-id'));
  88. });
  89. }
  90. // 图片预览
  91. html.find('[data-file-act="preview"]').click(function () {
  92. var url = $(this).data('url');
  93. Dcat.helpers.previewImage(url);
  94. });
  95. parent.formFiles[file.serverId] = file;
  96. parent.input.add(file.serverId);
  97. parent.$files.append(html);
  98. if (showImg) {
  99. setTimeout(function () {
  100. html.css('margin', '5px');
  101. }, _this.init ? 0 : 400);
  102. _this.init = 1;
  103. }
  104. }
  105. // 重新渲染已上传的文件
  106. reRender() {
  107. for (let i in this.uploadedFiles) {
  108. if (this.uploadedFiles[i]) {
  109. this.render(this.uploadedFiles[i])
  110. }
  111. }
  112. }
  113. // 移除已上传文件
  114. removeFormFile(fileId) {
  115. if (!fileId) {
  116. return;
  117. }
  118. let _this = this,
  119. parent = _this.uploader,
  120. uploader = _this.uploader,
  121. file = parent.formFiles[fileId];
  122. parent.input.delete(fileId);
  123. delete parent.formFiles[fileId];
  124. if (uploader && !file.fake) {
  125. uploader.removeFile(file);
  126. }
  127. parent.status.switch('decrOriginalFileNum');
  128. parent.status.switch('incrFileNumLimit');
  129. if (! Dcat.helpers.len(parent.formFiles) && ! Dcat.helpers.len(parent.percentages)) {
  130. parent.status.switch('pending');
  131. }
  132. }
  133. add(file) {
  134. if (!file.serverId || this.uploader.helper.searchUploadedFile(file.serverId) !== -1) {
  135. return;
  136. }
  137. this.uploadedFiles.push(file)
  138. }
  139. }