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