activeadmin_ueditor 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (289) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +56 -0
  3. data/Gemfile +3 -0
  4. data/LICENSE +21 -0
  5. data/LICENSE.txt +20 -0
  6. data/README.md +61 -0
  7. data/Rakefile +3 -0
  8. data/activeadmin_ueditor.gemspec +20 -0
  9. data/app/assets/javascripts/.DS_Store +0 -0
  10. data/app/assets/javascripts/activeadmin/.DS_Store +0 -0
  11. data/app/assets/javascripts/activeadmin/ueditor/.DS_Store +0 -0
  12. data/app/assets/javascripts/activeadmin/ueditor/ueditor.all.js +29568 -0
  13. data/app/assets/javascripts/activeadmin/ueditor/ueditor.all.min.js +18 -0
  14. data/app/assets/javascripts/activeadmin/ueditor/ueditor.config.js +414 -0
  15. data/app/assets/javascripts/activeadmin/ueditor_input.js +14 -0
  16. data/lib/activeadmin/ueditor/engine.rb +11 -0
  17. data/lib/activeadmin/ueditor/version.rb +7 -0
  18. data/lib/activeadmin/ueditor.rb +3 -0
  19. data/lib/activeadmin_ueditor.rb +5 -0
  20. data/lib/formtastic/inputs/ueditor_input.rb +16 -0
  21. data/public/assets/ueditor/dialogs/anchor/anchor.html +40 -0
  22. data/public/assets/ueditor/dialogs/attachment/attachment.css +681 -0
  23. data/public/assets/ueditor/dialogs/attachment/attachment.html +60 -0
  24. data/public/assets/ueditor/dialogs/attachment/attachment.js +754 -0
  25. data/public/assets/ueditor/dialogs/attachment/fileTypeImages/icon_chm.gif +0 -0
  26. data/public/assets/ueditor/dialogs/attachment/fileTypeImages/icon_default.png +0 -0
  27. data/public/assets/ueditor/dialogs/attachment/fileTypeImages/icon_doc.gif +0 -0
  28. data/public/assets/ueditor/dialogs/attachment/fileTypeImages/icon_exe.gif +0 -0
  29. data/public/assets/ueditor/dialogs/attachment/fileTypeImages/icon_jpg.gif +0 -0
  30. data/public/assets/ueditor/dialogs/attachment/fileTypeImages/icon_mp3.gif +0 -0
  31. data/public/assets/ueditor/dialogs/attachment/fileTypeImages/icon_mv.gif +0 -0
  32. data/public/assets/ueditor/dialogs/attachment/fileTypeImages/icon_pdf.gif +0 -0
  33. data/public/assets/ueditor/dialogs/attachment/fileTypeImages/icon_ppt.gif +0 -0
  34. data/public/assets/ueditor/dialogs/attachment/fileTypeImages/icon_psd.gif +0 -0
  35. data/public/assets/ueditor/dialogs/attachment/fileTypeImages/icon_rar.gif +0 -0
  36. data/public/assets/ueditor/dialogs/attachment/fileTypeImages/icon_txt.gif +0 -0
  37. data/public/assets/ueditor/dialogs/attachment/fileTypeImages/icon_xls.gif +0 -0
  38. data/public/assets/ueditor/dialogs/attachment/images/alignicon.gif +0 -0
  39. data/public/assets/ueditor/dialogs/attachment/images/alignicon.png +0 -0
  40. data/public/assets/ueditor/dialogs/attachment/images/bg.png +0 -0
  41. data/public/assets/ueditor/dialogs/attachment/images/file-icons.gif +0 -0
  42. data/public/assets/ueditor/dialogs/attachment/images/file-icons.png +0 -0
  43. data/public/assets/ueditor/dialogs/attachment/images/icons.gif +0 -0
  44. data/public/assets/ueditor/dialogs/attachment/images/icons.png +0 -0
  45. data/public/assets/ueditor/dialogs/attachment/images/image.png +0 -0
  46. data/public/assets/ueditor/dialogs/attachment/images/progress.png +0 -0
  47. data/public/assets/ueditor/dialogs/attachment/images/success.gif +0 -0
  48. data/public/assets/ueditor/dialogs/attachment/images/success.png +0 -0
  49. data/public/assets/ueditor/dialogs/background/background.css +94 -0
  50. data/public/assets/ueditor/dialogs/background/background.html +56 -0
  51. data/public/assets/ueditor/dialogs/background/background.js +376 -0
  52. data/public/assets/ueditor/dialogs/background/images/bg.png +0 -0
  53. data/public/assets/ueditor/dialogs/background/images/success.png +0 -0
  54. data/public/assets/ueditor/dialogs/charts/chart.config.js +65 -0
  55. data/public/assets/ueditor/dialogs/charts/charts.css +165 -0
  56. data/public/assets/ueditor/dialogs/charts/charts.html +89 -0
  57. data/public/assets/ueditor/dialogs/charts/charts.js +519 -0
  58. data/public/assets/ueditor/dialogs/charts/images/charts0.png +0 -0
  59. data/public/assets/ueditor/dialogs/charts/images/charts1.png +0 -0
  60. data/public/assets/ueditor/dialogs/charts/images/charts2.png +0 -0
  61. data/public/assets/ueditor/dialogs/charts/images/charts3.png +0 -0
  62. data/public/assets/ueditor/dialogs/charts/images/charts4.png +0 -0
  63. data/public/assets/ueditor/dialogs/charts/images/charts5.png +0 -0
  64. data/public/assets/ueditor/dialogs/emotion/emotion.css +43 -0
  65. data/public/assets/ueditor/dialogs/emotion/emotion.html +54 -0
  66. data/public/assets/ueditor/dialogs/emotion/emotion.js +186 -0
  67. data/public/assets/ueditor/dialogs/emotion/images/0.gif +0 -0
  68. data/public/assets/ueditor/dialogs/emotion/images/bface.gif +0 -0
  69. data/public/assets/ueditor/dialogs/emotion/images/cface.gif +0 -0
  70. data/public/assets/ueditor/dialogs/emotion/images/fface.gif +0 -0
  71. data/public/assets/ueditor/dialogs/emotion/images/jxface2.gif +0 -0
  72. data/public/assets/ueditor/dialogs/emotion/images/neweditor-tab-bg.png +0 -0
  73. data/public/assets/ueditor/dialogs/emotion/images/tface.gif +0 -0
  74. data/public/assets/ueditor/dialogs/emotion/images/wface.gif +0 -0
  75. data/public/assets/ueditor/dialogs/emotion/images/yface.gif +0 -0
  76. data/public/assets/ueditor/dialogs/gmap/gmap.html +89 -0
  77. data/public/assets/ueditor/dialogs/help/help.css +7 -0
  78. data/public/assets/ueditor/dialogs/help/help.html +82 -0
  79. data/public/assets/ueditor/dialogs/help/help.js +56 -0
  80. data/public/assets/ueditor/dialogs/image/image.css +894 -0
  81. data/public/assets/ueditor/dialogs/image/image.html +120 -0
  82. data/public/assets/ueditor/dialogs/image/image.js +1142 -0
  83. data/public/assets/ueditor/dialogs/image/images/alignicon.jpg +0 -0
  84. data/public/assets/ueditor/dialogs/image/images/bg.png +0 -0
  85. data/public/assets/ueditor/dialogs/image/images/icons.gif +0 -0
  86. data/public/assets/ueditor/dialogs/image/images/icons.png +0 -0
  87. data/public/assets/ueditor/dialogs/image/images/image.png +0 -0
  88. data/public/assets/ueditor/dialogs/image/images/progress.png +0 -0
  89. data/public/assets/ueditor/dialogs/image/images/success.gif +0 -0
  90. data/public/assets/ueditor/dialogs/image/images/success.png +0 -0
  91. data/public/assets/ueditor/dialogs/insertframe/insertframe.html +98 -0
  92. data/public/assets/ueditor/dialogs/internal.js +81 -0
  93. data/public/assets/ueditor/dialogs/link/link.html +126 -0
  94. data/public/assets/ueditor/dialogs/map/map.html +135 -0
  95. data/public/assets/ueditor/dialogs/map/show.html +118 -0
  96. data/public/assets/ueditor/dialogs/music/music.css +30 -0
  97. data/public/assets/ueditor/dialogs/music/music.html +32 -0
  98. data/public/assets/ueditor/dialogs/music/music.js +192 -0
  99. data/public/assets/ueditor/dialogs/preview/preview.html +40 -0
  100. data/public/assets/ueditor/dialogs/scrawl/images/addimg.png +0 -0
  101. data/public/assets/ueditor/dialogs/scrawl/images/brush.png +0 -0
  102. data/public/assets/ueditor/dialogs/scrawl/images/delimg.png +0 -0
  103. data/public/assets/ueditor/dialogs/scrawl/images/delimgH.png +0 -0
  104. data/public/assets/ueditor/dialogs/scrawl/images/empty.png +0 -0
  105. data/public/assets/ueditor/dialogs/scrawl/images/emptyH.png +0 -0
  106. data/public/assets/ueditor/dialogs/scrawl/images/eraser.png +0 -0
  107. data/public/assets/ueditor/dialogs/scrawl/images/redo.png +0 -0
  108. data/public/assets/ueditor/dialogs/scrawl/images/redoH.png +0 -0
  109. data/public/assets/ueditor/dialogs/scrawl/images/scale.png +0 -0
  110. data/public/assets/ueditor/dialogs/scrawl/images/scaleH.png +0 -0
  111. data/public/assets/ueditor/dialogs/scrawl/images/size.png +0 -0
  112. data/public/assets/ueditor/dialogs/scrawl/images/undo.png +0 -0
  113. data/public/assets/ueditor/dialogs/scrawl/images/undoH.png +0 -0
  114. data/public/assets/ueditor/dialogs/scrawl/scrawl.css +72 -0
  115. data/public/assets/ueditor/dialogs/scrawl/scrawl.html +95 -0
  116. data/public/assets/ueditor/dialogs/scrawl/scrawl.js +671 -0
  117. data/public/assets/ueditor/dialogs/searchreplace/searchreplace.html +102 -0
  118. data/public/assets/ueditor/dialogs/searchreplace/searchreplace.js +164 -0
  119. data/public/assets/ueditor/dialogs/snapscreen/snapscreen.html +58 -0
  120. data/public/assets/ueditor/dialogs/spechars/spechars.html +21 -0
  121. data/public/assets/ueditor/dialogs/spechars/spechars.js +57 -0
  122. data/public/assets/ueditor/dialogs/table/dragicon.png +0 -0
  123. data/public/assets/ueditor/dialogs/table/edittable.css +84 -0
  124. data/public/assets/ueditor/dialogs/table/edittable.html +64 -0
  125. data/public/assets/ueditor/dialogs/table/edittable.js +237 -0
  126. data/public/assets/ueditor/dialogs/table/edittd.html +61 -0
  127. data/public/assets/ueditor/dialogs/table/edittip.html +33 -0
  128. data/public/assets/ueditor/dialogs/template/config.js +42 -0
  129. data/public/assets/ueditor/dialogs/template/images/bg.gif +0 -0
  130. data/public/assets/ueditor/dialogs/template/images/pre0.png +0 -0
  131. data/public/assets/ueditor/dialogs/template/images/pre1.png +0 -0
  132. data/public/assets/ueditor/dialogs/template/images/pre2.png +0 -0
  133. data/public/assets/ueditor/dialogs/template/images/pre3.png +0 -0
  134. data/public/assets/ueditor/dialogs/template/images/pre4.png +0 -0
  135. data/public/assets/ueditor/dialogs/template/template.css +18 -0
  136. data/public/assets/ueditor/dialogs/template/template.html +26 -0
  137. data/public/assets/ueditor/dialogs/template/template.js +53 -0
  138. data/public/assets/ueditor/dialogs/video/images/bg.png +0 -0
  139. data/public/assets/ueditor/dialogs/video/images/center_focus.jpg +0 -0
  140. data/public/assets/ueditor/dialogs/video/images/file-icons.gif +0 -0
  141. data/public/assets/ueditor/dialogs/video/images/file-icons.png +0 -0
  142. data/public/assets/ueditor/dialogs/video/images/icons.gif +0 -0
  143. data/public/assets/ueditor/dialogs/video/images/icons.png +0 -0
  144. data/public/assets/ueditor/dialogs/video/images/image.png +0 -0
  145. data/public/assets/ueditor/dialogs/video/images/left_focus.jpg +0 -0
  146. data/public/assets/ueditor/dialogs/video/images/none_focus.jpg +0 -0
  147. data/public/assets/ueditor/dialogs/video/images/progress.png +0 -0
  148. data/public/assets/ueditor/dialogs/video/images/right_focus.jpg +0 -0
  149. data/public/assets/ueditor/dialogs/video/images/success.gif +0 -0
  150. data/public/assets/ueditor/dialogs/video/images/success.png +0 -0
  151. data/public/assets/ueditor/dialogs/video/video.css +635 -0
  152. data/public/assets/ueditor/dialogs/video/video.html +86 -0
  153. data/public/assets/ueditor/dialogs/video/video.js +791 -0
  154. data/public/assets/ueditor/dialogs/webapp/webapp.html +53 -0
  155. data/public/assets/ueditor/dialogs/wordimage/fClipboard_ueditor.swf +0 -0
  156. data/public/assets/ueditor/dialogs/wordimage/imageUploader.swf +0 -0
  157. data/public/assets/ueditor/dialogs/wordimage/tangram.js +1495 -0
  158. data/public/assets/ueditor/dialogs/wordimage/wordimage.html +111 -0
  159. data/public/assets/ueditor/dialogs/wordimage/wordimage.js +157 -0
  160. data/public/assets/ueditor/lang/en/en.js +684 -0
  161. data/public/assets/ueditor/lang/en/images/addimage.png +0 -0
  162. data/public/assets/ueditor/lang/en/images/alldeletebtnhoverskin.png +0 -0
  163. data/public/assets/ueditor/lang/en/images/alldeletebtnupskin.png +0 -0
  164. data/public/assets/ueditor/lang/en/images/background.png +0 -0
  165. data/public/assets/ueditor/lang/en/images/button.png +0 -0
  166. data/public/assets/ueditor/lang/en/images/copy.png +0 -0
  167. data/public/assets/ueditor/lang/en/images/deletedisable.png +0 -0
  168. data/public/assets/ueditor/lang/en/images/deleteenable.png +0 -0
  169. data/public/assets/ueditor/lang/en/images/listbackground.png +0 -0
  170. data/public/assets/ueditor/lang/en/images/localimage.png +0 -0
  171. data/public/assets/ueditor/lang/en/images/music.png +0 -0
  172. data/public/assets/ueditor/lang/en/images/rotateleftdisable.png +0 -0
  173. data/public/assets/ueditor/lang/en/images/rotateleftenable.png +0 -0
  174. data/public/assets/ueditor/lang/en/images/rotaterightdisable.png +0 -0
  175. data/public/assets/ueditor/lang/en/images/rotaterightenable.png +0 -0
  176. data/public/assets/ueditor/lang/en/images/upload.png +0 -0
  177. data/public/assets/ueditor/lang/zh-cn/images/copy.png +0 -0
  178. data/public/assets/ueditor/lang/zh-cn/images/localimage.png +0 -0
  179. data/public/assets/ueditor/lang/zh-cn/images/music.png +0 -0
  180. data/public/assets/ueditor/lang/zh-cn/images/upload.png +0 -0
  181. data/public/assets/ueditor/lang/zh-cn/zh-cn.js +669 -0
  182. data/public/assets/ueditor/themes/default/css/ueditor.css +1903 -0
  183. data/public/assets/ueditor/themes/default/css/ueditor.min.css +8 -0
  184. data/public/assets/ueditor/themes/default/dialogbase.css +100 -0
  185. data/public/assets/ueditor/themes/default/images/anchor.gif +0 -0
  186. data/public/assets/ueditor/themes/default/images/arrow.png +0 -0
  187. data/public/assets/ueditor/themes/default/images/arrow_down.png +0 -0
  188. data/public/assets/ueditor/themes/default/images/arrow_up.png +0 -0
  189. data/public/assets/ueditor/themes/default/images/button-bg.gif +0 -0
  190. data/public/assets/ueditor/themes/default/images/cancelbutton.gif +0 -0
  191. data/public/assets/ueditor/themes/default/images/charts.png +0 -0
  192. data/public/assets/ueditor/themes/default/images/cursor_h.gif +0 -0
  193. data/public/assets/ueditor/themes/default/images/cursor_h.png +0 -0
  194. data/public/assets/ueditor/themes/default/images/cursor_v.gif +0 -0
  195. data/public/assets/ueditor/themes/default/images/cursor_v.png +0 -0
  196. data/public/assets/ueditor/themes/default/images/dialog-title-bg.png +0 -0
  197. data/public/assets/ueditor/themes/default/images/filescan.png +0 -0
  198. data/public/assets/ueditor/themes/default/images/highlighted.gif +0 -0
  199. data/public/assets/ueditor/themes/default/images/icons-all.gif +0 -0
  200. data/public/assets/ueditor/themes/default/images/icons.gif +0 -0
  201. data/public/assets/ueditor/themes/default/images/icons.png +0 -0
  202. data/public/assets/ueditor/themes/default/images/loaderror.png +0 -0
  203. data/public/assets/ueditor/themes/default/images/loading.gif +0 -0
  204. data/public/assets/ueditor/themes/default/images/lock.gif +0 -0
  205. data/public/assets/ueditor/themes/default/images/neweditor-tab-bg.png +0 -0
  206. data/public/assets/ueditor/themes/default/images/pagebreak.gif +0 -0
  207. data/public/assets/ueditor/themes/default/images/scale.png +0 -0
  208. data/public/assets/ueditor/themes/default/images/sortable.png +0 -0
  209. data/public/assets/ueditor/themes/default/images/spacer.gif +0 -0
  210. data/public/assets/ueditor/themes/default/images/sparator_v.png +0 -0
  211. data/public/assets/ueditor/themes/default/images/table-cell-align.png +0 -0
  212. data/public/assets/ueditor/themes/default/images/tangram-colorpicker.png +0 -0
  213. data/public/assets/ueditor/themes/default/images/toolbar_bg.png +0 -0
  214. data/public/assets/ueditor/themes/default/images/unhighlighted.gif +0 -0
  215. data/public/assets/ueditor/themes/default/images/upload.png +0 -0
  216. data/public/assets/ueditor/themes/default/images/videologo.gif +0 -0
  217. data/public/assets/ueditor/themes/default/images/word.gif +0 -0
  218. data/public/assets/ueditor/themes/default/images/wordpaste.png +0 -0
  219. data/public/assets/ueditor/themes/iframe.css +1 -0
  220. data/public/assets/ueditor/third-party/SyntaxHighlighter/shCore.js +3655 -0
  221. data/public/assets/ueditor/third-party/SyntaxHighlighter/shCoreDefault.css +1 -0
  222. data/public/assets/ueditor/third-party/codemirror/codemirror.css +104 -0
  223. data/public/assets/ueditor/third-party/codemirror/codemirror.js +3581 -0
  224. data/public/assets/ueditor/third-party/highcharts/adapters/mootools-adapter.js +13 -0
  225. data/public/assets/ueditor/third-party/highcharts/adapters/mootools-adapter.src.js +313 -0
  226. data/public/assets/ueditor/third-party/highcharts/adapters/prototype-adapter.js +15 -0
  227. data/public/assets/ueditor/third-party/highcharts/adapters/prototype-adapter.src.js +316 -0
  228. data/public/assets/ueditor/third-party/highcharts/adapters/standalone-framework.js +17 -0
  229. data/public/assets/ueditor/third-party/highcharts/adapters/standalone-framework.src.js +583 -0
  230. data/public/assets/ueditor/third-party/highcharts/highcharts-more.js +50 -0
  231. data/public/assets/ueditor/third-party/highcharts/highcharts-more.src.js +2430 -0
  232. data/public/assets/ueditor/third-party/highcharts/highcharts.js +283 -0
  233. data/public/assets/ueditor/third-party/highcharts/highcharts.src.js +16974 -0
  234. data/public/assets/ueditor/third-party/highcharts/modules/annotations.js +7 -0
  235. data/public/assets/ueditor/third-party/highcharts/modules/annotations.src.js +401 -0
  236. data/public/assets/ueditor/third-party/highcharts/modules/canvas-tools.js +133 -0
  237. data/public/assets/ueditor/third-party/highcharts/modules/canvas-tools.src.js +3113 -0
  238. data/public/assets/ueditor/third-party/highcharts/modules/data.js +17 -0
  239. data/public/assets/ueditor/third-party/highcharts/modules/data.src.js +582 -0
  240. data/public/assets/ueditor/third-party/highcharts/modules/drilldown.js +11 -0
  241. data/public/assets/ueditor/third-party/highcharts/modules/drilldown.src.js +447 -0
  242. data/public/assets/ueditor/third-party/highcharts/modules/exporting.js +22 -0
  243. data/public/assets/ueditor/third-party/highcharts/modules/exporting.src.js +709 -0
  244. data/public/assets/ueditor/third-party/highcharts/modules/funnel.js +12 -0
  245. data/public/assets/ueditor/third-party/highcharts/modules/funnel.src.js +289 -0
  246. data/public/assets/ueditor/third-party/highcharts/modules/heatmap.js +1 -0
  247. data/public/assets/ueditor/third-party/highcharts/modules/heatmap.src.js +53 -0
  248. data/public/assets/ueditor/third-party/highcharts/modules/map.js +27 -0
  249. data/public/assets/ueditor/third-party/highcharts/modules/map.src.js +1002 -0
  250. data/public/assets/ueditor/third-party/highcharts/modules/no-data-to-display.js +12 -0
  251. data/public/assets/ueditor/third-party/highcharts/modules/no-data-to-display.src.js +128 -0
  252. data/public/assets/ueditor/third-party/highcharts/themes/dark-blue.js +254 -0
  253. data/public/assets/ueditor/third-party/highcharts/themes/dark-green.js +255 -0
  254. data/public/assets/ueditor/third-party/highcharts/themes/gray.js +257 -0
  255. data/public/assets/ueditor/third-party/highcharts/themes/grid.js +103 -0
  256. data/public/assets/ueditor/third-party/highcharts/themes/skies.js +89 -0
  257. data/public/assets/ueditor/third-party/jquery-1.10.2.js +9789 -0
  258. data/public/assets/ueditor/third-party/jquery-1.10.2.min.js +6 -0
  259. data/public/assets/ueditor/third-party/jquery-1.10.2.min.map +1 -0
  260. data/public/assets/ueditor/third-party/snapscreen/UEditorSnapscreen.exe +0 -0
  261. data/public/assets/ueditor/third-party/video-js/font/vjs.eot +0 -0
  262. data/public/assets/ueditor/third-party/video-js/font/vjs.svg +65 -0
  263. data/public/assets/ueditor/third-party/video-js/font/vjs.ttf +0 -0
  264. data/public/assets/ueditor/third-party/video-js/font/vjs.woff +0 -0
  265. data/public/assets/ueditor/third-party/video-js/video-js.css +766 -0
  266. data/public/assets/ueditor/third-party/video-js/video-js.min.css +5 -0
  267. data/public/assets/ueditor/third-party/video-js/video-js.swf +0 -0
  268. data/public/assets/ueditor/third-party/video-js/video.dev.js +7108 -0
  269. data/public/assets/ueditor/third-party/video-js/video.js +129 -0
  270. data/public/assets/ueditor/third-party/webuploader/Uploader.swf +0 -0
  271. data/public/assets/ueditor/third-party/webuploader/webuploader.css +28 -0
  272. data/public/assets/ueditor/third-party/webuploader/webuploader.custom.js +5670 -0
  273. data/public/assets/ueditor/third-party/webuploader/webuploader.custom.min.js +2 -0
  274. data/public/assets/ueditor/third-party/webuploader/webuploader.flashonly.js +4176 -0
  275. data/public/assets/ueditor/third-party/webuploader/webuploader.flashonly.min.js +2 -0
  276. data/public/assets/ueditor/third-party/webuploader/webuploader.html5only.js +5559 -0
  277. data/public/assets/ueditor/third-party/webuploader/webuploader.html5only.min.js +2 -0
  278. data/public/assets/ueditor/third-party/webuploader/webuploader.js +6733 -0
  279. data/public/assets/ueditor/third-party/webuploader/webuploader.min.js +2 -0
  280. data/public/assets/ueditor/third-party/webuploader/webuploader.withoutimage.js +4593 -0
  281. data/public/assets/ueditor/third-party/webuploader/webuploader.withoutimage.min.js +2 -0
  282. data/public/assets/ueditor/third-party/xss.min.js +1 -0
  283. data/public/assets/ueditor/third-party/zeroclipboard/ZeroClipboard.js +1256 -0
  284. data/public/assets/ueditor/third-party/zeroclipboard/ZeroClipboard.min.js +9 -0
  285. data/public/assets/ueditor/third-party/zeroclipboard/ZeroClipboard.swf +0 -0
  286. data/public/assets/ueditor/ueditor.parse.js +1022 -0
  287. data/public/assets/ueditor/ueditor.parse.min.js +7 -0
  288. data/screenshot.png +0 -0
  289. metadata +343 -0
