70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich * The FileFlash class provides a wrapper for a file pointer stored in Flash. The File wrapper
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich * also implements the mechanics for uploading a file and tracking its progress.
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich * @module file-flash
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich * The class provides a wrapper for a file pointer in Flash.
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich * @class FileFlash
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich * @extends Base
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich * @constructor
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * @param {Object} config Configuration object.
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich FileFlash.superclass.constructor.apply(this, arguments);
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * Construction logic executed during FileFlash instantiation.
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * @method initializer
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * Handler of events dispatched by the Flash player.
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * @method _swfEventHandler
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * @param {Event} event The event object received from the Flash player.
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * Signals that this file's upload has started.
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * @event uploadstart
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * @param event {Event} The event object for the `uploadstart` with the
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * following payload:
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * <dt>uploader</dt>
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * <dd>The Y.SWF instance of Flash uploader that's handling the upload.</dd>
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich case "uploadstart":
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich this.fire("uploadstart", {uploader: this.get("uploader")});
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich case "uploadprogress":
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * Signals that progress has been made on the upload of this file.
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * @event uploadprogress
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * @param event {Event} The event object for the `uploadprogress` with the
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * following payload:
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * <dt>originEvent</dt>
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * <dd>The original event fired by the Flash uploader instance.</dd>
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * <dt>bytesLoaded</dt>
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * <dd>The number of bytes of the file that has been uploaded.</dd>
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * <dt>bytesTotal</dt>
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * <dd>The total number of bytes in the file (the file size)</dd>
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * <dt>percentLoaded</dt>
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * <dd>The fraction of the file that has been uploaded, out of 100.</dd>
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich this.fire("uploadprogress", {originEvent: event,
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich percentLoaded: Math.min(100, Math.round(10000*event.bytesLoaded/event.bytesTotal)/100)
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich this._set("bytesUploaded", event.bytesLoaded);
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich case "uploadcomplete":
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * Signals that this file's upload has completed, but data has not yet been received from the server.
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * @event uploadfinished
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * @param event {Event} The event object for the `uploadfinished` with the
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * following payload:
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * <dt>originEvent</dt>
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * <dd>The original event fired by the Flash player instance.</dd>
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich this.fire("uploadfinished", {originEvent: event});
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich case "uploadcompletedata":
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * Signals that this file's upload has completed and data has been received from the server.
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * @event uploadcomplete
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * @param event {Event} The event object for the `uploadcomplete` with the
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * following payload:
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * <dt>originEvent</dt>
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * <dd>The original event fired by the Flash player instance.</dd>
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * <dt>data</dt>
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * <dd>The data returned by the server.</dd>
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich this.fire("uploadcomplete", {originEvent: event,
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich case "uploadcancel":
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * Signals that this file's upload has been cancelled.
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * @event uploadcancel
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * @param event {Event} The event object for the `uploadcancel` with the
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * following payload:
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * <dt>originEvent</dt>
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * <dd>The original event fired by the Flash player instance.</dd>
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich this.fire("uploadcancel", {originEvent: event});
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich case "uploaderror":
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * Signals that this file's upload has encountered an error.
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * @event uploaderror
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * @param event {Event} The event object for the `uploaderror` with the
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * following payload:
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * <dt>originEvent</dt>
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * <dd>The original event fired by the Flash player instance.</dd>
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * <dt>status</dt>
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * <dd>The status code reported by the Flash player instance.</dd>
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * <dt>statusText</dt>
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * <dd>The text of the error event reported by the Flash player instance</dd>
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich this.fire("uploaderror", {originEvent: event, statusText: event.text, status: event.status});
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * Starts the upload of a specific file.
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * @method startUpload
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * @param url {String} The URL to upload the file to.
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * @param parameters {Object} (optional) A set of key-value pairs to send as variables along with the file upload HTTP request.
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * @param fileFieldName {String} (optional) The name of the POST variable that should contain the uploaded file ('Filedata' by default)
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich startUpload: function(url, parameters, fileFieldName) {
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich myUploader.on("uploadstart", this._swfEventHandler, this);
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich myUploader.on("uploadprogress", this._swfEventHandler, this);
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich myUploader.on("uploadcomplete", this._swfEventHandler, this);
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich myUploader.on("uploadcompletedata", this._swfEventHandler, this);
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich myUploader.on("uploaderror", this._swfEventHandler, this);
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich myUploader.callSWF("upload", [id, url, params, fileField]);
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * Cancels the upload of a specific file, if currently in progress.
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * @method cancelUpload
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich this.get("uploader").callSWF("cancel", [this.get("id")]);
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * The identity of the class.
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * @property NAME
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * @type String
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * @default 'file'
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * The type of transport.
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * @property TYPE
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * @type String
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * @default 'flash'
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * Static property used to define the default attribute configuration of
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * @property ATTRS
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * @type {Object}
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * A String containing the unique id of the file wrapped by the FileFlash instance.
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * The id is supplied by the Flash player uploader.
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * @attribute id
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * @type {String}
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * The size of the file wrapped by FileFlash. This value is supplied by the Flash player uploader.
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * @attribute size
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * @type {Number}
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * The name of the file wrapped by FileFlash. This value is supplied by the Flash player uploader.
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * @attribute name
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * @type {String}
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * The date that the file wrapped by FileFlash was created on. This value is supplied by the Flash player uploader.
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * @attribute dateCreated
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * @type {Date}
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * The date that the file wrapped by FileFlash was last modified on. This value is supplied by the Flash player uploader.
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * @attribute dateModified
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * @type {Date}
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * The number of bytes of the file that has been uploaded to the server. This value is
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * non-zero only while a file is being uploaded.
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * @attribute bytesUploaded
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * @type {Date}
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * The type of the file wrapped by FileFlash. This value is provided by the Flash player
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * @attribute type
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * @type {String}
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * The instance of Y.SWF wrapping the Flash player uploader associated with this file.
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * @attribute uploder
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich * @type {SWF}