PageRenderTime 53ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/public/ext-3/docs/source/swfupload.html

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