|
1 YUI.add('file-flash', function (Y, NAME) { |
|
2 |
|
3 /** |
|
4 * The FileFlash class provides a wrapper for a file pointer stored in Flash. The File wrapper |
|
5 * also implements the mechanics for uploading a file and tracking its progress. |
|
6 * @module file-flash |
|
7 */ |
|
8 /** |
|
9 * The class provides a wrapper for a file pointer in Flash. |
|
10 * @class FileFlash |
|
11 * @extends Base |
|
12 * @constructor |
|
13 * @param {Object} config Configuration object. |
|
14 */ |
|
15 |
|
16 var FileFlash = function(o) { |
|
17 FileFlash.superclass.constructor.apply(this, arguments); |
|
18 }; |
|
19 |
|
20 Y.extend(FileFlash, Y.Base, { |
|
21 |
|
22 /** |
|
23 * Construction logic executed during FileFlash instantiation. |
|
24 * |
|
25 * @method initializer |
|
26 * @protected |
|
27 */ |
|
28 initializer : function (cfg) { |
|
29 if (!this.get("id")) { |
|
30 this._set("id", Y.guid("file")); |
|
31 } |
|
32 }, |
|
33 |
|
34 /** |
|
35 * Handler of events dispatched by the Flash player. |
|
36 * |
|
37 * @method _swfEventHandler |
|
38 * @param {Event} event The event object received from the Flash player. |
|
39 * @protected |
|
40 */ |
|
41 _swfEventHandler: function (event) { |
|
42 if (event.id === this.get("id")) { |
|
43 switch (event.type) { |
|
44 /** |
|
45 * Signals that this file's upload has started. |
|
46 * |
|
47 * @event uploadstart |
|
48 * @param event {Event} The event object for the `uploadstart` with the |
|
49 * following payload: |
|
50 * <dl> |
|
51 * <dt>uploader</dt> |
|
52 * <dd>The Y.SWF instance of Flash uploader that's handling the upload.</dd> |
|
53 * </dl> |
|
54 */ |
|
55 case "uploadstart": |
|
56 this.fire("uploadstart", {uploader: this.get("uploader")}); |
|
57 break; |
|
58 case "uploadprogress": |
|
59 |
|
60 /** |
|
61 * Signals that progress has been made on the upload of this file. |
|
62 * |
|
63 * @event uploadprogress |
|
64 * @param event {Event} The event object for the `uploadprogress` with the |
|
65 * following payload: |
|
66 * <dl> |
|
67 * <dt>originEvent</dt> |
|
68 * <dd>The original event fired by the Flash uploader instance.</dd> |
|
69 * <dt>bytesLoaded</dt> |
|
70 * <dd>The number of bytes of the file that has been uploaded.</dd> |
|
71 * <dt>bytesTotal</dt> |
|
72 * <dd>The total number of bytes in the file (the file size)</dd> |
|
73 * <dt>percentLoaded</dt> |
|
74 * <dd>The fraction of the file that has been uploaded, out of 100.</dd> |
|
75 * </dl> |
|
76 */ |
|
77 this.fire("uploadprogress", {originEvent: event, |
|
78 bytesLoaded: event.bytesLoaded, |
|
79 bytesTotal: event.bytesTotal, |
|
80 percentLoaded: Math.min(100, Math.round(10000*event.bytesLoaded/event.bytesTotal)/100) |
|
81 }); |
|
82 this._set("bytesUploaded", event.bytesLoaded); |
|
83 break; |
|
84 case "uploadcomplete": |
|
85 |
|
86 /** |
|
87 * Signals that this file's upload has completed, but data has not yet been received from the server. |
|
88 * |
|
89 * @event uploadfinished |
|
90 * @param event {Event} The event object for the `uploadfinished` with the |
|
91 * following payload: |
|
92 * <dl> |
|
93 * <dt>originEvent</dt> |
|
94 * <dd>The original event fired by the Flash player instance.</dd> |
|
95 * </dl> |
|
96 */ |
|
97 this.fire("uploadfinished", {originEvent: event}); |
|
98 break; |
|
99 case "uploadcompletedata": |
|
100 /** |
|
101 * Signals that this file's upload has completed and data has been received from the server. |
|
102 * |
|
103 * @event uploadcomplete |
|
104 * @param event {Event} The event object for the `uploadcomplete` with the |
|
105 * following payload: |
|
106 * <dl> |
|
107 * <dt>originEvent</dt> |
|
108 * <dd>The original event fired by the Flash player instance.</dd> |
|
109 * <dt>data</dt> |
|
110 * <dd>The data returned by the server.</dd> |
|
111 * </dl> |
|
112 */ |
|
113 this.fire("uploadcomplete", {originEvent: event, |
|
114 data: event.data}); |
|
115 break; |
|
116 case "uploadcancel": |
|
117 |
|
118 /** |
|
119 * Signals that this file's upload has been cancelled. |
|
120 * |
|
121 * @event uploadcancel |
|
122 * @param event {Event} The event object for the `uploadcancel` with the |
|
123 * following payload: |
|
124 * <dl> |
|
125 * <dt>originEvent</dt> |
|
126 * <dd>The original event fired by the Flash player instance.</dd> |
|
127 * </dl> |
|
128 */ |
|
129 this.fire("uploadcancel", {originEvent: event}); |
|
130 break; |
|
131 case "uploaderror": |
|
132 |
|
133 /** |
|
134 * Signals that this file's upload has encountered an error. |
|
135 * |
|
136 * @event uploaderror |
|
137 * @param event {Event} The event object for the `uploaderror` with the |
|
138 * following payload: |
|
139 * <dl> |
|
140 * <dt>originEvent</dt> |
|
141 * <dd>The original event fired by the Flash player instance.</dd> |
|
142 * <dt>status</dt> |
|
143 * <dd>The status code reported by the Flash Player. If it's an HTTP error, |
|
144 * then this corresponds to the HTTP status code received by the uploader.</dd> |
|
145 * <dt>statusText</dt> |
|
146 * <dd>The text of the error event reported by the Flash Player.</dd> |
|
147 * <dt>source</dt> |
|
148 * <dd>Either "http" (if it's an HTTP error), or "io" (if it's a network transmission |
|
149 * error.)</dd> |
|
150 * </dl> |
|
151 */ |
|
152 this.fire("uploaderror", {originEvent: event, status: event.status, statusText: event.message, source: event.source}); |
|
153 |
|
154 } |
|
155 } |
|
156 }, |
|
157 |
|
158 /** |
|
159 * Starts the upload of a specific file. |
|
160 * |
|
161 * @method startUpload |
|
162 * @param url {String} The URL to upload the file to. |
|
163 * @param parameters {Object} (optional) A set of key-value pairs to send as variables along with the file upload HTTP request. |
|
164 * @param fileFieldName {String} (optional) The name of the POST variable that should contain the uploaded file ('Filedata' by default) |
|
165 */ |
|
166 startUpload: function(url, parameters, fileFieldName) { |
|
167 |
|
168 if (this.get("uploader")) { |
|
169 |
|
170 var myUploader = this.get("uploader"), |
|
171 fileField = fileFieldName || "Filedata", |
|
172 id = this.get("id"), |
|
173 params = parameters || null; |
|
174 |
|
175 this._set("bytesUploaded", 0); |
|
176 |
|
177 myUploader.on("uploadstart", this._swfEventHandler, this); |
|
178 myUploader.on("uploadprogress", this._swfEventHandler, this); |
|
179 myUploader.on("uploadcomplete", this._swfEventHandler, this); |
|
180 myUploader.on("uploadcompletedata", this._swfEventHandler, this); |
|
181 myUploader.on("uploaderror", this._swfEventHandler, this); |
|
182 |
|
183 myUploader.callSWF("upload", [id, url, params, fileField]); |
|
184 } |
|
185 |
|
186 }, |
|
187 |
|
188 /** |
|
189 * Cancels the upload of a specific file, if currently in progress. |
|
190 * |
|
191 * @method cancelUpload |
|
192 */ |
|
193 cancelUpload: function () { |
|
194 if (this.get("uploader")) { |
|
195 this.get("uploader").callSWF("cancel", [this.get("id")]); |
|
196 this.fire("uploadcancel"); |
|
197 } |
|
198 } |
|
199 |
|
200 }, { |
|
201 |
|
202 /** |
|
203 * The identity of the class. |
|
204 * |
|
205 * @property NAME |
|
206 * @type String |
|
207 * @default 'file' |
|
208 * @readOnly |
|
209 * @protected |
|
210 * @static |
|
211 */ |
|
212 NAME: 'file', |
|
213 |
|
214 /** |
|
215 * The type of transport. |
|
216 * |
|
217 * @property TYPE |
|
218 * @type String |
|
219 * @default 'flash' |
|
220 * @readOnly |
|
221 * @protected |
|
222 * @static |
|
223 */ |
|
224 TYPE: "flash", |
|
225 |
|
226 /** |
|
227 * Static property used to define the default attribute configuration of |
|
228 * the File. |
|
229 * |
|
230 * @property ATTRS |
|
231 * @type {Object} |
|
232 * @protected |
|
233 * @static |
|
234 */ |
|
235 ATTRS: { |
|
236 |
|
237 /** |
|
238 * A String containing the unique id of the file wrapped by the FileFlash instance. |
|
239 * The id is supplied by the Flash player uploader. |
|
240 * |
|
241 * @attribute id |
|
242 * @type {String} |
|
243 * @initOnly |
|
244 */ |
|
245 id: { |
|
246 writeOnce: "initOnly", |
|
247 value: null |
|
248 }, |
|
249 |
|
250 /** |
|
251 * The size of the file wrapped by FileFlash. This value is supplied by the Flash player uploader. |
|
252 * |
|
253 * @attribute size |
|
254 * @type {Number} |
|
255 * @initOnly |
|
256 */ |
|
257 size: { |
|
258 writeOnce: "initOnly", |
|
259 value: 0 |
|
260 }, |
|
261 |
|
262 /** |
|
263 * The name of the file wrapped by FileFlash. This value is supplied by the Flash player uploader. |
|
264 * |
|
265 * @attribute name |
|
266 * @type {String} |
|
267 * @initOnly |
|
268 */ |
|
269 name: { |
|
270 writeOnce: "initOnly", |
|
271 value: null |
|
272 }, |
|
273 |
|
274 /** |
|
275 * The date that the file wrapped by FileFlash was created on. This value is supplied by the Flash player uploader. |
|
276 * |
|
277 * @attribute dateCreated |
|
278 * @type {Date} |
|
279 * @initOnly |
|
280 */ |
|
281 dateCreated: { |
|
282 writeOnce: "initOnly", |
|
283 value: null |
|
284 }, |
|
285 |
|
286 /** |
|
287 * The date that the file wrapped by FileFlash was last modified on. This value is supplied by the Flash player uploader. |
|
288 * |
|
289 * @attribute dateModified |
|
290 * @type {Date} |
|
291 * @initOnly |
|
292 */ |
|
293 dateModified: { |
|
294 writeOnce: "initOnly", |
|
295 value: null |
|
296 }, |
|
297 |
|
298 /** |
|
299 * The number of bytes of the file that has been uploaded to the server. This value is |
|
300 * non-zero only while a file is being uploaded. |
|
301 * |
|
302 * @attribute bytesUploaded |
|
303 * @type {Date} |
|
304 * @readOnly |
|
305 */ |
|
306 bytesUploaded: { |
|
307 readOnly: true, |
|
308 value: 0 |
|
309 }, |
|
310 |
|
311 /** |
|
312 * The type of the file wrapped by FileFlash. This value is provided by the Flash player |
|
313 * uploader. |
|
314 * |
|
315 * @attribute type |
|
316 * @type {String} |
|
317 * @initOnly |
|
318 */ |
|
319 type: { |
|
320 writeOnce: "initOnly", |
|
321 value: null |
|
322 }, |
|
323 |
|
324 /** |
|
325 * The instance of Y.SWF wrapping the Flash player uploader associated with this file. |
|
326 * |
|
327 * @attribute uploder |
|
328 * @type {SWF} |
|
329 * @initOnly |
|
330 */ |
|
331 uploader: { |
|
332 writeOnce: "initOnly", |
|
333 value: null |
|
334 } |
|
335 } |
|
336 }); |
|
337 |
|
338 Y.FileFlash = FileFlash; |
|
339 |
|
340 |
|
341 }, '@VERSION@', {"requires": ["base"]}); |