PageRenderTime 65ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 1ms

/sop/rc1/Tpl/default/Public/Js/swfupload.js

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