Status.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. export default class Status {
  2. constructor(Uploder) {
  3. this.uploader = Uploder;
  4. // 可能有pending, ready, uploading, confirm, done.
  5. this.state = 'pending';
  6. // 已上传文件数量
  7. this.originalFilesNum = Dcat.helpers.len(Uploder.options.preview);
  8. }
  9. switch(val, args) {
  10. let _this = this,
  11. parent = _this.uploader;
  12. args = args || {};
  13. if (val === _this.state) {
  14. return;
  15. }
  16. // 上传按钮状态
  17. if (parent.$uploadButton) {
  18. parent.$uploadButton.removeClass('state-' + _this.state);
  19. parent.$uploadButton.addClass('state-' + val);
  20. }
  21. _this.state = val;
  22. switch (_this.state) {
  23. case 'pending':
  24. _this.pending();
  25. break;
  26. case 'ready':
  27. _this.ready();
  28. break;
  29. case 'uploading':
  30. _this.uploading();
  31. break;
  32. case 'paused':
  33. _this.paused();
  34. break;
  35. case 'confirm':
  36. _this.confirm();
  37. break;
  38. case 'finish':
  39. _this.finish();
  40. break;
  41. case 'decrOriginalFileNum':
  42. _this.decrOriginalFileNum();
  43. break;
  44. case 'incrOriginalFileNum':
  45. _this.incrOriginalFileNum();
  46. break;
  47. case 'decrFileNumLimit': // 减少上传文件数量限制
  48. _this.decrFileNumLimit(args.num);
  49. break;
  50. case 'incrFileNumLimit': // 增加上传文件数量限制
  51. _this.incrFileNumLimit(args.num || 1);
  52. break;
  53. case 'init': // 初始化
  54. _this.init();
  55. break;
  56. }
  57. // 更新状态显示
  58. _this.updateStatusText();
  59. }
  60. incrOriginalFileNum() {
  61. this.originalFilesNum++;
  62. }
  63. decrOriginalFileNum() {
  64. if (this.originalFilesNum > 0) {
  65. this.originalFilesNum--;
  66. }
  67. }
  68. confirm() {
  69. let _this = this,
  70. parent = _this.uploader,
  71. uploader = parent.uploader,
  72. stats;
  73. if (uploader) {
  74. parent.$progress.hide();
  75. parent.$selector.find(parent.options.addFileButton).removeClass('element-invisible');
  76. parent.$uploadButton.text(parent.lang.trans('start_upload'));
  77. stats = uploader.getStats();
  78. if (stats.successNum && !stats.uploadFailNum) {
  79. _this.switch('finish');
  80. }
  81. }
  82. }
  83. paused() {
  84. let _this = this,
  85. parent = _this.uploader;
  86. parent.$progress.show();
  87. parent.$uploadButton.text(parent.lang.trans('go_on_upload'));
  88. }
  89. uploading() {
  90. let _this = this,
  91. parent = _this.uploader;
  92. parent.$selector.find(parent.options.addFileButton).addClass('element-invisible');
  93. parent.$progress.show();
  94. parent.$uploadButton.text(parent.lang.trans('pause_upload'));
  95. }
  96. pending() {
  97. let _this = this,
  98. parent = _this.uploader,
  99. options = parent.options;
  100. if (options.disabled) {
  101. return;
  102. }
  103. parent.$placeholder.removeClass('element-invisible');
  104. parent.$files.hide();
  105. parent.$statusBar.addClass('element-invisible');
  106. if (parent.isImage()) {
  107. parent.$wrapper.removeAttr('style');
  108. parent.$wrapper.find('.queueList').removeAttr('style');
  109. }
  110. parent.uploader.refresh();
  111. }
  112. // 减少上传文件数量限制
  113. decrFileNumLimit(num) {
  114. let _this = this,
  115. parent = _this.uploader,
  116. uploader = parent.uploader,
  117. fileLimit;
  118. if (!uploader) {
  119. return;
  120. }
  121. fileLimit = uploader.option('fileNumLimit');
  122. num = num || 1;
  123. if (fileLimit == '-1') {
  124. fileLimit = 0;
  125. }
  126. num = fileLimit >= num ? fileLimit - num : 0;
  127. if (num == 0) {
  128. num = '-1';
  129. }
  130. uploader.option('fileNumLimit', num);
  131. }
  132. // 增加上传文件数量限制
  133. incrFileNumLimit(num) {
  134. let _this = this,
  135. parent = _this.uploader,
  136. uploader = parent.uploader,
  137. fileLimit;
  138. if (!uploader) {
  139. return;
  140. }
  141. fileLimit = uploader.option('fileNumLimit');
  142. num = num || 1;
  143. if (fileLimit == '-1') {
  144. fileLimit = 0;
  145. }
  146. num = fileLimit + num;
  147. uploader.option('fileNumLimit', num);
  148. }
  149. ready() {
  150. let _this = this,
  151. parent = _this.uploader,
  152. options = parent.options;
  153. parent.$placeholder.addClass('element-invisible');
  154. parent.$selector.find(parent.options.addFileButton).removeClass('element-invisible');
  155. parent.$files.show();
  156. if (!options.disabled) {
  157. parent.$statusBar.removeClass('element-invisible');
  158. }
  159. parent.uploader.refresh();
  160. if (parent.isImage()) {
  161. parent.$wrapper.find('.queueList').css({'border': '1px solid #d3dde5', 'padding': '5px'});
  162. // $wrap.find('.queueList').removeAttr('style');
  163. }
  164. // 移除字段验证错误信息
  165. setTimeout(function () {
  166. parent.input.removeValidatorErrors();
  167. }, 10);
  168. }
  169. finish() {
  170. let _this = this,
  171. parent = _this.uploader,
  172. options = parent.options,
  173. uploader = parent.uploader,
  174. stats;
  175. if (uploader) {
  176. stats = uploader.getStats();
  177. if (stats.successNum) {
  178. Dcat.success(parent.lang.trans('upload_success_message', {success: stats.successNum}));
  179. setTimeout(function () {
  180. if (options.upload.fileNumLimit == 1) {
  181. // 单文件上传,需要重置文件上传个数
  182. uploader.request('get-stats').numOfSuccess = 0;
  183. }
  184. }, 10);
  185. } else {
  186. // 没有成功的图片,重设
  187. _this.state = 'done';
  188. Dcat.reload();
  189. }
  190. }
  191. }
  192. // 初始化
  193. init() {
  194. let _this = this,
  195. parent = _this.uploader,
  196. options = parent.options;
  197. parent.$uploadButton.addClass('state-' + _this.state);
  198. _this.updateProgress();
  199. if (_this.originalFilesNum || options.disabled) {
  200. parent.$placeholder.addClass('element-invisible');
  201. if (!options.disabled) {
  202. parent.$statusBar.show();
  203. } else {
  204. parent.$wrapper.addClass('disabled');
  205. }
  206. _this.switch('ready');
  207. } else if (parent.isImage()) {
  208. parent.$wrapper.removeAttr('style');
  209. parent.$wrapper.find('.queueList').css('margin', '0');
  210. }
  211. parent.uploader.refresh();
  212. }
  213. // 状态文本
  214. updateStatusText() {
  215. let _this = this,
  216. parent = _this.uploader,
  217. uploader = parent.uploader,
  218. __ = parent.lang.trans.bind(parent.lang),
  219. text = '',
  220. stats;
  221. if (!uploader) {
  222. return;
  223. }
  224. if (_this.state === 'ready') {
  225. stats = uploader.getStats();
  226. if (parent.fileCount) {
  227. text = __('selected_files', {num: parent.fileCount, size: WebUploader.formatSize(parent.fileSize)});
  228. } else {
  229. showSuccess();
  230. }
  231. } else if (_this.state === 'confirm') {
  232. stats = uploader.getStats();
  233. if (stats.uploadFailNum) {
  234. text = __('selected_has_failed', {success: stats.successNum, fail: stats.uploadFailNum});
  235. }
  236. } else {
  237. showSuccess();
  238. }
  239. function showSuccess() {
  240. stats = uploader.getStats();
  241. if (stats.successNum) {
  242. text = __('selected_success', {num: parent.fileCount, size: WebUploader.formatSize(parent.fileSize), success: stats.successNum});
  243. }
  244. if (stats.uploadFailNum) {
  245. text += (text ? __('dot') : '') + __('failed_num', {fail: stats.uploadFailNum});
  246. }
  247. }
  248. parent.$infoBox.html(text);
  249. }
  250. // 进度条更新
  251. updateProgress() {
  252. let _this = this,
  253. parent = _this.uploader,
  254. loaded = 0,
  255. total = 0,
  256. $bar = parent.$progress.find('.progress-bar'),
  257. percent;
  258. $.each(parent.percentages, function (k, v) {
  259. total += v[0];
  260. loaded += v[0] * v[1];
  261. });
  262. percent = total ? loaded / total : 0;
  263. percent = Math.round(percent * 100) + '%';
  264. $bar.text(percent);
  265. $bar.css('width', percent);
  266. _this.updateStatusText();
  267. }
  268. }