jquery.uploadify.v2.1.4.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. Uploadify v2.1.4
  3. Release Date: November 8, 2010
  4. Copyright (c) 2010 Ronnie Garcia, Travis Nickels
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. */
  21. if(jQuery)(
  22. function(jQuery){
  23. jQuery.extend(jQuery.fn,{
  24. uploadify:function(options) {
  25. jQuery(this).each(function(){
  26. var settings = jQuery.extend({
  27. id : jQuery(this).attr('id'), // The ID of the object being Uploadified
  28. uploader : 'uploadify.swf', // The path to the uploadify swf file
  29. script : 'uploadify.php', // The path to the uploadify backend upload script
  30. expressInstall : null, // The path to the express install swf file
  31. folder : '', // The path to the upload folder
  32. height : 30, // The height of the flash button
  33. width : 120, // The width of the flash button
  34. cancelImg : 'cancel.png', // The path to the cancel image for the default file queue item container
  35. wmode : 'opaque', // The wmode of the flash file
  36. scriptAccess : 'sameDomain', // Set to "always" to allow script access across domains
  37. fileDataName : 'Filedata', // The name of the file collection object in the backend upload script
  38. method : 'POST', // The method for sending variables to the backend upload script
  39. queueSizeLimit : 999, // The maximum size of the file queue
  40. simUploadLimit : 1, // The number of simultaneous uploads allowed
  41. queueID : false, // The optional ID of the queue container
  42. displayData : 'percentage', // Set to "speed" to show the upload speed in the default queue item
  43. removeCompleted : true, // Set to true if you want the queue items to be removed when a file is done uploading
  44. onInit : function() {}, // Function to run when uploadify is initialized
  45. onSelect : function() {}, // Function to run when a file is selected
  46. onSelectOnce : function() {}, // Function to run once when files are added to the queue
  47. onQueueFull : function() {}, // Function to run when the queue reaches capacity
  48. onCheck : function() {}, // Function to run when script checks for duplicate files on the server
  49. onCancel : function() {}, // Function to run when an item is cleared from the queue
  50. onClearQueue : function() {}, // Function to run when the queue is manually cleared
  51. onError : function() {}, // Function to run when an upload item returns an error
  52. onProgress : function() {}, // Function to run each time the upload progress is updated
  53. onComplete : function() {}, // Function to run when an upload is completed
  54. onAllComplete : function() {} // Function to run when all uploads are completed
  55. }, options);
  56. jQuery(this).data('settings',settings);
  57. var pagePath = location.pathname;
  58. pagePath = pagePath.split('/');
  59. pagePath.pop();
  60. pagePath = pagePath.join('/') + '/';
  61. var data = {};
  62. data.uploadifyID = settings.id;
  63. data.pagepath = pagePath;
  64. if (settings.buttonImg) data.buttonImg = escape(settings.buttonImg);
  65. if (settings.buttonText) data.buttonText = escape(settings.buttonText);
  66. if (settings.rollover) data.rollover = true;
  67. data.script = settings.script;
  68. data.folder = escape(settings.folder);
  69. if (settings.scriptData) {
  70. var scriptDataString = '';
  71. for (var name in settings.scriptData) {
  72. scriptDataString += '&' + name + '=' + settings.scriptData[name];
  73. }
  74. data.scriptData = escape(scriptDataString.substr(1));
  75. }
  76. data.width = settings.width;
  77. data.height = settings.height;
  78. data.wmode = settings.wmode;
  79. data.method = settings.method;
  80. data.queueSizeLimit = settings.queueSizeLimit;
  81. data.simUploadLimit = settings.simUploadLimit;
  82. if (settings.hideButton) data.hideButton = true;
  83. if (settings.fileDesc) data.fileDesc = settings.fileDesc;
  84. if (settings.fileExt) data.fileExt = settings.fileExt;
  85. if (settings.multi) data.multi = true;
  86. if (settings.auto) data.auto = true;
  87. if (settings.sizeLimit) data.sizeLimit = settings.sizeLimit;
  88. if (settings.checkScript) data.checkScript = settings.checkScript;
  89. if (settings.fileDataName) data.fileDataName = settings.fileDataName;
  90. if (settings.queueID) data.queueID = settings.queueID;
  91. if (settings.onInit() !== false) {
  92. jQuery(this).css('display','none');
  93. jQuery(this).after('<div id="' + jQuery(this).attr('id') + 'Uploader"></div>');
  94. swfobject.embedSWF(settings.uploader, settings.id + 'Uploader', settings.width, settings.height, '9.0.24', settings.expressInstall, data, {'quality':'high','wmode':settings.wmode,'allowScriptAccess':settings.scriptAccess},{},function(event) {
  95. if (typeof(settings.onSWFReady) == 'function' && event.success) settings.onSWFReady();
  96. });
  97. if (settings.queueID == false) {
  98. jQuery("#" + jQuery(this).attr('id') + "Uploader").after('<div id="' + jQuery(this).attr('id') + 'Queue" class="uploadifyQueue"></div>');
  99. } else {
  100. jQuery("#" + settings.queueID).addClass('uploadifyQueue');
  101. }
  102. }
  103. if (typeof(settings.onOpen) == 'function') {
  104. jQuery(this).bind("uploadifyOpen", settings.onOpen);
  105. }
  106. jQuery(this).bind("uploadifySelect", {'action': settings.onSelect, 'queueID': settings.queueID}, function(event, ID, fileObj) {
  107. if (event.data.action(event, ID, fileObj) !== false) {
  108. var byteSize = Math.round(fileObj.size / 1024 * 100) * .01;
  109. var suffix = 'KB';
  110. if (byteSize > 1000) {
  111. byteSize = Math.round(byteSize *.001 * 100) * .01;
  112. suffix = 'MB';
  113. }
  114. var sizeParts = byteSize.toString().split('.');
  115. if (sizeParts.length > 1) {
  116. byteSize = sizeParts[0] + '.' + sizeParts[1].substr(0,2);
  117. } else {
  118. byteSize = sizeParts[0];
  119. }
  120. if (fileObj.name.length > 20) {
  121. fileName = fileObj.name.substr(0,20) + '...';
  122. } else {
  123. fileName = fileObj.name;
  124. }
  125. queue = '#' + jQuery(this).attr('id') + 'Queue';
  126. if (event.data.queueID) {
  127. queue = '#' + event.data.queueID;
  128. }
  129. jQuery(queue).append('<div id="' + jQuery(this).attr('id') + ID + '" class="uploadifyQueueItem">\
  130. <div class="cancel">\
  131. <a href="javascript:jQuery(\'#' + jQuery(this).attr('id') + '\').uploadifyCancel(\'' + ID + '\')"><img src="' + settings.cancelImg + '" border="0" /></a>\
  132. </div>\
  133. <span class="fileName">' + fileName + ' (' + byteSize + suffix + ')</span><span class="percentage"></span>\
  134. <div class="uploadifyProgress">\
  135. <div id="' + jQuery(this).attr('id') + ID + 'ProgressBar" class="uploadifyProgressBar"><!--Progress Bar--></div>\
  136. </div>\
  137. </div>');
  138. }
  139. });
  140. jQuery(this).bind("uploadifySelectOnce", {'action': settings.onSelectOnce}, function(event, data) {
  141. event.data.action(event, data);
  142. if (settings.auto) {
  143. if (settings.checkScript) {
  144. jQuery(this).uploadifyUpload(null, false);
  145. } else {
  146. jQuery(this).uploadifyUpload(null, true);
  147. }
  148. }
  149. });
  150. jQuery(this).bind("uploadifyQueueFull", {'action': settings.onQueueFull}, function(event, queueSizeLimit) {
  151. if (event.data.action(event, queueSizeLimit) !== false) {
  152. alert('The queue is full. The max size is ' + queueSizeLimit + '.');
  153. }
  154. });
  155. jQuery(this).bind("uploadifyCheckExist", {'action': settings.onCheck}, function(event, checkScript, fileQueueObj, folder, single) {
  156. var postData = new Object();
  157. postData = fileQueueObj;
  158. postData.folder = (folder.substr(0,1) == '/') ? folder : pagePath + folder;
  159. if (single) {
  160. for (var ID in fileQueueObj) {
  161. var singleFileID = ID;
  162. }
  163. }
  164. jQuery.post(checkScript, postData, function(data) {
  165. for(var key in data) {
  166. if (event.data.action(event, data, key) !== false) {
  167. var replaceFile = confirm("Do you want to replace the file " + data[key] + "?");
  168. if (!replaceFile) {
  169. document.getElementById(jQuery(event.target).attr('id') + 'Uploader').cancelFileUpload(key,true,true);
  170. }
  171. }
  172. }
  173. if (single) {
  174. document.getElementById(jQuery(event.target).attr('id') + 'Uploader').startFileUpload(singleFileID, true);
  175. } else {
  176. document.getElementById(jQuery(event.target).attr('id') + 'Uploader').startFileUpload(null, true);
  177. }
  178. }, "json");
  179. });
  180. jQuery(this).bind("uploadifyCancel", {'action': settings.onCancel}, function(event, ID, fileObj, data, remove, clearFast) {
  181. if (event.data.action(event, ID, fileObj, data, clearFast) !== false) {
  182. if (remove) {
  183. var fadeSpeed = (clearFast == true) ? 0 : 250;
  184. jQuery("#" + jQuery(this).attr('id') + ID).fadeOut(fadeSpeed, function() { jQuery(this).remove() });
  185. }
  186. }
  187. });
  188. jQuery(this).bind("uploadifyClearQueue", {'action': settings.onClearQueue}, function(event, clearFast) {
  189. var queueID = (settings.queueID) ? settings.queueID : jQuery(this).attr('id') + 'Queue';
  190. if (clearFast) {
  191. jQuery("#" + queueID).find('.uploadifyQueueItem').remove();
  192. }
  193. if (event.data.action(event, clearFast) !== false) {
  194. jQuery("#" + queueID).find('.uploadifyQueueItem').each(function() {
  195. var index = jQuery('.uploadifyQueueItem').index(this);
  196. jQuery(this).delay(index * 100).fadeOut(250, function() { jQuery(this).remove() });
  197. });
  198. }
  199. });
  200. var errorArray = [];
  201. jQuery(this).bind("uploadifyError", {'action': settings.onError}, function(event, ID, fileObj, errorObj) {
  202. if (event.data.action(event, ID, fileObj, errorObj) !== false) {
  203. var fileArray = new Array(ID, fileObj, errorObj);
  204. errorArray.push(fileArray);
  205. jQuery("#" + jQuery(this).attr('id') + ID).find('.percentage').text(" - " + errorObj.type + " Error");
  206. jQuery("#" + jQuery(this).attr('id') + ID).find('.uploadifyProgress').hide();
  207. jQuery("#" + jQuery(this).attr('id') + ID).addClass('uploadifyError');
  208. }
  209. });
  210. if (typeof(settings.onUpload) == 'function') {
  211. jQuery(this).bind("uploadifyUpload", settings.onUpload);
  212. }
  213. jQuery(this).bind("uploadifyProgress", {'action': settings.onProgress, 'toDisplay': settings.displayData}, function(event, ID, fileObj, data) {
  214. if (event.data.action(event, ID, fileObj, data) !== false) {
  215. jQuery("#" + jQuery(this).attr('id') + ID + "ProgressBar").animate({'width': data.percentage + '%'},250,function() {
  216. if (data.percentage == 100) {
  217. jQuery(this).closest('.uploadifyProgress').fadeOut(250,function() {jQuery(this).remove()});
  218. }
  219. });
  220. if (event.data.toDisplay == 'percentage') displayData = ' - ' + data.percentage + '%';
  221. if (event.data.toDisplay == 'speed') displayData = ' - ' + data.speed + 'KB/s';
  222. if (event.data.toDisplay == null) displayData = ' ';
  223. jQuery("#" + jQuery(this).attr('id') + ID).find('.percentage').text(displayData);
  224. }
  225. });
  226. jQuery(this).bind("uploadifyComplete", {'action': settings.onComplete}, function(event, ID, fileObj, response, data) {
  227. if (event.data.action(event, ID, fileObj, unescape(response), data) !== false) {
  228. jQuery("#" + jQuery(this).attr('id') + ID).find('.percentage').text(' - Completed');
  229. if (settings.removeCompleted) {
  230. jQuery("#" + jQuery(event.target).attr('id') + ID).fadeOut(250,function() {jQuery(this).remove()});
  231. }
  232. jQuery("#" + jQuery(event.target).attr('id') + ID).addClass('completed');
  233. }
  234. });
  235. if (typeof(settings.onAllComplete) == 'function') {
  236. jQuery(this).bind("uploadifyAllComplete", {'action': settings.onAllComplete}, function(event, data) {
  237. if (event.data.action(event, data) !== false) {
  238. errorArray = [];
  239. }
  240. });
  241. }
  242. });
  243. },
  244. uploadifySettings:function(settingName, settingValue, resetObject) {
  245. var returnValue = false;
  246. jQuery(this).each(function() {
  247. if (settingName == 'scriptData' && settingValue != null) {
  248. if (resetObject) {
  249. var scriptData = settingValue;
  250. } else {
  251. var scriptData = jQuery.extend(jQuery(this).data('settings').scriptData, settingValue);
  252. }
  253. var scriptDataString = '';
  254. for (var name in scriptData) {
  255. scriptDataString += '&' + name + '=' + scriptData[name];
  256. }
  257. settingValue = escape(scriptDataString.substr(1));
  258. }
  259. returnValue = document.getElementById(jQuery(this).attr('id') + 'Uploader').updateSettings(settingName, settingValue);
  260. });
  261. if (settingValue == null) {
  262. if (settingName == 'scriptData') {
  263. var returnSplit = unescape(returnValue).split('&');
  264. var returnObj = new Object();
  265. for (var i = 0; i < returnSplit.length; i++) {
  266. var iSplit = returnSplit[i].split('=');
  267. returnObj[iSplit[0]] = iSplit[1];
  268. }
  269. returnValue = returnObj;
  270. }
  271. }
  272. return returnValue;
  273. },
  274. uploadifyUpload:function(ID,checkComplete) {
  275. jQuery(this).each(function() {
  276. if (!checkComplete) checkComplete = false;
  277. document.getElementById(jQuery(this).attr('id') + 'Uploader').startFileUpload(ID, checkComplete);
  278. });
  279. },
  280. uploadifyCancel:function(ID) {
  281. jQuery(this).each(function() {
  282. document.getElementById(jQuery(this).attr('id') + 'Uploader').cancelFileUpload(ID, true, true, false);
  283. });
  284. },
  285. uploadifyClearQueue:function() {
  286. jQuery(this).each(function() {
  287. document.getElementById(jQuery(this).attr('id') + 'Uploader').clearFileUploadQueue(false);
  288. });
  289. }
  290. })
  291. })(jQuery);