PageRenderTime 87ms CodeModel.GetById 43ms RepoModel.GetById 3ms app.codeStats 1ms

/VivoProfileSearch/SWFUpload/v2.2b3/swfupload.js

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