app.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. requirejs.config({
  2. baseUrl: '../../dist',
  3. paths: {
  4. jquery: '../examples/image-upload/jquery'
  5. }
  6. });
  7. require([ 'webuploader.flashonly' ], function( WebUploader ) {
  8. // 当domReady的时候开始初始化
  9. $(function() {
  10. var $wrap = $('#uploader'),
  11. // 图片容器
  12. $queue = $( '<ul class="filelist"></ul>' )
  13. .appendTo( $wrap.find( '.queueList' ) ),
  14. // 状态栏,包括进度和控制按钮
  15. $statusBar = $wrap.find( '.statusBar' ),
  16. // 文件总体选择信息。
  17. $info = $statusBar.find( '.info' ),
  18. // 上传按钮
  19. $upload = $wrap.find( '.uploadBtn' ),
  20. // 没选择文件之前的内容。
  21. $placeHolder = $wrap.find( '.placeholder' ),
  22. $progress = $statusBar.find( '.progress' ).hide(),
  23. // 添加的文件数量
  24. fileCount = 0,
  25. // 添加的文件总大小
  26. fileSize = 0,
  27. // 优化retina, 在retina下这个值是2
  28. ratio = window.devicePixelRatio || 1,
  29. // 缩略图大小
  30. thumbnailWidth = 110 * ratio,
  31. thumbnailHeight = 110 * ratio,
  32. // 可能有pedding, ready, uploading, confirm, done.
  33. state = 'pedding',
  34. // 所有文件的进度信息,key为file id
  35. percentages = {},
  36. supportTransition = (function(){
  37. var s = document.createElement('p').style,
  38. r = 'transition' in s ||
  39. 'WebkitTransition' in s ||
  40. 'MozTransition' in s ||
  41. 'msTransition' in s ||
  42. 'OTransition' in s;
  43. s = null;
  44. return r;
  45. })(),
  46. // WebUploader实例
  47. uploader;
  48. // 实例化
  49. uploader = WebUploader.create({
  50. pick: {
  51. id: '#filePicker',
  52. label: '点击选择图片'
  53. },
  54. dnd: '#dndArea',
  55. paste: '#uploader',
  56. swf: '../../dist/Uploader.swf',
  57. chunked: true,
  58. // runtimeOrder: 'flash',
  59. sendAsBinary: true,
  60. server: '../../server/fileupload.php',
  61. // server: 'http://liaoxuezhi.fe.baidu.com/webupload/fileupload.php',
  62. // server: 'http://www.2betop.net/fileupload.php',
  63. fileNumLimit: 300,
  64. fileSizeLimit: 200 * 1024 * 1024, // 200 M
  65. fileSingleSizeLimit: 50 * 1024 * 1024 // 50 M
  66. });
  67. // 添加“添加文件”的按钮,
  68. uploader.addButton({
  69. id: '#filePicker2',
  70. label: '继续添加'
  71. });
  72. // 当有文件添加进来时执行,负责view的创建
  73. function addFile( file ) {
  74. var $li = $( '<li id="' + file.id + '">' +
  75. '<p class="title">' + file.name + '</p>' +
  76. '<p class="imgWrap"></p>'+
  77. '<p class="progress"><span></span></p>' +
  78. '</li>' ),
  79. $btns = $('<div class="file-panel">' +
  80. '<span class="cancel">删除</span>' +
  81. '<span class="rotateRight">向右旋转</span>' +
  82. '<span class="rotateLeft">向左旋转</span></div>').appendTo( $li ),
  83. $prgress = $li.find('p.progress span'),
  84. $wrap = $li.find( 'p.imgWrap' ),
  85. $info = $('<p class="error"></p>'),
  86. showError = function( code ) {
  87. switch( code ) {
  88. case 'exceed_size':
  89. text = '文件大小超出';
  90. break;
  91. case 'interrupt':
  92. text = '上传暂停';
  93. break;
  94. default:
  95. text = '上传失败,请重试';
  96. break;
  97. }
  98. $info.text( text ).appendTo( $li );
  99. };
  100. if ( file.getStatus() === 'invalid' ) {
  101. showError( file.statusText );
  102. } else {
  103. // @todo lazyload
  104. $wrap.text( '预览中' );
  105. uploader.makeThumb( file, function( error, src ) {
  106. if ( error ) {
  107. $wrap.text( '不能预览' );
  108. return;
  109. }
  110. var img = $('<img src="'+src+'">');
  111. $wrap.empty().append( img );
  112. }, thumbnailWidth, thumbnailHeight );
  113. percentages[ file.id ] = [ file.size, 0 ];
  114. file.rotation = 0;
  115. }
  116. file.on('statuschange', function( cur, prev ) {
  117. if ( prev === 'progress' ) {
  118. $prgress.hide().width(0);
  119. } else if ( prev === 'queued' ) {
  120. $li.off( 'mouseenter mouseleave' );
  121. $btns.remove();
  122. }
  123. // 成功
  124. if ( cur === 'error' || cur === 'invalid' ) {
  125. console.log( file.statusText );
  126. showError( file.statusText );
  127. percentages[ file.id ][ 1 ] = 1;
  128. } else if ( cur === 'interrupt' ) {
  129. showError( 'interrupt' );
  130. } else if ( cur === 'queued' ) {
  131. percentages[ file.id ][ 1 ] = 0;
  132. } else if ( cur === 'progress' ) {
  133. $info.remove();
  134. $prgress.css('display', 'block');
  135. } else if ( cur === 'complete' ) {
  136. $li.append( '<span class="success"></span>' );
  137. }
  138. $li.removeClass( 'state-' + prev ).addClass( 'state-' + cur );
  139. });
  140. $li.on( 'mouseenter', function() {
  141. $btns.stop().animate({height: 30});
  142. });
  143. $li.on( 'mouseleave', function() {
  144. $btns.stop().animate({height: 0});
  145. });
  146. $btns.on( 'click', 'span', function() {
  147. var index = $(this).index(),
  148. deg;
  149. switch ( index ) {
  150. case 0:
  151. uploader.removeFile( file );
  152. return;
  153. case 1:
  154. file.rotation += 90;
  155. break;
  156. case 2:
  157. file.rotation -= 90;
  158. break;
  159. }
  160. if ( supportTransition ) {
  161. deg = 'rotate(' + file.rotation + 'deg)';
  162. $wrap.css({
  163. '-webkit-transform': deg,
  164. '-mos-transform': deg,
  165. '-o-transform': deg,
  166. 'transform': deg
  167. });
  168. } else {
  169. $wrap.css( 'filter', 'progid:DXImageTransform.Microsoft.BasicImage(rotation='+ (~~((file.rotation/90)%4 + 4)%4) +')');
  170. // use jquery animate to rotation
  171. // $({
  172. // rotation: rotation
  173. // }).animate({
  174. // rotation: file.rotation
  175. // }, {
  176. // easing: 'linear',
  177. // step: function( now ) {
  178. // now = now * Math.PI / 180;
  179. // var cos = Math.cos( now ),
  180. // sin = Math.sin( now );
  181. // $wrap.css( 'filter', "progid:DXImageTransform.Microsoft.Matrix(M11=" + cos + ",M12=" + (-sin) + ",M21=" + sin + ",M22=" + cos + ",SizingMethod='auto expand')");
  182. // }
  183. // });
  184. }
  185. });
  186. $li.appendTo( $queue );
  187. }
  188. // 负责view的销毁
  189. function removeFile( file ) {
  190. var $li = $('#'+file.id);
  191. delete percentages[ file.id ];
  192. updateTotalProgress();
  193. $li.off().find('.file-panel').off().end().remove();
  194. }
  195. function updateTotalProgress() {
  196. var loaded = 0,
  197. total = 0,
  198. spans = $progress.children(),
  199. percent;
  200. $.each( percentages, function( k, v ) {
  201. total += v[ 0 ];
  202. loaded += v[ 0 ] * v[ 1 ];
  203. } );
  204. percent = total ? loaded / total : 0;
  205. spans.eq( 0 ).text( Math.round( percent * 100 ) + '%' );
  206. spans.eq( 1 ).css( 'width', Math.round( percent * 100 ) + '%' );
  207. updateStatus();
  208. }
  209. function updateStatus() {
  210. var text = '', stats;
  211. if ( state === 'ready' ) {
  212. text = '选中' + fileCount + '张图片,共' +
  213. WebUploader.formatSize( fileSize ) + '。';
  214. } else if ( state === 'confirm' ) {
  215. stats = uploader.getStats();
  216. if ( stats.uploadFailNum ) {
  217. text = '已成功上传' + stats.successNum+ '张照片至XX相册,'+
  218. stats.uploadFailNum + '张照片上传失败,<a class="retry" href="#">重新上传</a>失败图片或<a class="ignore" href="#">忽略</a>'
  219. }
  220. } else {
  221. stats = uploader.getStats();
  222. text = '共' + fileCount + '张(' +
  223. WebUploader.formatSize( fileSize ) +
  224. '),已上传' + stats.successNum + '张';
  225. if ( stats.uploadFailNum ) {
  226. text += ',失败' + stats.uploadFailNum + '张';
  227. }
  228. }
  229. $info.html( text );
  230. }
  231. function setState( val ) {
  232. var file, stats;
  233. if ( val === state ) {
  234. return;
  235. }
  236. $upload.removeClass( 'state-' + state );
  237. $upload.addClass( 'state-' + val );
  238. state = val;
  239. switch ( state ) {
  240. case 'pedding':
  241. $placeHolder.removeClass( 'element-invisible' );
  242. $queue.hide();
  243. $statusBar.addClass( 'element-invisible' );
  244. uploader.refresh();
  245. break;
  246. case 'ready':
  247. $placeHolder.addClass( 'element-invisible' );
  248. $( '#filePicker2' ).removeClass( 'element-invisible');
  249. $queue.show();
  250. $statusBar.removeClass('element-invisible');
  251. uploader.refresh();
  252. break;
  253. case 'uploading':
  254. $( '#filePicker2' ).addClass( 'element-invisible' );
  255. $progress.show();
  256. $upload.text( '暂停上传' );
  257. break;
  258. case 'paused':
  259. $progress.show();
  260. $upload.text( '继续上传' );
  261. break;
  262. case 'confirm':
  263. $progress.hide();
  264. $upload.text( '开始上传' ).addClass( 'disabled' );
  265. stats = uploader.getStats();
  266. if ( stats.successNum && !stats.uploadFailNum ) {
  267. setState( 'finish' );
  268. return;
  269. }
  270. break;
  271. case 'finish':
  272. stats = uploader.getStats();
  273. if ( stats.successNum ) {
  274. alert( '上传成功' );
  275. } else {
  276. // 没有成功的图片,重设
  277. state = 'done';
  278. location.reload();
  279. }
  280. break;
  281. }
  282. updateStatus();
  283. }
  284. uploader.onUploadProgress = function( file, percentage ) {
  285. var $li = $('#'+file.id),
  286. $percent = $li.find('.progress span');
  287. $percent.css( 'width', percentage * 100 + '%' );
  288. percentages[ file.id ][ 1 ] = percentage;
  289. updateTotalProgress();
  290. };
  291. uploader.onFileQueued = function( file ) {
  292. fileCount++;
  293. fileSize += file.size;
  294. if ( fileCount === 1 ) {
  295. $placeHolder.addClass( 'element-invisible' );
  296. $statusBar.show();
  297. }
  298. addFile( file );
  299. setState( 'ready' );
  300. updateTotalProgress();
  301. };
  302. uploader.onFileDequeued = function( file ) {
  303. fileCount--;
  304. fileSize -= file.size;
  305. if ( !fileCount ) {
  306. setState( 'pedding' );
  307. }
  308. removeFile( file );
  309. updateTotalProgress();
  310. };
  311. uploader.on( 'all', function( type ) {
  312. var stats;
  313. switch( type ) {
  314. case 'uploadFinished':
  315. setState( 'confirm' );
  316. break;
  317. case 'startUpload':
  318. setState( 'uploading' );
  319. break;
  320. case 'stopUpload':
  321. setState( 'paused' );
  322. break;
  323. }
  324. });
  325. uploader.onError = function( code ) {
  326. alert( 'Eroor: ' + code );
  327. };
  328. $upload.on('click', function() {
  329. if ( $(this).hasClass( 'disabled' ) ) {
  330. return false;
  331. }
  332. if ( state === 'ready' ) {
  333. uploader.upload();
  334. } else if ( state === 'paused' ) {
  335. uploader.upload();
  336. } else if ( state === 'uploading' ) {
  337. uploader.stop();
  338. }
  339. });
  340. $info.on( 'click', '.retry', function() {
  341. uploader.retry();
  342. } );
  343. $info.on( 'click', '.ignore', function() {
  344. alert( 'todo' );
  345. } );
  346. $upload.addClass( 'state-' + state );
  347. updateTotalProgress();
  348. });
  349. });