PageRenderTime 55ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/patches/Servicios/sbin/www/swfupload.js

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