AddFile.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. $wrap = $li.find('p.imgWrap');
  93. var image = uploader.makeThumb(file, function (error, src) {
  94. var img;
  95. $wrap.empty();
  96. if (error) {
  97. $li.find('.title').show();
  98. $li.find('.file-type').show();
  99. return;
  100. }
  101. if (_this.uploader.helper.isSupportBase64) {
  102. img = $('<img src="' + src + '">');
  103. $wrap.append(img);
  104. } else {
  105. $li.find('.file-type').show();
  106. }
  107. });
  108. try {
  109. image.once('load', function () {
  110. file._info = file._info || image.info();
  111. file._meta = file._meta || image.meta();
  112. var width = file._info.width,
  113. height = file._info.height;
  114. // 验证图片宽高
  115. if (! _this.validateDimensions(file)) {
  116. Dcat.error('The image dimensions is invalid.');
  117. uploader.removeFile(file);
  118. return false;
  119. }
  120. image.resize(width, height);
  121. });
  122. } catch (e) {
  123. // 不是图片
  124. return setTimeout(function () {
  125. uploader.removeFile(file);
  126. }, 10);
  127. }
  128. }
  129. // 状态变化回调
  130. resolveStatusChangeCallback($li, $btns, file) {
  131. let _this = this,
  132. parent = _this.uploader;
  133. return function (cur, prev, a) {
  134. if (prev === 'progress') {
  135. // $prgress.hide().width(0);
  136. } else if (prev === 'queued') {
  137. $btns.find('[data-file-act="cancel"]').hide();
  138. $btns.find('[data-file-act="delete"]').show();
  139. }
  140. // 成功
  141. if (cur === 'error' || cur === 'invalid') {
  142. _this.showError($li, file.statusText, file);
  143. parent.percentages[file.id][1] = 1;
  144. } else if (cur === 'interrupt') {
  145. _this.showError($li, 'interrupt', file);
  146. } else if (cur === 'queued') {
  147. parent.percentages[file.id][1] = 0;
  148. } else if (cur === 'progress') {
  149. // 移除错误信息
  150. _this.removeError($li);
  151. // $prgress.css('display', 'block');
  152. } else if (cur === 'complete') {
  153. if (_this.uploader.isImage()) {
  154. $li.append('<span class="success"><em></em><i class="feather icon-check"></i></span>');
  155. } else {
  156. $li.find('._success').show();
  157. }
  158. }
  159. $li.removeClass('state-' + prev).addClass('state-' + cur);
  160. };
  161. }
  162. // 操作按钮回调
  163. resolveActionsCallback(file) {
  164. let _this = this,
  165. parent = _this.uploader,
  166. uploader = parent.uploader,
  167. helper = parent.helper;
  168. return function () {
  169. var index = $(this).data('file-act');
  170. switch (index) {
  171. case 'cancel':
  172. uploader.removeFile(file);
  173. return;
  174. case 'deleteurl':
  175. case 'delete':
  176. // 本地删除
  177. if (parent.options.removable) {
  178. parent.input.delete(file.serverId);
  179. return uploader.removeFile(file);
  180. }
  181. // 删除请求
  182. parent.request.delete(file, function () {
  183. // 删除成功回调
  184. parent.input.delete(file.serverId);
  185. uploader.removeFile(file);
  186. });
  187. break;
  188. case 'preview':
  189. Dcat.helpers.previewImage(parent.$wrapper.find('img').attr('src'), null, file.name);
  190. break;
  191. case 'order':
  192. $(this).attr('data-id', file.serverId);
  193. helper.orderFiles($(this));
  194. break;
  195. case 'download':
  196. window.open($(this).attr('data-id'));
  197. break;
  198. }
  199. };
  200. }
  201. // 移除错误信息
  202. removeError($li) {
  203. $li.find('.error').remove()
  204. }
  205. // 图片宽高验证
  206. validateDimensions(file) {
  207. let _this = this,
  208. parent = _this.uploader,
  209. options = parent.options,
  210. dimensions = options.dimensions,
  211. width = file._info.width,
  212. height = file._info.height,
  213. isset = Dcat.helpers.isset;
  214. // The image dimensions is invalid.
  215. if (! parent.isImage() || ! _this.isImage(file) || ! Dcat.helpers.len(options.dimensions)) {
  216. return true;
  217. }
  218. if (
  219. (isset(dimensions, 'width') && dimensions['width'] != width) ||
  220. (isset(dimensions, 'min_width') && dimensions['min_width'] > width) ||
  221. (isset(dimensions, 'max_width') && dimensions['max_width'] < width) ||
  222. (isset(dimensions, 'height') && dimensions['height'] != height) ||
  223. (isset(dimensions, 'min_height') && dimensions['min_height'] > height) ||
  224. (isset(dimensions, 'max_height') && dimensions['max_height'] < height) ||
  225. (isset(dimensions, 'ratio') && dimensions['ratio'] != (width / height))
  226. ) {
  227. return false;
  228. }
  229. return true;
  230. }
  231. // 判断是否是图片
  232. isImage (file) {
  233. return file.type.match(/^image/);
  234. }
  235. }