2 YUI 3.15.0 (build 834026e)
3 Copyright 2014 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
8 YUI.add('file-flash', function (Y, NAME) {
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.
16 * The class provides a wrapper for a file pointer in Flash.
20 * @param {Object} config Configuration object.
23 var FileFlash = function(o) {
24 FileFlash.superclass.constructor.apply(this, arguments);
27 Y.extend(FileFlash, Y.Base, {
30 * Construction logic executed during FileFlash instantiation.
35 initializer : function (cfg) {
36 if (!this.get("id")) {
37 this._set("id", Y.guid("file"));
42 * Handler of events dispatched by the Flash player.
44 * @method _swfEventHandler
45 * @param {Event} event The event object received from the Flash player.
48 _swfEventHandler: function (event) {
49 if (event.id === this.get("id")) {
52 * Signals that this file's upload has started.
55 * @param event {Event} The event object for the `uploadstart` with the
59 * <dd>The Y.SWF instance of Flash uploader that's handling the upload.</dd>
63 this.fire("uploadstart", {uploader: this.get("uploader")});
65 case "uploadprogress":
68 * Signals that progress has been made on the upload of this file.
70 * @event uploadprogress
71 * @param event {Event} The event object for the `uploadprogress` with the
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>
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>
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)
89 this._set("bytesUploaded", event.bytesLoaded);
91 case "uploadcomplete":
94 * Signals that this file's upload has completed, but data has not yet been received from the server.
96 * @event uploadfinished
97 * @param event {Event} The event object for the `uploadfinished` with the
100 * <dt>originEvent</dt>
101 * <dd>The original event fired by the Flash player instance.</dd>
104 this.fire("uploadfinished", {originEvent: event});
106 case "uploadcompletedata":
108 * Signals that this file's upload has completed and data has been received from the server.
110 * @event uploadcomplete
111 * @param event {Event} The event object for the `uploadcomplete` with the
114 * <dt>originEvent</dt>
115 * <dd>The original event fired by the Flash player instance.</dd>
117 * <dd>The data returned by the server.</dd>
120 this.fire("uploadcomplete", {originEvent: event,
126 * Signals that this file's upload has been cancelled.
128 * @event uploadcancel
129 * @param event {Event} The event object for the `uploadcancel` with the
132 * <dt>originEvent</dt>
133 * <dd>The original event fired by the Flash player instance.</dd>
136 this.fire("uploadcancel", {originEvent: event});
141 * Signals that this file's upload has encountered an error.
144 * @param event {Event} The event object for the `uploaderror` with the
147 * <dt>originEvent</dt>
148 * <dd>The original event fired by the Flash player instance.</dd>
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>
155 * <dd>Either "http" (if it's an HTTP error), or "io" (if it's a network transmission
159 this.fire("uploaderror", {originEvent: event, status: event.status, statusText: event.message, source: event.source});
166 * Starts the upload of a specific file.
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)
173 startUpload: function(url, parameters, fileFieldName) {
175 if (this.get("uploader")) {
177 var myUploader = this.get("uploader"),
178 fileField = fileFieldName || "Filedata",
180 params = parameters || null;
182 this._set("bytesUploaded", 0);
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);
190 myUploader.callSWF("upload", [id, url, params, fileField]);
196 * Cancels the upload of a specific file, if currently in progress.
198 * @method cancelUpload
200 cancelUpload: function () {
201 if (this.get("uploader")) {
202 this.get("uploader").callSWF("cancel", [this.get("id")]);
203 this.fire("uploadcancel");
210 * The identity of the class.
222 * The type of transport.
234 * Static property used to define the default attribute configuration of
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.
253 writeOnce: "initOnly",
258 * The size of the file wrapped by FileFlash. This value is supplied by the Flash player uploader.
265 writeOnce: "initOnly",
270 * The name of the file wrapped by FileFlash. This value is supplied by the Flash player uploader.
277 writeOnce: "initOnly",
282 * The date that the file wrapped by FileFlash was created on. This value is supplied by the Flash player uploader.
284 * @attribute dateCreated
289 writeOnce: "initOnly",
294 * The date that the file wrapped by FileFlash was last modified on. This value is supplied by the Flash player uploader.
296 * @attribute dateModified
301 writeOnce: "initOnly",
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.
309 * @attribute bytesUploaded
319 * The type of the file wrapped by FileFlash. This value is provided by the Flash player
327 writeOnce: "initOnly",
332 * The instance of Y.SWF wrapping the Flash player uploader associated with this file.
339 writeOnce: "initOnly",
345 Y.FileFlash = FileFlash;
348 }, '3.15.0', {"requires": ["base"]});