AddFile.js 9.6 KB

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