PageRenderTime 23ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/js/src/not_implemented/swfupload.js

http://xtraupload.googlecode.com/
JavaScript | 1032 lines | 724 code | 126 blank | 182 comment | 122 complexity | d5cda46ec749d2cff814bd70cae9990c MD5 | raw file
Possible License(s): Apache-2.0
  1. /**
  2. * SWFUpload v2.0 by Jacob Roberts, Nov 2007, http://www.swfupload.org, http://linebyline.blogspot.com
  3. * -------- -------- -------- -------- -------- -------- -------- --------
  4. * SWFUpload is (c) 2006 Lars Huring and Mammon Media and is released under the MIT License:
  5. * http://www.opensource.org/licenses/mit-license.php
  6. *
  7. * See Changelog.txt for version history
  8. *
  9. * Development Notes:
  10. * * This version of SWFUpload requires Flash Player 9.0.28 and should autodetect the correct flash version.
  11. * * In Linux Flash Player 9 setting the post file variable name does not work. It is always set to "Filedata".
  12. * * There is a lot of repeated code that could be refactored to single functions. Feel free.
  13. * * It's dangerous to do "circular calls" between Flash and JavaScript. I've taken steps to try to work around issues
  14. * by having the event calls pipe through setTimeout. However you should still avoid calling in to Flash from
  15. * within the event handler methods. Especially the "startUpload" event since it cannot use the setTimeout hack.
  16. */
  17. /* *********** */
  18. /* Constructor */
  19. /* *********** */
  20. var SWFUpload = function (init_settings) {
  21. this.initSWFUpload(init_settings);
  22. };
  23. SWFUpload.prototype.initSWFUpload = function (init_settings) {
  24. // Remove background flicker in IE (read this: http://misterpixel.blogspot.com/2006/09/forensic-analysis-of-ie6.html)
  25. // This doesn't have anything to do with SWFUpload but can help your UI behave better in IE.
  26. try {
  27. document.execCommand('BackgroundImageCache', false, true);
  28. } catch (ex1) {
  29. }
  30. try {
  31. this.customSettings = {}; // A container where developers can place their own settings associated with this instance.
  32. this.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(init_settings);
  40. this.loadFlash();
  41. this.displayDebugInfo();
  42. } catch (ex2) {
  43. this.debug(ex2);
  44. }
  45. }
  46. /* *************** */
  47. /* Static thingies */
  48. /* *************** */
  49. SWFUpload.instances = {};
  50. SWFUpload.movieCount = 0;
  51. SWFUpload.QUEUE_ERROR = {
  52. QUEUE_LIMIT_EXCEEDED : -100,
  53. FILE_EXCEEDS_SIZE_LIMIT : -110,
  54. ZERO_BYTE_FILE : -120,
  55. INVALID_FILETYPE : -130
  56. };
  57. SWFUpload.UPLOAD_ERROR = {
  58. HTTP_ERROR : -200,
  59. MISSING_UPLOAD_URL : -210,
  60. IO_ERROR : -220,
  61. SECURITY_ERROR : -230,
  62. UPLOAD_LIMIT_EXCEEDED : -240,
  63. UPLOAD_FAILED : -250,
  64. SPECIFIED_FILE_ID_NOT_FOUND : -260,
  65. FILE_VALIDATION_FAILED : -270,
  66. FILE_CANCELLED : -280,
  67. UPLOAD_STOPPED : -290
  68. };
  69. SWFUpload.FILE_STATUS = {
  70. QUEUED : -1,
  71. IN_PROGRESS : -2,
  72. ERROR : -3,
  73. COMPLETE : -4,
  74. CANCELLED : -5
  75. };
  76. /* ***************** */
  77. /* Instance Thingies */
  78. /* ***************** */
  79. // init is a private method that ensures that all the object settings are set, getting a default value if one was not assigned.
  80. SWFUpload.prototype.initSettings = function (init_settings) {
  81. // Upload backend settings
  82. this.addSetting("upload_url", init_settings.upload_url, "");
  83. this.addSetting("file_post_name", init_settings.file_post_name, "Filedata");
  84. this.addSetting("post_params", init_settings.post_params, {});
  85. // File Settings
  86. this.addSetting("file_types", init_settings.file_types, "*.*");
  87. this.addSetting("file_types_description", init_settings.file_types_description, "All Files");
  88. this.addSetting("file_size_limit", init_settings.file_size_limit, "1024");
  89. this.addSetting("file_upload_limit", init_settings.file_upload_limit, "0");
  90. this.addSetting("file_queue_limit", init_settings.file_queue_limit, "0");
  91. // Flash Settings
  92. this.addSetting("flash_url", init_settings.flash_url, "swfupload.swf");
  93. this.addSetting("flash_width", init_settings.flash_width, "1px");
  94. this.addSetting("flash_height", init_settings.flash_height, "1px");
  95. this.addSetting("flash_color", init_settings.flash_color, "#FFFFFF");
  96. // Debug Settings
  97. this.addSetting("debug_enabled", init_settings.debug, false);
  98. // Event Handlers
  99. this.flashReady_handler = SWFUpload.flashReady; // This is a non-overrideable event handler
  100. this.swfUploadLoaded_handler = this.retrieveSetting(init_settings.swfupload_loaded_handler, SWFUpload.swfUploadLoaded);
  101. this.fileDialogStart_handler = this.retrieveSetting(init_settings.file_dialog_start_handler, SWFUpload.fileDialogStart);
  102. this.fileQueued_handler = this.retrieveSetting(init_settings.file_queued_handler, SWFUpload.fileQueued);
  103. this.fileQueueError_handler = this.retrieveSetting(init_settings.file_queue_error_handler, SWFUpload.fileQueueError);
  104. this.fileDialogComplete_handler = this.retrieveSetting(init_settings.file_dialog_complete_handler, SWFUpload.fileDialogComplete);
  105. this.uploadStart_handler = this.retrieveSetting(init_settings.upload_start_handler, SWFUpload.uploadStart);
  106. this.uploadProgress_handler = this.retrieveSetting(init_settings.upload_progress_handler, SWFUpload.uploadProgress);
  107. this.uploadError_handler = this.retrieveSetting(init_settings.upload_error_handler, SWFUpload.uploadError);
  108. this.uploadSuccess_handler = this.retrieveSetting(init_settings.upload_success_handler, SWFUpload.uploadSuccess);
  109. this.uploadComplete_handler = this.retrieveSetting(init_settings.upload_complete_handler, SWFUpload.uploadComplete);
  110. this.debug_handler = this.retrieveSetting(init_settings.debug_handler, SWFUpload.debug);
  111. // Other settings
  112. this.customSettings = this.retrieveSetting(init_settings.custom_settings, {});
  113. };
  114. // loadFlash is a private method that generates the HTML tag for the Flash
  115. // It then adds the flash to the "target" or to the body and stores a
  116. // reference to the flash element in "movieElement".
  117. SWFUpload.prototype.loadFlash = function () {
  118. var html, target_element, container;
  119. // Make sure an element with the ID we are going to use doesn't already exist
  120. if (document.getElementById(this.movieName) !== null) {
  121. return false;
  122. }
  123. // Get the body tag where we will be adding the flash movie
  124. try {
  125. target_element = document.getElementsByTagName("body")[0];
  126. if (typeof(target_element) === "undefined" || target_element === null) {
  127. this.debug('Could not find the BODY element. SWFUpload failed to load.');
  128. return false;
  129. }
  130. } catch (ex) {
  131. return false;
  132. }
  133. // Append the container and load the flash
  134. container = document.createElement("div");
  135. container.style.width = this.getSetting("flash_width");
  136. container.style.height = this.getSetting("flash_height");
  137. target_element.appendChild(container);
  138. container.innerHTML = this.getFlashHTML(); // Using innerHTML is non-standard but the only sensible way to dynamically add Flash in IE (and maybe other browsers)
  139. };
  140. // Generates the embed/object tags needed to embed the flash in to the document
  141. SWFUpload.prototype.getFlashHTML = function () {
  142. var html = "";
  143. // Create Mozilla Embed HTML
  144. if (navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length) {
  145. // Build the basic embed html
  146. html = '<embed type="application/x-shockwave-flash" src="' + this.getSetting("flash_url") + '" width="' + this.getSetting("flash_width") + '" height="' + this.getSetting("flash_height") + '"';
  147. html += ' id="' + this.movieName + '" name="' + this.movieName + '" ';
  148. html += 'bgcolor="' + this.getSetting("flash_color") + '" quality="high" menu="false" flashvars="';
  149. html += this.getFlashVars();
  150. html += '" />';
  151. // Create IE Object HTML
  152. } else {
  153. // Build the basic Object tag
  154. html = '<object id="' + this.movieName + '" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="' + this.getSetting("flash_width") + '" height="' + this.getSetting("flash_height") + '">';
  155. html += '<param name="movie" value="' + this.getSetting("flash_url") + '">';
  156. html += '<param name="bgcolor" value="' + this.getSetting("flash_color") + '" />';
  157. html += '<param name="quality" value="high" />';
  158. html += '<param name="menu" value="false" />';
  159. html += '<param name="flashvars" value="' + this.getFlashVars() + '" />';
  160. html += '</object>';
  161. }
  162. return html;
  163. };
  164. // This private method builds the parameter string that will be passed
  165. // to flash.
  166. SWFUpload.prototype.getFlashVars = function () {
  167. // Build a string from the post param object
  168. var param_string = this.buildParamString();
  169. // Build the parameter string
  170. var html = "";
  171. html += "movieName=" + encodeURIComponent(this.movieName);
  172. html += "&uploadURL=" + encodeURIComponent(this.getSetting("upload_url"));
  173. html += "&params=" + encodeURIComponent(param_string);
  174. html += "&filePostName=" + encodeURIComponent(this.getSetting("file_post_name"));
  175. html += "&fileTypes=" + encodeURIComponent(this.getSetting("file_types"));
  176. html += "&fileTypesDescription=" + encodeURIComponent(this.getSetting("file_types_description"));
  177. html += "&fileSizeLimit=" + encodeURIComponent(this.getSetting("file_size_limit"));
  178. html += "&fileUploadLimit=" + encodeURIComponent(this.getSetting("file_upload_limit"));
  179. html += "&fileQueueLimit=" + encodeURIComponent(this.getSetting("file_queue_limit"));
  180. html += "&debugEnabled=" + encodeURIComponent(this.getSetting("debug_enabled"));
  181. return html;
  182. };
  183. SWFUpload.prototype.getMovieElement = function () {
  184. if (typeof(this.movieElement) === "undefined" || this.movieElement === null) {
  185. this.movieElement = document.getElementById(this.movieName);
  186. // Fix IEs "Flash can't callback when in a form" issue (http://www.extremefx.com.ar/blog/fixing-flash-external-interface-inside-form-on-internet-explorer)
  187. // Removed because Revision 6 always adds the flash to the body (inside a containing div)
  188. // If you insist on adding the Flash file inside a Form then in IE you have to make you wait until the DOM is ready
  189. // and run this code to make the form's ID available from the window object so Flash and JavaScript can communicate.
  190. //if (typeof(window[this.movieName]) === "undefined" || window[this.moveName] !== this.movieElement) {
  191. // window[this.movieName] = this.movieElement;
  192. //}
  193. }
  194. return this.movieElement;
  195. };
  196. SWFUpload.prototype.buildParamString = function () {
  197. var post_params = this.getSetting("post_params");
  198. var param_string_pairs = [];
  199. var i, value, name;
  200. // Retrieve the user defined parameters
  201. if (typeof(post_params) === "object") {
  202. for (name in post_params) {
  203. if (post_params.hasOwnProperty(name)) {
  204. if (typeof(post_params[name]) === "string") {
  205. param_string_pairs.push(encodeURIComponent(name) + "=" + encodeURIComponent(post_params[name]));
  206. }
  207. }
  208. }
  209. }
  210. return param_string_pairs.join("&");
  211. };
  212. // Saves a setting. If the value given is undefined or null then the default_value is used.
  213. SWFUpload.prototype.addSetting = function (name, value, default_value) {
  214. if (typeof(value) === "undefined" || value === null) {
  215. this.settings[name] = default_value;
  216. } else {
  217. this.settings[name] = value;
  218. }
  219. return this.settings[name];
  220. };
  221. // Gets a setting. Returns empty string if not found.
  222. SWFUpload.prototype.getSetting = function (name) {
  223. if (typeof(this.settings[name]) === "undefined") {
  224. return "";
  225. } else {
  226. return this.settings[name];
  227. }
  228. };
  229. // Gets a setting, if the setting is undefined then return the default value
  230. // This does not affect or use the interal setting object.
  231. SWFUpload.prototype.retrieveSetting = function (value, default_value) {
  232. if (typeof(value) === "undefined" || value === null) {
  233. return default_value;
  234. } else {
  235. return value;
  236. }
  237. };
  238. // It loops through all the settings and displays
  239. // them in the debug Console.
  240. SWFUpload.prototype.displayDebugInfo = function () {
  241. var key, debug_message = "";
  242. debug_message += "----- SWFUPLOAD SETTINGS ----\nID: " + this.moveName + "\n";
  243. debug_message += this.outputObject(this.settings);
  244. debug_message += "----- SWFUPLOAD SETTINGS END ----\n";
  245. debug_message += "\n";
  246. this.debug(debug_message);
  247. };
  248. SWFUpload.prototype.outputObject = function (object, prefix) {
  249. var output = "", key;
  250. if (typeof(prefix) !== "string") {
  251. prefix = "";
  252. }
  253. if (typeof(object) !== "object") {
  254. return "";
  255. }
  256. for (key in object) {
  257. if (object.hasOwnProperty(key)) {
  258. if (typeof(object[key]) === "object") {
  259. output += (prefix + key + ": { \n" + this.outputObject(object[key], "\t" + prefix) + prefix + "}" + "\n");
  260. } else {
  261. output += (prefix + key + ": " + object[key] + "\n");
  262. }
  263. }
  264. }
  265. return output;
  266. };
  267. /* *****************************
  268. -- Flash control methods --
  269. Your UI should use these
  270. to operate SWFUpload
  271. ***************************** */
  272. SWFUpload.prototype.selectFile = function () {
  273. var movie_element = this.getMovieElement();
  274. if (movie_element !== null && typeof(movie_element.SelectFile) === "function") {
  275. try {
  276. movie_element.SelectFile();
  277. }
  278. catch (ex) {
  279. this.debug("Could not call SelectFile: " + ex);
  280. }
  281. } else {
  282. this.debug("Could not find Flash element");
  283. }
  284. };
  285. SWFUpload.prototype.selectFiles = function () {
  286. var movie_element = this.getMovieElement();
  287. if (movie_element !== null && typeof(movie_element.SelectFiles) === "function") {
  288. try {
  289. movie_element.SelectFiles();
  290. }
  291. catch (ex) {
  292. this.debug("Could not call SelectFiles: " + ex);
  293. }
  294. } else {
  295. this.debug("Could not find Flash element");
  296. }
  297. };
  298. /* Start the upload. If a file_id is specified that file is uploaded. Otherwise the first
  299. * file in the queue is uploaded. If no files are in the queue then nothing happens.
  300. * This call uses setTimeout since Flash will be calling back in to JavaScript
  301. */
  302. SWFUpload.prototype.startUpload = function (file_id) {
  303. var self = this;
  304. var movie_element = this.getMovieElement();
  305. if (movie_element !== null && typeof(movie_element.StartUpload) === "function") {
  306. setTimeout(
  307. function () {
  308. try {
  309. movie_element.StartUpload(file_id);
  310. }
  311. catch (ex) {
  312. self.debug("Could not call StartUpload: " + ex);
  313. }
  314. }, 0
  315. );
  316. } else {
  317. this.debug("Could not find Flash element");
  318. }
  319. };
  320. /* Cancels a the file upload. You must specify a file_id */
  321. SWFUpload.prototype.cancelUpload = function (file_id) {
  322. var movie_element = this.getMovieElement();
  323. if (movie_element !== null && typeof(movie_element.CancelUpload) === "function") {
  324. try {
  325. movie_element.CancelUpload(file_id);
  326. }
  327. catch (ex) {
  328. this.debug("Could not call CancelUpload: " + ex);
  329. }
  330. } else {
  331. this.debug("Could not find Flash element");
  332. }
  333. };
  334. // Stops the current upload. The file is re-queued. If nothing is currently uploading then nothing happens.
  335. SWFUpload.prototype.stopUpload = function () {
  336. var movie_element = this.getMovieElement();
  337. if (movie_element !== null && typeof(movie_element.StopUpload) === "function") {
  338. try {
  339. movie_element.StopUpload();
  340. }
  341. catch (ex) {
  342. this.debug("Could not call StopUpload: " + ex);
  343. }
  344. } else {
  345. this.debug("Could not find Flash element");
  346. }
  347. };
  348. /* ************************
  349. * Settings methods
  350. * These methods change the settings inside SWFUpload
  351. * They shouldn't need to be called in a setTimeout since they
  352. * should not call back from Flash to JavaScript (except perhaps in a Debug call)
  353. * and some need to return data so setTimeout won't work.
  354. */
  355. /* Gets the file statistics object. It looks like this (where n = number):
  356. {
  357. files_queued: n,
  358. complete_uploads: n,
  359. upload_errors: n,
  360. uploads_cancelled: n,
  361. queue_errors: n
  362. }
  363. */
  364. SWFUpload.prototype.getStats = function () {
  365. var self = this;
  366. var movie_element = this.getMovieElement();
  367. if (movie_element !== null && typeof(movie_element.GetStats) === "function") {
  368. try {
  369. return movie_element.GetStats();
  370. }
  371. catch (ex) {
  372. self.debug("Could not call GetStats");
  373. }
  374. } else {
  375. this.debug("Could not find Flash element");
  376. }
  377. };
  378. SWFUpload.prototype.setStats = function (stats_object) {
  379. var self = this;
  380. var movie_element = this.getMovieElement();
  381. if (movie_element !== null && typeof(movie_element.SetStats) === "function") {
  382. try {
  383. movie_element.SetStats(stats_object);
  384. }
  385. catch (ex) {
  386. self.debug("Could not call SetStats");
  387. }
  388. } else {
  389. this.debug("Could not find Flash element");
  390. }
  391. };
  392. SWFUpload.prototype.getFile = function (file_id) {
  393. var self = this;
  394. var movie_element = this.getMovieElement();
  395. if (typeof(file_id) === "number") {
  396. if (movie_element !== null && typeof(movie_element.GetFileByIndex) === "function") {
  397. try {
  398. return movie_element.GetFileByIndex(file_id);
  399. }
  400. catch (ex) {
  401. self.debug("Could not call GetFileByIndex");
  402. }
  403. } else {
  404. this.debug("Could not find Flash element");
  405. }
  406. } else {
  407. if (movie_element !== null && typeof(movie_element.GetFile) === "function") {
  408. try {
  409. return movie_element.GetFile(file_id);
  410. }
  411. catch (ex) {
  412. self.debug("Could not call GetFile");
  413. }
  414. } else {
  415. this.debug("Could not find Flash element");
  416. }
  417. }
  418. };
  419. SWFUpload.prototype.addFileParam = function (file_id, name, value) {
  420. var self = this;
  421. var movie_element = this.getMovieElement();
  422. if (movie_element !== null && typeof(movie_element.AddFileParam) === "function") {
  423. try {
  424. return movie_element.AddFileParam(file_id, name, value);
  425. }
  426. catch (ex) {
  427. self.debug("Could not call AddFileParam");
  428. }
  429. } else {
  430. this.debug("Could not find Flash element");
  431. }
  432. };
  433. SWFUpload.prototype.removeFileParam = function (file_id, name) {
  434. var self = this;
  435. var movie_element = this.getMovieElement();
  436. if (movie_element !== null && typeof(movie_element.RemoveFileParam) === "function") {
  437. try {
  438. return movie_element.RemoveFileParam(file_id, name);
  439. }
  440. catch (ex) {
  441. self.debug("Could not call AddFileParam");
  442. }
  443. } else {
  444. this.debug("Could not find Flash element");
  445. }
  446. };
  447. SWFUpload.prototype.setUploadURL = function (url) {
  448. var movie_element = this.getMovieElement();
  449. if (movie_element !== null && typeof(movie_element.SetUploadURL) === "function") {
  450. try {
  451. this.addSetting("upload_url", url);
  452. movie_element.SetUploadURL(this.getSetting("upload_url"));
  453. }
  454. catch (ex) {
  455. this.debug("Could not call SetUploadURL");
  456. }
  457. } else {
  458. this.debug("Could not find Flash element in setUploadURL");
  459. }
  460. };
  461. SWFUpload.prototype.setPostParams = function (param_object) {
  462. var movie_element = this.getMovieElement();
  463. if (movie_element !== null && typeof(movie_element.SetPostParams) === "function") {
  464. try {
  465. this.addSetting("post_params", param_object);
  466. movie_element.SetPostParams(this.getSetting("post_params"));
  467. }
  468. catch (ex) {
  469. this.debug("Could not call SetPostParams");
  470. }
  471. } else {
  472. this.debug("Could not find Flash element in SetPostParams");
  473. }
  474. };
  475. SWFUpload.prototype.setFileTypes = function (types, description) {
  476. var movie_element = this.getMovieElement();
  477. if (movie_element !== null && typeof(movie_element.SetFileTypes) === "function") {
  478. try {
  479. this.addSetting("file_types", types);
  480. this.addSetting("file_types_description", description);
  481. movie_element.SetFileTypes(this.getSetting("file_types"), this.getSetting("file_types_description"));
  482. }
  483. catch (ex) {
  484. this.debug("Could not call SetFileTypes");
  485. }
  486. } else {
  487. this.debug("Could not find Flash element in SetFileTypes");
  488. }
  489. };
  490. SWFUpload.prototype.setFileSizeLimit = function (file_size_limit) {
  491. var movie_element = this.getMovieElement();
  492. if (movie_element !== null && typeof(movie_element.SetFileSizeLimit) === "function") {
  493. try {
  494. this.addSetting("file_size_limit", file_size_limit);
  495. movie_element.SetFileSizeLimit(this.getSetting("file_size_limit"));
  496. }
  497. catch (ex) {
  498. this.debug("Could not call SetFileSizeLimit");
  499. }
  500. } else {
  501. this.debug("Could not find Flash element in SetFileSizeLimit");
  502. }
  503. };
  504. SWFUpload.prototype.setFileUploadLimit = function (file_upload_limit) {
  505. var movie_element = this.getMovieElement();
  506. if (movie_element !== null && typeof(movie_element.SetFileUploadLimit) === "function") {
  507. try {
  508. this.addSetting("file_upload_limit", file_upload_limit);
  509. movie_element.SetFileUploadLimit(this.getSetting("file_upload_limit"));
  510. }
  511. catch (ex) {
  512. this.debug("Could not call SetFileUploadLimit");
  513. }
  514. } else {
  515. this.debug("Could not find Flash element in SetFileUploadLimit");
  516. }
  517. };
  518. SWFUpload.prototype.setFileQueueLimit = function (file_queue_limit) {
  519. var movie_element = this.getMovieElement();
  520. if (movie_element !== null && typeof(movie_element.SetFileQueueLimit) === "function") {
  521. try {
  522. this.addSetting("file_queue_limit", file_queue_limit);
  523. movie_element.SetFileQueueLimit(this.getSetting("file_queue_limit"));
  524. }
  525. catch (ex) {
  526. this.debug("Could not call SetFileQueueLimit");
  527. }
  528. } else {
  529. this.debug("Could not find Flash element in SetFileQueueLimit");
  530. }
  531. };
  532. SWFUpload.prototype.setFilePostName = function (file_post_name) {
  533. var movie_element = this.getMovieElement();
  534. if (movie_element !== null && typeof(movie_element.SetFilePostName) === "function") {
  535. try {
  536. this.addSetting("file_post_name", file_post_name);
  537. movie_element.SetFilePostName(this.getSetting("file_post_name"));
  538. }
  539. catch (ex) {
  540. this.debug("Could not call SetFilePostName");
  541. }
  542. } else {
  543. this.debug("Could not find Flash element in SetFilePostName");
  544. }
  545. };
  546. SWFUpload.prototype.setDebugEnabled = function (debug_enabled) {
  547. var movie_element = this.getMovieElement();
  548. if (movie_element !== null && typeof(movie_element.SetDebugEnabled) === "function") {
  549. try {
  550. this.addSetting("debug_enabled", debug_enabled);
  551. movie_element.SetDebugEnabled(this.getSetting("debug_enabled"));
  552. }
  553. catch (ex) {
  554. this.debug("Could not call SetDebugEnabled");
  555. }
  556. } else {
  557. this.debug("Could not find Flash element in SetDebugEnabled");
  558. }
  559. };
  560. /* *******************************
  561. Internal Event Callers
  562. Don't override these! These event callers ensure that your custom event handlers
  563. are called safely and in order.
  564. ******************************* */
  565. /* This is the callback method that the Flash movie will call when it has been loaded and is ready to go.
  566. Calling this or showUI() "manually" will bypass the Flash Detection built in to SWFUpload.
  567. Use a ui_function setting if you want to control the UI loading after the flash has loaded.
  568. */
  569. SWFUpload.prototype.flashReady = function () {
  570. var self = this;
  571. if (typeof(self.fileDialogStart_handler) === "function") {
  572. this.eventQueue[this.eventQueue.length] = function() { self.flashReady_handler(); };
  573. setTimeout(function () { self.executeNextEvent();}, 0);
  574. } else {
  575. this.debug("fileDialogStart event not defined");
  576. }
  577. };
  578. /*
  579. Event Queue. Rather can call events directly from Flash they events are
  580. are placed in a queue and then executed. This ensures that each event is
  581. executed in the order it was called which is not guarenteed when calling
  582. setTimeout. Out of order events was especially problematic in Safari.
  583. */
  584. SWFUpload.prototype.executeNextEvent = function () {
  585. var f = this.eventQueue.shift();
  586. if (typeof(f) === "function") {
  587. f();
  588. }
  589. }
  590. /* This is a chance to do something before the browse window opens */
  591. SWFUpload.prototype.fileDialogStart = function () {
  592. var self = this;
  593. if (typeof(self.fileDialogStart_handler) === "function") {
  594. this.eventQueue[this.eventQueue.length] = function() { self.fileDialogStart_handler(); };
  595. setTimeout(function () { self.executeNextEvent();}, 0);
  596. } else {
  597. this.debug("fileDialogStart event not defined");
  598. }
  599. };
  600. /* Called when a file is successfully added to the queue. */
  601. SWFUpload.prototype.fileQueued = function (file) {
  602. var self = this;
  603. if (typeof(self.fileQueued_handler) === "function") {
  604. this.eventQueue[this.eventQueue.length] = function() { self.fileQueued_handler(file); };
  605. setTimeout(function () { self.executeNextEvent();}, 0);
  606. } else {
  607. this.debug("fileQueued event not defined");
  608. }
  609. };
  610. /* Handle errors that occur when an attempt to queue a file fails. */
  611. SWFUpload.prototype.fileQueueError = function (file, error_code, message) {
  612. var self = this;
  613. if (typeof(self.fileQueueError_handler) === "function") {
  614. this.eventQueue[this.eventQueue.length] = function() { self.fileQueueError_handler(file, error_code, message); };
  615. setTimeout(function () { self.executeNextEvent();}, 0);
  616. } else {
  617. this.debug("fileQueueError event not defined");
  618. }
  619. };
  620. /* Called after the file dialog has closed and the selected files have been queued.
  621. You could call startUpload here if you want the queued files to begin uploading immediately. */
  622. SWFUpload.prototype.fileDialogComplete = function (num_files_selected) {
  623. var self = this;
  624. if (typeof(self.fileDialogComplete_handler) === "function") {
  625. this.eventQueue[this.eventQueue.length] = function() { self.fileDialogComplete_handler(num_files_selected); };
  626. setTimeout(function () { self.executeNextEvent();}, 0);
  627. } else {
  628. this.debug("fileDialogComplete event not defined");
  629. }
  630. };
  631. /* Gets called when a file upload is about to be started. Return true to continue the upload. Return false to stop the upload.
  632. If you return false then uploadError and uploadComplete are called (like normal).
  633. This is a good place to do any file validation you need.
  634. */
  635. SWFUpload.prototype.uploadStart = function (file) {
  636. var self = this;
  637. if (typeof(self.fileDialogComplete_handler) === "function") {
  638. this.eventQueue[this.eventQueue.length] = function() { self.returnUploadStart(self.uploadStart_handler(file)); };
  639. setTimeout(function () { self.executeNextEvent();}, 0);
  640. } else {
  641. this.debug("uploadStart event not defined");
  642. }
  643. };
  644. /* Note: Internal use only. This function returns the result of uploadStart to
  645. flash. Since returning values in the normal way can result in Flash/JS circular
  646. call issues we split up the call in a Timeout. This is transparent from the API
  647. point of view.
  648. */
  649. SWFUpload.prototype.returnUploadStart = function (return_value) {
  650. var movie_element = this.getMovieElement();
  651. if (movie_element !== null && typeof(movie_element.ReturnUploadStart) === "function") {
  652. try {
  653. movie_element.ReturnUploadStart(return_value);
  654. }
  655. catch (ex) {
  656. this.debug("Could not call ReturnUploadStart");
  657. }
  658. } else {
  659. this.debug("Could not find Flash element in returnUploadStart");
  660. }
  661. };
  662. /* Called during upload as the file progresses. Use this event to update your UI. */
  663. SWFUpload.prototype.uploadProgress = function (file, bytes_complete, bytes_total) {
  664. var self = this;
  665. if (typeof(self.uploadProgress_handler) === "function") {
  666. this.eventQueue[this.eventQueue.length] = function() { self.uploadProgress_handler(file, bytes_complete, bytes_total); };
  667. setTimeout(function () { self.executeNextEvent();}, 0);
  668. } else {
  669. this.debug("uploadProgress event not defined");
  670. }
  671. };
  672. /* Called when an error occurs during an upload. Use error_code and the SWFUpload.UPLOAD_ERROR constants to determine
  673. which error occurred. The uploadComplete event is called after an error code indicating that the next file is
  674. ready for upload. For files cancelled out of order the uploadComplete event will not be called. */
  675. SWFUpload.prototype.uploadError = function (file, error_code, message) {
  676. var self = this;
  677. if (typeof(this.uploadError_handler) === "function") {
  678. this.eventQueue[this.eventQueue.length] = function() { self.uploadError_handler(file, error_code, message); };
  679. setTimeout(function () { self.executeNextEvent();}, 0);
  680. } else {
  681. this.debug("uploadError event not defined");
  682. }
  683. };
  684. /* This gets called when a file finishes uploading and the server-side upload script has completed and returned a 200
  685. status code. Any text returned by the server is available in server_data.
  686. **NOTE: The upload script MUST return some text or the uploadSuccess and uploadComplete events will not fire and the
  687. upload will become 'stuck'. */
  688. SWFUpload.prototype.uploadSuccess = function (file, server_data) {
  689. var self = this;
  690. if (typeof(self.uploadSuccess_handler) === "function") {
  691. this.eventQueue[this.eventQueue.length] = function() { self.uploadSuccess_handler(file, server_data); };
  692. setTimeout(function () { self.executeNextEvent();}, 0);
  693. } else {
  694. this.debug("uploadSuccess event not defined");
  695. }
  696. };
  697. /* uploadComplete is called when the file is uploaded or an error occurred and SWFUpload is ready to make the next upload.
  698. If you want the next upload to start to automatically you can call startUpload() from this event. */
  699. SWFUpload.prototype.uploadComplete = function (file) {
  700. var self = this;
  701. if (typeof(self.uploadComplete_handler) === "function") {
  702. this.eventQueue[this.eventQueue.length] = function() { self.uploadComplete_handler(file); };
  703. setTimeout(function () { self.executeNextEvent();}, 0);
  704. } else {
  705. this.debug("uploadComplete event not defined");
  706. }
  707. };
  708. /* Called by SWFUpload JavaScript and Flash functions when debug is enabled. By default it writes messages to the
  709. internal debug console. You can override this event and have messages written where you want. */
  710. SWFUpload.prototype.debug = function (message) {
  711. var self = this;
  712. if (typeof(self.debug_handler) === "function") {
  713. this.eventQueue[this.eventQueue.length] = function() { self.debug_handler(message); };
  714. setTimeout(function () { self.executeNextEvent();}, 0);
  715. } else {
  716. this.eventQueue[this.eventQueue.length] = function() { self.debugMessage(message); };
  717. setTimeout(function () { self.executeNextEvent();}, 0);
  718. }
  719. };
  720. /* **********************************
  721. Default Event Handlers.
  722. These event handlers are used by default if an overriding handler is
  723. not defined in the SWFUpload settings object.
  724. JS Note: even though these are defined on the SWFUpload object (rather than the prototype) they
  725. are attached (read: copied) to a SWFUpload instance and 'this' is given the proper context.
  726. ********************************** */
  727. /* This is a special event handler that has no override in the settings. Flash calls this when it has
  728. been loaded by the browser and is ready for interaction. You should not override it. If you need
  729. to do something with SWFUpload has loaded then use the swfupload_loaded_handler setting.
  730. */
  731. SWFUpload.flashReady = function () {
  732. try {
  733. this.debug("Flash called back and is ready.");
  734. if (typeof(this.swfUploadLoaded_handler) === "function") {
  735. this.swfUploadLoaded_handler();
  736. }
  737. } catch (ex) {
  738. this.debug(ex);
  739. }
  740. };
  741. /* This is a chance to something immediately after SWFUpload has loaded.
  742. Like, hide the default/degraded upload form and display the SWFUpload form. */
  743. SWFUpload.swfUploadLoaded = function () {
  744. };
  745. /* This is a chance to do something before the browse window opens */
  746. SWFUpload.fileDialogStart = function () {
  747. };
  748. /* Called when a file is successfully added to the queue. */
  749. SWFUpload.fileQueued = function (file) {
  750. };
  751. /* Handle errors that occur when an attempt to queue a file fails. */
  752. SWFUpload.fileQueueError = function (file, error_code, message) {
  753. try {
  754. switch (error_code) {
  755. case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
  756. this.debug("Error Code: File too big, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
  757. break;
  758. case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
  759. this.debug("Error Code: Zero Byte File, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
  760. break;
  761. case SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED:
  762. this.debug("Error Code: Upload limit reached, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
  763. break;
  764. case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
  765. this.debug("Error Code: File extension is not allowed, Message: " + message);
  766. break;
  767. default:
  768. this.debug("Error Code: Unhandled error occured. Errorcode: " + error_code);
  769. }
  770. } catch (ex) {
  771. this.debug(ex);
  772. }
  773. };
  774. /* Called after the file dialog has closed and the selected files have been queued.
  775. You could call startUpload here if you want the queued files to begin uploading immediately. */
  776. SWFUpload.fileDialogComplete = function (num_files_selected) {
  777. };
  778. /* Gets called when a file upload is about to be started. Return true to continue the upload. Return false to stop the upload.
  779. If you return false then the uploadError callback is called and then uploadComplete (like normal).
  780. This is a good place to do any file validation you need.
  781. This is the only function that cannot be called on a setTimeout because it must return a value to Flash.
  782. You SHOULD NOT make any calls in to Flash (e.i, changing settings, getting stats, etc). Flash Player bugs prevent
  783. calls in to Flash from working reliably.
  784. */
  785. SWFUpload.uploadStart = function (file) {
  786. return true;
  787. };
  788. // Called during upload as the file progresses
  789. SWFUpload.uploadProgress = function (file, bytes_complete, bytes_total) {
  790. this.debug("File Progress: " + file.id + ", Bytes: " + bytes_complete + ". Total: " + bytes_total);
  791. };
  792. /* This gets called when a file finishes uploading and the upload script has completed and returned a 200 status code. Any text returned by the
  793. server is available in server_data. The upload script must return some text or uploadSuccess will not fire (neither will uploadComplete). */
  794. SWFUpload.uploadSuccess = function (file, server_data) {
  795. };
  796. /* This is called last. The file is uploaded or an error occurred and SWFUpload is ready to make the next upload.
  797. If you want to automatically start the next file just call startUpload from here.
  798. */
  799. SWFUpload.uploadComplete = function (file) {
  800. };
  801. // Called by SWFUpload JavaScript and Flash functions when debug is enabled.
  802. // Override this method in your settings to call your own debug message handler
  803. SWFUpload.debug = function (message) {
  804. if (this.getSetting("debug_enabled")) {
  805. this.debugMessage(message);
  806. }
  807. };
  808. /* Called when an upload occurs during upload. For HTTP errors 'message' will contain the HTTP STATUS CODE */
  809. SWFUpload.uploadError = function (file, error_code, message) {
  810. try {
  811. switch (errcode) {
  812. case SWFUpload.UPLOAD_ERROR.SPECIFIED_FILE_ID_NOT_FOUND:
  813. this.debug("Error Code: File ID specified for upload was not found, Message: " + msg);
  814. break;
  815. case SWFUpload.UPLOAD_ERROR.HTTP_ERROR:
  816. this.debug("Error Code: HTTP Error, File name: " + file.name + ", Message: " + msg);
  817. break;
  818. case SWFUpload.UPLOAD_ERROR.MISSING_UPLOAD_URL:
  819. this.debug("Error Code: No backend file, File name: " + file.name + ", Message: " + msg);
  820. break;
  821. case SWFUpload.UPLOAD_ERROR.IO_ERROR:
  822. this.debug("Error Code: IO Error, File name: " + file.name + ", Message: " + msg);
  823. break;
  824. case SWFUpload.UPLOAD_ERROR.SECURITY_ERROR:
  825. this.debug("Error Code: Security Error, File name: " + file.name + ", Message: " + msg);
  826. break;
  827. case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
  828. this.debug("Error Code: Upload limit reached, File name: " + file.name + ", File size: " + file.size + ", Message: " + msg);
  829. break;
  830. case SWFUpload.UPLOAD_ERROR.UPLOAD_FAILED:
  831. this.debug("Error Code: Upload Initialization exception, File name: " + file.name + ", File size: " + file.size + ", Message: " + msg);
  832. break;
  833. case SWFUpload.UPLOAD_ERROR.FILE_VALIDATION_FAILED:
  834. this.debug("Error Code: uploadStart callback returned false, File name: " + file.name + ", File size: " + file.size + ", Message: " + msg);
  835. break;
  836. case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED:
  837. this.debug("Error Code: The file upload was cancelled, File name: " + file.name + ", File size: " + file.size + ", Message: " + msg);
  838. break;
  839. case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
  840. this.debug("Error Code: The file upload was stopped, File name: " + file.name + ", File size: " + file.size + ", Message: " + msg);
  841. break;
  842. default:
  843. this.debug("Error Code: Unhandled error occured. Errorcode: " + errcode);
  844. }
  845. } catch (ex) {
  846. this.debug(ex);
  847. }
  848. };
  849. /* **********************************
  850. Debug Console
  851. The debug console is a self contained, in page location
  852. for debug message to be sent. The Debug Console adds
  853. itself to the body if necessary.
  854. The console is automatically scrolled as messages appear.
  855. You can override this console (to use FireBug's console for instance) by setting the debug event method to your own function
  856. that handles the debug message
  857. ********************************** */
  858. SWFUpload.prototype.debugMessage = function (message) {
  859. var exception_message, exception_values;
  860. if (typeof(message) === "object" && typeof(message.name) === "string" && typeof(message.message) === "string") {
  861. exception_message = "";
  862. exception_values = [];
  863. for (var key in message) {
  864. exception_values.push(key + ": " + message[key]);
  865. }
  866. exception_message = exception_values.join("\n");
  867. exception_values = exception_message.split("\n");
  868. exception_message = "EXCEPTION: " + exception_values.join("\nEXCEPTION: ");
  869. SWFUpload.Console.writeLine(exception_message);
  870. } else {
  871. SWFUpload.Console.writeLine(message);
  872. }
  873. };
  874. SWFUpload.Console = {};
  875. SWFUpload.Console.writeLine = function (message) {
  876. var console, documentForm;
  877. try {
  878. console = document.getElementById("SWFUpload_Console");
  879. if (!console) {
  880. documentForm = document.createElement("form");
  881. document.getElementsByTagName("body")[0].appendChild(documentForm);
  882. console = document.createElement("textarea");
  883. console.id = "SWFUpload_Console";
  884. console.style.fontFamily = "monospace";
  885. console.setAttribute("wrap", "off");
  886. console.wrap = "off";
  887. console.style.overflow = "auto";
  888. console.style.width = "700px";
  889. console.style.height = "350px";
  890. console.style.margin = "5px";
  891. documentForm.appendChild(console);
  892. }
  893. console.value += message + "\n";
  894. console.scrollTop = console.scrollHeight - console.clientHeight;
  895. } catch (ex) {
  896. alert("Exception: " + ex.name + " Message: " + ex.message);
  897. }
  898. };