AddFile.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. export default class AddFile {
  2. constructor(Uploder) {
  3. this.uploader = Uploder;
  4. }
  5. // 渲染新文件
  6. render(file) {
  7. let _this = this,
  8. parent = _this.uploader,
  9. showImg = parent.isImage(),
  10. size = WebUploader.formatSize(file.size),
  11. $li,
  12. $btns,
  13. fileName = file.name || null;
  14. if (showImg) {
  15. $li = $(`<li id="${parent.getFileViewSelector(file.id)}" title="${fileName}" >
  16. <p class="file-type">${(file.ext.toUpperCase() || 'FILE')}</p>
  17. <p class="imgWrap "></p>
  18. <p class="title" style="">${file.name}</p>
  19. <p class="title" style="margin-bottom:20px;">(<b>${size}</b>)</p>
  20. </li>`);
  21. $btns = $(`<div class="file-panel">
  22. <a class="btn btn-sm btn-white" data-file-act="cancel"><i class="feather icon-x red-dark" style="font-size:13px"></i></a>
  23. <a class="btn btn-sm btn-white" data-file-act="delete" style="display: none">
  24. <i class="feather icon-trash red-dark" style="font-size:13px"></i></a>
  25. <a class="btn btn-sm btn-white" data-file-act="preview" ><i class="feather icon-zoom-in"></i></a>
  26. <a class='btn btn-sm btn-white' data-file-act='order' data-order="1" style="display: none"><i class='feather icon-arrow-up'></i></a>
  27. <a class='btn btn-sm btn-white' data-file-act='order' data-order="0" style="display: none"><i class='feather icon-arrow-down'></i></a>
  28. </div>`).appendTo($li);
  29. } else {
  30. $li = $(`
  31. <li id="${parent.getFileViewSelector(file.id)}" title="${file.nam}">
  32. <p class="title" style="display:block">
  33. <i class="feather icon-check green _success icon-success"></i>
  34. ${file.name} (${size})
  35. </p>
  36. </li>
  37. `);
  38. $btns = $(`
  39. <span style="right: 65px;" class="file-action d-none" data-file-act='order' data-order="1"><i class='feather icon-arrow-up'></i></span>
  40. <span style="right: 45px;" class="file-action d-none" data-file-act='order' data-order="0"><i class='feather icon-arrow-down'></i></span>
  41. <span style="right: 25px;" class="file-action d-none" data-file-act='download' data-id=''><i class='feather icon-download-cloud'></i></span>
  42. <span data-file-act="cancel" class="file-action" style="font-size:13px">
  43. <i class="feather icon-x red-dark"></i>
  44. </span>
  45. <span data-file-act="delete" class="file-action" style="display:none">
  46. <i class="feather icon-trash red-dark"></i>
  47. </span>
  48. `).appendTo($li);
  49. }
  50. $li.appendTo(parent.$files);
  51. setTimeout(function () {
  52. $li.css({margin: '5px'});
  53. }, 50);
  54. if (file.getStatus() === 'invalid') {
  55. _this.showError($li, file.statusText, file);
  56. } else {
  57. if (showImg) {
  58. // 显示图片
  59. _this.showImage($li, file)
  60. }
  61. parent.percentages[file.id] = [file.size, 0];
  62. file.rotation = 0;
  63. }
  64. file.on('statuschange', _this.resolveStatusChangeCallback($li, $btns, file));
  65. let $act = showImg ? $btns.find('a') : $btns;
  66. $act.on('click', _this.resolveActionsCallback(file));
  67. }
  68. // 显示错误信息
  69. showError ($li, code, file) {
  70. let _this = this,
  71. lang = _this.uploader.lang,
  72. text = '',
  73. $info = $('<p class="error"></p>');
  74. switch (code) {
  75. case 'exceed_size':
  76. text = lang.trans('exceed_size');
  77. break;
  78. case 'interrupt':
  79. text = lang.trans('interrupt');
  80. break;
  81. default:
  82. text = lang.trans('upload_failed');
  83. break;
  84. }
  85. _this.uploader.faildFiles[file.id] = file;
  86. $info.text(text).appendTo($li);
  87. }
  88. // 显示图片
  89. showImage($li, file) {
  90. let _this = this,
  91. uploader = _this.uploader.uploader,
  92. lang = _this.uploader.lang,
  93. $wrap = $li.find('p.imgWrap');
  94. var image = uploader.makeThumb(file, function (error, src) {
  95. var img;
  96. $wrap.empty();
  97. if (error) {
  98. $li.find('.title').show();
  99. $li.find('.file-type').show();
  100. return;
  101. }
  102. if (_this.uploader.helper.isSupportBase64) {
  103. img = $('<img src="' + src + '">');
  104. $wrap.append(img);
  105. } else {
  106. $li.find('.file-type').show();
  107. }
  108. });
  109. try {
  110. image.once('load', function () {
  111. file._info = file._info || image.info();
  112. file._meta = file._meta || image.meta();
  113. var width = file._info.width,
  114. height = file._info.height;
  115. // 验证图片宽高
  116. if (! _this.validateDimensions(file)) {
  117. Dcat.error(lang.trans('dimensions') || 'The image dimensions is invalid.');
  118. uploader.removeFile(file);
  119. return false;
  120. }
  121. image.resize(width, height);
  122. });
  123. } catch (e) {
  124. // 不是图片
  125. return setTimeout(function () {
  126. uploader.removeFile(file);
  127. }, 10);
  128. }
  129. }
  130. // 状态变化回调
  131. resolveStatusChangeCallback($li, $btns, file) {
  132. let _this = this,
  133. parent = _this.uploader;
  134. return function (cur, prev, a) {
  135. if (prev === 'progress') {
  136. // $prgress.hide().width(0);
  137. } else if (prev === 'queued') {
  138. $btns.find('[data-file-act="cancel"]').hide();
  139. $btns.find('[data-file-act="delete"]').show();
  140. }
  141. // 成功
  142. if (cur === 'error' || cur === 'invalid') {
  143. _this.showError($li, file.statusText, file);
  144. parent.percentages[file.id][1] = 1;
  145. } else if (cur === 'interrupt') {
  146. _this.showError($li, 'interrupt', file);
  147. } else if (cur === 'queued') {
  148. parent.percentages[file.id][1] = 0;
  149. } else if (cur === 'progress') {
  150. // 移除错误信息
  151. _this.removeError($li);
  152. // $prgress.css('display', 'block');
  153. } else if (cur === 'complete') {
  154. if (_this.uploader.isImage()) {
  155. $li.append('<span class="success"><em></em><i class="feather icon-check"></i></span>');
  156. } else {
  157. $li.find('._success').show();
  158. }
  159. }
  160. $li.removeClass('state-' + prev).addClass('state-' + cur);
  161. };
  162. }
  163. // 操作按钮回调
  164. resolveActionsCallback(file) {
  165. let _this = this,
  166. parent = _this.uploader,
  167. uploader = parent.uploader,
  168. helper = parent.helper;
  169. return function () {
  170. var index = $(this).data('file-act');
  171. switch (index) {
  172. case 'cancel':
  173. uploader.removeFile(file);
  174. return;
  175. case 'deleteurl':
  176. case 'delete':
  177. // 本地删除
  178. if (parent.options.removable) {
  179. parent.input.delete(file.serverId);
  180. return uploader.removeFile(file);
  181. }
  182. // 删除请求
  183. parent.request.delete(file, function () {
  184. // 删除成功回调
  185. parent.input.delete(file.serverId);
  186. uploader.removeFile(file);
  187. });
  188. break;
  189. case 'preview':
  190. Dcat.helpers.previewImage($(this).parent().parent().find('img').attr('src'), null, file.name);
  191. break;
  192. case 'order':
  193. $(this).attr('data-id', file.serverId);
  194. helper.orderFiles($(this));
  195. break;
  196. case 'download':
  197. window.open($(this).attr('data-id'));
  198. break;
  199. }
  200. };
  201. }
  202. // 移除错误信息
  203. removeError($li) {
  204. $li.find('.error').remove()
  205. }
  206. // 图片宽高验证
  207. validateDimensions(file) {
  208. let _this = this,
  209. parent = _this.uploader,
  210. options = parent.options,
  211. dimensions = options.dimensions,
  212. width = file._info.width,
  213. height = file._info.height,
  214. isset = Dcat.helpers.isset;
  215. // The image dimensions is invalid.
  216. if (! parent.isImage() || ! _this.isImage(file) || ! Dcat.helpers.len(options.dimensions)) {
  217. return true;
  218. }
  219. if (
  220. (isset(dimensions, 'width') && dimensions['width'] != width) ||
  221. (isset(dimensions, 'min_width') && dimensions['min_width'] > width) ||
  222. (isset(dimensions, 'max_width') && dimensions['max_width'] < width) ||
  223. (isset(dimensions, 'height') && dimensions['height'] != height) ||
  224. (isset(dimensions, 'min_height') && dimensions['min_height'] > height) ||
  225. (isset(dimensions, 'max_height') && dimensions['max_height'] < height) ||
  226. (isset(dimensions, 'ratio') && dimensions['ratio'] != (width / height))
  227. ) {
  228. return false;
  229. }
  230. return true;
  231. }
  232. // 判断是否是图片
  233. isImage (file) {
  234. return file.type.match(/^image/);
  235. }
  236. }