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

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

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