2 Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
8 YUI.add('uploader', function(Y) {
11 * Upload files to the server with support for file filtering, multiple file uploads
12 * and progress monitoring.
19 var SWFURL = Y.Env.cdn + "uploader/assets/uploader.swf";
22 * The Uploader widget is a tool for uploading files to the server.
25 * @requires base, node, event, swf
29 * Creates the Uploader instance and keeps the initialization data
34 * @param {Object} config (optional) Configuration parameters for the Uploader. The following parameters are available:
36 * <dt>boundingBox : String|Node (required)</dt>
38 * <dt>buttonSkin : String (optional)</dt>
40 * <dt>transparent : String (optional)</dt>
42 * <dt>swfURL : String (optional)</dt>
47 function Uploader (config /*Object*/) {
49 Uploader.superclass.constructor.apply(this, arguments);
51 if (config.hasOwnProperty("boundingBox")) {
52 this.set("boundingBox", config.boundingBox);
55 if (config.hasOwnProperty("buttonSkin")) {
56 this.set("buttonSkin", config.buttonSkin);
58 if (config.hasOwnProperty("transparent")) {
59 this.set("transparent", config.transparent);
61 if (config.hasOwnProperty("swfURL")) {
62 this.set("swfURL", config.swfURL);
67 Y.extend(Uploader, Y.Base, {
70 * The reference to the instance of Y.SWF that encapsulates the instance of the Flash player with uploader logic.
73 * @property uploaderswf
80 * The id of this instance of uploader.
89 * Construction logic executed during Uploader instantiation.
94 initializer : function () {
96 this._id = Y.guid("uploader");
97 var oElement = Node.one(this.get("boundingBox"));
99 var params = {version: "10.0.45",
100 fixedAttributes: {allowScriptAccess:"always", allowNetworking:"all", scale: "noscale"},
103 if (this.get("buttonSkin") != "") {
104 params.flashVars["buttonSkin"] = this.get("buttonSkin");
106 if (this.get("transparent")) {
107 params.fixedAttributes["wmode"] = "transparent";
110 this.uploaderswf = new Y.SWF(oElement, this.get("swfURL"), params);
112 var upswf = this.uploaderswf;
113 var relEvent = Y.bind(this._relayEvent, this);
116 * Announces that the uploader is ready and available for calling methods
117 * and setting properties
119 * @event uploaderReady
120 * @param event {Event} The event object for the uploaderReady.
122 upswf.on ("swfReady", Y.bind(this._initializeUploader, this));
125 * Fired when the mouse button is clicked on the Uploader's 'Browse' button.
128 * @param event {Event} The event object for the click.
130 upswf.on ("click", relEvent);
133 * Fires when the user has finished selecting a set of files to be uploaded.
136 * @param event {Event} The event object for the fileSelect.
139 * <dd>The file list Object with entries in the following format:
140 fileList[fileID] = {id: fileID, name: fileName, cDate: fileCDate, mDate: fileMDate, size: fileSize}</dd>
143 upswf.on ("fileselect", relEvent);
146 * Fired when the mouse button is pressed on the Uploader's 'Browse' button.
149 * @param event {Event} The event object for the mousedown.
151 upswf.on ("mousedown", relEvent);
154 * Fired when the mouse button is raised on the Uploader's 'Browse' button.
157 * @param event {Event} The event object for the mouseup.
159 upswf.on ("mouseup", relEvent);
162 * Fired when the mouse leaves the Uploader's 'Browse' button.
165 * @param event {Event} The event object for the mouseleave.
167 upswf.on ("mouseleave", relEvent);
170 * Fired when the mouse enters the Uploader's 'Browse' button.
173 * @param event {Event} The event object for the mouseenter.
175 upswf.on ("mouseenter", relEvent);
178 * Announces that the uploader is ready and available for calling methods
179 * and setting properties
181 * @event uploadcancel
182 * @param event {Event} The event object for the uploaderReady.
185 * <dd><code>drag:start</code> event from the thumb</dd>
188 upswf.on ("uploadcancel", relEvent);
191 * Fires when a specific file's upload is cancelled.
193 * @event uploadcomplete
194 * @param event {Event} The event object for the uploadcancel.
197 * <dd>The id of the file whose upload has been cancelled.</dd>
200 upswf.on ("uploadcomplete", relEvent);
203 * If the server has sent a response to the file upload, this event is
204 * fired and the response is added to its payload.
206 * @event uploadcompletedata
207 * @param event {Event} The event object for the uploadcompletedata.
210 * <dd>The id of the file for which the response is being provided.</dd>
212 * <dd>The content of the server response.</dd>
215 upswf.on ("uploadcompletedata", relEvent);
218 * Provides error information if an error has occurred during the upload.
221 * @param event {Event} The event object for the uploadeerror.
224 * <dd>The id of the file for which the upload error has occurred.</dd>
226 * <dd>Relevant error information.</dd>
229 upswf.on ("uploaderror", relEvent);
232 * Provides progress information on a specific file upload.
234 * @event uploadprogress
235 * @param event {Event} The event object for the uploadprogress.
238 * <dd>The id of the file for which the progress information is being provided.</dd>
239 * <dt>bytesLoaded</dt>
240 * <dd>The number of bytes of the file that has been uploaded.</dd>
241 * <dt>bytesTotal</dt>
242 * <dd>The total number of bytes in the file that is being uploaded.</dd>
245 upswf.on ("uploadprogress", relEvent);
248 * Announces that the upload has been started for a specific file.
251 * @param event {Event} The event object for the uploadstart.
254 * <dd>The id of the file whose upload has been started.</dd>
257 upswf.on ("uploadstart", relEvent);
261 * Removes a specific file from the upload queue.
264 * @param fileID {String} The ID of the file to be removed
265 * @return {Object} The updated file list, which is an object of the format:
266 * fileList[fileID] = {id: fileID, name: fileName, cDate: fileCDate, mDate: fileMDate, size: fileSize}
268 removeFile : function (fileID /*String*/) {
269 return this.uploaderswf.callSWF("removeFile", [fileID]);
273 * Clears the upload queue.
275 * @method clearFileList
276 * @return {Boolean} This method always returns true.
278 clearFileList : function () {
279 return this.uploaderswf.callSWF("clearFileList", []);
283 * Starts the upload of a specific file.
286 * @param fileID {String} The ID of the file to be uploaded.
287 * @param url {String} The URL to upload the file to.
288 * @param method {String} (optional) The HTTP method to use for sending additional variables, either 'GET' or 'POST' ('GET' by default)
289 * @param postVars {Object} (optional) A set of key-value pairs to send as variables along with the file upload HTTP request.
290 * @param postFileVarName {String} (optional) The name of the POST variable that should contain the uploaded file ('Filedata' by default)
291 * @return {Boolean} This method always returns true.
293 upload : function (fileID /*String*/, url /*String*/, method /*String*/, postVars /*Object*/, postFileVarName /*String*/) {
294 if (Y.Lang.isArray(fileID)) {
295 return this.uploaderswf.callSWF("uploadThese", [fileID, url, method, postVars, postFileVarName]);
297 else if (Y.Lang.isString(fileID)) {
298 return this.uploaderswf.callSWF("upload", [fileID, url, method, postVars, postFileVarName]);
304 * Starts the upload of a set of files, as specified in the first argument.
305 * The upload queue is managed automatically.
307 * @method uploadThese
308 * @param fileIDs {Array} The array of IDs of the files to be uploaded.
309 * @param url {String} The URL to upload the files to.
310 * @param method {String} (optional) The HTTP method to use for sending additional variables, either 'GET' or 'POST' ('GET' by default)
311 * @param postVars {Object} (optional) A set of key-value pairs to send as variables along with the file upload HTTP request.
312 * @param postFileVarName {String} (optional) The name of the POST variable that should contain the uploaded file ('Filedata' by default)
314 uploadThese : function (fileIDs /*Array*/, url /*String*/, method /*String*/, postVars /*Object*/, postFileVarName /*String*/) {
315 return this.uploaderswf.callSWF("uploadThese", [fileIDs, url, method, postVars, postFileVarName]);
319 * Starts the upload of the files in the upload queue.
320 * The upload queue is managed automatically.
323 * @param url {String} The URL to upload the files to.
324 * @param method {String} (optional) The HTTP method to use for sending additional variables, either 'GET' or 'POST' ('GET' by default)
325 * @param postVars {Object} (optional) A set of key-value pairs to send as variables along with the file upload HTTP request.
326 * @param postFileVarName {String} (optional) The name of the POST variable that should contain the uploaded file ('Filedata' by default).
328 uploadAll : function (url /*String*/, method /*String*/, postVars /*Object*/, postFileVarName /*String*/) {
329 return this.uploaderswf.callSWF("uploadAll", [url, method, postVars,postFileVarName]);
333 * Cancels the upload of a specific file, if currently in progress.
336 * @param fileID {String} (optional) The ID of the file whose upload should be cancelled. If no ID is specified, all uploads are cancelled.
338 cancel : function (fileID /*String*/) {
339 return this.uploaderswf.callSWF("cancel", [fileID]);
344 * Setter for the 'log' property.
345 * @method setAllowLogging
346 * @param value {Boolean} The value for the 'log' property.
348 setAllowLogging : function (value /*Boolean*/) {
349 this.uploaderswf.callSWF("setAllowLogging", [value]);
354 * Setter for the 'multiFiles' property.
355 * @method setAllowMultipleFiles
356 * @param value {Boolean} The value for the 'multiFiles' property.
358 setAllowMultipleFiles : function (value /*Boolean*/) {
359 this.uploaderswf.callSWF("setAllowMultipleFiles", [value]);
364 * Setter for the 'simLimit' property.
365 * @method setSimUploadLimit
366 * @param value {Boolean} The value for the 'simLimit' property.
368 setSimUploadLimit : function (value /*int*/) {
369 this.uploaderswf.callSWF("setSimUploadLimit", [value]);
374 * Setter for the 'fileFilters' property.
375 * @method setFileFilters
376 * @param value {Boolean} The value for the 'fileFilters' property.
378 setFileFilters : function (fileFilters /*Array*/) {
379 this.uploaderswf.callSWF("setFileFilters", [fileFilters]);
383 * Enables the uploader user input (mouse clicks on the 'Browse' button). If the button skin
384 * is applied, the sprite is reset from the "disabled" state.
388 enable : function () {
389 this.uploaderswf.callSWF("enable");
393 * Disables the uploader user input (mouse clicks on the 'Browse' button). If the button skin
394 * is applied, the sprite is set to the 'disabled' state.
398 disable : function () {
399 this.uploaderswf.callSWF("disable");
404 * Called when the uploader SWF is initialized
405 * @method _initializeUploader
406 * @param event {Object} The event to be propagated from Flash.
408 _initializeUploader: function (event) {
409 this.publish("uploaderReady", {fireOnce:true});
410 this.fire("uploaderReady", {});
415 * Called when an event is dispatched from Uploader
416 * @method _relayEvent
417 * @param event {Object} The event to be propagated from Flash.
419 _relayEvent: function (event) {
420 this.fire(event.type, event);
425 return "Uploader " + this._id;
432 * The flag that allows Flash player to
433 * output debug messages to its trace stack
434 * (if the Flash debug player is used).
442 setter : "setAllowLogging"
446 * The flag that allows the user to select
447 * more than one files during the 'Browse'
448 * dialog (using 'Shift' or 'Ctrl' keys).
450 * @attribute multiFiles
456 setter : "setAllowMultipleFiles"
460 * The number of files that can be uploaded
461 * simultaneously if the automatic queue management
462 * is used. This value can be in the range between 2
465 * @attribute simLimit
471 setter : "setSimUploadLimit"
475 * The array of filters on file extensions for
476 * the 'Browse' dialog. These filters only provide
477 * convenience for the user and do not strictly
478 * limit the selection to certain file extensions.
479 * Each item in the array must contain a 'description'
480 * property, and an 'extensions' property that must be
481 * in the form "*.ext;*.ext;*.ext;..."
483 * @attribute fileFilters
489 setter : "setFileFilters"
493 * The Node containing the uploader's 'Browse' button.
495 * @attribute boundingBox
502 writeOnce: 'initOnly'
506 * The URL of the image sprite for skinning the uploader's 'Browse' button.
508 * @attribute buttonSkin
515 writeOnce: 'initOnly'
519 * The flag indicating whether the uploader is rendered
520 * with a transparent background.
522 * @attribute transparent
529 writeOnce: 'initOnly'
533 * The URL of the uploader's SWF.
537 * @default "assets/uploader.swf"
542 writeOnce: 'initOnly'
548 Y.Uploader = Uploader;
551 }, '3.3.0' ,{requires:['swf', 'base', 'node', 'event']});