3 Copyright 2012 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
7 YUI.add('file-flash', function(Y) {
10 * The FileFlash class provides a wrapper for a file pointer stored in Flash. The File wrapper
11 * also implements the mechanics for uploading a file and tracking its progress.
15 * The class provides a wrapper for a file pointer in Flash.
19 * @param {Object} config Configuration object.
22 var FileFlash = function(o) {
23 FileFlash.superclass.constructor.apply(this, arguments);
26 Y.extend(FileFlash, Y.Base, {
29 * Construction logic executed during FileFlash instantiation.
34 initializer : function (cfg) {
35 if (!this.get("id")) {
36 this._set("id", Y.guid("file"));
41 * Handler of events dispatched by the Flash player.
43 * @method _swfEventHandler
44 * @param {Event} event The event object received from the Flash player.
47 _swfEventHandler: function (event) {
48 if (event.id === this.get("id")) {
51 * Signals that this file's upload has started.
54 * @param event {Event} The event object for the `uploadstart` with the
58 * <dd>The Y.SWF instance of Flash uploader that's handling the upload.</dd>
62 this.fire("uploadstart", {uploader: this.get("uploader")});
64 case "uploadprogress":
67 * Signals that progress has been made on the upload of this file.
69 * @event uploadprogress
70 * @param event {Event} The event object for the `uploadprogress` with the
73 * <dt>originEvent</dt>
74 * <dd>The original event fired by the Flash uploader instance.</dd>
75 * <dt>bytesLoaded</dt>
76 * <dd>The number of bytes of the file that has been uploaded.</dd>
78 * <dd>The total number of bytes in the file (the file size)</dd>
79 * <dt>percentLoaded</dt>
80 * <dd>The fraction of the file that has been uploaded, out of 100.</dd>
83 this.fire("uploadprogress", {originEvent: event,
84 bytesLoaded: event.bytesLoaded,
85 bytesTotal: event.bytesTotal,
86 percentLoaded: Math.min(100, Math.round(10000*event.bytesLoaded/event.bytesTotal)/100)
88 this._set("bytesUploaded", event.bytesLoaded);
90 case "uploadcomplete":
93 * Signals that this file's upload has completed, but data has not yet been received from the server.
95 * @event uploadfinished
96 * @param event {Event} The event object for the `uploadfinished` with the
99 * <dt>originEvent</dt>
100 * <dd>The original event fired by the Flash player instance.</dd>
103 this.fire("uploadfinished", {originEvent: event});
105 case "uploadcompletedata":
107 * Signals that this file's upload has completed and data has been received from the server.
109 * @event uploadcomplete
110 * @param event {Event} The event object for the `uploadcomplete` with the
113 * <dt>originEvent</dt>
114 * <dd>The original event fired by the Flash player instance.</dd>
116 * <dd>The data returned by the server.</dd>
119 this.fire("uploadcomplete", {originEvent: event,
125 * Signals that this file's upload has been cancelled.
127 * @event uploadcancel
128 * @param event {Event} The event object for the `uploadcancel` with the
131 * <dt>originEvent</dt>
132 * <dd>The original event fired by the Flash player instance.</dd>
135 this.fire("uploadcancel", {originEvent: event});
140 * Signals that this file's upload has encountered an error.
143 * @param event {Event} The event object for the `uploaderror` with the
146 * <dt>originEvent</dt>
147 * <dd>The original event fired by the Flash player instance.</dd>
149 * <dd>The status code reported by the Flash player instance.</dd>
150 * <dt>statusText</dt>
151 * <dd>The text of the error event reported by the Flash player instance</dd>
154 this.fire("uploaderror", {originEvent: event, statusText: event.text, status: event.status});
161 * Starts the upload of a specific file.
163 * @method startUpload
164 * @param url {String} The URL to upload the file to.
165 * @param parameters {Object} (optional) A set of key-value pairs to send as variables along with the file upload HTTP request.
166 * @param fileFieldName {String} (optional) The name of the POST variable that should contain the uploaded file ('Filedata' by default)
168 startUpload: function(url, parameters, fileFieldName) {
170 if (this.get("uploader")) {
172 var myUploader = this.get("uploader"),
173 fileField = fileFieldName || "Filedata",
175 params = parameters || null;
177 this._set("bytesUploaded", 0);
179 myUploader.on("uploadstart", this._swfEventHandler, this);
180 myUploader.on("uploadprogress", this._swfEventHandler, this);
181 myUploader.on("uploadcomplete", this._swfEventHandler, this);
182 myUploader.on("uploadcompletedata", this._swfEventHandler, this);
183 myUploader.on("uploaderror", this._swfEventHandler, this);
185 myUploader.callSWF("upload", [id, url, params, fileField]);
191 * Cancels the upload of a specific file, if currently in progress.
193 * @method cancelUpload
195 cancelUpload: function () {
196 if (this.get("uploader")) {
197 this.get("uploader").callSWF("cancel", [this.get("id")]);
204 * The identity of the class.
216 * The type of transport.
228 * Static property used to define the default attribute configuration of
239 * A String containing the unique id of the file wrapped by the FileFlash instance.
240 * The id is supplied by the Flash player uploader.
247 writeOnce: "initOnly",
252 * The size of the file wrapped by FileFlash. This value is supplied by the Flash player uploader.
259 writeOnce: "initOnly",
264 * The name of the file wrapped by FileFlash. This value is supplied by the Flash player uploader.
271 writeOnce: "initOnly",
276 * The date that the file wrapped by FileFlash was created on. This value is supplied by the Flash player uploader.
278 * @attribute dateCreated
283 writeOnce: "initOnly",
288 * The date that the file wrapped by FileFlash was last modified on. This value is supplied by the Flash player uploader.
290 * @attribute dateModified
295 writeOnce: "initOnly",
300 * The number of bytes of the file that has been uploaded to the server. This value is
301 * non-zero only while a file is being uploaded.
303 * @attribute bytesUploaded
313 * The type of the file wrapped by FileFlash. This value is provided by the Flash player
321 writeOnce: "initOnly",
326 * The instance of Y.SWF wrapping the Flash player uploader associated with this file.
333 writeOnce: "initOnly",
339 Y.FileFlash = FileFlash;
342 }, '3.5.0' ,{requires:['base']});