swfupload.queue.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. Queue Plug-in
  3. Features:
  4. *Adds a cancelQueue() method for cancelling the entire queue.
  5. *All queued files are uploaded when startUpload() is called.
  6. *If false is returned from uploadComplete then the queue upload is stopped.
  7. If false is not returned (strict comparison) then the queue upload is continued.
  8. *Adds a QueueComplete event that is fired when all the queued files have finished uploading.
  9. Set the event handler with the queue_complete_handler setting.
  10. */
  11. var SWFUpload;
  12. if (typeof(SWFUpload) === "function") {
  13. SWFUpload.queue = {};
  14. SWFUpload.prototype.initSettings = (function (oldInitSettings) {
  15. return function () {
  16. if (typeof(oldInitSettings) === "function") {
  17. oldInitSettings.call(this);
  18. }
  19. this.queueSettings = {};
  20. this.queueSettings.queue_cancelled_flag = false;
  21. this.queueSettings.queue_upload_count = 0;
  22. this.queueSettings.user_upload_complete_handler = this.settings.upload_complete_handler;
  23. this.queueSettings.user_upload_start_handler = this.settings.upload_start_handler;
  24. this.settings.upload_complete_handler = SWFUpload.queue.uploadCompleteHandler;
  25. this.settings.upload_start_handler = SWFUpload.queue.uploadStartHandler;
  26. this.settings.queue_complete_handler = this.settings.queue_complete_handler || null;
  27. };
  28. })(SWFUpload.prototype.initSettings);
  29. SWFUpload.prototype.startUpload = function (fileID) {
  30. this.queueSettings.queue_cancelled_flag = false;
  31. this.callFlash("StartUpload", [fileID]);
  32. };
  33. SWFUpload.prototype.cancelQueue = function () {
  34. this.queueSettings.queue_cancelled_flag = true;
  35. this.stopUpload();
  36. var stats = this.getStats();
  37. while (stats.files_queued > 0) {
  38. this.cancelUpload();
  39. stats = this.getStats();
  40. }
  41. };
  42. SWFUpload.queue.uploadStartHandler = function (file) {
  43. var returnValue;
  44. if (typeof(this.queueSettings.user_upload_start_handler) === "function") {
  45. returnValue = this.queueSettings.user_upload_start_handler.call(this, file);
  46. }
  47. // To prevent upload a real "FALSE" value must be returned, otherwise default to a real "TRUE" value.
  48. returnValue = (returnValue === false) ? false : true;
  49. this.queueSettings.queue_cancelled_flag = !returnValue;
  50. return returnValue;
  51. };
  52. SWFUpload.queue.uploadCompleteHandler = function (file) {
  53. var user_upload_complete_handler = this.queueSettings.user_upload_complete_handler;
  54. var continueUpload;
  55. if (file.filestatus === SWFUpload.FILE_STATUS.COMPLETE) {
  56. this.queueSettings.queue_upload_count++;
  57. }
  58. if (typeof(user_upload_complete_handler) === "function") {
  59. continueUpload = (user_upload_complete_handler.call(this, file) === false) ? false : true;
  60. } else if (file.filestatus === SWFUpload.FILE_STATUS.QUEUED) {
  61. // If the file was stopped and re-queued don't restart the upload
  62. continueUpload = false;
  63. } else {
  64. continueUpload = true;
  65. }
  66. if (continueUpload) {
  67. var stats = this.getStats();
  68. if (stats.files_queued > 0 && this.queueSettings.queue_cancelled_flag === false) {
  69. this.startUpload();
  70. } else if (this.queueSettings.queue_cancelled_flag === false) {
  71. this.queueEvent("queue_complete_handler", [this.queueSettings.queue_upload_count]);
  72. this.queueSettings.queue_upload_count = 0;
  73. } else {
  74. this.queueSettings.queue_cancelled_flag = false;
  75. this.queueSettings.queue_upload_count = 0;
  76. }
  77. }
  78. };
  79. }