@@ -0,0 +1,4176 @@
1
+ /*! WebUploader 0.1.2 */
2
+
3
+
4
+ /**
5
+ * @fileOverview 让内部各个部件的代码可以用[amd](https://github.com/amdjs/amdjs-api/wiki/AMD)模块定义方式组织起来。
6
+ *
7
+ * AMD API 内部的简单不完全实现,请忽略。只有当WebUploader被合并成一个文件的时候才会引入。
8
+ */
9
+ (function( root, factory ) {
10
+ var modules = {},
11
+
12
+ // 内部require, 简单不完全实现。
13
+ // https://github.com/amdjs/amdjs-api/wiki/require
14
+ _require = function( deps, callback ) {
15
+ var args, len, i;
16
+
17
+ // 如果deps不是数组,则直接返回指定module
18
+ if ( typeof deps === 'string' ) {
19
+ return getModule( deps );
20
+ } else {
21
+ args = [];
22
+ for( len = deps.length, i = 0; i < len; i++ ) {
23
+ args.push( getModule( deps[ i ] ) );
24
+ }
25
+
26
+ return callback.apply( null, args );
27
+ }
28
+ },
29
+
30
+ // 内部define,暂时不支持不指定id.
31
+ _define = function( id, deps, factory ) {
32
+ if ( arguments.length === 2 ) {
33
+ factory = deps;
34
+ deps = null;
35
+ }
36
+
37
+ _require( deps || [], function() {
38
+ setModule( id, factory, arguments );
39
+ });
40
+ },
41
+
42
+ // 设置module, 兼容CommonJs写法。
43
+ setModule = function( id, factory, args ) {
44
+ var module = {
45
+ exports: factory
46
+ },
47
+ returned;
48
+
49
+ if ( typeof factory === 'function' ) {
50
+ args.length || (args = [ _require, module.exports, module ]);
51
+ returned = factory.apply( null, args );
52
+ returned !== undefined && (module.exports = returned);
53
+ }
54
+
55
+ modules[ id ] = module.exports;
56
+ },
57
+
58
+ // 根据id获取module
59
+ getModule = function( id ) {
60
+ var module = modules[ id ] || root[ id ];
61
+
62
+ if ( !module ) {
63
+ throw new Error( '`' + id + '` is undefined' );
64
+ }
65
+
66
+ return module;
67
+ },
68
+
69
+ // 将所有modules,将路径ids装换成对象。
70
+ exportsTo = function( obj ) {
71
+ var key, host, parts, part, last, ucFirst;
72
+
73
+ // make the first character upper case.
74
+ ucFirst = function( str ) {
75
+ return str && (str.charAt( 0 ).toUpperCase() + str.substr( 1 ));
76
+ };
77
+
78
+ for ( key in modules ) {
79
+ host = obj;
80
+
81
+ if ( !modules.hasOwnProperty( key ) ) {
82
+ continue;
83
+ }
84
+
85
+ parts = key.split('/');
86
+ last = ucFirst( parts.pop() );
87
+
88
+ while( (part = ucFirst( parts.shift() )) ) {
89
+ host[ part ] = host[ part ] || {};
90
+ host = host[ part ];
91
+ }
92
+
93
+ host[ last ] = modules[ key ];
94
+ }
95
+ },
96
+
97
+ exports = factory( root, _define, _require ),
98
+ origin;
99
+
100
+ // exports every module.
101
+ exportsTo( exports );
102
+
103
+ if ( typeof module === 'object' && typeof module.exports === 'object' ) {
104
+
105
+ // For CommonJS and CommonJS-like environments where a proper window is present,
106
+ module.exports = exports;
107
+ } else if ( typeof define === 'function' && define.amd ) {
108
+
109
+ // Allow using this built library as an AMD module
110
+ // in another project. That other project will only
111
+ // see this AMD call, not the internal modules in
112
+ // the closure below.
113
+ define([], exports );
114
+ } else {
115
+
116
+ // Browser globals case. Just assign the
117
+ // result to a property on the global.
118
+ origin = root.WebUploader;
119
+ root.WebUploader = exports;
120
+ root.WebUploader.noConflict = function() {
121
+ root.WebUploader = origin;
122
+ };
123
+ }
124
+ })( this, function( window, define, require ) {
125
+
126
+
127
+ /**
128
+ * @fileOverview jQuery or Zepto
129
+ */
130
+ define('dollar-third',[],function() {
131
+ return window.jQuery || window.Zepto;
132
+ });
133
+ /**
134
+ * @fileOverview Dom 操作相关
135
+ */
136
+ define('dollar',[
137
+ 'dollar-third'
138
+ ], function( _ ) {
139
+ return _;
140
+ });
141
+ /**
142
+ * @fileOverview 使用jQuery的Promise
143
+ */
144
+ define('promise-third',[
145
+ 'dollar'
146
+ ], function( $ ) {
147
+ return {
148
+ Deferred: $.Deferred,
149
+ when: $.when,
150
+
151
+ isPromise: function( anything ) {
152
+ return anything && typeof anything.then === 'function';
153
+ }
154
+ };
155
+ });
156
+ /**
157
+ * @fileOverview Promise/A+
158
+ */
159
+ define('promise',[
160
+ 'promise-third'
161
+ ], function( _ ) {
162
+ return _;
163
+ });
164
+ /**
165
+ * @fileOverview 基础类方法。
166
+ */
167
+
168
+ /**
169
+ * Web Uploader内部类的详细说明,以下提及的功能类,都可以在`WebUploader`这个变量中访问到。
170
+ *
171
+ * As you know, Web Uploader的每个文件都是用过[AMD](https://github.com/amdjs/amdjs-api/wiki/AMD)规范中的`define`组织起来的, 每个Module都会有个module id.
172
+ * 默认module id该文件的路径,而此路径将会转化成名字空间存放在WebUploader中。如:
173
+ *
174
+ * * module `base`:WebUploader.Base
175
+ * * module `file`: WebUploader.File
176
+ * * module `lib/dnd`: WebUploader.Lib.Dnd
177
+ * * module `runtime/html5/dnd`: WebUploader.Runtime.Html5.Dnd
178
+ *
179
+ *
180
+ * 以下文档将可能省略`WebUploader`前缀。
181
+ * @module WebUploader
182
+ * @title WebUploader API文档
183
+ */
184
+ define('base',[
185
+ 'dollar',
186
+ 'promise'
187
+ ], function( $, promise ) {
188
+
189
+ var noop = function() {},
190
+ call = Function.call;
191
+
192
+ // http://jsperf.com/uncurrythis
193
+ // 反科里化
194
+ function uncurryThis( fn ) {
195
+ return function() {
196
+ return call.apply( fn, arguments );
197
+ };
198
+ }
199
+
200
+ function bindFn( fn, context ) {
201
+ return function() {
202
+ return fn.apply( context, arguments );
203
+ };
204
+ }
205
+
206
+ function createObject( proto ) {
207
+ var f;
208
+
209
+ if ( Object.create ) {
210
+ return Object.create( proto );
211
+ } else {
212
+ f = function() {};
213
+ f.prototype = proto;
214
+ return new f();
215
+ }
216
+ }
217
+
218
+
219
+ /**
220
+ * 基础类,提供一些简单常用的方法。
221
+ * @class Base
222
+ */
223
+ return {
224
+
225
+ /**
226
+ * @property {String} version 当前版本号。
227
+ */
228
+ version: '0.1.2',
229
+
230
+ /**
231
+ * @property {jQuery|Zepto} $ 引用依赖的jQuery或者Zepto对象。
232
+ */
233
+ $: $,
234
+
235
+ Deferred: promise.Deferred,
236
+
237
+ isPromise: promise.isPromise,
238
+
239
+ when: promise.when,
240
+
241
+ /**
242
+ * @description 简单的浏览器检查结果。
243
+ *
244
+ * * `webkit` webkit版本号,如果浏览器为非webkit内核,此属性为`undefined`。
245
+ * * `chrome` chrome浏览器版本号,如果浏览器为chrome,此属性为`undefined`。
246
+ * * `ie` ie浏览器版本号,如果浏览器为非ie,此属性为`undefined`。**暂不支持ie10+**
247
+ * * `firefox` firefox浏览器版本号,如果浏览器为非firefox,此属性为`undefined`。
248
+ * * `safari` safari浏览器版本号,如果浏览器为非safari,此属性为`undefined`。
249
+ * * `opera` opera浏览器版本号,如果浏览器为非opera,此属性为`undefined`。
250
+ *
251
+ * @property {Object} [browser]
252
+ */
253
+ browser: (function( ua ) {
254
+ var ret = {},
255
+ webkit = ua.match( /WebKit\/([\d.]+)/ ),
256
+ chrome = ua.match( /Chrome\/([\d.]+)/ ) ||
257
+ ua.match( /CriOS\/([\d.]+)/ ),
258
+
259
+ ie = ua.match( /MSIE\s([\d\.]+)/ ) ||
260
+ ua.match(/(?:trident)(?:.*rv:([\w.]+))?/i),
261
+ firefox = ua.match( /Firefox\/([\d.]+)/ ),
262
+ safari = ua.match( /Safari\/([\d.]+)/ ),
263
+ opera = ua.match( /OPR\/([\d.]+)/ );
264
+
265
+ webkit && (ret.webkit = parseFloat( webkit[ 1 ] ));
266
+ chrome && (ret.chrome = parseFloat( chrome[ 1 ] ));
267
+ ie && (ret.ie = parseFloat( ie[ 1 ] ));
268
+ firefox && (ret.firefox = parseFloat( firefox[ 1 ] ));
269
+ safari && (ret.safari = parseFloat( safari[ 1 ] ));
270
+ opera && (ret.opera = parseFloat( opera[ 1 ] ));
271
+
272
+ return ret;
273
+ })( navigator.userAgent ),
274
+
275
+ /**
276
+ * @description 操作系统检查结果。
277
+ *
278
+ * * `android` 如果在android浏览器环境下,此值为对应的android版本号,否则为`undefined`。
279
+ * * `ios` 如果在ios浏览器环境下,此值为对应的ios版本号,否则为`undefined`。
280
+ * @property {Object} [os]
281
+ */
282
+ os: (function( ua ) {
283
+ var ret = {},
284
+
285
+ // osx = !!ua.match( /\(Macintosh\; Intel / ),
286
+ android = ua.match( /(?:Android);?[\s\/]+([\d.]+)?/ ),
287
+ ios = ua.match( /(?:iPad|iPod|iPhone).*OS\s([\d_]+)/ );
288
+
289
+ // osx && (ret.osx = true);
290
+ android && (ret.android = parseFloat( android[ 1 ] ));
291
+ ios && (ret.ios = parseFloat( ios[ 1 ].replace( /_/g, '.' ) ));
292
+
293
+ return ret;
294
+ })( navigator.userAgent ),
295
+
296
+ /**
297
+ * 实现类与类之间的继承。
298
+ * @method inherits
299
+ * @grammar Base.inherits( super ) => child
300
+ * @grammar Base.inherits( super, protos ) => child
301
+ * @grammar Base.inherits( super, protos, statics ) => child
302
+ * @param {Class} super 父类
303
+ * @param {Object | Function} [protos] 子类或者对象。如果对象中包含constructor,子类将是用此属性值。
304
+ * @param {Function} [protos.constructor] 子类构造器,不指定的话将创建个临时的直接执行父类构造器的方法。
305
+ * @param {Object} [statics] 静态属性或方法。
306
+ * @return {Class} 返回子类。
307
+ * @example
308
+ * function Person() {
309
+ * console.log( 'Super' );
310
+ * }
311
+ * Person.prototype.hello = function() {
312
+ * console.log( 'hello' );
313
+ * };
314
+ *
315
+ * var Manager = Base.inherits( Person, {
316
+ * world: function() {
317
+ * console.log( 'World' );
318
+ * }
319
+ * });
320
+ *
321
+ * // 因为没有指定构造器,父类的构造器将会执行。
322
+ * var instance = new Manager(); // => Super
323
+ *
324
+ * // 继承子父类的方法
325
+ * instance.hello(); // => hello
326
+ * instance.world(); // => World
327
+ *
328
+ * // 子类的__super__属性指向父类
329
+ * console.log( Manager.__super__ === Person ); // => true
330
+ */
331
+ inherits: function( Super, protos, staticProtos ) {
332
+ var child;
333
+
334
+ if ( typeof protos === 'function' ) {
335
+ child = protos;
336
+ protos = null;
337
+ } else if ( protos && protos.hasOwnProperty('constructor') ) {
338
+ child = protos.constructor;
339
+ } else {
340
+ child = function() {
341
+ return Super.apply( this, arguments );
342
+ };
343
+ }
344
+
345
+ // 复制静态方法
346
+ $.extend( true, child, Super, staticProtos || {} );
347
+
348
+ /* jshint camelcase: false */
349
+
350
+ // 让子类的__super__属性指向父类。
351
+ child.__super__ = Super.prototype;
352
+
353
+ // 构建原型,添加原型方法或属性。
354
+ // 暂时用Object.create实现。
355
+ child.prototype = createObject( Super.prototype );
356
+ protos && $.extend( true, child.prototype, protos );
357
+
358
+ return child;
359
+ },
360
+
361
+ /**
362
+ * 一个不做任何事情的方法。可以用来赋值给默认的callback.
363
+ * @method noop
364
+ */
365
+ noop: noop,
366
+
367
+ /**
368
+ * 返回一个新的方法,此方法将已指定的`context`来执行。
369
+ * @grammar Base.bindFn( fn, context ) => Function
370
+ * @method bindFn
371
+ * @example
372
+ * var doSomething = function() {
373
+ * console.log( this.name );
374
+ * },
375
+ * obj = {
376
+ * name: 'Object Name'
377
+ * },
378
+ * aliasFn = Base.bind( doSomething, obj );
379
+ *
380
+ * aliasFn(); // => Object Name
381
+ *
382
+ */
383
+ bindFn: bindFn,
384
+
385
+ /**
386
+ * 引用Console.log如果存在的话,否则引用一个[空函数loop](#WebUploader:Base.log)。
387
+ * @grammar Base.log( args... ) => undefined
388
+ * @method log
389
+ */
390
+ log: (function() {
391
+ if ( window.console ) {
392
+ return bindFn( console.log, console );
393
+ }
394
+ return noop;
395
+ })(),
396
+
397
+ nextTick: (function() {
398
+
399
+ return function( cb ) {
400
+ setTimeout( cb, 1 );
401
+ };
402
+
403
+ // @bug 当浏览器不在当前窗口时就停了。
404
+ // var next = window.requestAnimationFrame ||
405
+ // window.webkitRequestAnimationFrame ||
406
+ // window.mozRequestAnimationFrame ||
407
+ // function( cb ) {
408
+ // window.setTimeout( cb, 1000 / 60 );
409
+ // };
410
+
411
+ // // fix: Uncaught TypeError: Illegal invocation
412
+ // return bindFn( next, window );
413
+ })(),
414
+
415
+ /**
416
+ * 被[uncurrythis](http://www.2ality.com/2011/11/uncurrying-this.html)的数组slice方法。
417
+ * 将用来将非数组对象转化成数组对象。
418
+ * @grammar Base.slice( target, start[, end] ) => Array
419
+ * @method slice
420
+ * @example
421
+ * function doSomthing() {
422
+ * var args = Base.slice( arguments, 1 );
423
+ * console.log( args );
424
+ * }
425
+ *
426
+ * doSomthing( 'ignored', 'arg2', 'arg3' ); // => Array ["arg2", "arg3"]
427
+ */
428
+ slice: uncurryThis( [].slice ),
429
+
430
+ /**
431
+ * 生成唯一的ID
432
+ * @method guid
433
+ * @grammar Base.guid() => String
434
+ * @grammar Base.guid( prefx ) => String
435
+ */
436
+ guid: (function() {
437
+ var counter = 0;
438
+
439
+ return function( prefix ) {
440
+ var guid = (+new Date()).toString( 32 ),
441
+ i = 0;
442
+
443
+ for ( ; i < 5; i++ ) {
444
+ guid += Math.floor( Math.random() * 65535 ).toString( 32 );
445
+ }
446
+
447
+ return (prefix || 'wu_') + guid + (counter++).toString( 32 );
448
+ };
449
+ })(),
450
+
451
+ /**
452
+ * 格式化文件大小, 输出成带单位的字符串
453
+ * @method formatSize
454
+ * @grammar Base.formatSize( size ) => String
455
+ * @grammar Base.formatSize( size, pointLength ) => String
456
+ * @grammar Base.formatSize( size, pointLength, units ) => String
457
+ * @param {Number} size 文件大小
458
+ * @param {Number} [pointLength=2] 精确到的小数点数。
459
+ * @param {Array} [units=[ 'B', 'K', 'M', 'G', 'TB' ]] 单位数组。从字节,到千字节,一直往上指定。如果单位数组里面只指定了到了K(千字节),同时文件大小大于M, 此方法的输出将还是显示成多少K.
460
+ * @example
461
+ * console.log( Base.formatSize( 100 ) ); // => 100B
462
+ * console.log( Base.formatSize( 1024 ) ); // => 1.00K
463
+ * console.log( Base.formatSize( 1024, 0 ) ); // => 1K
464
+ * console.log( Base.formatSize( 1024 * 1024 ) ); // => 1.00M
465
+ * console.log( Base.formatSize( 1024 * 1024 * 1024 ) ); // => 1.00G
466
+ * console.log( Base.formatSize( 1024 * 1024 * 1024, 0, ['B', 'KB', 'MB'] ) ); // => 1024MB
467
+ */
468
+ formatSize: function( size, pointLength, units ) {
469
+ var unit;
470
+
471
+ units = units || [ 'B', 'K', 'M', 'G', 'TB' ];
472
+
473
+ while ( (unit = units.shift()) && size > 1024 ) {
474
+ size = size / 1024;
475
+ }
476
+
477
+ return (unit === 'B' ? size : size.toFixed( pointLength || 2 )) +
478
+ unit;
479
+ }
480
+ };
481
+ });
482
+ /**
483
+ * 事件处理类,可以独立使用,也可以扩展给对象使用。
484
+ * @fileOverview Mediator
485
+ */
486
+ define('mediator',[
487
+ 'base'
488
+ ], function( Base ) {
489
+ var $ = Base.$,
490
+ slice = [].slice,
491
+ separator = /\s+/,
492
+ protos;
493
+
494
+ // 根据条件过滤出事件handlers.
495
+ function findHandlers( arr, name, callback, context ) {
496
+ return $.grep( arr, function( handler ) {
497
+ return handler &&
498
+ (!name || handler.e === name) &&
499
+ (!callback || handler.cb === callback ||
500
+ handler.cb._cb === callback) &&
501
+ (!context || handler.ctx === context);
502
+ });
503
+ }
504
+
505
+ function eachEvent( events, callback, iterator ) {
506
+ // 不支持对象,只支持多个event用空格隔开
507
+ $.each( (events || '').split( separator ), function( _, key ) {
508
+ iterator( key, callback );
509
+ });
510
+ }
511
+
512
+ function triggerHanders( events, args ) {
513
+ var stoped = false,
514
+ i = -1,
515
+ len = events.length,
516
+ handler;
517
+
518
+ while ( ++i < len ) {
519
+ handler = events[ i ];
520
+
521
+ if ( handler.cb.apply( handler.ctx2, args ) === false ) {
522
+ stoped = true;
523
+ break;
524
+ }
525
+ }
526
+
527
+ return !stoped;
528
+ }
529
+
530
+ protos = {
531
+
532
+ /**
533
+ * 绑定事件。
534
+ *
535
+ * `callback`方法在执行时,arguments将会来源于trigger的时候携带的参数。如
536
+ * ```javascript
537
+ * var obj = {};
538
+ *
539
+ * // 使得obj有事件行为
540
+ * Mediator.installTo( obj );
541
+ *
542
+ * obj.on( 'testa', function( arg1, arg2 ) {
543
+ * console.log( arg1, arg2 ); // => 'arg1', 'arg2'
544
+ * });
545
+ *
546
+ * obj.trigger( 'testa', 'arg1', 'arg2' );
547
+ * ```
548
+ *
549
+ * 如果`callback`中,某一个方法`return false`了,则后续的其他`callback`都不会被执行到。
550
+ * 切会影响到`trigger`方法的返回值,为`false`。
551
+ *
552
+ * `on`还可以用来添加一个特殊事件`all`, 这样所有的事件触发都会响应到。同时此类`callback`中的arguments有一个不同处,
553
+ * 就是第一个参数为`type`,记录当前是什么事件在触发。此类`callback`的优先级比脚低,会再正常`callback`执行完后触发。
554
+ * ```javascript
555
+ * obj.on( 'all', function( type, arg1, arg2 ) {
556
+ * console.log( type, arg1, arg2 ); // => 'testa', 'arg1', 'arg2'
557
+ * });
558
+ * ```
559
+ *
560
+ * @method on
561
+ * @grammar on( name, callback[, context] ) => self
562
+ * @param {String} name 事件名,支持多个事件用空格隔开
563
+ * @param {Function} callback 事件处理器
564
+ * @param {Object} [context] 事件处理器的上下文。
565
+ * @return {self} 返回自身,方便链式
566
+ * @chainable
567
+ * @class Mediator
568
+ */
569
+ on: function( name, callback, context ) {
570
+ var me = this,
571
+ set;
572
+
573
+ if ( !callback ) {
574
+ return this;
575
+ }
576
+
577
+ set = this._events || (this._events = []);
578
+
579
+ eachEvent( name, callback, function( name, callback ) {
580
+ var handler = { e: name };
581
+
582
+ handler.cb = callback;
583
+ handler.ctx = context;
584
+ handler.ctx2 = context || me;
585
+ handler.id = set.length;
586
+
587
+ set.push( handler );
588
+ });
589
+
590
+ return this;
591
+ },
592
+
593
+ /**
594
+ * 绑定事件,且当handler执行完后,自动解除绑定。
595
+ * @method once
596
+ * @grammar once( name, callback[, context] ) => self
597
+ * @param {String} name 事件名
598
+ * @param {Function} callback 事件处理器
599
+ * @param {Object} [context] 事件处理器的上下文。
600
+ * @return {self} 返回自身,方便链式
601
+ * @chainable
602
+ */
603
+ once: function( name, callback, context ) {
604
+ var me = this;
605
+
606
+ if ( !callback ) {
607
+ return me;
608
+ }
609
+
610
+ eachEvent( name, callback, function( name, callback ) {
611
+ var once = function() {
612
+ me.off( name, once );
613
+ return callback.apply( context || me, arguments );
614
+ };
615
+
616
+ once._cb = callback;
617
+ me.on( name, once, context );
618
+ });
619
+
620
+ return me;
621
+ },
622
+
623
+ /**
624
+ * 解除事件绑定
625
+ * @method off
626
+ * @grammar off( [name[, callback[, context] ] ] ) => self
627
+ * @param {String} [name] 事件名
628
+ * @param {Function} [callback] 事件处理器
629
+ * @param {Object} [context] 事件处理器的上下文。
630
+ * @return {self} 返回自身,方便链式
631
+ * @chainable
632
+ */
633
+ off: function( name, cb, ctx ) {
634
+ var events = this._events;
635
+
636
+ if ( !events ) {
637
+ return this;
638
+ }
639
+
640
+ if ( !name && !cb && !ctx ) {
641
+ this._events = [];
642
+ return this;
643
+ }
644
+
645
+ eachEvent( name, cb, function( name, cb ) {
646
+ $.each( findHandlers( events, name, cb, ctx ), function() {
647
+ delete events[ this.id ];
648
+ });
649
+ });
650
+
651
+ return this;
652
+ },
653
+
654
+ /**
655
+ * 触发事件
656
+ * @method trigger
657
+ * @grammar trigger( name[, args...] ) => self
658
+ * @param {String} type 事件名
659
+ * @param {*} [...] 任意参数
660
+ * @return {Boolean} 如果handler中return false了,则返回false, 否则返回true
661
+ */
662
+ trigger: function( type ) {
663
+ var args, events, allEvents;
664
+
665
+ if ( !this._events || !type ) {
666
+ return this;
667
+ }
668
+
669
+ args = slice.call( arguments, 1 );
670
+ events = findHandlers( this._events, type );
671
+ allEvents = findHandlers( this._events, 'all' );
672
+
673
+ return triggerHanders( events, args ) &&
674
+ triggerHanders( allEvents, arguments );
675
+ }
676
+ };
677
+
678
+ /**
679
+ * 中介者,它本身是个单例,但可以通过[installTo](#WebUploader:Mediator:installTo)方法,使任何对象具备事件行为。
680
+ * 主要目的是负责模块与模块之间的合作,降低耦合度。
681
+ *
682
+ * @class Mediator
683
+ */
684
+ return $.extend({
685
+
686
+ /**
687
+ * 可以通过这个接口,使任何对象具备事件功能。
688
+ * @method installTo
689
+ * @param {Object} obj 需要具备事件行为的对象。
690
+ * @return {Object} 返回obj.
691
+ */
692
+ installTo: function( obj ) {
693
+ return $.extend( obj, protos );
694
+ }
695
+
696
+ }, protos );
697
+ });
698
+ /**
699
+ * @fileOverview Uploader上传类
700
+ */
701
+ define('uploader',[
702
+ 'base',
703
+ 'mediator'
704
+ ], function( Base, Mediator ) {
705
+
706
+ var $ = Base.$;
707
+
708
+ /**
709
+ * 上传入口类。
710
+ * @class Uploader
711
+ * @constructor
712
+ * @grammar new Uploader( opts ) => Uploader
713
+ * @example
714
+ * var uploader = WebUploader.Uploader({
715
+ * swf: 'path_of_swf/Uploader.swf',
716
+ *
717
+ * // 开起分片上传。
718
+ * chunked: true
719
+ * });
720
+ */
721
+ function Uploader( opts ) {
722
+ this.options = $.extend( true, {}, Uploader.options, opts );
723
+ this._init( this.options );
724
+ }
725
+
726
+ // default Options
727
+ // widgets中有相应扩展
728
+ Uploader.options = {};
729
+ Mediator.installTo( Uploader.prototype );
730
+
731
+ // 批量添加纯命令式方法。
732
+ $.each({
733
+ upload: 'start-upload',
734
+ stop: 'stop-upload',
735
+ getFile: 'get-file',
736
+ getFiles: 'get-files',
737
+ addFile: 'add-file',
738
+ addFiles: 'add-file',
739
+ sort: 'sort-files',
740
+ removeFile: 'remove-file',
741
+ skipFile: 'skip-file',
742
+ retry: 'retry',
743
+ isInProgress: 'is-in-progress',
744
+ makeThumb: 'make-thumb',
745
+ getDimension: 'get-dimension',
746
+ addButton: 'add-btn',
747
+ getRuntimeType: 'get-runtime-type',
748
+ refresh: 'refresh',
749
+ disable: 'disable',
750
+ enable: 'enable',
751
+ reset: 'reset'
752
+ }, function( fn, command ) {
753
+ Uploader.prototype[ fn ] = function() {
754
+ return this.request( command, arguments );
755
+ };
756
+ });
757
+
758
+ $.extend( Uploader.prototype, {
759
+ state: 'pending',
760
+
761
+ _init: function( opts ) {
762
+ var me = this;
763
+
764
+ me.request( 'init', opts, function() {
765
+ me.state = 'ready';
766
+ me.trigger('ready');
767
+ });
768
+ },
769
+
770
+ /**
771
+ * 获取或者设置Uploader配置项。
772
+ * @method option
773
+ * @grammar option( key ) => *
774
+ * @grammar option( key, val ) => self
775
+ * @example
776
+ *
777
+ * // 初始状态图片上传前不会压缩
778
+ * var uploader = new WebUploader.Uploader({
779
+ * resize: null;
780
+ * });
781
+ *
782
+ * // 修改后图片上传前,尝试将图片压缩到1600 * 1600
783
+ * uploader.options( 'resize', {
784
+ * width: 1600,
785
+ * height: 1600
786
+ * });
787
+ */
788
+ option: function( key, val ) {
789
+ var opts = this.options;
790
+
791
+ // setter
792
+ if ( arguments.length > 1 ) {
793
+
794
+ if ( $.isPlainObject( val ) &&
795
+ $.isPlainObject( opts[ key ] ) ) {
796
+ $.extend( opts[ key ], val );
797
+ } else {
798
+ opts[ key ] = val;
799
+ }
800
+
801
+ } else { // getter
802
+ return key ? opts[ key ] : opts;
803
+ }
804
+ },
805
+
806
+ /**
807
+ * 获取文件统计信息。返回一个包含一下信息的对象。
808
+ * * `successNum` 上传成功的文件数
809
+ * * `uploadFailNum` 上传失败的文件数
810
+ * * `cancelNum` 被删除的文件数
811
+ * * `invalidNum` 无效的文件数
812
+ * * `queueNum` 还在队列中的文件数
813
+ * @method getStats
814
+ * @grammar getStats() => Object
815
+ */
816
+ getStats: function() {
817
+ // return this._mgr.getStats.apply( this._mgr, arguments );
818
+ var stats = this.request('get-stats');
819
+
820
+ return {
821
+ successNum: stats.numOfSuccess,
822
+
823
+ // who care?
824
+ // queueFailNum: 0,
825
+ cancelNum: stats.numOfCancel,
826
+ invalidNum: stats.numOfInvalid,
827
+ uploadFailNum: stats.numOfUploadFailed,
828
+ queueNum: stats.numOfQueue
829
+ };
830
+ },
831
+
832
+ // 需要重写此方法来来支持opts.onEvent和instance.onEvent的处理器
833
+ trigger: function( type/*, args...*/ ) {
834
+ var args = [].slice.call( arguments, 1 ),
835
+ opts = this.options,
836
+ name = 'on' + type.substring( 0, 1 ).toUpperCase() +
837
+ type.substring( 1 );
838
+
839
+ if (
840
+ // 调用通过on方法注册的handler.
841
+ Mediator.trigger.apply( this, arguments ) === false ||
842
+
843
+ // 调用opts.onEvent
844
+ $.isFunction( opts[ name ] ) &&
845
+ opts[ name ].apply( this, args ) === false ||
846
+
847
+ // 调用this.onEvent
848
+ $.isFunction( this[ name ] ) &&
849
+ this[ name ].apply( this, args ) === false ||
850
+
851
+ // 广播所有uploader的事件。
852
+ Mediator.trigger.apply( Mediator,
853
+ [ this, type ].concat( args ) ) === false ) {
854
+
855
+ return false;
856
+ }
857
+
858
+ return true;
859
+ },
860
+
861
+ // widgets/widget.js将补充此方法的详细文档。
862
+ request: Base.noop
863
+ });
864
+
865
+ /**
866
+ * 创建Uploader实例,等同于new Uploader( opts );
867
+ * @method create
868
+ * @class Base
869
+ * @static
870
+ * @grammar Base.create( opts ) => Uploader
871
+ */
872
+ Base.create = Uploader.create = function( opts ) {
873
+ return new Uploader( opts );
874
+ };
875
+
876
+ // 暴露Uploader,可以通过它来扩展业务逻辑。
877
+ Base.Uploader = Uploader;
878
+
879
+ return Uploader;
880
+ });
881
+ /**
882
+ * @fileOverview Runtime管理器,负责Runtime的选择, 连接
883
+ */
884
+ define('runtime/runtime',[
885
+ 'base',
886
+ 'mediator'
887
+ ], function( Base, Mediator ) {
888
+
889
+ var $ = Base.$,
890
+ factories = {},
891
+
892
+ // 获取对象的第一个key
893
+ getFirstKey = function( obj ) {
894
+ for ( var key in obj ) {
895
+ if ( obj.hasOwnProperty( key ) ) {
896
+ return key;
897
+ }
898
+ }
899
+ return null;
900
+ };
901
+
902
+ // 接口类。
903
+ function Runtime( options ) {
904
+ this.options = $.extend({
905
+ container: document.body
906
+ }, options );
907
+ this.uid = Base.guid('rt_');
908
+ }
909
+
910
+ $.extend( Runtime.prototype, {
911
+
912
+ getContainer: function() {
913
+ var opts = this.options,
914
+ parent, container;
915
+
916
+ if ( this._container ) {
917
+ return this._container;
918
+ }
919
+
920
+ parent = $( opts.container || document.body );
921
+ container = $( document.createElement('div') );
922
+
923
+ container.attr( 'id', 'rt_' + this.uid );
924
+ container.css({
925
+ position: 'absolute',
926
+ top: '0px',
927
+ left: '0px',
928
+ width: '1px',
929
+ height: '1px',
930
+ overflow: 'hidden'
931
+ });
932
+
933
+ parent.append( container );
934
+ parent.addClass('webuploader-container');
935
+ this._container = container;
936
+ return container;
937
+ },
938
+
939
+ init: Base.noop,
940
+ exec: Base.noop,
941
+
942
+ destroy: function() {
943
+ if ( this._container ) {
944
+ this._container.parentNode.removeChild( this.__container );
945
+ }
946
+
947
+ this.off();
948
+ }
949
+ });
950
+
951
+ Runtime.orders = 'html5,flash';
952
+
953
+
954
+ /**
955
+ * 添加Runtime实现。
956
+ * @param {String} type 类型
957
+ * @param {Runtime} factory 具体Runtime实现。
958
+ */
959
+ Runtime.addRuntime = function( type, factory ) {
960
+ factories[ type ] = factory;
961
+ };
962
+
963
+ Runtime.hasRuntime = function( type ) {
964
+ return !!(type ? factories[ type ] : getFirstKey( factories ));
965
+ };
966
+
967
+ Runtime.create = function( opts, orders ) {
968
+ var type, runtime;
969
+
970
+ orders = orders || Runtime.orders;
971
+ $.each( orders.split( /\s*,\s*/g ), function() {
972
+ if ( factories[ this ] ) {
973
+ type = this;
974
+ return false;
975
+ }
976
+ });
977
+
978
+ type = type || getFirstKey( factories );
979
+
980
+ if ( !type ) {
981
+ throw new Error('Runtime Error');
982
+ }
983
+
984
+ runtime = new factories[ type ]( opts );
985
+ return runtime;
986
+ };
987
+
988
+ Mediator.installTo( Runtime.prototype );
989
+ return Runtime;
990
+ });
991
+
992
+ /**
993
+ * @fileOverview Runtime管理器,负责Runtime的选择, 连接
994
+ */
995
+ define('runtime/client',[
996
+ 'base',
997
+ 'mediator',
998
+ 'runtime/runtime'
999
+ ], function( Base, Mediator, Runtime ) {
1000
+
1001
+ var cache;
1002
+
1003
+ cache = (function() {
1004
+ var obj = {};
1005
+
1006
+ return {
1007
+ add: function( runtime ) {
1008
+ obj[ runtime.uid ] = runtime;
1009
+ },
1010
+
1011
+ get: function( ruid, standalone ) {
1012
+ var i;
1013
+
1014
+ if ( ruid ) {
1015
+ return obj[ ruid ];
1016
+ }
1017
+
1018
+ for ( i in obj ) {
1019
+ // 有些类型不能重用,比如filepicker.
1020
+ if ( standalone && obj[ i ].__standalone ) {
1021
+ continue;
1022
+ }
1023
+
1024
+ return obj[ i ];
1025
+ }
1026
+
1027
+ return null;
1028
+ },
1029
+
1030
+ remove: function( runtime ) {
1031
+ delete obj[ runtime.uid ];
1032
+ }
1033
+ };
1034
+ })();
1035
+
1036
+ function RuntimeClient( component, standalone ) {
1037
+ var deferred = Base.Deferred(),
1038
+ runtime;
1039
+
1040
+ this.uid = Base.guid('client_');
1041
+
1042
+ // 允许runtime没有初始化之前,注册一些方法在初始化后执行。
1043
+ this.runtimeReady = function( cb ) {
1044
+ return deferred.done( cb );
1045
+ };
1046
+
1047
+ this.connectRuntime = function( opts, cb ) {
1048
+
1049
+ // already connected.
1050
+ if ( runtime ) {
1051
+ throw new Error('already connected!');
1052
+ }
1053
+
1054
+ deferred.done( cb );
1055
+
1056
+ if ( typeof opts === 'string' && cache.get( opts ) ) {
1057
+ runtime = cache.get( opts );
1058
+ }
1059
+
1060
+ // 像filePicker只能独立存在,不能公用。
1061
+ runtime = runtime || cache.get( null, standalone );
1062
+
1063
+ // 需要创建
1064
+ if ( !runtime ) {
1065
+ runtime = Runtime.create( opts, opts.runtimeOrder );
1066
+ runtime.__promise = deferred.promise();
1067
+ runtime.once( 'ready', deferred.resolve );
1068
+ runtime.init();
1069
+ cache.add( runtime );
1070
+ runtime.__client = 1;
1071
+ } else {
1072
+ // 来自cache
1073
+ Base.$.extend( runtime.options, opts );
1074
+ runtime.__promise.then( deferred.resolve );
1075
+ runtime.__client++;
1076
+ }
1077
+
1078
+ standalone && (runtime.__standalone = standalone);
1079
+ return runtime;
1080
+ };
1081
+
1082
+ this.getRuntime = function() {
1083
+ return runtime;
1084
+ };
1085
+
1086
+ this.disconnectRuntime = function() {
1087
+ if ( !runtime ) {
1088
+ return;
1089
+ }
1090
+
1091
+ runtime.__client--;
1092
+
1093
+ if ( runtime.__client <= 0 ) {
1094
+ cache.remove( runtime );
1095
+ delete runtime.__promise;
1096
+ runtime.destroy();
1097
+ }
1098
+
1099
+ runtime = null;
1100
+ };
1101
+
1102
+ this.exec = function() {
1103
+ if ( !runtime ) {
1104
+ return;
1105
+ }
1106
+
1107
+ var args = Base.slice( arguments );
1108
+ component && args.unshift( component );
1109
+
1110
+ return runtime.exec.apply( this, args );
1111
+ };
1112
+
1113
+ this.getRuid = function() {
1114
+ return runtime && runtime.uid;
1115
+ };
1116
+
1117
+ this.destroy = (function( destroy ) {
1118
+ return function() {
1119
+ destroy && destroy.apply( this, arguments );
1120
+ this.trigger('destroy');
1121
+ this.off();
1122
+ this.exec('destroy');
1123
+ this.disconnectRuntime();
1124
+ };
1125
+ })( this.destroy );
1126
+ }
1127
+
1128
+ Mediator.installTo( RuntimeClient.prototype );
1129
+ return RuntimeClient;
1130
+ });
1131
+ /**
1132
+ * @fileOverview Blob
1133
+ */
1134
+ define('lib/blob',[
1135
+ 'base',
1136
+ 'runtime/client'
1137
+ ], function( Base, RuntimeClient ) {
1138
+
1139
+ function Blob( ruid, source ) {
1140
+ var me = this;
1141
+
1142
+ me.source = source;
1143
+ me.ruid = ruid;
1144
+
1145
+ RuntimeClient.call( me, 'Blob' );
1146
+
1147
+ this.uid = source.uid || this.uid;
1148
+ this.type = source.type || '';
1149
+ this.size = source.size || 0;
1150
+
1151
+ if ( ruid ) {
1152
+ me.connectRuntime( ruid );
1153
+ }
1154
+ }
1155
+
1156
+ Base.inherits( RuntimeClient, {
1157
+ constructor: Blob,
1158
+
1159
+ slice: function( start, end ) {
1160
+ return this.exec( 'slice', start, end );
1161
+ },
1162
+
1163
+ getSource: function() {
1164
+ return this.source;
1165
+ }
1166
+ });
1167
+
1168
+ return Blob;
1169
+ });
1170
+ /**
1171
+ * 为了统一化Flash的File和HTML5的File而存在。
1172
+ * 以至于要调用Flash里面的File,也可以像调用HTML5版本的File一下。
1173
+ * @fileOverview File
1174
+ */
1175
+ define('lib/file',[
1176
+ 'base',
1177
+ 'lib/blob'
1178
+ ], function( Base, Blob ) {
1179
+
1180
+ var uid = 1,
1181
+ rExt = /\.([^.]+)$/;
1182
+
1183
+ function File( ruid, file ) {
1184
+ var ext;
1185
+
1186
+ Blob.apply( this, arguments );
1187
+ this.name = file.name || ('untitled' + uid++);
1188
+ ext = rExt.exec( file.name ) ? RegExp.$1.toLowerCase() : '';
1189
+
1190
+ // todo 支持其他类型文件的转换。
1191
+
1192
+ // 如果有mimetype, 但是文件名里面没有找出后缀规律
1193
+ if ( !ext && this.type ) {
1194
+ ext = /\/(jpg|jpeg|png|gif|bmp)$/i.exec( this.type ) ?
1195
+ RegExp.$1.toLowerCase() : '';
1196
+ this.name += '.' + ext;
1197
+ }
1198
+
1199
+ // 如果没有指定mimetype, 但是知道文件后缀。
1200
+ if ( !this.type && ~'jpg,jpeg,png,gif,bmp'.indexOf( ext ) ) {
1201
+ this.type = 'image/' + (ext === 'jpg' ? 'jpeg' : ext);
1202
+ }
1203
+
1204
+ this.ext = ext;
1205
+ this.lastModifiedDate = file.lastModifiedDate ||
1206
+ (new Date()).toLocaleString();
1207
+ }
1208
+
1209
+ return Base.inherits( Blob, File );
1210
+ });
1211
+
1212
+ /**
1213
+ * @fileOverview 错误信息
1214
+ */
1215
+ define('lib/filepicker',[
1216
+ 'base',
1217
+ 'runtime/client',
1218
+ 'lib/file'
1219
+ ], function( Base, RuntimeClent, File ) {
1220
+
1221
+ var $ = Base.$;
1222
+
1223
+ function FilePicker( opts ) {
1224
+ opts = this.options = $.extend({}, FilePicker.options, opts );
1225
+
1226
+ opts.container = $( opts.id );
1227
+
1228
+ if ( !opts.container.length ) {
1229
+ throw new Error('按钮指定错误');
1230
+ }
1231
+
1232
+ opts.innerHTML = opts.innerHTML || opts.label ||
1233
+ opts.container.html() || '';
1234
+
1235
+ opts.button = $( opts.button || document.createElement('div') );
1236
+ opts.button.html( opts.innerHTML );
1237
+ opts.container.html( opts.button );
1238
+
1239
+ RuntimeClent.call( this, 'FilePicker', true );
1240
+ }
1241
+
1242
+ FilePicker.options = {
1243
+ button: null,
1244
+ container: null,
1245
+ label: null,
1246
+ innerHTML: null,
1247
+ multiple: true,
1248
+ accept: null,
1249
+ name: 'file'
1250
+ };
1251
+
1252
+ Base.inherits( RuntimeClent, {
1253
+ constructor: FilePicker,
1254
+
1255
+ init: function() {
1256
+ var me = this,
1257
+ opts = me.options,
1258
+ button = opts.button;
1259
+
1260
+ button.addClass('webuploader-pick');
1261
+
1262
+ me.on( 'all', function( type ) {
1263
+ var files;
1264
+
1265
+ switch ( type ) {
1266
+ case 'mouseenter':
1267
+ button.addClass('webuploader-pick-hover');
1268
+ break;
1269
+
1270
+ case 'mouseleave':
1271
+ button.removeClass('webuploader-pick-hover');
1272
+ break;
1273
+
1274
+ case 'change':
1275
+ files = me.exec('getFiles');
1276
+ me.trigger( 'select', $.map( files, function( file ) {
1277
+ file = new File( me.getRuid(), file );
1278
+
1279
+ // 记录来源。
1280
+ file._refer = opts.container;
1281
+ return file;
1282
+ }), opts.container );
1283
+ break;
1284
+ }
1285
+ });
1286
+
1287
+ me.connectRuntime( opts, function() {
1288
+ me.refresh();
1289
+ me.exec( 'init', opts );
1290
+ me.trigger('ready');
1291
+ });
1292
+
1293
+ $( window ).on( 'resize', function() {
1294
+ me.refresh();
1295
+ });
1296
+ },
1297
+
1298
+ refresh: function() {
1299
+ var shimContainer = this.getRuntime().getContainer(),
1300
+ button = this.options.button,
1301
+ width = button.outerWidth ?
1302
+ button.outerWidth() : button.width(),
1303
+
1304
+ height = button.outerHeight ?
1305
+ button.outerHeight() : button.height(),
1306
+
1307
+ pos = button.offset();
1308
+
1309
+ width && height && shimContainer.css({
1310
+ bottom: 'auto',
1311
+ right: 'auto',
1312
+ width: width + 'px',
1313
+ height: height + 'px'
1314
+ }).offset( pos );
1315
+ },
1316
+
1317
+ enable: function() {
1318
+ var btn = this.options.button;
1319
+
1320
+ btn.removeClass('webuploader-pick-disable');
1321
+ this.refresh();
1322
+ },
1323
+
1324
+ disable: function() {
1325
+ var btn = this.options.button;
1326
+
1327
+ this.getRuntime().getContainer().css({
1328
+ top: '-99999px'
1329
+ });
1330
+
1331
+ btn.addClass('webuploader-pick-disable');
1332
+ },
1333
+
1334
+ destroy: function() {
1335
+ if ( this.runtime ) {
1336
+ this.exec('destroy');
1337
+ this.disconnectRuntime();
1338
+ }
1339
+ }
1340
+ });
1341
+
1342
+ return FilePicker;
1343
+ });
1344
+
1345
+ /**
1346
+ * @fileOverview 组件基类。
1347
+ */
1348
+ define('widgets/widget',[
1349
+ 'base',
1350
+ 'uploader'
1351
+ ], function( Base, Uploader ) {
1352
+
1353
+ var $ = Base.$,
1354
+ _init = Uploader.prototype._init,
1355
+ IGNORE = {},
1356
+ widgetClass = [];
1357
+
1358
+ function isArrayLike( obj ) {
1359
+ if ( !obj ) {
1360
+ return false;
1361
+ }
1362
+
1363
+ var length = obj.length,
1364
+ type = $.type( obj );
1365
+
1366
+ if ( obj.nodeType === 1 && length ) {
1367
+ return true;
1368
+ }
1369
+
1370
+ return type === 'array' || type !== 'function' && type !== 'string' &&
1371
+ (length === 0 || typeof length === 'number' && length > 0 &&
1372
+ (length - 1) in obj);
1373
+ }
1374
+
1375
+ function Widget( uploader ) {
1376
+ this.owner = uploader;
1377
+ this.options = uploader.options;
1378
+ }
1379
+
1380
+ $.extend( Widget.prototype, {
1381
+
1382
+ init: Base.noop,
1383
+
1384
+ // 类Backbone的事件监听声明,监听uploader实例上的事件
1385
+ // widget直接无法监听事件,事件只能通过uploader来传递
1386
+ invoke: function( apiName, args ) {
1387
+
1388
+ /*
1389
+ {
1390
+ 'make-thumb': 'makeThumb'
1391
+ }
1392
+ */
1393
+ var map = this.responseMap;
1394
+
1395
+ // 如果无API响应声明则忽略
1396
+ if ( !map || !(apiName in map) || !(map[ apiName ] in this) ||
1397
+ !$.isFunction( this[ map[ apiName ] ] ) ) {
1398
+
1399
+ return IGNORE;
1400
+ }
1401
+
1402
+ return this[ map[ apiName ] ].apply( this, args );
1403
+
1404
+ },
1405
+
1406
+ /**
1407
+ * 发送命令。当传入`callback`或者`handler`中返回`promise`时。返回一个当所有`handler`中的promise都完成后完成的新`promise`。
1408
+ * @method request
1409
+ * @grammar request( command, args ) => * | Promise
1410
+ * @grammar request( command, args, callback ) => Promise
1411
+ * @for Uploader
1412
+ */
1413
+ request: function() {
1414
+ return this.owner.request.apply( this.owner, arguments );
1415
+ }
1416
+ });
1417
+
1418
+ // 扩展Uploader.
1419
+ $.extend( Uploader.prototype, {
1420
+
1421
+ // 覆写_init用来初始化widgets
1422
+ _init: function() {
1423
+ var me = this,
1424
+ widgets = me._widgets = [];
1425
+
1426
+ $.each( widgetClass, function( _, klass ) {
1427
+ widgets.push( new klass( me ) );
1428
+ });
1429
+
1430
+ return _init.apply( me, arguments );
1431
+ },
1432
+
1433
+ request: function( apiName, args, callback ) {
1434
+ var i = 0,
1435
+ widgets = this._widgets,
1436
+ len = widgets.length,
1437
+ rlts = [],
1438
+ dfds = [],
1439
+ widget, rlt, promise, key;
1440
+
1441
+ args = isArrayLike( args ) ? args : [ args ];
1442
+
1443
+ for ( ; i < len; i++ ) {
1444
+ widget = widgets[ i ];
1445
+ rlt = widget.invoke( apiName, args );
1446
+
1447
+ if ( rlt !== IGNORE ) {
1448
+
1449
+ // Deferred对象
1450
+ if ( Base.isPromise( rlt ) ) {
1451
+ dfds.push( rlt );
1452
+ } else {
1453
+ rlts.push( rlt );
1454
+ }
1455
+ }
1456
+ }
1457
+
1458
+ // 如果有callback,则用异步方式。
1459
+ if ( callback || dfds.length ) {
1460
+ promise = Base.when.apply( Base, dfds );
1461
+ key = promise.pipe ? 'pipe' : 'then';
1462
+
1463
+ // 很重要不能删除。删除了会死循环。
1464
+ // 保证执行顺序。让callback总是在下一个tick中执行。
1465
+ return promise[ key ](function() {
1466
+ var deferred = Base.Deferred(),
1467
+ args = arguments;
1468
+
1469
+ setTimeout(function() {
1470
+ deferred.resolve.apply( deferred, args );
1471
+ }, 1 );
1472
+
1473
+ return deferred.promise();
1474
+ })[ key ]( callback || Base.noop );
1475
+ } else {
1476
+ return rlts[ 0 ];
1477
+ }
1478
+ }
1479
+ });
1480
+
1481
+ /**
1482
+ * 添加组件
1483
+ * @param {object} widgetProto 组件原型,构造函数通过constructor属性定义
1484
+ * @param {object} responseMap API名称与函数实现的映射
1485
+ * @example
1486
+ * Uploader.register( {
1487
+ * init: function( options ) {},
1488
+ * makeThumb: function() {}
1489
+ * }, {
1490
+ * 'make-thumb': 'makeThumb'
1491
+ * } );
1492
+ */
1493
+ Uploader.register = Widget.register = function( responseMap, widgetProto ) {
1494
+ var map = { init: 'init' },
1495
+ klass;
1496
+
1497
+ if ( arguments.length === 1 ) {
1498
+ widgetProto = responseMap;
1499
+ widgetProto.responseMap = map;
1500
+ } else {
1501
+ widgetProto.responseMap = $.extend( map, responseMap );
1502
+ }
1503
+
1504
+ klass = Base.inherits( Widget, widgetProto );
1505
+ widgetClass.push( klass );
1506
+
1507
+ return klass;
1508
+ };
1509
+
1510
+ return Widget;
1511
+ });
1512
+ /**
1513
+ * @fileOverview 文件选择相关
1514
+ */
1515
+ define('widgets/filepicker',[
1516
+ 'base',
1517
+ 'uploader',
1518
+ 'lib/filepicker',
1519
+ 'widgets/widget'
1520
+ ], function( Base, Uploader, FilePicker ) {
1521
+ var $ = Base.$;
1522
+
1523
+ $.extend( Uploader.options, {
1524
+
1525
+ /**
1526
+ * @property {Selector | Object} [pick=undefined]
1527
+ * @namespace options
1528
+ * @for Uploader
1529
+ * @description 指定选择文件的按钮容器,不指定则不创建按钮。
1530
+ *
1531
+ * * `id` {Seletor} 指定选择文件的按钮容器,不指定则不创建按钮。
1532
+ * * `label` {String} 请采用 `innerHTML` 代替
1533
+ * * `innerHTML` {String} 指定按钮文字。不指定时优先从指定的容器中看是否自带文字。
1534
+ * * `multiple` {Boolean} 是否开起同时选择多个文件能力。
1535
+ */
1536
+ pick: null,
1537
+
1538
+ /**
1539
+ * @property {Arroy} [accept=null]
1540
+ * @namespace options
1541
+ * @for Uploader
1542
+ * @description 指定接受哪些类型的文件。 由于目前还有ext转mimeType表,所以这里需要分开指定。
1543
+ *
1544
+ * * `title` {String} 文字描述
1545
+ * * `extensions` {String} 允许的文件后缀,不带点,多个用逗号分割。
1546
+ * * `mimeTypes` {String} 多个用逗号分割。
1547
+ *
1548
+ * 如:
1549
+ *
1550
+ * ```
1551
+ * {
1552
+ * title: 'Images',
1553
+ * extensions: 'gif,jpg,jpeg,bmp,png',
1554
+ * mimeTypes: 'image/*'
1555
+ * }
1556
+ * ```
1557
+ */
1558
+ accept: null/*{
1559
+ title: 'Images',
1560
+ extensions: 'gif,jpg,jpeg,bmp,png',
1561
+ mimeTypes: 'image/*'
1562
+ }*/
1563
+ });
1564
+
1565
+ return Uploader.register({
1566
+ 'add-btn': 'addButton',
1567
+ refresh: 'refresh',
1568
+ disable: 'disable',
1569
+ enable: 'enable'
1570
+ }, {
1571
+
1572
+ init: function( opts ) {
1573
+ this.pickers = [];
1574
+ return opts.pick && this.addButton( opts.pick );
1575
+ },
1576
+
1577
+ refresh: function() {
1578
+ $.each( this.pickers, function() {
1579
+ this.refresh();
1580
+ });
1581
+ },
1582
+
1583
+ /**
1584
+ * @method addButton
1585
+ * @for Uploader
1586
+ * @grammar addButton( pick ) => Promise
1587
+ * @description
1588
+ * 添加文件选择按钮,如果一个按钮不够,需要调用此方法来添加。参数跟[options.pick](#WebUploader:Uploader:options)一致。
1589
+ * @example
1590
+ * uploader.addButton({
1591
+ * id: '#btnContainer',
1592
+ * innerHTML: '选择文件'
1593
+ * });
1594
+ */
1595
+ addButton: function( pick ) {
1596
+ var me = this,
1597
+ opts = me.options,
1598
+ accept = opts.accept,
1599
+ options, picker, deferred;
1600
+
1601
+ if ( !pick ) {
1602
+ return;
1603
+ }
1604
+
1605
+ deferred = Base.Deferred();
1606
+ $.isPlainObject( pick ) || (pick = {
1607
+ id: pick
1608
+ });
1609
+
1610
+ options = $.extend({}, pick, {
1611
+ accept: $.isPlainObject( accept ) ? [ accept ] : accept,
1612
+ swf: opts.swf,
1613
+ runtimeOrder: opts.runtimeOrder
1614
+ });
1615
+
1616
+ picker = new FilePicker( options );
1617
+
1618
+ picker.once( 'ready', deferred.resolve );
1619
+ picker.on( 'select', function( files ) {
1620
+ me.owner.request( 'add-file', [ files ]);
1621
+ });
1622
+ picker.init();
1623
+
1624
+ this.pickers.push( picker );
1625
+
1626
+ return deferred.promise();
1627
+ },
1628
+
1629
+ disable: function() {
1630
+ $.each( this.pickers, function() {
1631
+ this.disable();
1632
+ });
1633
+ },
1634
+
1635
+ enable: function() {
1636
+ $.each( this.pickers, function() {
1637
+ this.enable();
1638
+ });
1639
+ }
1640
+ });
1641
+ });
1642
+ /**
1643
+ * @fileOverview Image
1644
+ */
1645
+ define('lib/image',[
1646
+ 'base',
1647
+ 'runtime/client',
1648
+ 'lib/blob'
1649
+ ], function( Base, RuntimeClient, Blob ) {
1650
+ var $ = Base.$;
1651
+
1652
+ // 构造器。
1653
+ function Image( opts ) {
1654
+ this.options = $.extend({}, Image.options, opts );
1655
+ RuntimeClient.call( this, 'Image' );
1656
+
1657
+ this.on( 'load', function() {
1658
+ this._info = this.exec('info');
1659
+ this._meta = this.exec('meta');
1660
+ });
1661
+ }
1662
+
1663
+ // 默认选项。
1664
+ Image.options = {
1665
+
1666
+ // 默认的图片处理质量
1667
+ quality: 90,
1668
+
1669
+ // 是否裁剪
1670
+ crop: false,
1671
+
1672
+ // 是否保留头部信息
1673
+ preserveHeaders: true,
1674
+
1675
+ // 是否允许放大。
1676
+ allowMagnify: true
1677
+ };
1678
+
1679
+ // 继承RuntimeClient.
1680
+ Base.inherits( RuntimeClient, {
1681
+ constructor: Image,
1682
+
1683
+ info: function( val ) {
1684
+
1685
+ // setter
1686
+ if ( val ) {
1687
+ this._info = val;
1688
+ return this;
1689
+ }
1690
+
1691
+ // getter
1692
+ return this._info;
1693
+ },
1694
+
1695
+ meta: function( val ) {
1696
+
1697
+ // setter
1698
+ if ( val ) {
1699
+ this._meta = val;
1700
+ return this;
1701
+ }
1702
+
1703
+ // getter
1704
+ return this._meta;
1705
+ },
1706
+
1707
+ loadFromBlob: function( blob ) {
1708
+ var me = this,
1709
+ ruid = blob.getRuid();
1710
+
1711
+ this.connectRuntime( ruid, function() {
1712
+ me.exec( 'init', me.options );
1713
+ me.exec( 'loadFromBlob', blob );
1714
+ });
1715
+ },
1716
+
1717
+ resize: function() {
1718
+ var args = Base.slice( arguments );
1719
+ return this.exec.apply( this, [ 'resize' ].concat( args ) );
1720
+ },
1721
+
1722
+ getAsDataUrl: function( type ) {
1723
+ return this.exec( 'getAsDataUrl', type );
1724
+ },
1725
+
1726
+ getAsBlob: function( type ) {
1727
+ var blob = this.exec( 'getAsBlob', type );
1728
+
1729
+ return new Blob( this.getRuid(), blob );
1730
+ }
1731
+ });
1732
+
1733
+ return Image;
1734
+ });
1735
+ /**
1736
+ * @fileOverview 图片操作, 负责预览图片和上传前压缩图片
1737
+ */
1738
+ define('widgets/image',[
1739
+ 'base',
1740
+ 'uploader',
1741
+ 'lib/image',
1742
+ 'widgets/widget'
1743
+ ], function( Base, Uploader, Image ) {
1744
+
1745
+ var $ = Base.$,
1746
+ throttle;
1747
+
1748
+ // 根据要处理的文件大小来节流,一次不能处理太多,会卡。
1749
+ throttle = (function( max ) {
1750
+ var occupied = 0,
1751
+ waiting = [],
1752
+ tick = function() {
1753
+ var item;
1754
+
1755
+ while ( waiting.length && occupied < max ) {
1756
+ item = waiting.shift();
1757
+ occupied += item[ 0 ];
1758
+ item[ 1 ]();
1759
+ }
1760
+ };
1761
+
1762
+ return function( emiter, size, cb ) {
1763
+ waiting.push([ size, cb ]);
1764
+ emiter.once( 'destroy', function() {
1765
+ occupied -= size;
1766
+ setTimeout( tick, 1 );
1767
+ });
1768
+ setTimeout( tick, 1 );
1769
+ };
1770
+ })( 5 * 1024 * 1024 );
1771
+
1772
+ $.extend( Uploader.options, {
1773
+
1774
+ /**
1775
+ * @property {Object} [thumb]
1776
+ * @namespace options
1777
+ * @for Uploader
1778
+ * @description 配置生成缩略图的选项。
1779
+ *
1780
+ * 默认为:
1781
+ *
1782
+ * ```javascript
1783
+ * {
1784
+ * width: 110,
1785
+ * height: 110,
1786
+ *
1787
+ * // 图片质量,只有type为`image/jpeg`的时候才有效。
1788
+ * quality: 70,
1789
+ *
1790
+ * // 是否允许放大,如果想要生成小图的时候不失真,此选项应该设置为false.
1791
+ * allowMagnify: true,
1792
+ *
1793
+ * // 是否允许裁剪。
1794
+ * crop: true,
1795
+ *
1796
+ * // 是否保留头部meta信息。
1797
+ * preserveHeaders: false,
1798
+ *
1799
+ * // 为空的话则保留原有图片格式。
1800
+ * // 否则强制转换成指定的类型。
1801
+ * type: 'image/jpeg'
1802
+ * }
1803
+ * ```
1804
+ */
1805
+ thumb: {
1806
+ width: 110,
1807
+ height: 110,
1808
+ quality: 70,
1809
+ allowMagnify: true,
1810
+ crop: true,
1811
+ preserveHeaders: false,
1812
+
1813
+ // 为空的话则保留原有图片格式。
1814
+ // 否则强制转换成指定的类型。
1815
+ // IE 8下面 base64 大小不能超过 32K 否则预览失败,而非 jpeg 编码的图片很可
1816
+ // 能会超过 32k, 所以这里设置成预览的时候都是 image/jpeg
1817
+ type: 'image/jpeg'
1818
+ },
1819
+
1820
+ /**
1821
+ * @property {Object} [compress]
1822
+ * @namespace options
1823
+ * @for Uploader
1824
+ * @description 配置压缩的图片的选项。如果此选项为`false`, 则图片在上传前不进行压缩。
1825
+ *
1826
+ * 默认为:
1827
+ *
1828
+ * ```javascript
1829
+ * {
1830
+ * width: 1600,
1831
+ * height: 1600,
1832
+ *
1833
+ * // 图片质量,只有type为`image/jpeg`的时候才有效。
1834
+ * quality: 90,
1835
+ *
1836
+ * // 是否允许放大,如果想要生成小图的时候不失真,此选项应该设置为false.
1837
+ * allowMagnify: false,
1838
+ *
1839
+ * // 是否允许裁剪。
1840
+ * crop: false,
1841
+ *
1842
+ * // 是否保留头部meta信息。
1843
+ * preserveHeaders: true
1844
+ * }
1845
+ * ```
1846
+ */
1847
+ compress: {
1848
+ width: 1600,
1849
+ height: 1600,
1850
+ quality: 90,
1851
+ allowMagnify: false,
1852
+ crop: false,
1853
+ preserveHeaders: true
1854
+ }
1855
+ });
1856
+
1857
+ return Uploader.register({
1858
+ 'make-thumb': 'makeThumb',
1859
+ 'before-send-file': 'compressImage'
1860
+ }, {
1861
+
1862
+
1863
+ /**
1864
+ * 生成缩略图,此过程为异步,所以需要传入`callback`。
1865
+ * 通常情况在图片加入队里后调用此方法来生成预览图以增强交互效果。
1866
+ *
1867
+ * `callback`中可以接收到两个参数。
1868
+ * * 第一个为error,如果生成缩略图有错误,此error将为真。
1869
+ * * 第二个为ret, 缩略图的Data URL值。
1870
+ *
1871
+ * **注意**
1872
+ * Date URL在IE6/7中不支持,所以不用调用此方法了,直接显示一张暂不支持预览图片好了。
1873
+ *
1874
+ *
1875
+ * @method makeThumb
1876
+ * @grammar makeThumb( file, callback ) => undefined
1877
+ * @grammar makeThumb( file, callback, width, height ) => undefined
1878
+ * @for Uploader
1879
+ * @example
1880
+ *
1881
+ * uploader.on( 'fileQueued', function( file ) {
1882
+ * var $li = ...;
1883
+ *
1884
+ * uploader.makeThumb( file, function( error, ret ) {
1885
+ * if ( error ) {
1886
+ * $li.text('预览错误');
1887
+ * } else {
1888
+ * $li.append('<img alt="" src="' + ret + '" />');
1889
+ * }
1890
+ * });
1891
+ *
1892
+ * });
1893
+ */
1894
+ makeThumb: function( file, cb, width, height ) {
1895
+ var opts, image;
1896
+
1897
+ file = this.request( 'get-file', file );
1898
+
1899
+ // 只预览图片格式。
1900
+ if ( !file.type.match( /^image/ ) ) {
1901
+ cb( true );
1902
+ return;
1903
+ }
1904
+
1905
+ opts = $.extend({}, this.options.thumb );
1906
+
1907
+ // 如果传入的是object.
1908
+ if ( $.isPlainObject( width ) ) {
1909
+ opts = $.extend( opts, width );
1910
+ width = null;
1911
+ }
1912
+
1913
+ width = width || opts.width;
1914
+ height = height || opts.height;
1915
+
1916
+ image = new Image( opts );
1917
+
1918
+ image.once( 'load', function() {
1919
+ file._info = file._info || image.info();
1920
+ file._meta = file._meta || image.meta();
1921
+ image.resize( width, height );
1922
+ });
1923
+
1924
+ image.once( 'complete', function() {
1925
+ cb( false, image.getAsDataUrl( opts.type ) );
1926
+ image.destroy();
1927
+ });
1928
+
1929
+ image.once( 'error', function() {
1930
+ cb( true );
1931
+ image.destroy();
1932
+ });
1933
+
1934
+ throttle( image, file.source.size, function() {
1935
+ file._info && image.info( file._info );
1936
+ file._meta && image.meta( file._meta );
1937
+ image.loadFromBlob( file.source );
1938
+ });
1939
+ },
1940
+
1941
+ compressImage: function( file ) {
1942
+ var opts = this.options.compress || this.options.resize,
1943
+ compressSize = opts && opts.compressSize || 300 * 1024,
1944
+ image, deferred;
1945
+
1946
+ file = this.request( 'get-file', file );
1947
+
1948
+ // 只预览图片格式。
1949
+ if ( !opts || !~'image/jpeg,image/jpg'.indexOf( file.type ) ||
1950
+ file.size < compressSize ||
1951
+ file._compressed ) {
1952
+ return;
1953
+ }
1954
+
1955
+ opts = $.extend({}, opts );
1956
+ deferred = Base.Deferred();
1957
+
1958
+ image = new Image( opts );
1959
+
1960
+ deferred.always(function() {
1961
+ image.destroy();
1962
+ image = null;
1963
+ });
1964
+ image.once( 'error', deferred.reject );
1965
+ image.once( 'load', function() {
1966
+ file._info = file._info || image.info();
1967
+ file._meta = file._meta || image.meta();
1968
+ image.resize( opts.width, opts.height );
1969
+ });
1970
+
1971
+ image.once( 'complete', function() {
1972
+ var blob, size;
1973
+
1974
+ // 移动端 UC / qq 浏览器的无图模式下
1975
+ // ctx.getImageData 处理大图的时候会报 Exception
1976
+ // INDEX_SIZE_ERR: DOM Exception 1
1977
+ try {
1978
+ blob = image.getAsBlob( opts.type );
1979
+
1980
+ size = file.size;
1981
+
1982
+ // 如果压缩后,比原来还大则不用压缩后的。
1983
+ if ( blob.size < size ) {
1984
+ // file.source.destroy && file.source.destroy();
1985
+ file.source = blob;
1986
+ file.size = blob.size;
1987
+
1988
+ file.trigger( 'resize', blob.size, size );
1989
+ }
1990
+
1991
+ // 标记,避免重复压缩。
1992
+ file._compressed = true;
1993
+ deferred.resolve();
1994
+ } catch ( e ) {
1995
+ // 出错了直接继续,让其上传原始图片
1996
+ deferred.resolve();
1997
+ }
1998
+ });
1999
+
2000
+ file._info && image.info( file._info );
2001
+ file._meta && image.meta( file._meta );
2002
+
2003
+ image.loadFromBlob( file.source );
2004
+ return deferred.promise();
2005
+ }
2006
+ });
2007
+ });
2008
+ /**
2009
+ * @fileOverview 文件属性封装
2010
+ */
2011
+ define('file',[
2012
+ 'base',
2013
+ 'mediator'
2014
+ ], function( Base, Mediator ) {
2015
+
2016
+ var $ = Base.$,
2017
+ idPrefix = 'WU_FILE_',
2018
+ idSuffix = 0,
2019
+ rExt = /\.([^.]+)$/,
2020
+ statusMap = {};
2021
+
2022
+ function gid() {
2023
+ return idPrefix + idSuffix++;
2024
+ }
2025
+
2026
+ /**
2027
+ * 文件类
2028
+ * @class File
2029
+ * @constructor 构造函数
2030
+ * @grammar new File( source ) => File
2031
+ * @param {Lib.File} source [lib.File](#Lib.File)实例, 此source对象是带有Runtime信息的。
2032
+ */
2033
+ function WUFile( source ) {
2034
+
2035
+ /**
2036
+ * 文件名,包括扩展名(后缀)
2037
+ * @property name
2038
+ * @type {string}
2039
+ */
2040
+ this.name = source.name || 'Untitled';
2041
+
2042
+ /**
2043
+ * 文件体积(字节)
2044
+ * @property size
2045
+ * @type {uint}
2046
+ * @default 0
2047
+ */
2048
+ this.size = source.size || 0;
2049
+
2050
+ /**
2051
+ * 文件MIMETYPE类型,与文件类型的对应关系请参考[http://t.cn/z8ZnFny](http://t.cn/z8ZnFny)
2052
+ * @property type
2053
+ * @type {string}
2054
+ * @default 'application'
2055
+ */
2056
+ this.type = source.type || 'application';
2057
+
2058
+ /**
2059
+ * 文件最后修改日期
2060
+ * @property lastModifiedDate
2061
+ * @type {int}
2062
+ * @default 当前时间戳
2063
+ */
2064
+ this.lastModifiedDate = source.lastModifiedDate || (new Date() * 1);
2065
+
2066
+ /**
2067
+ * 文件ID,每个对象具有唯一ID,与文件名无关
2068
+ * @property id
2069
+ * @type {string}
2070
+ */
2071
+ this.id = gid();
2072
+
2073
+ /**
2074
+ * 文件扩展名,通过文件名获取,例如test.png的扩展名为png
2075
+ * @property ext
2076
+ * @type {string}
2077
+ */
2078
+ this.ext = rExt.exec( this.name ) ? RegExp.$1 : '';
2079
+
2080
+
2081
+ /**
2082
+ * 状态文字说明。在不同的status语境下有不同的用途。
2083
+ * @property statusText
2084
+ * @type {string}
2085
+ */
2086
+ this.statusText = '';
2087
+
2088
+ // 存储文件状态,防止通过属性直接修改
2089
+ statusMap[ this.id ] = WUFile.Status.INITED;
2090
+
2091
+ this.source = source;
2092
+ this.loaded = 0;
2093
+
2094
+ this.on( 'error', function( msg ) {
2095
+ this.setStatus( WUFile.Status.ERROR, msg );
2096
+ });
2097
+ }
2098
+
2099
+ $.extend( WUFile.prototype, {
2100
+
2101
+ /**
2102
+ * 设置状态,状态变化时会触发`change`事件。
2103
+ * @method setStatus
2104
+ * @grammar setStatus( status[, statusText] );
2105
+ * @param {File.Status|String} status [文件状态值](#WebUploader:File:File.Status)
2106
+ * @param {String} [statusText=''] 状态说明,常在error时使用,用http, abort,server等来标记是由于什么原因导致文件错误。
2107
+ */
2108
+ setStatus: function( status, text ) {
2109
+
2110
+ var prevStatus = statusMap[ this.id ];
2111
+
2112
+ typeof text !== 'undefined' && (this.statusText = text);
2113
+
2114
+ if ( status !== prevStatus ) {
2115
+ statusMap[ this.id ] = status;
2116
+ /**
2117
+ * 文件状态变化
2118
+ * @event statuschange
2119
+ */
2120
+ this.trigger( 'statuschange', status, prevStatus );
2121
+ }
2122
+
2123
+ },
2124
+
2125
+ /**
2126
+ * 获取文件状态
2127
+ * @return {File.Status}
2128
+ * @example
2129
+ 文件状态具体包括以下几种类型:
2130
+ {
2131
+ // 初始化
2132
+ INITED: 0,
2133
+ // 已入队列
2134
+ QUEUED: 1,
2135
+ // 正在上传
2136
+ PROGRESS: 2,
2137
+ // 上传出错
2138
+ ERROR: 3,
2139
+ // 上传成功
2140
+ COMPLETE: 4,
2141
+ // 上传取消
2142
+ CANCELLED: 5
2143
+ }
2144
+ */
2145
+ getStatus: function() {
2146
+ return statusMap[ this.id ];
2147
+ },
2148
+
2149
+ /**
2150
+ * 获取文件原始信息。
2151
+ * @return {*}
2152
+ */
2153
+ getSource: function() {
2154
+ return this.source;
2155
+ },
2156
+
2157
+ destory: function() {
2158
+ delete statusMap[ this.id ];
2159
+ }
2160
+ });
2161
+
2162
+ Mediator.installTo( WUFile.prototype );
2163
+
2164
+ /**
2165
+ * 文件状态值,具体包括以下几种类型:
2166
+ * * `inited` 初始状态
2167
+ * * `queued` 已经进入队列, 等待上传
2168
+ * * `progress` 上传中
2169
+ * * `complete` 上传完成。
2170
+ * * `error` 上传出错,可重试
2171
+ * * `interrupt` 上传中断,可续传。
2172
+ * * `invalid` 文件不合格,不能重试上传。会自动从队列中移除。
2173
+ * * `cancelled` 文件被移除。
2174
+ * @property {Object} Status
2175
+ * @namespace File
2176
+ * @class File
2177
+ * @static
2178
+ */
2179
+ WUFile.Status = {
2180
+ INITED: 'inited', // 初始状态
2181
+ QUEUED: 'queued', // 已经进入队列, 等待上传
2182
+ PROGRESS: 'progress', // 上传中
2183
+ ERROR: 'error', // 上传出错,可重试
2184
+ COMPLETE: 'complete', // 上传完成。
2185
+ CANCELLED: 'cancelled', // 上传取消。
2186
+ INTERRUPT: 'interrupt', // 上传中断,可续传。
2187
+ INVALID: 'invalid' // 文件不合格,不能重试上传。
2188
+ };
2189
+
2190
+ return WUFile;
2191
+ });
2192
+
2193
+ /**
2194
+ * @fileOverview 文件队列
2195
+ */
2196
+ define('queue',[
2197
+ 'base',
2198
+ 'mediator',
2199
+ 'file'
2200
+ ], function( Base, Mediator, WUFile ) {
2201
+
2202
+ var $ = Base.$,
2203
+ STATUS = WUFile.Status;
2204
+
2205
+ /**
2206
+ * 文件队列, 用来存储各个状态中的文件。
2207
+ * @class Queue
2208
+ * @extends Mediator
2209
+ */
2210
+ function Queue() {
2211
+
2212
+ /**
2213
+ * 统计文件数。
2214
+ * * `numOfQueue` 队列中的文件数。
2215
+ * * `numOfSuccess` 上传成功的文件数
2216
+ * * `numOfCancel` 被移除的文件数
2217
+ * * `numOfProgress` 正在上传中的文件数
2218
+ * * `numOfUploadFailed` 上传错误的文件数。
2219
+ * * `numOfInvalid` 无效的文件数。
2220
+ * @property {Object} stats
2221
+ */
2222
+ this.stats = {
2223
+ numOfQueue: 0,
2224
+ numOfSuccess: 0,
2225
+ numOfCancel: 0,
2226
+ numOfProgress: 0,
2227
+ numOfUploadFailed: 0,
2228
+ numOfInvalid: 0
2229
+ };
2230
+
2231
+ // 上传队列,仅包括等待上传的文件
2232
+ this._queue = [];
2233
+
2234
+ // 存储所有文件
2235
+ this._map = {};
2236
+ }
2237
+
2238
+ $.extend( Queue.prototype, {
2239
+
2240
+ /**
2241
+ * 将新文件加入对队列尾部
2242
+ *
2243
+ * @method append
2244
+ * @param {File} file 文件对象
2245
+ */
2246
+ append: function( file ) {
2247
+ this._queue.push( file );
2248
+ this._fileAdded( file );
2249
+ return this;
2250
+ },
2251
+
2252
+ /**
2253
+ * 将新文件加入对队列头部
2254
+ *
2255
+ * @method prepend
2256
+ * @param {File} file 文件对象
2257
+ */
2258
+ prepend: function( file ) {
2259
+ this._queue.unshift( file );
2260
+ this._fileAdded( file );
2261
+ return this;
2262
+ },
2263
+
2264
+ /**
2265
+ * 获取文件对象
2266
+ *
2267
+ * @method getFile
2268
+ * @param {String} fileId 文件ID
2269
+ * @return {File}
2270
+ */
2271
+ getFile: function( fileId ) {
2272
+ if ( typeof fileId !== 'string' ) {
2273
+ return fileId;
2274
+ }
2275
+ return this._map[ fileId ];
2276
+ },
2277
+
2278
+ /**
2279
+ * 从队列中取出一个指定状态的文件。
2280
+ * @grammar fetch( status ) => File
2281
+ * @method fetch
2282
+ * @param {String} status [文件状态值](#WebUploader:File:File.Status)
2283
+ * @return {File} [File](#WebUploader:File)
2284
+ */
2285
+ fetch: function( status ) {
2286
+ var len = this._queue.length,
2287
+ i, file;
2288
+
2289
+ status = status || STATUS.QUEUED;
2290
+
2291
+ for ( i = 0; i < len; i++ ) {
2292
+ file = this._queue[ i ];
2293
+
2294
+ if ( status === file.getStatus() ) {
2295
+ return file;
2296
+ }
2297
+ }
2298
+
2299
+ return null;
2300
+ },
2301
+
2302
+ /**
2303
+ * 对队列进行排序,能够控制文件上传顺序。
2304
+ * @grammar sort( fn ) => undefined
2305
+ * @method sort
2306
+ * @param {Function} fn 排序方法
2307
+ */
2308
+ sort: function( fn ) {
2309
+ if ( typeof fn === 'function' ) {
2310
+ this._queue.sort( fn );
2311
+ }
2312
+ },
2313
+
2314
+ /**
2315
+ * 获取指定类型的文件列表, 列表中每一个成员为[File](#WebUploader:File)对象。
2316
+ * @grammar getFiles( [status1[, status2 ...]] ) => Array
2317
+ * @method getFiles
2318
+ * @param {String} [status] [文件状态值](#WebUploader:File:File.Status)
2319
+ */
2320
+ getFiles: function() {
2321
+ var sts = [].slice.call( arguments, 0 ),
2322
+ ret = [],
2323
+ i = 0,
2324
+ len = this._queue.length,
2325
+ file;
2326
+
2327
+ for ( ; i < len; i++ ) {
2328
+ file = this._queue[ i ];
2329
+
2330
+ if ( sts.length && !~$.inArray( file.getStatus(), sts ) ) {
2331
+ continue;
2332
+ }
2333
+
2334
+ ret.push( file );
2335
+ }
2336
+
2337
+ return ret;
2338
+ },
2339
+
2340
+ _fileAdded: function( file ) {
2341
+ var me = this,
2342
+ existing = this._map[ file.id ];
2343
+
2344
+ if ( !existing ) {
2345
+ this._map[ file.id ] = file;
2346
+
2347
+ file.on( 'statuschange', function( cur, pre ) {
2348
+ me._onFileStatusChange( cur, pre );
2349
+ });
2350
+ }
2351
+
2352
+ file.setStatus( STATUS.QUEUED );
2353
+ },
2354
+
2355
+ _onFileStatusChange: function( curStatus, preStatus ) {
2356
+ var stats = this.stats;
2357
+
2358
+ switch ( preStatus ) {
2359
+ case STATUS.PROGRESS:
2360
+ stats.numOfProgress--;
2361
+ break;
2362
+
2363
+ case STATUS.QUEUED:
2364
+ stats.numOfQueue --;
2365
+ break;
2366
+
2367
+ case STATUS.ERROR:
2368
+ stats.numOfUploadFailed--;
2369
+ break;
2370
+
2371
+ case STATUS.INVALID:
2372
+ stats.numOfInvalid--;
2373
+ break;
2374
+ }
2375
+
2376
+ switch ( curStatus ) {
2377
+ case STATUS.QUEUED:
2378
+ stats.numOfQueue++;
2379
+ break;
2380
+
2381
+ case STATUS.PROGRESS:
2382
+ stats.numOfProgress++;
2383
+ break;
2384
+
2385
+ case STATUS.ERROR:
2386
+ stats.numOfUploadFailed++;
2387
+ break;
2388
+
2389
+ case STATUS.COMPLETE:
2390
+ stats.numOfSuccess++;
2391
+ break;
2392
+
2393
+ case STATUS.CANCELLED:
2394
+ stats.numOfCancel++;
2395
+ break;
2396
+
2397
+ case STATUS.INVALID:
2398
+ stats.numOfInvalid++;
2399
+ break;
2400
+ }
2401
+ }
2402
+
2403
+ });
2404
+
2405
+ Mediator.installTo( Queue.prototype );
2406
+
2407
+ return Queue;
2408
+ });
2409
+ /**
2410
+ * @fileOverview 队列
2411
+ */
2412
+ define('widgets/queue',[
2413
+ 'base',
2414
+ 'uploader',
2415
+ 'queue',
2416
+ 'file',
2417
+ 'lib/file',
2418
+ 'runtime/client',
2419
+ 'widgets/widget'
2420
+ ], function( Base, Uploader, Queue, WUFile, File, RuntimeClient ) {
2421
+
2422
+ var $ = Base.$,
2423
+ rExt = /\.\w+$/,
2424
+ Status = WUFile.Status;
2425
+
2426
+ return Uploader.register({
2427
+ 'sort-files': 'sortFiles',
2428
+ 'add-file': 'addFiles',
2429
+ 'get-file': 'getFile',
2430
+ 'fetch-file': 'fetchFile',
2431
+ 'get-stats': 'getStats',
2432
+ 'get-files': 'getFiles',
2433
+ 'remove-file': 'removeFile',
2434
+ 'retry': 'retry',
2435
+ 'reset': 'reset',
2436
+ 'accept-file': 'acceptFile'
2437
+ }, {
2438
+
2439
+ init: function( opts ) {
2440
+ var me = this,
2441
+ deferred, len, i, item, arr, accept, runtime;
2442
+
2443
+ if ( $.isPlainObject( opts.accept ) ) {
2444
+ opts.accept = [ opts.accept ];
2445
+ }
2446
+
2447
+ // accept中的中生成匹配正则。
2448
+ if ( opts.accept ) {
2449
+ arr = [];
2450
+
2451
+ for ( i = 0, len = opts.accept.length; i < len; i++ ) {
2452
+ item = opts.accept[ i ].extensions;
2453
+ item && arr.push( item );
2454
+ }
2455
+
2456
+ if ( arr.length ) {
2457
+ accept = '\\.' + arr.join(',')
2458
+ .replace( /,/g, '$|\\.' )
2459
+ .replace( /\*/g, '.*' ) + '$';
2460
+ }
2461
+
2462
+ me.accept = new RegExp( accept, 'i' );
2463
+ }
2464
+
2465
+ me.queue = new Queue();
2466
+ me.stats = me.queue.stats;
2467
+
2468
+ // 如果当前不是html5运行时,那就算了。
2469
+ // 不执行后续操作
2470
+ if ( this.request('predict-runtime-type') !== 'html5' ) {
2471
+ return;
2472
+ }
2473
+
2474
+ // 创建一个 html5 运行时的 placeholder
2475
+ // 以至于外部添加原生 File 对象的时候能正确包裹一下供 webuploader 使用。
2476
+ deferred = Base.Deferred();
2477
+ runtime = new RuntimeClient('Placeholder');
2478
+ runtime.connectRuntime({
2479
+ runtimeOrder: 'html5'
2480
+ }, function() {
2481
+ me._ruid = runtime.getRuid();
2482
+ deferred.resolve();
2483
+ });
2484
+ return deferred.promise();
2485
+ },
2486
+
2487
+
2488
+ // 为了支持外部直接添加一个原生File对象。
2489
+ _wrapFile: function( file ) {
2490
+ if ( !(file instanceof WUFile) ) {
2491
+
2492
+ if ( !(file instanceof File) ) {
2493
+ if ( !this._ruid ) {
2494
+ throw new Error('Can\'t add external files.');
2495
+ }
2496
+ file = new File( this._ruid, file );
2497
+ }
2498
+
2499
+ file = new WUFile( file );
2500
+ }
2501
+
2502
+ return file;
2503
+ },
2504
+
2505
+ // 判断文件是否可以被加入队列
2506
+ acceptFile: function( file ) {
2507
+ var invalid = !file || file.size < 6 || this.accept &&
2508
+
2509
+ // 如果名字中有后缀,才做后缀白名单处理。
2510
+ rExt.exec( file.name ) && !this.accept.test( file.name );
2511
+
2512
+ return !invalid;
2513
+ },
2514
+
2515
+
2516
+ /**
2517
+ * @event beforeFileQueued
2518
+ * @param {File} file File对象
2519
+ * @description 当文件被加入队列之前触发,此事件的handler返回值为`false`,则此文件不会被添加进入队列。
2520
+ * @for Uploader
2521
+ */
2522
+
2523
+ /**
2524
+ * @event fileQueued
2525
+ * @param {File} file File对象
2526
+ * @description 当文件被加入队列以后触发。
2527
+ * @for Uploader
2528
+ */
2529
+
2530
+ _addFile: function( file ) {
2531
+ var me = this;
2532
+
2533
+ file = me._wrapFile( file );
2534
+
2535
+ // 不过类型判断允许不允许,先派送 `beforeFileQueued`
2536
+ if ( !me.owner.trigger( 'beforeFileQueued', file ) ) {
2537
+ return;
2538
+ }
2539
+
2540
+ // 类型不匹配,则派送错误事件,并返回。
2541
+ if ( !me.acceptFile( file ) ) {
2542
+ me.owner.trigger( 'error', 'Q_TYPE_DENIED', file );
2543
+ return;
2544
+ }
2545
+
2546
+ me.queue.append( file );
2547
+ me.owner.trigger( 'fileQueued', file );
2548
+ return file;
2549
+ },
2550
+
2551
+ getFile: function( fileId ) {
2552
+ return this.queue.getFile( fileId );
2553
+ },
2554
+
2555
+ /**
2556
+ * @event filesQueued
2557
+ * @param {File} files 数组,内容为原始File(lib/File)对象。
2558
+ * @description 当一批文件添加进队列以后触发。
2559
+ * @for Uploader
2560
+ */
2561
+
2562
+ /**
2563
+ * @method addFiles
2564
+ * @grammar addFiles( file ) => undefined
2565
+ * @grammar addFiles( [file1, file2 ...] ) => undefined
2566
+ * @param {Array of File or File} [files] Files 对象 数组
2567
+ * @description 添加文件到队列
2568
+ * @for Uploader
2569
+ */
2570
+ addFiles: function( files ) {
2571
+ var me = this;
2572
+
2573
+ if ( !files.length ) {
2574
+ files = [ files ];
2575
+ }
2576
+
2577
+ files = $.map( files, function( file ) {
2578
+ return me._addFile( file );
2579
+ });
2580
+
2581
+ me.owner.trigger( 'filesQueued', files );
2582
+
2583
+ if ( me.options.auto ) {
2584
+ me.request('start-upload');
2585
+ }
2586
+ },
2587
+
2588
+ getStats: function() {
2589
+ return this.stats;
2590
+ },
2591
+
2592
+ /**
2593
+ * @event fileDequeued
2594
+ * @param {File} file File对象
2595
+ * @description 当文件被移除队列后触发。
2596
+ * @for Uploader
2597
+ */
2598
+
2599
+ /**
2600
+ * @method removeFile
2601
+ * @grammar removeFile( file ) => undefined
2602
+ * @grammar removeFile( id ) => undefined
2603
+ * @param {File|id} file File对象或这File对象的id
2604
+ * @description 移除某一文件。
2605
+ * @for Uploader
2606
+ * @example
2607
+ *
2608
+ * $li.on('click', '.remove-this', function() {
2609
+ * uploader.removeFile( file );
2610
+ * })
2611
+ */
2612
+ removeFile: function( file ) {
2613
+ var me = this;
2614
+
2615
+ file = file.id ? file : me.queue.getFile( file );
2616
+
2617
+ file.setStatus( Status.CANCELLED );
2618
+ me.owner.trigger( 'fileDequeued', file );
2619
+ },
2620
+
2621
+ /**
2622
+ * @method getFiles
2623
+ * @grammar getFiles() => Array
2624
+ * @grammar getFiles( status1, status2, status... ) => Array
2625
+ * @description 返回指定状态的文件集合,不传参数将返回所有状态的文件。
2626
+ * @for Uploader
2627
+ * @example
2628
+ * console.log( uploader.getFiles() ); // => all files
2629
+ * console.log( uploader.getFiles('error') ) // => all error files.
2630
+ */
2631
+ getFiles: function() {
2632
+ return this.queue.getFiles.apply( this.queue, arguments );
2633
+ },
2634
+
2635
+ fetchFile: function() {
2636
+ return this.queue.fetch.apply( this.queue, arguments );
2637
+ },
2638
+
2639
+ /**
2640
+ * @method retry
2641
+ * @grammar retry() => undefined
2642
+ * @grammar retry( file ) => undefined
2643
+ * @description 重试上传,重试指定文件,或者从出错的文件开始重新上传。
2644
+ * @for Uploader
2645
+ * @example
2646
+ * function retry() {
2647
+ * uploader.retry();
2648
+ * }
2649
+ */
2650
+ retry: function( file, noForceStart ) {
2651
+ var me = this,
2652
+ files, i, len;
2653
+
2654
+ if ( file ) {
2655
+ file = file.id ? file : me.queue.getFile( file );
2656
+ file.setStatus( Status.QUEUED );
2657
+ noForceStart || me.request('start-upload');
2658
+ return;
2659
+ }
2660
+
2661
+ files = me.queue.getFiles( Status.ERROR );
2662
+ i = 0;
2663
+ len = files.length;
2664
+
2665
+ for ( ; i < len; i++ ) {
2666
+ file = files[ i ];
2667
+ file.setStatus( Status.QUEUED );
2668
+ }
2669
+
2670
+ me.request('start-upload');
2671
+ },
2672
+
2673
+ /**
2674
+ * @method sort
2675
+ * @grammar sort( fn ) => undefined
2676
+ * @description 排序队列中的文件,在上传之前调整可以控制上传顺序。
2677
+ * @for Uploader
2678
+ */
2679
+ sortFiles: function() {
2680
+ return this.queue.sort.apply( this.queue, arguments );
2681
+ },
2682
+
2683
+ /**
2684
+ * @method reset
2685
+ * @grammar reset() => undefined
2686
+ * @description 重置uploader。目前只重置了队列。
2687
+ * @for Uploader
2688
+ * @example
2689
+ * uploader.reset();
2690
+ */
2691
+ reset: function() {
2692
+ this.queue = new Queue();
2693
+ this.stats = this.queue.stats;
2694
+ }
2695
+ });
2696
+
2697
+ });
2698
+ /**
2699
+ * @fileOverview 添加获取Runtime相关信息的方法。
2700
+ */
2701
+ define('widgets/runtime',[
2702
+ 'uploader',
2703
+ 'runtime/runtime',
2704
+ 'widgets/widget'
2705
+ ], function( Uploader, Runtime ) {
2706
+
2707
+ Uploader.support = function() {
2708
+ return Runtime.hasRuntime.apply( Runtime, arguments );
2709
+ };
2710
+
2711
+ return Uploader.register({
2712
+ 'predict-runtime-type': 'predictRuntmeType'
2713
+ }, {
2714
+
2715
+ init: function() {
2716
+ if ( !this.predictRuntmeType() ) {
2717
+ throw Error('Runtime Error');
2718
+ }
2719
+ },
2720
+
2721
+ /**
2722
+ * 预测Uploader将采用哪个`Runtime`
2723
+ * @grammar predictRuntmeType() => String
2724
+ * @method predictRuntmeType
2725
+ * @for Uploader
2726
+ */
2727
+ predictRuntmeType: function() {
2728
+ var orders = this.options.runtimeOrder || Runtime.orders,
2729
+ type = this.type,
2730
+ i, len;
2731
+
2732
+ if ( !type ) {
2733
+ orders = orders.split( /\s*,\s*/g );
2734
+
2735
+ for ( i = 0, len = orders.length; i < len; i++ ) {
2736
+ if ( Runtime.hasRuntime( orders[ i ] ) ) {
2737
+ this.type = type = orders[ i ];
2738
+ break;
2739
+ }
2740
+ }
2741
+ }
2742
+
2743
+ return type;
2744
+ }
2745
+ });
2746
+ });
2747
+ /**
2748
+ * @fileOverview Transport
2749
+ */
2750
+ define('lib/transport',[
2751
+ 'base',
2752
+ 'runtime/client',
2753
+ 'mediator'
2754
+ ], function( Base, RuntimeClient, Mediator ) {
2755
+
2756
+ var $ = Base.$;
2757
+
2758
+ function Transport( opts ) {
2759
+ var me = this;
2760
+
2761
+ opts = me.options = $.extend( true, {}, Transport.options, opts || {} );
2762
+ RuntimeClient.call( this, 'Transport' );
2763
+
2764
+ this._blob = null;
2765
+ this._formData = opts.formData || {};
2766
+ this._headers = opts.headers || {};
2767
+
2768
+ this.on( 'progress', this._timeout );
2769
+ this.on( 'load error', function() {
2770
+ me.trigger( 'progress', 1 );
2771
+ clearTimeout( me._timer );
2772
+ });
2773
+ }
2774
+
2775
+ Transport.options = {
2776
+ server: '',
2777
+ method: 'POST',
2778
+
2779
+ // 跨域时,是否允许携带cookie, 只有html5 runtime才有效
2780
+ withCredentials: false,
2781
+ fileVal: 'file',
2782
+ timeout: 2 * 60 * 1000, // 2分钟
2783
+ formData: {},
2784
+ headers: {},
2785
+ sendAsBinary: false
2786
+ };
2787
+
2788
+ $.extend( Transport.prototype, {
2789
+
2790
+ // 添加Blob, 只能添加一次,最后一次有效。
2791
+ appendBlob: function( key, blob, filename ) {
2792
+ var me = this,
2793
+ opts = me.options;
2794
+
2795
+ if ( me.getRuid() ) {
2796
+ me.disconnectRuntime();
2797
+ }
2798
+
2799
+ // 连接到blob归属的同一个runtime.
2800
+ me.connectRuntime( blob.ruid, function() {
2801
+ me.exec('init');
2802
+ });
2803
+
2804
+ me._blob = blob;
2805
+ opts.fileVal = key || opts.fileVal;
2806
+ opts.filename = filename || opts.filename;
2807
+ },
2808
+
2809
+ // 添加其他字段
2810
+ append: function( key, value ) {
2811
+ if ( typeof key === 'object' ) {
2812
+ $.extend( this._formData, key );
2813
+ } else {
2814
+ this._formData[ key ] = value;
2815
+ }
2816
+ },
2817
+
2818
+ setRequestHeader: function( key, value ) {
2819
+ if ( typeof key === 'object' ) {
2820
+ $.extend( this._headers, key );
2821
+ } else {
2822
+ this._headers[ key ] = value;
2823
+ }
2824
+ },
2825
+
2826
+ send: function( method ) {
2827
+ this.exec( 'send', method );
2828
+ this._timeout();
2829
+ },
2830
+
2831
+ abort: function() {
2832
+ clearTimeout( this._timer );
2833
+ return this.exec('abort');
2834
+ },
2835
+
2836
+ destroy: function() {
2837
+ this.trigger('destroy');
2838
+ this.off();
2839
+ this.exec('destroy');
2840
+ this.disconnectRuntime();
2841
+ },
2842
+
2843
+ getResponse: function() {
2844
+ return this.exec('getResponse');
2845
+ },
2846
+
2847
+ getResponseAsJson: function() {
2848
+ return this.exec('getResponseAsJson');
2849
+ },
2850
+
2851
+ getStatus: function() {
2852
+ return this.exec('getStatus');
2853
+ },
2854
+
2855
+ _timeout: function() {
2856
+ var me = this,
2857
+ duration = me.options.timeout;
2858
+
2859
+ if ( !duration ) {
2860
+ return;
2861
+ }
2862
+
2863
+ clearTimeout( me._timer );
2864
+ me._timer = setTimeout(function() {
2865
+ me.abort();
2866
+ me.trigger( 'error', 'timeout' );
2867
+ }, duration );
2868
+ }
2869
+
2870
+ });
2871
+
2872
+ // 让Transport具备事件功能。
2873
+ Mediator.installTo( Transport.prototype );
2874
+
2875
+ return Transport;
2876
+ });
2877
+ /**
2878
+ * @fileOverview 负责文件上传相关。
2879
+ */
2880
+ define('widgets/upload',[
2881
+ 'base',
2882
+ 'uploader',
2883
+ 'file',
2884
+ 'lib/transport',
2885
+ 'widgets/widget'
2886
+ ], function( Base, Uploader, WUFile, Transport ) {
2887
+
2888
+ var $ = Base.$,
2889
+ isPromise = Base.isPromise,
2890
+ Status = WUFile.Status;
2891
+
2892
+ // 添加默认配置项
2893
+ $.extend( Uploader.options, {
2894
+
2895
+
2896
+ /**
2897
+ * @property {Boolean} [prepareNextFile=false]
2898
+ * @namespace options
2899
+ * @for Uploader
2900
+ * @description 是否允许在文件传输时提前把下一个文件准备好。
2901
+ * 对于一个文件的准备工作比较耗时,比如图片压缩,md5序列化。
2902
+ * 如果能提前在当前文件传输期处理,可以节省总体耗时。
2903
+ */
2904
+ prepareNextFile: false,
2905
+
2906
+ /**
2907
+ * @property {Boolean} [chunked=false]
2908
+ * @namespace options
2909
+ * @for Uploader
2910
+ * @description 是否要分片处理大文件上传。
2911
+ */
2912
+ chunked: false,
2913
+
2914
+ /**
2915
+ * @property {Boolean} [chunkSize=5242880]
2916
+ * @namespace options
2917
+ * @for Uploader
2918
+ * @description 如果要分片,分多大一片? 默认大小为5M.
2919
+ */
2920
+ chunkSize: 5 * 1024 * 1024,
2921
+
2922
+ /**
2923
+ * @property {Boolean} [chunkRetry=2]
2924
+ * @namespace options
2925
+ * @for Uploader
2926
+ * @description 如果某个分片由于网络问题出错,允许自动重传多少次?
2927
+ */
2928
+ chunkRetry: 2,
2929
+
2930
+ /**
2931
+ * @property {Boolean} [threads=3]
2932
+ * @namespace options
2933
+ * @for Uploader
2934
+ * @description 上传并发数。允许同时最大上传进程数。
2935
+ */
2936
+ threads: 3,
2937
+
2938
+
2939
+ /**
2940
+ * @property {Object} [formData]
2941
+ * @namespace options
2942
+ * @for Uploader
2943
+ * @description 文件上传请求的参数表,每次发送都会发送此对象中的参数。
2944
+ */
2945
+ formData: null
2946
+
2947
+ /**
2948
+ * @property {Object} [fileVal='file']
2949
+ * @namespace options
2950
+ * @for Uploader
2951
+ * @description 设置文件上传域的name。
2952
+ */
2953
+
2954
+ /**
2955
+ * @property {Object} [method='POST']
2956
+ * @namespace options
2957
+ * @for Uploader
2958
+ * @description 文件上传方式,`POST`或者`GET`。
2959
+ */
2960
+
2961
+ /**
2962
+ * @property {Object} [sendAsBinary=false]
2963
+ * @namespace options
2964
+ * @for Uploader
2965
+ * @description 是否已二进制的流的方式发送文件,这样整个上传内容`php://input`都为文件内容,
2966
+ * 其他参数在$_GET数组中。
2967
+ */
2968
+ });
2969
+
2970
+ // 负责将文件切片。
2971
+ function CuteFile( file, chunkSize ) {
2972
+ var pending = [],
2973
+ blob = file.source,
2974
+ total = blob.size,
2975
+ chunks = chunkSize ? Math.ceil( total / chunkSize ) : 1,
2976
+ start = 0,
2977
+ index = 0,
2978
+ len;
2979
+
2980
+ while ( index < chunks ) {
2981
+ len = Math.min( chunkSize, total - start );
2982
+
2983
+ pending.push({
2984
+ file: file,
2985
+ start: start,
2986
+ end: chunkSize ? (start + len) : total,
2987
+ total: total,
2988
+ chunks: chunks,
2989
+ chunk: index++
2990
+ });
2991
+ start += len;
2992
+ }
2993
+
2994
+ file.blocks = pending.concat();
2995
+ file.remaning = pending.length;
2996
+
2997
+ return {
2998
+ file: file,
2999
+
3000
+ has: function() {
3001
+ return !!pending.length;
3002
+ },
3003
+
3004
+ fetch: function() {
3005
+ return pending.shift();
3006
+ }
3007
+ };
3008
+ }
3009
+
3010
+ Uploader.register({
3011
+ 'start-upload': 'start',
3012
+ 'stop-upload': 'stop',
3013
+ 'skip-file': 'skipFile',
3014
+ 'is-in-progress': 'isInProgress'
3015
+ }, {
3016
+
3017
+ init: function() {
3018
+ var owner = this.owner;
3019
+
3020
+ this.runing = false;
3021
+
3022
+ // 记录当前正在传的数据,跟threads相关
3023
+ this.pool = [];
3024
+
3025
+ // 缓存即将上传的文件。
3026
+ this.pending = [];
3027
+
3028
+ // 跟踪还有多少分片没有完成上传。
3029
+ this.remaning = 0;
3030
+ this.__tick = Base.bindFn( this._tick, this );
3031
+
3032
+ owner.on( 'uploadComplete', function( file ) {
3033
+ // 把其他块取消了。
3034
+ file.blocks && $.each( file.blocks, function( _, v ) {
3035
+ v.transport && (v.transport.abort(), v.transport.destroy());
3036
+ delete v.transport;
3037
+ });
3038
+
3039
+ delete file.blocks;
3040
+ delete file.remaning;
3041
+ });
3042
+ },
3043
+
3044
+ /**
3045
+ * @event startUpload
3046
+ * @description 当开始上传流程时触发。
3047
+ * @for Uploader
3048
+ */
3049
+
3050
+ /**
3051
+ * 开始上传。此方法可以从初始状态调用开始上传流程,也可以从暂停状态调用,继续上传流程。
3052
+ * @grammar upload() => undefined
3053
+ * @method upload
3054
+ * @for Uploader
3055
+ */
3056
+ start: function() {
3057
+ var me = this;
3058
+
3059
+ // 移出invalid的文件
3060
+ $.each( me.request( 'get-files', Status.INVALID ), function() {
3061
+ me.request( 'remove-file', this );
3062
+ });
3063
+
3064
+ if ( me.runing ) {
3065
+ return;
3066
+ }
3067
+
3068
+ me.runing = true;
3069
+
3070
+ // 如果有暂停的,则续传
3071
+ $.each( me.pool, function( _, v ) {
3072
+ var file = v.file;
3073
+
3074
+ if ( file.getStatus() === Status.INTERRUPT ) {
3075
+ file.setStatus( Status.PROGRESS );
3076
+ me._trigged = false;
3077
+ v.transport && v.transport.send();
3078
+ }
3079
+ });
3080
+
3081
+ me._trigged = false;
3082
+ me.owner.trigger('startUpload');
3083
+ Base.nextTick( me.__tick );
3084
+ },
3085
+
3086
+ /**
3087
+ * @event stopUpload
3088
+ * @description 当开始上传流程暂停时触发。
3089
+ * @for Uploader
3090
+ */
3091
+
3092
+ /**
3093
+ * 暂停上传。第一个参数为是否中断上传当前正在上传的文件。
3094
+ * @grammar stop() => undefined
3095
+ * @grammar stop( true ) => undefined
3096
+ * @method stop
3097
+ * @for Uploader
3098
+ */
3099
+ stop: function( interrupt ) {
3100
+ var me = this;
3101
+
3102
+ if ( me.runing === false ) {
3103
+ return;
3104
+ }
3105
+
3106
+ me.runing = false;
3107
+
3108
+ interrupt && $.each( me.pool, function( _, v ) {
3109
+ v.transport && v.transport.abort();
3110
+ v.file.setStatus( Status.INTERRUPT );
3111
+ });
3112
+
3113
+ me.owner.trigger('stopUpload');
3114
+ },
3115
+
3116
+ /**
3117
+ * 判断`Uplaode`r是否正在上传中。
3118
+ * @grammar isInProgress() => Boolean
3119
+ * @method isInProgress
3120
+ * @for Uploader
3121
+ */
3122
+ isInProgress: function() {
3123
+ return !!this.runing;
3124
+ },
3125
+
3126
+ getStats: function() {
3127
+ return this.request('get-stats');
3128
+ },
3129
+
3130
+ /**
3131
+ * 掉过一个文件上传,直接标记指定文件为已上传状态。
3132
+ * @grammar skipFile( file ) => undefined
3133
+ * @method skipFile
3134
+ * @for Uploader
3135
+ */
3136
+ skipFile: function( file, status ) {
3137
+ file = this.request( 'get-file', file );
3138
+
3139
+ file.setStatus( status || Status.COMPLETE );
3140
+ file.skipped = true;
3141
+
3142
+ // 如果正在上传。
3143
+ file.blocks && $.each( file.blocks, function( _, v ) {
3144
+ var _tr = v.transport;
3145
+
3146
+ if ( _tr ) {
3147
+ _tr.abort();
3148
+ _tr.destroy();
3149
+ delete v.transport;
3150
+ }
3151
+ });
3152
+
3153
+ this.owner.trigger( 'uploadSkip', file );
3154
+ },
3155
+
3156
+ /**
3157
+ * @event uploadFinished
3158
+ * @description 当所有文件上传结束时触发。
3159
+ * @for Uploader
3160
+ */
3161
+ _tick: function() {
3162
+ var me = this,
3163
+ opts = me.options,
3164
+ fn, val;
3165
+
3166
+ // 上一个promise还没有结束,则等待完成后再执行。
3167
+ if ( me._promise ) {
3168
+ return me._promise.always( me.__tick );
3169
+ }
3170
+
3171
+ // 还有位置,且还有文件要处理的话。
3172
+ if ( me.pool.length < opts.threads && (val = me._nextBlock()) ) {
3173
+ me._trigged = false;
3174
+
3175
+ fn = function( val ) {
3176
+ me._promise = null;
3177
+
3178
+ // 有可能是reject过来的,所以要检测val的类型。
3179
+ val && val.file && me._startSend( val );
3180
+ Base.nextTick( me.__tick );
3181
+ };
3182
+
3183
+ me._promise = isPromise( val ) ? val.always( fn ) : fn( val );
3184
+
3185
+ // 没有要上传的了,且没有正在传输的了。
3186
+ } else if ( !me.remaning && !me.getStats().numOfQueue ) {
3187
+ me.runing = false;
3188
+
3189
+ me._trigged || Base.nextTick(function() {
3190
+ me.owner.trigger('uploadFinished');
3191
+ });
3192
+ me._trigged = true;
3193
+ }
3194
+ },
3195
+
3196
+ _nextBlock: function() {
3197
+ var me = this,
3198
+ act = me._act,
3199
+ opts = me.options,
3200
+ next, done;
3201
+
3202
+ // 如果当前文件还有没有需要传输的,则直接返回剩下的。
3203
+ if ( act && act.has() &&
3204
+ act.file.getStatus() === Status.PROGRESS ) {
3205
+
3206
+ // 是否提前准备下一个文件
3207
+ if ( opts.prepareNextFile && !me.pending.length ) {
3208
+ me._prepareNextFile();
3209
+ }
3210
+
3211
+ return act.fetch();
3212
+
3213
+ // 否则,如果正在运行,则准备下一个文件,并等待完成后返回下个分片。
3214
+ } else if ( me.runing ) {
3215
+
3216
+ // 如果缓存中有,则直接在缓存中取,没有则去queue中取。
3217
+ if ( !me.pending.length && me.getStats().numOfQueue ) {
3218
+ me._prepareNextFile();
3219
+ }
3220
+
3221
+ next = me.pending.shift();
3222
+ done = function( file ) {
3223
+ if ( !file ) {
3224
+ return null;
3225
+ }
3226
+
3227
+ act = CuteFile( file, opts.chunked ? opts.chunkSize : 0 );
3228
+ me._act = act;
3229
+ return act.fetch();
3230
+ };
3231
+
3232
+ // 文件可能还在prepare中,也有可能已经完全准备好了。
3233
+ return isPromise( next ) ?
3234
+ next[ next.pipe ? 'pipe' : 'then']( done ) :
3235
+ done( next );
3236
+ }
3237
+ },
3238
+
3239
+
3240
+ /**
3241
+ * @event uploadStart
3242
+ * @param {File} file File对象
3243
+ * @description 某个文件开始上传前触发,一个文件只会触发一次。
3244
+ * @for Uploader
3245
+ */
3246
+ _prepareNextFile: function() {
3247
+ var me = this,
3248
+ file = me.request('fetch-file'),
3249
+ pending = me.pending,
3250
+ promise;
3251
+
3252
+ if ( file ) {
3253
+ promise = me.request( 'before-send-file', file, function() {
3254
+
3255
+ // 有可能文件被skip掉了。文件被skip掉后,状态坑定不是Queued.
3256
+ if ( file.getStatus() === Status.QUEUED ) {
3257
+ me.owner.trigger( 'uploadStart', file );
3258
+ file.setStatus( Status.PROGRESS );
3259
+ return file;
3260
+ }
3261
+
3262
+ return me._finishFile( file );
3263
+ });
3264
+
3265
+ // 如果还在pending中,则替换成文件本身。
3266
+ promise.done(function() {
3267
+ var idx = $.inArray( promise, pending );
3268
+
3269
+ ~idx && pending.splice( idx, 1, file );
3270
+ });
3271
+
3272
+ // befeore-send-file的钩子就有错误发生。
3273
+ promise.fail(function( reason ) {
3274
+ file.setStatus( Status.ERROR, reason );
3275
+ me.owner.trigger( 'uploadError', file, reason );
3276
+ me.owner.trigger( 'uploadComplete', file );
3277
+ });
3278
+
3279
+ pending.push( promise );
3280
+ }
3281
+ },
3282
+
3283
+ // 让出位置了,可以让其他分片开始上传
3284
+ _popBlock: function( block ) {
3285
+ var idx = $.inArray( block, this.pool );
3286
+
3287
+ this.pool.splice( idx, 1 );
3288
+ block.file.remaning--;
3289
+ this.remaning--;
3290
+ },
3291
+
3292
+ // 开始上传,可以被掉过。如果promise被reject了,则表示跳过此分片。
3293
+ _startSend: function( block ) {
3294
+ var me = this,
3295
+ file = block.file,
3296
+ promise;
3297
+
3298
+ me.pool.push( block );
3299
+ me.remaning++;
3300
+
3301
+ // 如果没有分片,则直接使用原始的。
3302
+ // 不会丢失content-type信息。
3303
+ block.blob = block.chunks === 1 ? file.source :
3304
+ file.source.slice( block.start, block.end );
3305
+
3306
+ // hook, 每个分片发送之前可能要做些异步的事情。
3307
+ promise = me.request( 'before-send', block, function() {
3308
+
3309
+ // 有可能文件已经上传出错了,所以不需要再传输了。
3310
+ if ( file.getStatus() === Status.PROGRESS ) {
3311
+ me._doSend( block );
3312
+ } else {
3313
+ me._popBlock( block );
3314
+ Base.nextTick( me.__tick );
3315
+ }
3316
+ });
3317
+
3318
+ // 如果为fail了,则跳过此分片。
3319
+ promise.fail(function() {
3320
+ if ( file.remaning === 1 ) {
3321
+ me._finishFile( file ).always(function() {
3322
+ block.percentage = 1;
3323
+ me._popBlock( block );
3324
+ me.owner.trigger( 'uploadComplete', file );
3325
+ Base.nextTick( me.__tick );
3326
+ });
3327
+ } else {
3328
+ block.percentage = 1;
3329
+ me._popBlock( block );
3330
+ Base.nextTick( me.__tick );
3331
+ }
3332
+ });
3333
+ },
3334
+
3335
+
3336
+ /**
3337
+ * @event uploadBeforeSend
3338
+ * @param {Object} object
3339
+ * @param {Object} data 默认的上传参数,可以扩展此对象来控制上传参数。
3340
+ * @description 当某个文件的分块在发送前触发,主要用来询问是否要添加附带参数,大文件在开起分片上传的前提下此事件可能会触发多次。
3341
+ * @for Uploader
3342
+ */
3343
+
3344
+ /**
3345
+ * @event uploadAccept
3346
+ * @param {Object} object
3347
+ * @param {Object} ret 服务端的返回数据,json格式,如果服务端不是json格式,从ret._raw中取数据,自行解析。
3348
+ * @description 当某个文件上传到服务端响应后,会派送此事件来询问服务端响应是否有效。如果此事件handler返回值为`false`, 则此文件将派送`server`类型的`uploadError`事件。
3349
+ * @for Uploader
3350
+ */
3351
+
3352
+ /**
3353
+ * @event uploadProgress
3354
+ * @param {File} file File对象
3355
+ * @param {Number} percentage 上传进度
3356
+ * @description 上传过程中触发,携带上传进度。
3357
+ * @for Uploader
3358
+ */
3359
+
3360
+
3361
+ /**
3362
+ * @event uploadError
3363
+ * @param {File} file File对象
3364
+ * @param {String} reason 出错的code
3365
+ * @description 当文件上传出错时触发。
3366
+ * @for Uploader
3367
+ */
3368
+
3369
+ /**
3370
+ * @event uploadSuccess
3371
+ * @param {File} file File对象
3372
+ * @param {Object} response 服务端返回的数据
3373
+ * @description 当文件上传成功时触发。
3374
+ * @for Uploader
3375
+ */
3376
+
3377
+ /**
3378
+ * @event uploadComplete
3379
+ * @param {File} [file] File对象
3380
+ * @description 不管成功或者失败,文件上传完成时触发。
3381
+ * @for Uploader
3382
+ */
3383
+
3384
+ // 做上传操作。
3385
+ _doSend: function( block ) {
3386
+ var me = this,
3387
+ owner = me.owner,
3388
+ opts = me.options,
3389
+ file = block.file,
3390
+ tr = new Transport( opts ),
3391
+ data = $.extend({}, opts.formData ),
3392
+ headers = $.extend({}, opts.headers ),
3393
+ requestAccept, ret;
3394
+
3395
+ block.transport = tr;
3396
+
3397
+ tr.on( 'destroy', function() {
3398
+ delete block.transport;
3399
+ me._popBlock( block );
3400
+ Base.nextTick( me.__tick );
3401
+ });
3402
+
3403
+ // 广播上传进度。以文件为单位。
3404
+ tr.on( 'progress', function( percentage ) {
3405
+ var totalPercent = 0,
3406
+ uploaded = 0;
3407
+
3408
+ // 可能没有abort掉,progress还是执行进来了。
3409
+ // if ( !file.blocks ) {
3410
+ // return;
3411
+ // }
3412
+
3413
+ totalPercent = block.percentage = percentage;
3414
+
3415
+ if ( block.chunks > 1 ) { // 计算文件的整体速度。
3416
+ $.each( file.blocks, function( _, v ) {
3417
+ uploaded += (v.percentage || 0) * (v.end - v.start);
3418
+ });
3419
+
3420
+ totalPercent = uploaded / file.size;
3421
+ }
3422
+
3423
+ owner.trigger( 'uploadProgress', file, totalPercent || 0 );
3424
+ });
3425
+
3426
+ // 用来询问,是否返回的结果是有错误的。
3427
+ requestAccept = function( reject ) {
3428
+ var fn;
3429
+
3430
+ ret = tr.getResponseAsJson() || {};
3431
+ ret._raw = tr.getResponse();
3432
+ fn = function( value ) {
3433
+ reject = value;
3434
+ };
3435
+
3436
+ // 服务端响应了,不代表成功了,询问是否响应正确。
3437
+ if ( !owner.trigger( 'uploadAccept', block, ret, fn ) ) {
3438
+ reject = reject || 'server';
3439
+ }
3440
+
3441
+ return reject;
3442
+ };
3443
+
3444
+ // 尝试重试,然后广播文件上传出错。
3445
+ tr.on( 'error', function( type, flag ) {
3446
+ block.retried = block.retried || 0;
3447
+
3448
+ // 自动重试
3449
+ if ( block.chunks > 1 && ~'http,abort'.indexOf( type ) &&
3450
+ block.retried < opts.chunkRetry ) {
3451
+
3452
+ block.retried++;
3453
+ tr.send();
3454
+
3455
+ } else {
3456
+
3457
+ // http status 500 ~ 600
3458
+ if ( !flag && type === 'server' ) {
3459
+ type = requestAccept( type );
3460
+ }
3461
+
3462
+ file.setStatus( Status.ERROR, type );
3463
+ owner.trigger( 'uploadError', file, type );
3464
+ owner.trigger( 'uploadComplete', file );
3465
+ }
3466
+ });
3467
+
3468
+ // 上传成功
3469
+ tr.on( 'load', function() {
3470
+ var reason;
3471
+
3472
+ // 如果非预期,转向上传出错。
3473
+ if ( (reason = requestAccept()) ) {
3474
+ tr.trigger( 'error', reason, true );
3475
+ return;
3476
+ }
3477
+
3478
+ // 全部上传完成。
3479
+ if ( file.remaning === 1 ) {
3480
+ me._finishFile( file, ret );
3481
+ } else {
3482
+ tr.destroy();
3483
+ }
3484
+ });
3485
+
3486
+ // 配置默认的上传字段。
3487
+ data = $.extend( data, {
3488
+ id: file.id,
3489
+ name: file.name,
3490
+ type: file.type,
3491
+ lastModifiedDate: file.lastModifiedDate,
3492
+ size: file.size
3493
+ });
3494
+
3495
+ block.chunks > 1 && $.extend( data, {
3496
+ chunks: block.chunks,
3497
+ chunk: block.chunk
3498
+ });
3499
+
3500
+ // 在发送之间可以添加字段什么的。。。
3501
+ // 如果默认的字段不够使用,可以通过监听此事件来扩展
3502
+ owner.trigger( 'uploadBeforeSend', block, data, headers );
3503
+
3504
+ // 开始发送。
3505
+ tr.appendBlob( opts.fileVal, block.blob, file.name );
3506
+ tr.append( data );
3507
+ tr.setRequestHeader( headers );
3508
+ tr.send();
3509
+ },
3510
+
3511
+ // 完成上传。
3512
+ _finishFile: function( file, ret, hds ) {
3513
+ var owner = this.owner;
3514
+
3515
+ return owner
3516
+ .request( 'after-send-file', arguments, function() {
3517
+ file.setStatus( Status.COMPLETE );
3518
+ owner.trigger( 'uploadSuccess', file, ret, hds );
3519
+ })
3520
+ .fail(function( reason ) {
3521
+
3522
+ // 如果外部已经标记为invalid什么的,不再改状态。
3523
+ if ( file.getStatus() === Status.PROGRESS ) {
3524
+ file.setStatus( Status.ERROR, reason );
3525
+ }
3526
+
3527
+ owner.trigger( 'uploadError', file, reason );
3528
+ })
3529
+ .always(function() {
3530
+ owner.trigger( 'uploadComplete', file );
3531
+ });
3532
+ }
3533
+
3534
+ });
3535
+ });
3536
+ /**
3537
+ * @fileOverview 各种验证,包括文件总大小是否超出、单文件是否超出和文件是否重复。
3538
+ */
3539
+
3540
+ define('widgets/validator',[
3541
+ 'base',
3542
+ 'uploader',
3543
+ 'file',
3544
+ 'widgets/widget'
3545
+ ], function( Base, Uploader, WUFile ) {
3546
+
3547
+ var $ = Base.$,
3548
+ validators = {},
3549
+ api;
3550
+
3551
+ /**
3552
+ * @event error
3553
+ * @param {String} type 错误类型。
3554
+ * @description 当validate不通过时,会以派送错误事件的形式通知调用者。通过`upload.on('error', handler)`可以捕获到此类错误,目前有以下错误会在特定的情况下派送错来。
3555
+ *
3556
+ * * `Q_EXCEED_NUM_LIMIT` 在设置了`fileNumLimit`且尝试给`uploader`添加的文件数量超出这个值时派送。
3557
+ * * `Q_EXCEED_SIZE_LIMIT` 在设置了`Q_EXCEED_SIZE_LIMIT`且尝试给`uploader`添加的文件总大小超出这个值时派送。
3558
+ * @for Uploader
3559
+ */
3560
+
3561
+ // 暴露给外面的api
3562
+ api = {
3563
+
3564
+ // 添加验证器
3565
+ addValidator: function( type, cb ) {
3566
+ validators[ type ] = cb;
3567
+ },
3568
+
3569
+ // 移除验证器
3570
+ removeValidator: function( type ) {
3571
+ delete validators[ type ];
3572
+ }
3573
+ };
3574
+
3575
+ // 在Uploader初始化的时候启动Validators的初始化
3576
+ Uploader.register({
3577
+ init: function() {
3578
+ var me = this;
3579
+ $.each( validators, function() {
3580
+ this.call( me.owner );
3581
+ });
3582
+ }
3583
+ });
3584
+
3585
+ /**
3586
+ * @property {int} [fileNumLimit=undefined]
3587
+ * @namespace options
3588
+ * @for Uploader
3589
+ * @description 验证文件总数量, 超出则不允许加入队列。
3590
+ */
3591
+ api.addValidator( 'fileNumLimit', function() {
3592
+ var uploader = this,
3593
+ opts = uploader.options,
3594
+ count = 0,
3595
+ max = opts.fileNumLimit >> 0,
3596
+ flag = true;
3597
+
3598
+ if ( !max ) {
3599
+ return;
3600
+ }
3601
+
3602
+ uploader.on( 'beforeFileQueued', function( file ) {
3603
+
3604
+ if ( count >= max && flag ) {
3605
+ flag = false;
3606
+ this.trigger( 'error', 'Q_EXCEED_NUM_LIMIT', max, file );
3607
+ setTimeout(function() {
3608
+ flag = true;
3609
+ }, 1 );
3610
+ }
3611
+
3612
+ return count >= max ? false : true;
3613
+ });
3614
+
3615
+ uploader.on( 'fileQueued', function() {
3616
+ count++;
3617
+ });
3618
+
3619
+ uploader.on( 'fileDequeued', function() {
3620
+ count--;
3621
+ });
3622
+
3623
+ uploader.on( 'uploadFinished', function() {
3624
+ count = 0;
3625
+ });
3626
+ });
3627
+
3628
+
3629
+ /**
3630
+ * @property {int} [fileSizeLimit=undefined]
3631
+ * @namespace options
3632
+ * @for Uploader
3633
+ * @description 验证文件总大小是否超出限制, 超出则不允许加入队列。
3634
+ */
3635
+ api.addValidator( 'fileSizeLimit', function() {
3636
+ var uploader = this,
3637
+ opts = uploader.options,
3638
+ count = 0,
3639
+ max = opts.fileSizeLimit >> 0,
3640
+ flag = true;
3641
+
3642
+ if ( !max ) {
3643
+ return;
3644
+ }
3645
+
3646
+ uploader.on( 'beforeFileQueued', function( file ) {
3647
+ var invalid = count + file.size > max;
3648
+
3649
+ if ( invalid && flag ) {
3650
+ flag = false;
3651
+ this.trigger( 'error', 'Q_EXCEED_SIZE_LIMIT', max, file );
3652
+ setTimeout(function() {
3653
+ flag = true;
3654
+ }, 1 );
3655
+ }
3656
+
3657
+ return invalid ? false : true;
3658
+ });
3659
+
3660
+ uploader.on( 'fileQueued', function( file ) {
3661
+ count += file.size;
3662
+ });
3663
+
3664
+ uploader.on( 'fileDequeued', function( file ) {
3665
+ count -= file.size;
3666
+ });
3667
+
3668
+ uploader.on( 'uploadFinished', function() {
3669
+ count = 0;
3670
+ });
3671
+ });
3672
+
3673
+ /**
3674
+ * @property {int} [fileSingleSizeLimit=undefined]
3675
+ * @namespace options
3676
+ * @for Uploader
3677
+ * @description 验证单个文件大小是否超出限制, 超出则不允许加入队列。
3678
+ */
3679
+ api.addValidator( 'fileSingleSizeLimit', function() {
3680
+ var uploader = this,
3681
+ opts = uploader.options,
3682
+ max = opts.fileSingleSizeLimit;
3683
+
3684
+ if ( !max ) {
3685
+ return;
3686
+ }
3687
+
3688
+ uploader.on( 'beforeFileQueued', function( file ) {
3689
+
3690
+ if ( file.size > max ) {
3691
+ file.setStatus( WUFile.Status.INVALID, 'exceed_size' );
3692
+ this.trigger( 'error', 'F_EXCEED_SIZE', file );
3693
+ return false;
3694
+ }
3695
+
3696
+ });
3697
+
3698
+ });
3699
+
3700
+ /**
3701
+ * @property {int} [duplicate=undefined]
3702
+ * @namespace options
3703
+ * @for Uploader
3704
+ * @description 去重, 根据文件名字、文件大小和最后修改时间来生成hash Key.
3705
+ */
3706
+ api.addValidator( 'duplicate', function() {
3707
+ var uploader = this,
3708
+ opts = uploader.options,
3709
+ mapping = {};
3710
+
3711
+ if ( opts.duplicate ) {
3712
+ return;
3713
+ }
3714
+
3715
+ function hashString( str ) {
3716
+ var hash = 0,
3717
+ i = 0,
3718
+ len = str.length,
3719
+ _char;
3720
+
3721
+ for ( ; i < len; i++ ) {
3722
+ _char = str.charCodeAt( i );
3723
+ hash = _char + (hash << 6) + (hash << 16) - hash;
3724
+ }
3725
+
3726
+ return hash;
3727
+ }
3728
+
3729
+ uploader.on( 'beforeFileQueued', function( file ) {
3730
+ var hash = file.__hash || (file.__hash = hashString( file.name +
3731
+ file.size + file.lastModifiedDate ));
3732
+
3733
+ // 已经重复了
3734
+ if ( mapping[ hash ] ) {
3735
+ this.trigger( 'error', 'F_DUPLICATE', file );
3736
+ return false;
3737
+ }
3738
+ });
3739
+
3740
+ uploader.on( 'fileQueued', function( file ) {
3741
+ var hash = file.__hash;
3742
+
3743
+ hash && (mapping[ hash ] = true);
3744
+ });
3745
+
3746
+ uploader.on( 'fileDequeued', function( file ) {
3747
+ var hash = file.__hash;
3748
+
3749
+ hash && (delete mapping[ hash ]);
3750
+ });
3751
+ });
3752
+
3753
+ return api;
3754
+ });
3755
+
3756
+ /**
3757
+ * @fileOverview Runtime管理器,负责Runtime的选择, 连接
3758
+ */
3759
+ define('runtime/compbase',[],function() {
3760
+
3761
+ function CompBase( owner, runtime ) {
3762
+
3763
+ this.owner = owner;
3764
+ this.options = owner.options;
3765
+
3766
+ this.getRuntime = function() {
3767
+ return runtime;
3768
+ };
3769
+
3770
+ this.getRuid = function() {
3771
+ return runtime.uid;
3772
+ };
3773
+
3774
+ this.trigger = function() {
3775
+ return owner.trigger.apply( owner, arguments );
3776
+ };
3777
+ }
3778
+
3779
+ return CompBase;
3780
+ });
3781
+ /**
3782
+ * @fileOverview FlashRuntime
3783
+ */
3784
+ define('runtime/flash/runtime',[
3785
+ 'base',
3786
+ 'runtime/runtime',
3787
+ 'runtime/compbase'
3788
+ ], function( Base, Runtime, CompBase ) {
3789
+
3790
+ var $ = Base.$,
3791
+ type = 'flash',
3792
+ components = {};
3793
+
3794
+
3795
+ function getFlashVersion() {
3796
+ var version;
3797
+
3798
+ try {
3799
+ version = navigator.plugins[ 'Shockwave Flash' ];
3800
+ version = version.description;
3801
+ } catch ( ex ) {
3802
+ try {
3803
+ version = new ActiveXObject('ShockwaveFlash.ShockwaveFlash')
3804
+ .GetVariable('$version');
3805
+ } catch ( ex2 ) {
3806
+ version = '0.0';
3807
+ }
3808
+ }
3809
+ version = version.match( /\d+/g );
3810
+ return parseFloat( version[ 0 ] + '.' + version[ 1 ], 10 );
3811
+ }
3812
+
3813
+ function FlashRuntime() {
3814
+ var pool = {},
3815
+ clients = {},
3816
+ destory = this.destory,
3817
+ me = this,
3818
+ jsreciver = Base.guid('webuploader_');
3819
+
3820
+ Runtime.apply( me, arguments );
3821
+ me.type = type;
3822
+
3823
+
3824
+ // 这个方法的调用者,实际上是RuntimeClient
3825
+ me.exec = function( comp, fn/*, args...*/ ) {
3826
+ var client = this,
3827
+ uid = client.uid,
3828
+ args = Base.slice( arguments, 2 ),
3829
+ instance;
3830
+
3831
+ clients[ uid ] = client;
3832
+
3833
+ if ( components[ comp ] ) {
3834
+ if ( !pool[ uid ] ) {
3835
+ pool[ uid ] = new components[ comp ]( client, me );
3836
+ }
3837
+
3838
+ instance = pool[ uid ];
3839
+
3840
+ if ( instance[ fn ] ) {
3841
+ return instance[ fn ].apply( instance, args );
3842
+ }
3843
+ }
3844
+
3845
+ return me.flashExec.apply( client, arguments );
3846
+ };
3847
+
3848
+ function handler( evt, obj ) {
3849
+ var type = evt.type || evt,
3850
+ parts, uid;
3851
+
3852
+ parts = type.split('::');
3853
+ uid = parts[ 0 ];
3854
+ type = parts[ 1 ];
3855
+
3856
+ // console.log.apply( console, arguments );
3857
+
3858
+ if ( type === 'Ready' && uid === me.uid ) {
3859
+ me.trigger('ready');
3860
+ } else if ( clients[ uid ] ) {
3861
+ clients[ uid ].trigger( type.toLowerCase(), evt, obj );
3862
+ }
3863
+
3864
+ // Base.log( evt, obj );
3865
+ }
3866
+
3867
+ // flash的接受器。
3868
+ window[ jsreciver ] = function() {
3869
+ var args = arguments;
3870
+
3871
+ // 为了能捕获得到。
3872
+ setTimeout(function() {
3873
+ handler.apply( null, args );
3874
+ }, 1 );
3875
+ };
3876
+
3877
+ this.jsreciver = jsreciver;
3878
+
3879
+ this.destory = function() {
3880
+ // @todo 删除池子中的所有实例
3881
+ return destory && destory.apply( this, arguments );
3882
+ };
3883
+
3884
+ this.flashExec = function( comp, fn ) {
3885
+ var flash = me.getFlash(),
3886
+ args = Base.slice( arguments, 2 );
3887
+
3888
+ return flash.exec( this.uid, comp, fn, args );
3889
+ };
3890
+
3891
+ // @todo
3892
+ }
3893
+
3894
+ Base.inherits( Runtime, {
3895
+ constructor: FlashRuntime,
3896
+
3897
+ init: function() {
3898
+ var container = this.getContainer(),
3899
+ opts = this.options,
3900
+ html;
3901
+
3902
+ // if not the minimal height, shims are not initialized
3903
+ // in older browsers (e.g FF3.6, IE6,7,8, Safari 4.0,5.0, etc)
3904
+ container.css({
3905
+ position: 'absolute',
3906
+ top: '-8px',
3907
+ left: '-8px',
3908
+ width: '9px',
3909
+ height: '9px',
3910
+ overflow: 'hidden'
3911
+ });
3912
+
3913
+ // insert flash object
3914
+ html = '<object id="' + this.uid + '" type="application/' +
3915
+ 'x-shockwave-flash" data="' + opts.swf + '" ';
3916
+
3917
+ if ( Base.browser.ie ) {
3918
+ html += 'classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" ';
3919
+ }
3920
+
3921
+ html += 'width="100%" height="100%" style="outline:0">' +
3922
+ '<param name="movie" value="' + opts.swf + '" />' +
3923
+ '<param name="flashvars" value="uid=' + this.uid +
3924
+ '&jsreciver=' + this.jsreciver + '" />' +
3925
+ '<param name="wmode" value="transparent" />' +
3926
+ '<param name="allowscriptaccess" value="always" />' +
3927
+ '</object>';
3928
+
3929
+ container.html( html );
3930
+ },
3931
+
3932
+ getFlash: function() {
3933
+ if ( this._flash ) {
3934
+ return this._flash;
3935
+ }
3936
+
3937
+ this._flash = $( '#' + this.uid ).get( 0 );
3938
+ return this._flash;
3939
+ }
3940
+
3941
+ });
3942
+
3943
+ FlashRuntime.register = function( name, component ) {
3944
+ component = components[ name ] = Base.inherits( CompBase, $.extend({
3945
+
3946
+ // @todo fix this later
3947
+ flashExec: function() {
3948
+ var owner = this.owner,
3949
+ runtime = this.getRuntime();
3950
+
3951
+ return runtime.flashExec.apply( owner, arguments );
3952
+ }
3953
+ }, component ) );
3954
+
3955
+ return component;
3956
+ };
3957
+
3958
+ if ( getFlashVersion() >= 11.4 ) {
3959
+ Runtime.addRuntime( type, FlashRuntime );
3960
+ }
3961
+
3962
+ return FlashRuntime;
3963
+ });
3964
+ /**
3965
+ * @fileOverview FilePicker
3966
+ */
3967
+ define('runtime/flash/filepicker',[
3968
+ 'base',
3969
+ 'runtime/flash/runtime'
3970
+ ], function( Base, FlashRuntime ) {
3971
+ var $ = Base.$;
3972
+
3973
+ return FlashRuntime.register( 'FilePicker', {
3974
+ init: function( opts ) {
3975
+ var copy = $.extend({}, opts ),
3976
+ len, i;
3977
+
3978
+ // 修复Flash再没有设置title的情况下无法弹出flash文件选择框的bug.
3979
+ len = copy.accept && copy.accept.length;
3980
+ for ( i = 0; i < len; i++ ) {
3981
+ if ( !copy.accept[ i ].title ) {
3982
+ copy.accept[ i ].title = 'Files';
3983
+ }
3984
+ }
3985
+
3986
+ delete copy.button;
3987
+ delete copy.container;
3988
+
3989
+ this.flashExec( 'FilePicker', 'init', copy );
3990
+ },
3991
+
3992
+ destroy: function() {
3993
+ // todo
3994
+ }
3995
+ });
3996
+ });
3997
+ /**
3998
+ * @fileOverview 图片压缩
3999
+ */
4000
+ define('runtime/flash/image',[
4001
+ 'runtime/flash/runtime'
4002
+ ], function( FlashRuntime ) {
4003
+
4004
+ return FlashRuntime.register( 'Image', {
4005
+ // init: function( options ) {
4006
+ // var owner = this.owner;
4007
+
4008
+ // this.flashExec( 'Image', 'init', options );
4009
+ // owner.on( 'load', function() {
4010
+ // debugger;
4011
+ // });
4012
+ // },
4013
+
4014
+ loadFromBlob: function( blob ) {
4015
+ var owner = this.owner;
4016
+
4017
+ owner.info() && this.flashExec( 'Image', 'info', owner.info() );
4018
+ owner.meta() && this.flashExec( 'Image', 'meta', owner.meta() );
4019
+
4020
+ this.flashExec( 'Image', 'loadFromBlob', blob.uid );
4021
+ }
4022
+ });
4023
+ });
4024
+ /**
4025
+ * @fileOverview Transport flash实现
4026
+ */
4027
+ define('runtime/flash/transport',[
4028
+ 'base',
4029
+ 'runtime/flash/runtime',
4030
+ 'runtime/client'
4031
+ ], function( Base, FlashRuntime, RuntimeClient ) {
4032
+ var $ = Base.$;
4033
+
4034
+ return FlashRuntime.register( 'Transport', {
4035
+ init: function() {
4036
+ this._status = 0;
4037
+ this._response = null;
4038
+ this._responseJson = null;
4039
+ },
4040
+
4041
+ send: function() {
4042
+ var owner = this.owner,
4043
+ opts = this.options,
4044
+ xhr = this._initAjax(),
4045
+ blob = owner._blob,
4046
+ server = opts.server,
4047
+ binary;
4048
+
4049
+ xhr.connectRuntime( blob.ruid );
4050
+
4051
+ if ( opts.sendAsBinary ) {
4052
+ server += (/\?/.test( server ) ? '&' : '?') +
4053
+ $.param( owner._formData );
4054
+
4055
+ binary = blob.uid;
4056
+ } else {
4057
+ $.each( owner._formData, function( k, v ) {
4058
+ xhr.exec( 'append', k, v );
4059
+ });
4060
+
4061
+ xhr.exec( 'appendBlob', opts.fileVal, blob.uid,
4062
+ opts.filename || owner._formData.name || '' );
4063
+ }
4064
+
4065
+ this._setRequestHeader( xhr, opts.headers );
4066
+ xhr.exec( 'send', {
4067
+ method: opts.method,
4068
+ url: server
4069
+ }, binary );
4070
+ },
4071
+
4072
+ getStatus: function() {
4073
+ return this._status;
4074
+ },
4075
+
4076
+ getResponse: function() {
4077
+ return this._response;
4078
+ },
4079
+
4080
+ getResponseAsJson: function() {
4081
+ return this._responseJson;
4082
+ },
4083
+
4084
+ abort: function() {
4085
+ var xhr = this._xhr;
4086
+
4087
+ if ( xhr ) {
4088
+ xhr.exec('abort');
4089
+ xhr.destroy();
4090
+ this._xhr = xhr = null;
4091
+ }
4092
+ },
4093
+
4094
+ destroy: function() {
4095
+ this.abort();
4096
+ },
4097
+
4098
+ _initAjax: function() {
4099
+ var me = this,
4100
+ xhr = new RuntimeClient('XMLHttpRequest');
4101
+
4102
+ xhr.on( 'uploadprogress progress', function( e ) {
4103
+ return me.trigger( 'progress', e.loaded / e.total );
4104
+ });
4105
+
4106
+ xhr.on( 'load', function() {
4107
+ var status = xhr.exec('getStatus'),
4108
+ err = '';
4109
+
4110
+ xhr.off();
4111
+ me._xhr = null;
4112
+
4113
+ if ( status >= 200 && status < 300 ) {
4114
+ me._response = xhr.exec('getResponse');
4115
+ me._responseJson = xhr.exec('getResponseAsJson');
4116
+ } else if ( status >= 500 && status < 600 ) {
4117
+ me._response = xhr.exec('getResponse');
4118
+ me._responseJson = xhr.exec('getResponseAsJson');
4119
+ err = 'server';
4120
+ } else {
4121
+ err = 'http';
4122
+ }
4123
+
4124
+ xhr.destroy();
4125
+ xhr = null;
4126
+
4127
+ return err ? me.trigger( 'error', err ) : me.trigger('load');
4128
+ });
4129
+
4130
+ xhr.on( 'error', function() {
4131
+ xhr.off();
4132
+ me._xhr = null;
4133
+ me.trigger( 'error', 'http' );
4134
+ });
4135
+
4136
+ me._xhr = xhr;
4137
+ return xhr;
4138
+ },
4139
+
4140
+ _setRequestHeader: function( xhr, headers ) {
4141
+ $.each( headers, function( key, val ) {
4142
+ xhr.exec( 'setRequestHeader', key, val );
4143
+ });
4144
+ }
4145
+ });
4146
+ });
4147
+ /**
4148
+ * @fileOverview 只有flash实现的文件版本。
4149
+ */
4150
+ define('preset/flashonly',[
4151
+ 'base',
4152
+
4153
+ // widgets
4154
+ 'widgets/filepicker',
4155
+ 'widgets/image',
4156
+ 'widgets/queue',
4157
+ 'widgets/runtime',
4158
+ 'widgets/upload',
4159
+ 'widgets/validator',
4160
+
4161
+ // runtimes
4162
+
4163
+ // flash
4164
+ 'runtime/flash/filepicker',
4165
+ 'runtime/flash/image',
4166
+ 'runtime/flash/transport'
4167
+ ], function( Base ) {
4168
+ return Base;
4169
+ });
4170
+ define('webuploader',[
4171
+ 'preset/flashonly'
4172
+ ], function( preset ) {
4173
+ return preset;
4174
+ });
4175
+ return require('webuploader');
4176
+ });