uploader_plupload.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /* ------------------------------------------------------------------------------
  2. *
  3. * # Plupload multiple file uploader
  4. *
  5. * Demo JS code for uploader_plupload.html page
  6. *
  7. * ---------------------------------------------------------------------------- */
  8. // Setup module
  9. // ------------------------------
  10. var Plupload = function() {
  11. //
  12. // Setup module components
  13. //
  14. // Bootstrap file upload
  15. var _componentPlupload = function() {
  16. if (!$().pluploadQueue) {
  17. console.warn('Warning - Plupload files are not loaded.');
  18. return;
  19. }
  20. // Setup all runtimes
  21. $('.file-uploader').pluploadQueue({
  22. runtimes: 'html5, html4, Flash, Silverlight',
  23. url: '../../../../global_assets/demo_data/uploader/plupload.json',
  24. chunk_size: '300Kb',
  25. unique_names: true,
  26. header: true,
  27. filters: {
  28. max_file_size: '300Kb',
  29. mime_types: [{
  30. title: 'Image files',
  31. extensions: 'jpg,gif,png'
  32. }]
  33. },
  34. resize: {
  35. width: 320,
  36. height: 240,
  37. quality: 90
  38. }
  39. });
  40. // Setup flash version
  41. $('.flash-uploader').pluploadQueue({
  42. runtimes: 'flash',
  43. url: '../../../../global_assets/demo_data/uploader/plupload.json',
  44. chunk_size: '300Kb',
  45. unique_names: true,
  46. filters: {
  47. max_file_size: '300Kb',
  48. mime_types: [{
  49. title: 'Image files',
  50. extensions: 'jpg,gif,png'
  51. }]
  52. },
  53. resize: {
  54. width: 320,
  55. height: 240,
  56. quality: 90
  57. },
  58. flash_swf_url: '../../../../global_assets/js/plugins/uploaders/plupload/files/Moxie.swf'
  59. });
  60. // Setup html5 version
  61. $('.html5-uploader').pluploadQueue({
  62. runtimes: 'html5',
  63. url: '../../../../global_assets/demo_data/uploader/plupload.json',
  64. chunk_size: '300Kb',
  65. unique_names: true,
  66. filters: {
  67. max_file_size: '300Kb',
  68. mime_types: [{
  69. title: 'Image files',
  70. extensions: 'jpg,gif,png'
  71. }]
  72. },
  73. resize: {
  74. width: 320,
  75. height: 240,
  76. quality: 90
  77. }
  78. });
  79. // Setup html4 version
  80. $('.html4-uploader').pluploadQueue({
  81. runtimes: 'html4',
  82. url: '../../../../global_assets/demo_data/uploader/plupload.json',
  83. unique_names: true,
  84. filters: {
  85. max_file_size: '300Kb',
  86. mime_types: [{
  87. title: 'Image files',
  88. extensions: 'jpg,gif,png'
  89. }]
  90. }
  91. });
  92. // Events
  93. $('.uploader-events').pluploadQueue({
  94. runtimes: 'html5,flash,silverlight,html4',
  95. url: '../../../../global_assets/demo_data/uploader/plupload.json',
  96. chunk_size: '300Kb',
  97. unique_names: true,
  98. resize: {
  99. width: 320,
  100. height: 240,
  101. quality: 90
  102. },
  103. filters: {
  104. max_file_size: '300Kb',
  105. mime_types: [{
  106. title: 'Image files',
  107. extensions: 'jpg,gif,png'
  108. }]
  109. },
  110. flash_swf_url: '../../../../global_assets/js/plugins/uploaders/plupload/files/Moxie.swf',
  111. silverlight_xap_url: '/plupload/js/Moxie.xap',
  112. preinit: {
  113. Init: function(up, info) {
  114. log('[Init]', 'Info:', info, 'Features:', up.features);
  115. },
  116. UploadFile: function(up, file) {
  117. log('[UploadFile]', file);
  118. }
  119. },
  120. init: {
  121. Browse: function(up) {
  122. log('[Browse]'); // Called when file picker is clicked
  123. },
  124. Refresh: function(up) {
  125. log('[Refresh]'); // Called when the position or dimensions of the picker change
  126. },
  127. StateChanged: function(up) {
  128. log('[StateChanged]', up.state == plupload.STARTED ? 'STARTED': 'STOPPED'); // Called when the state of the queue is changed
  129. },
  130. QueueChanged: function(up) {
  131. log('[QueueChanged]'); // Called when queue is changed by adding or removing files
  132. },
  133. OptionChanged: function(up, name, value, oldValue) {
  134. log('[OptionChanged]', 'Option Name: ', name, 'Value: ', value, 'Old Value: ', oldValue); // Called when one of the configuration options is changed
  135. },
  136. BeforeUpload: function(up, file) {
  137. log('[BeforeUpload]', 'File: ', file); // Called right before the upload for a given file starts, can be used to cancel it if required
  138. },
  139. UploadProgress: function(up, file) {
  140. log('[UploadProgress]', 'File:', file, 'Total:', up.total); // Called while file is being uploaded
  141. },
  142. FileFiltered: function(up, file) {
  143. log('[FileFiltered]', 'File:', file); // Called when file successfully files all the filters
  144. },
  145. FilesAdded: function(up, files) {
  146. log('[FilesAdded]'); // Called when files are added to queue
  147. plupload.each(files, function(file) {
  148. log(' File:', file);
  149. });
  150. },
  151. FilesRemoved: function(up, files) {
  152. log('[FilesRemoved]'); // Called when files are removed from queue
  153. plupload.each(files, function(file) {
  154. log(' File:', file);
  155. });
  156. },
  157. FileUploaded: function(up, file, info) {
  158. log('[FileUploaded] File:', file, 'Info:', info); // Called when file has finished uploading
  159. },
  160. ChunkUploaded: function(up, file, info) {
  161. log('[ChunkUploaded] File:', file, 'Info:', info); // Called when file chunk has finished uploading
  162. },
  163. UploadComplete: function(up, files) {
  164. log('[UploadComplete]'); // Called when all files are either uploaded or failed
  165. },
  166. Destroy: function(up) {
  167. log('[Destroy] '); // Called when uploader is destroyed
  168. },
  169. Error: function(up, args) {
  170. log('[Error] ', args); // Called when error occurs
  171. }
  172. }
  173. });
  174. // Write log
  175. function log() {
  176. var str = '';
  177. plupload.each(arguments, function(arg) {
  178. var row = '';
  179. if (typeof(arg) != 'string') {
  180. plupload.each(arg, function(value, key) {
  181. // Convert items in File objects to human readable form
  182. if (arg instanceof plupload.File) {
  183. // Convert status to human readable
  184. switch (value) {
  185. case plupload.QUEUED:
  186. value = 'QUEUED';
  187. break;
  188. case plupload.UPLOADING:
  189. value = 'UPLOADING';
  190. break;
  191. case plupload.FAILED:
  192. value = 'FAILED';
  193. break;
  194. case plupload.DONE:
  195. value = 'DONE';
  196. break;
  197. }
  198. }
  199. if (typeof(value) != 'function') {
  200. row += (row ? ', ': '') + key + '=' + value;
  201. }
  202. });
  203. str += row + ' ';
  204. }
  205. else {
  206. str += arg + ' ';
  207. }
  208. });
  209. var log = $('#log');
  210. log.append(str + '<br>');
  211. log.scrollTop(log[0].scrollHeight);
  212. }
  213. };
  214. //
  215. // Return objects assigned to module
  216. //
  217. return {
  218. init: function() {
  219. _componentPlupload();
  220. }
  221. }
  222. }();
  223. // Initialize module
  224. // ------------------------------
  225. document.addEventListener('DOMContentLoaded', function() {
  226. Plupload.init();
  227. });