Upgrade to ExtJS 3.1.1 - Released 02/08/2010
[extjs.git] / examples / image-organizer / SWFUpload / plugins / swfupload.queue.js
1 /*!
2  * Ext JS Library 3.1.1
3  * Copyright(c) 2006-2010 Ext JS, LLC
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 /*\r
8         Queue Plug-in\r
9         \r
10         Features:\r
11                 *Adds a cancelQueue() method for cancelling the entire queue.\r
12                 *All queued files are uploaded when startUpload() is called.\r
13                 *If false is returned from uploadComplete then the queue upload is stopped.\r
14                  If false is not returned (strict comparison) then the queue upload is continued.\r
15                 *Adds a QueueComplete event that is fired when all the queued files have finished uploading.\r
16                  Set the event handler with the queue_complete_handler setting.\r
17                 \r
18         */\r
19 \r
20 var SWFUpload;\r
21 if (typeof(SWFUpload) === "function") {\r
22         SWFUpload.queue = {};\r
23         \r
24         SWFUpload.prototype.initSettings = (function (oldInitSettings) {\r
25                 return function () {\r
26                         if (typeof(oldInitSettings) === "function") {\r
27                                 oldInitSettings.call(this);\r
28                         }\r
29                         \r
30                         this.queueSettings = {};\r
31                         \r
32                         this.queueSettings.queue_cancelled_flag = false;\r
33                         this.queueSettings.queue_upload_count = 0;\r
34                         \r
35                         this.queueSettings.user_upload_complete_handler = this.settings.upload_complete_handler;\r
36                         this.queueSettings.user_upload_start_handler = this.settings.upload_start_handler;\r
37                         this.settings.upload_complete_handler = SWFUpload.queue.uploadCompleteHandler;\r
38                         this.settings.upload_start_handler = SWFUpload.queue.uploadStartHandler;\r
39                         \r
40                         this.settings.queue_complete_handler = this.settings.queue_complete_handler || null;\r
41                 };\r
42         })(SWFUpload.prototype.initSettings);\r
43 \r
44         SWFUpload.prototype.startUpload = function (fileID) {\r
45                 this.queueSettings.queue_cancelled_flag = false;\r
46                 this.callFlash("StartUpload", [fileID]);\r
47         };\r
48 \r
49         SWFUpload.prototype.cancelQueue = function () {\r
50                 this.queueSettings.queue_cancelled_flag = true;\r
51                 this.stopUpload();\r
52                 \r
53                 var stats = this.getStats();\r
54                 while (stats.files_queued > 0) {\r
55                         this.cancelUpload();\r
56                         stats = this.getStats();\r
57                 }\r
58         };\r
59         \r
60         SWFUpload.queue.uploadStartHandler = function (file) {\r
61                 var returnValue;\r
62                 if (typeof(this.queueSettings.user_upload_start_handler) === "function") {\r
63                         returnValue = this.queueSettings.user_upload_start_handler.call(this, file);\r
64                 }\r
65                 \r
66                 // To prevent upload a real "FALSE" value must be returned, otherwise default to a real "TRUE" value.\r
67                 returnValue = (returnValue === false) ? false : true;\r
68                 \r
69                 this.queueSettings.queue_cancelled_flag = !returnValue;\r
70 \r
71                 return returnValue;\r
72         };\r
73         \r
74         SWFUpload.queue.uploadCompleteHandler = function (file) {\r
75                 var user_upload_complete_handler = this.queueSettings.user_upload_complete_handler;\r
76                 var continueUpload;\r
77                 \r
78                 if (file.filestatus === SWFUpload.FILE_STATUS.COMPLETE) {\r
79                         this.queueSettings.queue_upload_count++;\r
80                 }\r
81 \r
82                 if (typeof(user_upload_complete_handler) === "function") {\r
83                         continueUpload = (user_upload_complete_handler.call(this, file) === false) ? false : true;\r
84                 } else if (file.filestatus === SWFUpload.FILE_STATUS.QUEUED) {\r
85                         // If the file was stopped and re-queued don't restart the upload\r
86                         continueUpload = false;\r
87                 } else {\r
88                         continueUpload = true;\r
89                 }\r
90                 \r
91                 if (continueUpload) {\r
92                         var stats = this.getStats();\r
93                         if (stats.files_queued > 0 && this.queueSettings.queue_cancelled_flag === false) {\r
94                                 this.startUpload();\r
95                         } else if (this.queueSettings.queue_cancelled_flag === false) {\r
96                                 this.queueEvent("queue_complete_handler", [this.queueSettings.queue_upload_count]);\r
97                                 this.queueSettings.queue_upload_count = 0;\r
98                         } else {\r
99                                 this.queueSettings.queue_cancelled_flag = false;\r
100                                 this.queueSettings.queue_upload_count = 0;\r
101                         }\r
102                 }\r
103         };\r
104 }