| 1 | /** |
|---|
| 2 | * SWFUpload v2.1.0 by Jacob Roberts, Feb 2008, http://www.swfupload.org, http://swfupload.googlecode.com, http://www.swfupload.org |
|---|
| 3 | * -------- -------- -------- -------- -------- -------- -------- -------- |
|---|
| 4 | * SWFUpload is (c) 2006 Lars Huring, Olov Nilzᅵn and Mammon Media and is released under the MIT License: |
|---|
| 5 | * http://www.opensource.org/licenses/mit-license.php |
|---|
| 6 | * |
|---|
| 7 | * See Changelog.txt for version history |
|---|
| 8 | * |
|---|
| 9 | */ |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | /* *********** */ |
|---|
| 13 | /* Constructor */ |
|---|
| 14 | /* *********** */ |
|---|
| 15 | |
|---|
| 16 | var SWFUpload = function (settings) { |
|---|
| 17 | this.initSWFUpload(settings); |
|---|
| 18 | }; |
|---|
| 19 | |
|---|
| 20 | SWFUpload.prototype.initSWFUpload = function (settings) { |
|---|
| 21 | try { |
|---|
| 22 | this.customSettings = {}; // A container where developers can place their own settings associated with this instance. |
|---|
| 23 | this.settings = settings; |
|---|
| 24 | this.eventQueue = []; |
|---|
| 25 | this.movieName = "SWFUpload_" + SWFUpload.movieCount++; |
|---|
| 26 | this.movieElement = null; |
|---|
| 27 | |
|---|
| 28 | // Setup global control tracking |
|---|
| 29 | SWFUpload.instances[this.movieName] = this; |
|---|
| 30 | |
|---|
| 31 | // Load the settings. Load the Flash movie. |
|---|
| 32 | this.initSettings(); |
|---|
| 33 | this.loadFlash(); |
|---|
| 34 | this.displayDebugInfo(); |
|---|
| 35 | } catch (ex) { |
|---|
| 36 | delete SWFUpload.instances[this.movieName]; |
|---|
| 37 | throw ex; |
|---|
| 38 | } |
|---|
| 39 | }; |
|---|
| 40 | |
|---|
| 41 | /* *************** */ |
|---|
| 42 | /* Static Members */ |
|---|
| 43 | /* *************** */ |
|---|
| 44 | SWFUpload.instances = {}; |
|---|
| 45 | SWFUpload.movieCount = 0; |
|---|
| 46 | SWFUpload.version = "2.1.0"; |
|---|
| 47 | SWFUpload.QUEUE_ERROR = { |
|---|
| 48 | QUEUE_LIMIT_EXCEEDED : -100, |
|---|
| 49 | FILE_EXCEEDS_SIZE_LIMIT : -110, |
|---|
| 50 | ZERO_BYTE_FILE : -120, |
|---|
| 51 | INVALID_FILETYPE : -130 |
|---|
| 52 | }; |
|---|
| 53 | SWFUpload.UPLOAD_ERROR = { |
|---|
| 54 | HTTP_ERROR : -200, |
|---|
| 55 | MISSING_UPLOAD_URL : -210, |
|---|
| 56 | IO_ERROR : -220, |
|---|
| 57 | SECURITY_ERROR : -230, |
|---|
| 58 | UPLOAD_LIMIT_EXCEEDED : -240, |
|---|
| 59 | UPLOAD_FAILED : -250, |
|---|
| 60 | SPECIFIED_FILE_ID_NOT_FOUND : -260, |
|---|
| 61 | FILE_VALIDATION_FAILED : -270, |
|---|
| 62 | FILE_CANCELLED : -280, |
|---|
| 63 | UPLOAD_STOPPED : -290 |
|---|
| 64 | }; |
|---|
| 65 | SWFUpload.FILE_STATUS = { |
|---|
| 66 | QUEUED : -1, |
|---|
| 67 | IN_PROGRESS : -2, |
|---|
| 68 | ERROR : -3, |
|---|
| 69 | COMPLETE : -4, |
|---|
| 70 | CANCELLED : -5 |
|---|
| 71 | }; |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | /* ******************** */ |
|---|
| 75 | /* Instance Members */ |
|---|
| 76 | /* ******************** */ |
|---|
| 77 | |
|---|
| 78 | // Private: initSettings ensures that all the |
|---|
| 79 | // settings are set, getting a default value if one was not assigned. |
|---|
| 80 | SWFUpload.prototype.initSettings = function () { |
|---|
| 81 | this.ensureDefault = function (settingName, defaultValue) { |
|---|
| 82 | this.settings[settingName] = (this.settings[settingName] == undefined) ? defaultValue : this.settings[settingName]; |
|---|
| 83 | }; |
|---|
| 84 | |
|---|
| 85 | // Upload backend settings |
|---|
| 86 | this.ensureDefault("upload_url", ""); |
|---|
| 87 | this.ensureDefault("file_post_name", "Filedata"); |
|---|
| 88 | this.ensureDefault("post_params", {}); |
|---|
| 89 | this.ensureDefault("use_query_string", false); |
|---|
| 90 | this.ensureDefault("requeue_on_error", false); |
|---|
| 91 | |
|---|
| 92 | // File Settings |
|---|
| 93 | this.ensureDefault("file_types", "*.*"); |
|---|
| 94 | this.ensureDefault("file_types_description", "All Files"); |
|---|
| 95 | this.ensureDefault("file_size_limit", 0); // Default zero means "unlimited" |
|---|
| 96 | this.ensureDefault("file_upload_limit", 0); |
|---|
| 97 | this.ensureDefault("file_queue_limit", 0); |
|---|
| 98 | |
|---|
| 99 | // Flash Settings |
|---|
| 100 | this.ensureDefault("flash_url", "swfupload_f9.swf"); |
|---|
| 101 | this.ensureDefault("flash_color", "#FFFFFF"); |
|---|
| 102 | |
|---|
| 103 | // Debug Settings |
|---|
| 104 | this.ensureDefault("debug", false); |
|---|
| 105 | this.settings.debug_enabled = this.settings.debug; // Here to maintain v2 API |
|---|
| 106 | |
|---|
| 107 | // Event Handlers |
|---|
| 108 | this.settings.return_upload_start_handler = this.returnUploadStart; |
|---|
| 109 | this.ensureDefault("swfupload_loaded_handler", null); |
|---|
| 110 | this.ensureDefault("file_dialog_start_handler", null); |
|---|
| 111 | this.ensureDefault("file_queued_handler", null); |
|---|
| 112 | this.ensureDefault("file_queue_error_handler", null); |
|---|
| 113 | this.ensureDefault("file_dialog_complete_handler", null); |
|---|
| 114 | |
|---|
| 115 | this.ensureDefault("upload_start_handler", null); |
|---|
| 116 | this.ensureDefault("upload_progress_handler", null); |
|---|
| 117 | this.ensureDefault("upload_error_handler", null); |
|---|
| 118 | this.ensureDefault("upload_success_handler", null); |
|---|
| 119 | this.ensureDefault("upload_complete_handler", null); |
|---|
| 120 | |
|---|
| 121 | this.ensureDefault("debug_handler", this.debugMessage); |
|---|
| 122 | |
|---|
| 123 | this.ensureDefault("custom_settings", {}); |
|---|
| 124 | |
|---|
| 125 | // Other settings |
|---|
| 126 | this.customSettings = this.settings.custom_settings; |
|---|
| 127 | |
|---|
| 128 | delete this.ensureDefault; |
|---|
| 129 | }; |
|---|
| 130 | |
|---|
| 131 | // Private: loadFlash generates the HTML tag for the Flash |
|---|
| 132 | // It then adds the flash to the body |
|---|
| 133 | SWFUpload.prototype.loadFlash = function () { |
|---|
| 134 | var targetElement, container; |
|---|
| 135 | |
|---|
| 136 | // Make sure an element with the ID we are going to use doesn't already exist |
|---|
| 137 | if (document.getElementById(this.movieName) !== null) { |
|---|
| 138 | throw "ID " + this.movieName + " is already in use. The Flash Object could not be added"; |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | // Get the body tag where we will be adding the flash movie |
|---|
| 142 | targetElement = document.getElementsByTagName("body")[0]; |
|---|
| 143 | |
|---|
| 144 | if (targetElement == undefined) { |
|---|
| 145 | throw "Could not find the 'body' element."; |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | // Append the container and load the flash |
|---|
| 149 | container = document.createElement("div"); |
|---|
| 150 | container.style.width = "1px"; |
|---|
| 151 | container.style.height = "1px"; |
|---|
| 152 | |
|---|
| 153 | targetElement.appendChild(container); |
|---|
| 154 | container.innerHTML = this.getFlashHTML(); // Using innerHTML is non-standard but the only sensible way to dynamically add Flash in IE (and maybe other browsers) |
|---|
| 155 | }; |
|---|
| 156 | |
|---|
| 157 | // Private: getFlashHTML generates the object tag needed to embed the flash in to the document |
|---|
| 158 | SWFUpload.prototype.getFlashHTML = function () { |
|---|
| 159 | // Flash Satay object syntax: http://www.alistapart.com/articles/flashsatay |
|---|
| 160 | return ['<object id="', this.movieName, '" type="application/x-shockwave-flash" data="', this.settings.flash_url, '" width="1" height="1" style="-moz-user-focus: ignore;">', |
|---|
| 161 | '<param name="movie" value="', this.settings.flash_url, '" />', |
|---|
| 162 | '<param name="bgcolor" value="', this.settings.flash_color, '" />', |
|---|
| 163 | '<param name="quality" value="high" />', |
|---|
| 164 | '<param name="menu" value="false" />', |
|---|
| 165 | '<param name="allowScriptAccess" value="always" />', |
|---|
| 166 | '<param name="flashvars" value="' + this.getFlashVars() + '" />', |
|---|
| 167 | '</object>'].join(""); |
|---|
| 168 | }; |
|---|
| 169 | |
|---|
| 170 | // Private: getFlashVars builds the parameter string that will be passed |
|---|
| 171 | // to flash in the flashvars param. |
|---|
| 172 | SWFUpload.prototype.getFlashVars = function () { |
|---|
| 173 | // Build a string from the post param object |
|---|
| 174 | var paramString = this.buildParamString(); |
|---|
| 175 | |
|---|
| 176 | // Build the parameter string |
|---|
| 177 | return ["movieName=", encodeURIComponent(this.movieName), |
|---|
| 178 | "&uploadURL=", encodeURIComponent(this.settings.upload_url), |
|---|
| 179 | "&useQueryString=", encodeURIComponent(this.settings.use_query_string), |
|---|
| 180 | "&requeueOnError=", encodeURIComponent(this.settings.requeue_on_error), |
|---|
| 181 | "&params=", encodeURIComponent(paramString), |
|---|
| 182 | "&filePostName=", encodeURIComponent(this.settings.file_post_name), |
|---|
| 183 | "&fileTypes=", encodeURIComponent(this.settings.file_types), |
|---|
| 184 | "&fileTypesDescription=", encodeURIComponent(this.settings.file_types_description), |
|---|
| 185 | "&fileSizeLimit=", encodeURIComponent(this.settings.file_size_limit), |
|---|
| 186 | "&fileUploadLimit=", encodeURIComponent(this.settings.file_upload_limit), |
|---|
| 187 | "&fileQueueLimit=", encodeURIComponent(this.settings.file_queue_limit), |
|---|
| 188 | "&debugEnabled=", encodeURIComponent(this.settings.debug_enabled)].join(""); |
|---|
| 189 | }; |
|---|
| 190 | |
|---|
| 191 | // Public: getMovieElement retrieves the DOM reference to the Flash element added by SWFUpload |
|---|
| 192 | // The element is cached after the first lookup |
|---|
| 193 | SWFUpload.prototype.getMovieElement = function () { |
|---|
| 194 | if (this.movieElement == undefined) { |
|---|
| 195 | this.movieElement = document.getElementById(this.movieName); |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | if (this.movieElement === null) { |
|---|
| 199 | throw "Could not find Flash element"; |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | return this.movieElement; |
|---|
| 203 | }; |
|---|
| 204 | |
|---|
| 205 | // Private: buildParamString takes the name/value pairs in the post_params setting object |
|---|
| 206 | // and joins them up in to a string formatted "name=value&name=value" |
|---|
| 207 | SWFUpload.prototype.buildParamString = function () { |
|---|
| 208 | var postParams = this.settings.post_params; |
|---|
| 209 | var paramStringPairs = []; |
|---|
| 210 | |
|---|
| 211 | if (typeof(postParams) === "object") { |
|---|
| 212 | for (var name in postParams) { |
|---|
| 213 | if (postParams.hasOwnProperty(name)) { |
|---|
| 214 | paramStringPairs.push(encodeURIComponent(name.toString()) + "=" + encodeURIComponent(postParams[name].toString())); |
|---|
| 215 | } |
|---|
| 216 | } |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | return paramStringPairs.join("&"); |
|---|
| 220 | }; |
|---|
| 221 | |
|---|
| 222 | // Public: Used to remove a SWFUpload instance from the page. This method strives to remove |
|---|
| 223 | // all references to the SWF, and other objects so memory is properly freed. |
|---|
| 224 | // Returns true if everything was destroyed. Returns a false if a failure occurs leaving SWFUpload in an inconsistant state. |
|---|
| 225 | SWFUpload.prototype.destroy = function () { |
|---|
| 226 | try { |
|---|
| 227 | // Make sure Flash is done before we try to remove it |
|---|
| 228 | this.stopUpload(); |
|---|
| 229 | |
|---|
| 230 | // Remove the SWFUpload DOM nodes |
|---|
| 231 | var movieElement = null; |
|---|
| 232 | try { |
|---|
| 233 | movieElement = this.getMovieElement(); |
|---|
| 234 | } catch (ex) { |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | if (movieElement != undefined && movieElement.parentNode != undefined && typeof(movieElement.parentNode.removeChild) === "function") { |
|---|
| 238 | var container = movieElement.parentNode; |
|---|
| 239 | if (container != undefined) { |
|---|
| 240 | container.removeChild(movieElement); |
|---|
| 241 | if (container.parentNode != undefined && typeof(container.parentNode.removeChild) === "function") { |
|---|
| 242 | container.parentNode.removeChild(container); |
|---|
| 243 | } |
|---|
| 244 | } |
|---|
| 245 | } |
|---|
| 246 | |
|---|
| 247 | // Destroy references |
|---|
| 248 | SWFUpload.instances[this.movieName] = null; |
|---|
| 249 | delete SWFUpload.instances[this.movieName]; |
|---|
| 250 | |
|---|
| 251 | delete this.movieElement; |
|---|
| 252 | delete this.settings; |
|---|
| 253 | delete this.customSettings; |
|---|
| 254 | delete this.eventQueue; |
|---|
| 255 | delete this.movieName; |
|---|
| 256 | |
|---|
| 257 | return true; |
|---|
| 258 | } catch (ex1) { |
|---|
| 259 | return false; |
|---|
| 260 | } |
|---|
| 261 | }; |
|---|
| 262 | |
|---|
| 263 | // Public: displayDebugInfo prints out settings and configuration |
|---|
| 264 | // information about this SWFUpload instance. |
|---|
| 265 | // This function (and any references to it) can be deleted when placing |
|---|
| 266 | // SWFUpload in production. |
|---|
| 267 | SWFUpload.prototype.displayDebugInfo = function () { |
|---|
| 268 | this.debug( |
|---|
| 269 | [ |
|---|
| 270 | "---SWFUpload Instance Info---\n", |
|---|
| 271 | "Version: ", SWFUpload.version, "\n", |
|---|
| 272 | "Movie Name: ", this.movieName, "\n", |
|---|
| 273 | "Settings:\n", |
|---|
| 274 | "\t", "upload_url: ", this.settings.upload_url, "\n", |
|---|
| 275 | "\t", "use_query_string: ", this.settings.use_query_string.toString(), "\n", |
|---|
| 276 | "\t", "file_post_name: ", this.settings.file_post_name, "\n", |
|---|
| 277 | "\t", "post_params: ", this.settings.post_params.toString(), "\n", |
|---|
| 278 | "\t", "file_types: ", this.settings.file_types, "\n", |
|---|
| 279 | "\t", "file_types_description: ", this.settings.file_types_description, "\n", |
|---|
| 280 | "\t", "file_size_limit: ", this.settings.file_size_limit, "\n", |
|---|
| 281 | "\t", "file_upload_limit: ", this.settings.file_upload_limit, "\n", |
|---|
| 282 | "\t", "file_queue_limit: ", this.settings.file_queue_limit, "\n", |
|---|
| 283 | "\t", "flash_url: ", this.settings.flash_url, "\n", |
|---|
| 284 | "\t", "flash_color: ", this.settings.flash_color, "\n", |
|---|
| 285 | "\t", "debug: ", this.settings.debug.toString(), "\n", |
|---|
| 286 | "\t", "custom_settings: ", this.settings.custom_settings.toString(), "\n", |
|---|
| 287 | "Event Handlers:\n", |
|---|
| 288 | "\t", "swfupload_loaded_handler assigned: ", (typeof(this.settings.swfupload_loaded_handler) === "function").toString(), "\n", |
|---|
| 289 | "\t", "file_dialog_start_handler assigned: ", (typeof(this.settings.file_dialog_start_handler) === "function").toString(), "\n", |
|---|
| 290 | "\t", "file_queued_handler assigned: ", (typeof(this.settings.file_queued_handler) === "function").toString(), "\n", |
|---|
| 291 | "\t", "file_queue_error_handler assigned: ", (typeof(this.settings.file_queue_error_handler) === "function").toString(), "\n", |
|---|
| 292 | "\t", "upload_start_handler assigned: ", (typeof(this.settings.upload_start_handler) === "function").toString(), "\n", |
|---|
| 293 | "\t", "upload_progress_handler assigned: ", (typeof(this.settings.upload_progress_handler) === "function").toString(), "\n", |
|---|
| 294 | "\t", "upload_error_handler assigned: ", (typeof(this.settings.upload_error_handler) === "function").toString(), "\n", |
|---|
| 295 | "\t", "upload_success_handler assigned: ", (typeof(this.settings.upload_success_handler) === "function").toString(), "\n", |
|---|
| 296 | "\t", "upload_complete_handler assigned: ", (typeof(this.settings.upload_complete_handler) === "function").toString(), "\n", |
|---|
| 297 | "\t", "debug_handler assigned: ", (typeof(this.settings.debug_handler) === "function").toString(), "\n" |
|---|
| 298 | ].join("") |
|---|
| 299 | ); |
|---|
| 300 | }; |
|---|
| 301 | |
|---|
| 302 | /* Note: addSetting and getSetting are no longer used by SWFUpload but are included |
|---|
| 303 | the maintain v2 API compatibility |
|---|
| 304 | */ |
|---|
| 305 | // Public: (Deprecated) addSetting adds a setting value. If the value given is undefined or null then the default_value is used. |
|---|
| 306 | SWFUpload.prototype.addSetting = function (name, value, default_value) { |
|---|
| 307 | if (value == undefined) { |
|---|
| 308 | return (this.settings[name] = default_value); |
|---|
| 309 | } else { |
|---|
| 310 | return (this.settings[name] = value); |
|---|
| 311 | } |
|---|
| 312 | }; |
|---|
| 313 | |
|---|
| 314 | // Public: (Deprecated) getSetting gets a setting. Returns an empty string if the setting was not found. |
|---|
| 315 | SWFUpload.prototype.getSetting = function (name) { |
|---|
| 316 | if (this.settings[name] != undefined) { |
|---|
| 317 | return this.settings[name]; |
|---|
| 318 | } |
|---|
| 319 | |
|---|
| 320 | return ""; |
|---|
| 321 | }; |
|---|
| 322 | |
|---|
| 323 | |
|---|
| 324 | |
|---|
| 325 | // Private: callFlash handles function calls made to the Flash element. |
|---|
| 326 | // Calls are made with a setTimeout for some functions to work around |
|---|
| 327 | // bugs in the ExternalInterface library. |
|---|
| 328 | SWFUpload.prototype.callFlash = function (functionName, argumentArray) { |
|---|
| 329 | argumentArray = argumentArray || []; |
|---|
| 330 | |
|---|
| 331 | var self = this; |
|---|
| 332 | var callFunction = function () { |
|---|
| 333 | var movieElement = self.getMovieElement(); |
|---|
| 334 | var returnValue; |
|---|
| 335 | if (typeof(movieElement[functionName]) === "function") { |
|---|
| 336 | // We have to go through all this if/else stuff because the Flash functions don't have apply() and only accept the exact number of arguments. |
|---|
| 337 | if (argumentArray.length === 0) { |
|---|
| 338 | returnValue = movieElement[functionName](); |
|---|
| 339 | } else if (argumentArray.length === 1) { |
|---|
| 340 | returnValue = movieElement[functionName](argumentArray[0]); |
|---|
| 341 | } else if (argumentArray.length === 2) { |
|---|
| 342 | returnValue = movieElement[functionName](argumentArray[0], argumentArray[1]); |
|---|
| 343 | } else if (argumentArray.length === 3) { |
|---|
| 344 | returnValue = movieElement[functionName](argumentArray[0], argumentArray[1], argumentArray[2]); |
|---|
| 345 | } else { |
|---|
| 346 | throw "Too many arguments"; |
|---|
| 347 | } |
|---|
| 348 | |
|---|
| 349 | // Unescape file post param values |
|---|
| 350 | if (returnValue != undefined && typeof(returnValue.post) === "object") { |
|---|
| 351 | returnValue = self.unescapeFilePostParams(returnValue); |
|---|
| 352 | } |
|---|
| 353 | |
|---|
| 354 | return returnValue; |
|---|
| 355 | } else { |
|---|
| 356 | throw "Invalid function name"; |
|---|
| 357 | } |
|---|
| 358 | }; |
|---|
| 359 | |
|---|
| 360 | return callFunction(); |
|---|
| 361 | }; |
|---|
| 362 | |
|---|
| 363 | |
|---|
| 364 | /* ***************************** |
|---|
| 365 | -- Flash control methods -- |
|---|
| 366 | Your UI should use these |
|---|
| 367 | to operate SWFUpload |
|---|
| 368 | ***************************** */ |
|---|
| 369 | |
|---|
| 370 | // Public: selectFile causes a File Selection Dialog window to appear. This |
|---|
| 371 | // dialog only allows 1 file to be selected. |
|---|
| 372 | SWFUpload.prototype.selectFile = function () { |
|---|
| 373 | this.callFlash("SelectFile"); |
|---|
| 374 | }; |
|---|
| 375 | |
|---|
| 376 | // Public: selectFiles causes a File Selection Dialog window to appear/ This |
|---|
| 377 | // dialog allows the user to select any number of files |
|---|
| 378 | // Flash Bug Warning: Flash limits the number of selectable files based on the combined length of the file names. |
|---|
| 379 | // If the selection name length is too long the dialog will fail in an unpredictable manner. There is no work-around |
|---|
| 380 | // for this bug. |
|---|
| 381 | SWFUpload.prototype.selectFiles = function () { |
|---|
| 382 | this.callFlash("SelectFiles"); |
|---|
| 383 | }; |
|---|
| 384 | |
|---|
| 385 | |
|---|
| 386 | // Public: startUpload starts uploading the first file in the queue unless |
|---|
| 387 | // the optional parameter 'fileID' specifies the ID |
|---|
| 388 | SWFUpload.prototype.startUpload = function (fileID) { |
|---|
| 389 | this.callFlash("StartUpload", [fileID]); |
|---|
| 390 | }; |
|---|
| 391 | |
|---|
| 392 | /* Cancels a the file upload. You must specify a file_id */ |
|---|
| 393 | // Public: cancelUpload cancels any queued file. The fileID parameter |
|---|
| 394 | // must be specified. |
|---|
| 395 | SWFUpload.prototype.cancelUpload = function (fileID) { |
|---|
| 396 | this.callFlash("CancelUpload", [fileID]); |
|---|
| 397 | }; |
|---|
| 398 | |
|---|
| 399 | // Public: stopUpload stops the current upload and requeues the file at the beginning of the queue. |
|---|
| 400 | // If nothing is currently uploading then nothing happens. |
|---|
| 401 | SWFUpload.prototype.stopUpload = function () { |
|---|
| 402 | this.callFlash("StopUpload"); |
|---|
| 403 | }; |
|---|
| 404 | |
|---|
| 405 | /* ************************ |
|---|
| 406 | * Settings methods |
|---|
| 407 | * These methods change the SWFUpload settings. |
|---|
| 408 | * SWFUpload settings should not be changed directly on the settings object |
|---|
| 409 | * since many of the settings need to be passed to Flash in order to take |
|---|
| 410 | * effect. |
|---|
| 411 | * *********************** */ |
|---|
| 412 | |
|---|
| 413 | // Public: getStats gets the file statistics object. |
|---|
| 414 | SWFUpload.prototype.getStats = function () { |
|---|
| 415 | return this.callFlash("GetStats"); |
|---|
| 416 | }; |
|---|
| 417 | |
|---|
| 418 | // Public: setStats changes the SWFUpload statistics. You shouldn't need to |
|---|
| 419 | // change the statistics but you can. Changing the statistics does not |
|---|
| 420 | // affect SWFUpload accept for the successful_uploads count which is used |
|---|
| 421 | // by the upload_limit setting to determine how many files the user may upload. |
|---|
| 422 | SWFUpload.prototype.setStats = function (statsObject) { |
|---|
| 423 | this.callFlash("SetStats", [statsObject]); |
|---|
| 424 | }; |
|---|
| 425 | |
|---|
| 426 | // Public: getFile retrieves a File object by ID or Index. If the file is |
|---|
| 427 | // not found then 'null' is returned. |
|---|
| 428 | SWFUpload.prototype.getFile = function (fileID) { |
|---|
| 429 | if (typeof(fileID) === "number") { |
|---|
| 430 | return this.callFlash("GetFileByIndex", [fileID]); |
|---|
| 431 | } else { |
|---|
| 432 | return this.callFlash("GetFile", [fileID]); |
|---|
| 433 | } |
|---|
| 434 | }; |
|---|
| 435 | |
|---|
| 436 | // Public: addFileParam sets a name/value pair that will be posted with the |
|---|
| 437 | // file specified by the Files ID. If the name already exists then the |
|---|
| 438 | // exiting value will be overwritten. |
|---|
| 439 | SWFUpload.prototype.addFileParam = function (fileID, name, value) { |
|---|
| 440 | return this.callFlash("AddFileParam", [fileID, name, value]); |
|---|
| 441 | }; |
|---|
| 442 | |
|---|
| 443 | // Public: removeFileParam removes a previously set (by addFileParam) name/value |
|---|
| 444 | // pair from the specified file. |
|---|
| 445 | SWFUpload.prototype.removeFileParam = function (fileID, name) { |
|---|
| 446 | this.callFlash("RemoveFileParam", [fileID, name]); |
|---|
| 447 | }; |
|---|
| 448 | |
|---|
| 449 | // Public: setUploadUrl changes the upload_url setting. |
|---|
| 450 | SWFUpload.prototype.setUploadURL = function (url) { |
|---|
| 451 | this.settings.upload_url = url.toString(); |
|---|
| 452 | this.callFlash("SetUploadURL", [url]); |
|---|
| 453 | }; |
|---|
| 454 | |
|---|
| 455 | // Public: setPostParams changes the post_params setting |
|---|
| 456 | SWFUpload.prototype.setPostParams = function (paramsObject) { |
|---|
| 457 | this.settings.post_params = paramsObject; |
|---|
| 458 | this.callFlash("SetPostParams", [paramsObject]); |
|---|
| 459 | }; |
|---|
| 460 | |
|---|
| 461 | // Public: addPostParam adds post name/value pair. Each name can have only one value. |
|---|
| 462 | SWFUpload.prototype.addPostParam = function (name, value) { |
|---|
| 463 | this.settings.post_params[name] = value; |
|---|
| 464 | this.callFlash("SetPostParams", [this.settings.post_params]); |
|---|
| 465 | }; |
|---|
| 466 | |
|---|
| 467 | // Public: removePostParam deletes post name/value pair. |
|---|
| 468 | SWFUpload.prototype.removePostParam = function (name) { |
|---|
| 469 | delete this.settings.post_params[name]; |
|---|
| 470 | this.callFlash("SetPostParams", [this.settings.post_params]); |
|---|
| 471 | }; |
|---|
| 472 | |
|---|
| 473 | // Public: setFileTypes changes the file_types setting and the file_types_description setting |
|---|
| 474 | SWFUpload.prototype.setFileTypes = function (types, description) { |
|---|
| 475 | this.settings.file_types = types; |
|---|
| 476 | this.settings.file_types_description = description; |
|---|
| 477 | this.callFlash("SetFileTypes", [types, description]); |
|---|
| 478 | }; |
|---|
| 479 | |
|---|
| 480 | // Public: setFileSizeLimit changes the file_size_limit setting |
|---|
| 481 | SWFUpload.prototype.setFileSizeLimit = function (fileSizeLimit) { |
|---|
| 482 | this.settings.file_size_limit = fileSizeLimit; |
|---|
| 483 | this.callFlash("SetFileSizeLimit", [fileSizeLimit]); |
|---|
| 484 | }; |
|---|
| 485 | |
|---|
| 486 | // Public: setFileUploadLimit changes the file_upload_limit setting |
|---|
| 487 | SWFUpload.prototype.setFileUploadLimit = function (fileUploadLimit) { |
|---|
| 488 | this.settings.file_upload_limit = fileUploadLimit; |
|---|
| 489 | this.callFlash("SetFileUploadLimit", [fileUploadLimit]); |
|---|
| 490 | }; |
|---|
| 491 | |
|---|
| 492 | // Public: setFileQueueLimit changes the file_queue_limit setting |
|---|
| 493 | SWFUpload.prototype.setFileQueueLimit = function (fileQueueLimit) { |
|---|
| 494 | this.settings.file_queue_limit = fileQueueLimit; |
|---|
| 495 | this.callFlash("SetFileQueueLimit", [fileQueueLimit]); |
|---|
| 496 | }; |
|---|
| 497 | |
|---|
| 498 | // Public: setFilePostName changes the file_post_name setting |
|---|
| 499 | SWFUpload.prototype.setFilePostName = function (filePostName) { |
|---|
| 500 | this.settings.file_post_name = filePostName; |
|---|
| 501 | this.callFlash("SetFilePostName", [filePostName]); |
|---|
| 502 | }; |
|---|
| 503 | |
|---|
| 504 | // Public: setUseQueryString changes the use_query_string setting |
|---|
| 505 | SWFUpload.prototype.setUseQueryString = function (useQueryString) { |
|---|
| 506 | this.settings.use_query_string = useQueryString; |
|---|
| 507 | this.callFlash("SetUseQueryString", [useQueryString]); |
|---|
| 508 | }; |
|---|
| 509 | |
|---|
| 510 | // Public: setRequeueOnError changes the requeue_on_error setting |
|---|
| 511 | SWFUpload.prototype.setRequeueOnError = function (requeueOnError) { |
|---|
| 512 | this.settings.requeue_on_error = requeueOnError; |
|---|
| 513 | this.callFlash("SetRequeueOnError", [requeueOnError]); |
|---|
| 514 | }; |
|---|
| 515 | |
|---|
| 516 | // Public: setDebugEnabled changes the debug_enabled setting |
|---|
| 517 | SWFUpload.prototype.setDebugEnabled = function (debugEnabled) { |
|---|
| 518 | this.settings.debug_enabled = debugEnabled; |
|---|
| 519 | this.callFlash("SetDebugEnabled", [debugEnabled]); |
|---|
| 520 | }; |
|---|
| 521 | |
|---|
| 522 | |
|---|
| 523 | /* ******************************* |
|---|
| 524 | Flash Event Interfaces |
|---|
| 525 | These functions are used by Flash to trigger the various |
|---|
| 526 | events. |
|---|
| 527 | |
|---|
| 528 | All these functions a Private. |
|---|
| 529 | |
|---|
| 530 | Because the ExternalInterface library is buggy the event calls |
|---|
| 531 | are added to a queue and the queue then executed by a setTimeout. |
|---|
| 532 | This ensures that events are executed in a determinate order and that |
|---|
| 533 | the ExternalInterface bugs are avoided. |
|---|
| 534 | ******************************* */ |
|---|
| 535 | |
|---|
| 536 | SWFUpload.prototype.queueEvent = function (handlerName, argumentArray) { |
|---|
| 537 | // Warning: Don't call this.debug inside here or you'll create an infinite loop |
|---|
| 538 | |
|---|
| 539 | if (argumentArray == undefined) { |
|---|
| 540 | argumentArray = []; |
|---|
| 541 | } else if (!(argumentArray instanceof Array)) { |
|---|
| 542 | argumentArray = [argumentArray]; |
|---|
| 543 | } |
|---|
| 544 | |
|---|
| 545 | var self = this; |
|---|
| 546 | if (typeof(this.settings[handlerName]) === "function") { |
|---|
| 547 | // Queue the event |
|---|
| 548 | this.eventQueue.push(function () { |
|---|
| 549 | this.settings[handlerName].apply(this, argumentArray); |
|---|
| 550 | }); |
|---|
| 551 | |
|---|
| 552 | // Execute the next queued event |
|---|
| 553 | setTimeout(function () { |
|---|
| 554 | self.executeNextEvent(); |
|---|
| 555 | }, 0); |
|---|
| 556 | |
|---|
| 557 | } else if (this.settings[handlerName] !== null) { |
|---|
| 558 | throw "Event handler " + handlerName + " is unknown or is not a function"; |
|---|
| 559 | } |
|---|
| 560 | }; |
|---|
| 561 | |
|---|
| 562 | // Private: Causes the next event in the queue to be executed. Since events are queued using a setTimeout |
|---|
| 563 | // we must queue them in order to garentee that they are executed in order. |
|---|
| 564 | SWFUpload.prototype.executeNextEvent = function () { |
|---|
| 565 | // Warning: Don't call this.debug inside here or you'll create an infinite loop |
|---|
| 566 | |
|---|
| 567 | var f = this.eventQueue ? this.eventQueue.shift() : null; |
|---|
| 568 | if (typeof(f) === "function") { |
|---|
| 569 | f.apply(this); |
|---|
| 570 | } |
|---|
| 571 | }; |
|---|
| 572 | |
|---|
| 573 | // Private: unescapeFileParams is part of a workaround for a flash bug where objects passed through ExternalInterfance cannot have |
|---|
| 574 | // properties that contain characters that are not valid for JavaScript identifiers. To work around this |
|---|
| 575 | // the Flash Component escapes the parameter names and we must unescape again before passing them along. |
|---|
| 576 | SWFUpload.prototype.unescapeFilePostParams = function (file) { |
|---|
| 577 | var reg = /[$]([0-9a-f]{4})/i; |
|---|
| 578 | var unescapedPost = {}; |
|---|
| 579 | var uk; |
|---|
| 580 | |
|---|
| 581 | if (file != undefined) { |
|---|
| 582 | for (var k in file.post) { |
|---|
| 583 | if (file.post.hasOwnProperty(k)) { |
|---|
| 584 | uk = k; |
|---|
| 585 | var match; |
|---|
| 586 | while ((match = reg.exec(uk)) !== null) { |
|---|
| 587 | uk = uk.replace(match[0], String.fromCharCode(parseInt("0x"+match[1], 16))); |
|---|
| 588 | } |
|---|
| 589 | unescapedPost[uk] = file.post[k]; |
|---|
| 590 | } |
|---|
| 591 | } |
|---|
| 592 | |
|---|
| 593 | file.post = unescapedPost; |
|---|
| 594 | } |
|---|
| 595 | |
|---|
| 596 | return file; |
|---|
| 597 | }; |
|---|
| 598 | |
|---|
| 599 | SWFUpload.prototype.flashReady = function () { |
|---|
| 600 | // Check that the movie element is loaded correctly with its ExternalInterface methods defined |
|---|
| 601 | var movieElement = this.getMovieElement(); |
|---|
| 602 | if (typeof(movieElement.StartUpload) !== "function") { |
|---|
| 603 | throw "ExternalInterface methods failed to initialize."; |
|---|
| 604 | } |
|---|
| 605 | |
|---|
| 606 | this.queueEvent("swfupload_loaded_handler"); |
|---|
| 607 | }; |
|---|
| 608 | |
|---|
| 609 | |
|---|
| 610 | /* This is a chance to do something before the browse window opens */ |
|---|
| 611 | SWFUpload.prototype.fileDialogStart = function () { |
|---|
| 612 | this.queueEvent("file_dialog_start_handler"); |
|---|
| 613 | }; |
|---|
| 614 | |
|---|
| 615 | |
|---|
| 616 | /* Called when a file is successfully added to the queue. */ |
|---|
| 617 | SWFUpload.prototype.fileQueued = function (file) { |
|---|
| 618 | file = this.unescapeFilePostParams(file); |
|---|
| 619 | this.queueEvent("file_queued_handler", file); |
|---|
| 620 | }; |
|---|
| 621 | |
|---|
| 622 | |
|---|
| 623 | /* Handle errors that occur when an attempt to queue a file fails. */ |
|---|
| 624 | SWFUpload.prototype.fileQueueError = function (file, errorCode, message) { |
|---|
| 625 | file = this.unescapeFilePostParams(file); |
|---|
| 626 | this.queueEvent("file_queue_error_handler", [file, errorCode, message]); |
|---|
| 627 | }; |
|---|
| 628 | |
|---|
| 629 | /* Called after the file dialog has closed and the selected files have been queued. |
|---|
| 630 | You could call startUpload here if you want the queued files to begin uploading immediately. */ |
|---|
| 631 | SWFUpload.prototype.fileDialogComplete = function (numFilesSelected, numFilesQueued) { |
|---|
| 632 | this.queueEvent("file_dialog_complete_handler", [numFilesSelected, numFilesQueued]); |
|---|
| 633 | }; |
|---|
| 634 | |
|---|
| 635 | SWFUpload.prototype.uploadStart = function (file) { |
|---|
| 636 | file = this.unescapeFilePostParams(file); |
|---|
| 637 | this.queueEvent("return_upload_start_handler", file); |
|---|
| 638 | }; |
|---|
| 639 | |
|---|
| 640 | SWFUpload.prototype.returnUploadStart = function (file) { |
|---|
| 641 | var returnValue; |
|---|
| 642 | if (typeof(this.settings.upload_start_handler) === "function") { |
|---|
| 643 | file = this.unescapeFilePostParams(file); |
|---|
| 644 | returnValue = this.settings.upload_start_handler.call(this, file); |
|---|
| 645 | } else if (this.settings.upload_start_handler != undefined) { |
|---|
| 646 | throw "upload_start_handler must be a function"; |
|---|
| 647 | } |
|---|
| 648 | |
|---|
| 649 | // Convert undefined to true so if nothing is returned from the upload_start_handler it is |
|---|
| 650 | // interpretted as 'true'. |
|---|
| 651 | if (returnValue === undefined) { |
|---|
| 652 | returnValue = true; |
|---|
| 653 | } |
|---|
| 654 | |
|---|
| 655 | returnValue = !!returnValue; |
|---|
| 656 | |
|---|
| 657 | this.callFlash("ReturnUploadStart", [returnValue]); |
|---|
| 658 | }; |
|---|
| 659 | |
|---|
| 660 | |
|---|
| 661 | |
|---|
| 662 | SWFUpload.prototype.uploadProgress = function (file, bytesComplete, bytesTotal) { |
|---|
| 663 | file = this.unescapeFilePostParams(file); |
|---|
| 664 | this.queueEvent("upload_progress_handler", [file, bytesComplete, bytesTotal]); |
|---|
| 665 | }; |
|---|
| 666 | |
|---|
| 667 | SWFUpload.prototype.uploadError = function (file, errorCode, message) { |
|---|
| 668 | file = this.unescapeFilePostParams(file); |
|---|
| 669 | this.queueEvent("upload_error_handler", [file, errorCode, message]); |
|---|
| 670 | }; |
|---|
| 671 | |
|---|
| 672 | SWFUpload.prototype.uploadSuccess = function (file, serverData) { |
|---|
| 673 | file = this.unescapeFilePostParams(file); |
|---|
| 674 | this.queueEvent("upload_success_handler", [file, serverData]); |
|---|
| 675 | }; |
|---|
| 676 | |
|---|
| 677 | SWFUpload.prototype.uploadComplete = function (file) { |
|---|
| 678 | file = this.unescapeFilePostParams(file); |
|---|
| 679 | this.queueEvent("upload_complete_handler", file); |
|---|
| 680 | }; |
|---|
| 681 | |
|---|
| 682 | /* Called by SWFUpload JavaScript and Flash functions when debug is enabled. By default it writes messages to the |
|---|
| 683 | internal debug console. You can override this event and have messages written where you want. */ |
|---|
| 684 | SWFUpload.prototype.debug = function (message) { |
|---|
| 685 | this.queueEvent("debug_handler", message); |
|---|
| 686 | }; |
|---|
| 687 | |
|---|
| 688 | |
|---|
| 689 | /* ********************************** |
|---|
| 690 | Debug Console |
|---|
| 691 | The debug console is a self contained, in page location |
|---|
| 692 | for debug message to be sent. The Debug Console adds |
|---|
| 693 | itself to the body if necessary. |
|---|
| 694 | |
|---|
| 695 | The console is automatically scrolled as messages appear. |
|---|
| 696 | |
|---|
| 697 | If you are using your own debug handler or when you deploy to production and |
|---|
| 698 | have debug disabled you can remove these functions to reduce the file size |
|---|
| 699 | and complexity. |
|---|
| 700 | ********************************** */ |
|---|
| 701 | |
|---|
| 702 | // Private: debugMessage is the default debug_handler. If you want to print debug messages |
|---|
| 703 | // call the debug() function. When overriding the function your own function should |
|---|
| 704 | // check to see if the debug setting is true before outputting debug information. |
|---|
| 705 | SWFUpload.prototype.debugMessage = function (message) { |
|---|
| 706 | if (this.settings.debug) { |
|---|
| 707 | var exceptionMessage, exceptionValues = []; |
|---|
| 708 | |
|---|
| 709 | // Check for an exception object and print it nicely |
|---|
| 710 | if (typeof(message) === "object" && typeof(message.name) === "string" && typeof(message.message) === "string") { |
|---|
| 711 | for (var key in message) { |
|---|
| 712 | if (message.hasOwnProperty(key)) { |
|---|
| 713 | exceptionValues.push(key + ": " + message[key]); |
|---|
| 714 | } |
|---|
| 715 | } |
|---|
| 716 | exceptionMessage = exceptionValues.join("\n") || ""; |
|---|
| 717 | exceptionValues = exceptionMessage.split("\n"); |
|---|
| 718 | exceptionMessage = "EXCEPTION: " + exceptionValues.join("\nEXCEPTION: "); |
|---|
| 719 | SWFUpload.Console.writeLine(exceptionMessage); |
|---|
| 720 | } else { |
|---|
| 721 | SWFUpload.Console.writeLine(message); |
|---|
| 722 | } |
|---|
| 723 | } |
|---|
| 724 | }; |
|---|
| 725 | |
|---|
| 726 | SWFUpload.Console = {}; |
|---|
| 727 | SWFUpload.Console.writeLine = function (message) { |
|---|
| 728 | var console, documentForm; |
|---|
| 729 | |
|---|
| 730 | try { |
|---|
| 731 | console = document.getElementById("SWFUpload_Console"); |
|---|
| 732 | |
|---|
| 733 | if (!console) { |
|---|
| 734 | documentForm = document.createElement("form"); |
|---|
| 735 | document.getElementsByTagName("body")[0].appendChild(documentForm); |
|---|
| 736 | |
|---|
| 737 | console = document.createElement("textarea"); |
|---|
| 738 | console.id = "SWFUpload_Console"; |
|---|
| 739 | console.style.fontFamily = "monospace"; |
|---|
| 740 | console.setAttribute("wrap", "off"); |
|---|
| 741 | console.wrap = "off"; |
|---|
| 742 | console.style.overflow = "auto"; |
|---|
| 743 | console.style.width = "700px"; |
|---|
| 744 | console.style.height = "350px"; |
|---|
| 745 | console.style.margin = "5px"; |
|---|
| 746 | documentForm.appendChild(console); |
|---|
| 747 | } |
|---|
| 748 | |
|---|
| 749 | console.value += message + "\n"; |
|---|
| 750 | |
|---|
| 751 | console.scrollTop = console.scrollHeight - console.clientHeight; |
|---|
| 752 | } catch (ex) { |
|---|
| 753 | alert("Exception: " + ex.name + " Message: " + ex.message); |
|---|
| 754 | } |
|---|
| 755 | }; |
|---|