jquery.uploadify (Source).js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. Uploadify v1.6.2
  3. Copyright (C) 2009 by Ronnie Garcia
  4. Co-developed by Travis Nickels
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. var flashVer = -1;
  17. if (navigator.plugins != null && navigator.plugins.length > 0) {
  18. if (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) {
  19. var swVer2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
  20. var flashDescription = navigator.plugins["Shockwave Flash" + swVer2].description;
  21. var descArray = flashDescription.split(" ");
  22. var tempArrayMajor = descArray[2].split(".");
  23. var versionMajor = tempArrayMajor[0];
  24. var versionMinor = tempArrayMajor[1];
  25. var versionRevision = descArray[3];
  26. if (versionRevision == "") {
  27. versionRevision = descArray[4];
  28. }
  29. if (versionRevision[0] == "d") {
  30. versionRevision = versionRevision.substring(1);
  31. } else if (versionRevision[0] == "r") {
  32. ersionRevision = versionRevision.substring(1);
  33. if (versionRevision.indexOf("d") > 0) {
  34. versionRevision = versionRevision.substring(0, versionRevision.indexOf("d"));
  35. }
  36. }
  37. var flashVer = versionMajor + "." + versionMinor + "." + versionRevision;
  38. }
  39. } else if ( $.browser.msie ) {
  40. var version;
  41. var axo;
  42. var e;
  43. try {
  44. axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
  45. version = axo.GetVariable("$version");
  46. } catch (e) {
  47. }
  48. flashVer = version.replace("WIN ","").replace(",",".");
  49. }
  50. flashVer = flashVer.split(".")[0];
  51. if(jQuery)(
  52. function($){
  53. $.extend($.fn,{
  54. fileUpload:function(options) {
  55. if (flashVer >= 9) {
  56. $(this).each(function(){
  57. settings = $.extend({
  58. uploader: 'uploader.swf',
  59. script: 'uploader.php',
  60. folder: '',
  61. height: 30,
  62. width: 110,
  63. cancelImg: 'cancel.png',
  64. wmode: 'opaque',
  65. scriptAccess: 'sameDomain',
  66. fileDataName: 'Filedata',
  67. displayData: 'percentage',
  68. onInit: function() {},
  69. onSelect: function() {},
  70. onCheck: function() {},
  71. onCancel: function() {},
  72. onError: function() {},
  73. onProgress: function() {},
  74. onComplete: function() {}
  75. }, options);
  76. var pagePath = location.pathname;
  77. pagePath = pagePath.split('/');
  78. pagePath.pop();
  79. pagePath = pagePath.join('/') + '/';
  80. var data = '&pagepath=' + pagePath;
  81. if (settings.buttonImg) data += '&buttonImg=' + escape(settings.buttonImg);
  82. if (settings.buttonText) data += '&buttonText=' + escape(settings.buttonText);
  83. if (settings.rollover) data += '&rollover=true';
  84. data += '&script=' + settings.script;
  85. data += '&folder=' + escape(settings.folder);
  86. if (settings.scriptData) {
  87. var scriptDataString = '';
  88. for (var name in settings.scriptData) {
  89. scriptDataString += '&' + name + '=' + settings.scriptData[name];
  90. }
  91. data += '&scriptData=' + escape(scriptDataString);
  92. }
  93. data += '&btnWidth=' + settings.width;
  94. data += '&btnHeight=' + settings.height;
  95. data += '&wmode=' + settings.wmode;
  96. if (settings.hideButton) data += '&hideButton=true';
  97. if (settings.fileDesc) data += '&fileDesc=' + settings.fileDesc + '&fileExt=' + settings.fileExt;
  98. if (settings.multi) data += '&multi=true';
  99. if (settings.auto) data += '&auto=true';
  100. if (settings.sizeLimit) data += '&sizeLimit=' + settings.sizeLimit;
  101. if (settings.simUploadLimit) data += '&simUploadLimit=' + settings.simUploadLimit;
  102. if (settings.checkScript) data += '&checkScript=' + settings.checkScript;
  103. if (settings.fileDataName) data += '&fileDataName=' + settings.fileDataName;
  104. if ($.browser.msie) {
  105. flashElement = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="' + settings.width + '" height="' + settings.height + '" id="' + $(this).attr("id") + 'Uploader" class="fileUploaderBtn">\
  106. <param name="movie" value="' + settings.uploader + '?fileUploadID=' + $(this).attr("id") + data + '" />\
  107. <param name="quality" value="high" />\
  108. <param name="wmode" value="' + settings.wmode + '" />\
  109. <param name="allowScriptAccess" value="' + settings.scriptAccess + '">\
  110. <param name="swfversion" value="9.0.0.0" />\
  111. </object>';
  112. } else {
  113. flashElement = '<embed src="' + settings.uploader + '?fileUploadID=' + $(this).attr("id") + data + '" quality="high" width="' + settings.width + '" height="' + settings.height + '" id="' + $(this).attr("id") + 'Uploader" class="fileUploaderBtn" name="' + $(this).attr("id") + 'Uploader" allowScriptAccess="' + settings.scriptAccess + '" wmode="' + settings.wmode + '" type="application/x-shockwave-flash" />';
  114. }
  115. if (settings.onInit() !== false) {
  116. $(this).css('display','none');
  117. if ($.browser.msie) {
  118. $(this).after('<div id="' + $(this).attr("id") + 'Uploader"></div>');
  119. document.getElementById($(this).attr("id") + 'Uploader').outerHTML = flashElement;
  120. } else {
  121. $(this).after(flashElement);
  122. }
  123. $("#" + $(this).attr('id') + "Uploader").after('<div id="' + $(this).attr('id') + 'Queue" class="fileUploadQueue"></div>');
  124. }
  125. $(this).bind("rfuSelect", {'action': settings.onSelect}, function(event, queueID, fileObj) {
  126. if (event.data.action(event, queueID, fileObj) !== false) {
  127. var byteSize = Math.round(fileObj.size / 1024 * 100) * .01;
  128. var suffix = 'KB';
  129. if (byteSize > 1000) {
  130. byteSize = Math.round(byteSize *.001 * 100) * .01;
  131. suffix = 'MB';
  132. }
  133. var sizeParts = byteSize.toString().split('.');
  134. if (sizeParts.length > 1) {
  135. byteSize = sizeParts[0] + '.' + sizeParts[1].substr(0,2);
  136. } else {
  137. byteSize = sizeParts[0];
  138. }
  139. if (fileObj.name.length > 20) {
  140. fileName = fileObj.name.substr(0,20) + '...';
  141. } else {
  142. fileName = fileObj.name;
  143. }
  144. $('#' + $(this).attr('id') + 'Queue').append('<div id="' + $(this).attr('id') + queueID + '" class="fileUploadQueueItem">\
  145. <div class="cancel">\
  146. <a href="javascript:$(\'#' + $(this).attr('id') + '\').fileUploadCancel(\'' + queueID + '\')"><img src="' + settings.cancelImg + '" border="0" /></a>\
  147. </div>\
  148. <span class="fileName">' + fileName + ' (' + byteSize + suffix + ')</span><span class="percentage">&nbsp;</span>\
  149. <div class="fileUploadProgress" style="width: 100%;">\
  150. <div id="' + $(this).attr('id') + queueID + 'ProgressBar" class="fileUploadProgressBar" style="width: 1px; height: 3px;"></div>\
  151. </div>\
  152. </div>');
  153. }
  154. });
  155. if (typeof(settings.onSelectOnce) == 'function') {
  156. $(this).bind("rfuSelectOnce", settings.onSelectOnce);
  157. }
  158. $(this).bind("rfuCheckExist", {'action': settings.onCheck}, function(event, checkScript, fileQueue, folder, single) {
  159. var postData = new Object();
  160. postData.folder = pagePath + folder;
  161. for (var queueID in fileQueue) {
  162. postData[queueID] = fileQueue[queueID];
  163. if (single) {
  164. var singleFileID = queueID;
  165. }
  166. }
  167. $.post(checkScript, postData, function(data) {
  168. for(var key in data) {
  169. if (event.data.action(event, checkScript, fileQueue, folder, single) !== false) {
  170. var replaceFile = confirm('Do you want to replace the file \'' + data[key] + '\'?');
  171. if (!replaceFile) {
  172. document.getElementById($(event.target).attr('id') + 'Uploader').cancelFileUpload(key);
  173. }
  174. }
  175. }
  176. if (single) {
  177. document.getElementById($(event.target).attr('id') + 'Uploader').startFileUpload(singleFileID, true);
  178. } else {
  179. document.getElementById($(event.target).attr('id') + 'Uploader').startFileUpload(null, true);
  180. }
  181. }, "json");
  182. });
  183. $(this).bind("rfuCancel", {'action': settings.onCancel}, function(event, queueID, fileObj, data) {
  184. if (event.data.action(event, queueID, fileObj, data) !== false) {
  185. $("#" + $(this).attr('id') + queueID).fadeOut(250, function() { $("#" + $(this).attr('id') + queueID).remove()});
  186. }
  187. });
  188. $(this).bind("rfuClearQueue", {'action': settings.onClearQueue}, function() {
  189. if (event.data.action() !== false) {
  190. $('#' + $(this).attr('id') + 'Queue').contents().fadeOut(250, function() {$('#' + $(this).attr('id') + 'Queue').empty()});
  191. }
  192. });
  193. $(this).bind("rfuError", {'action': settings.onError}, function(event, queueID, fileObj, errorObj) {
  194. if (event.data.action(event, queueID, fileObj, errorObj) !== false) {
  195. $("#" + $(this).attr('id') + queueID + " .fileName").text(errorObj.type + " Error - " + fileObj.name);
  196. $("#" + $(this).attr('id') + queueID).css({'border': '3px solid #FBCBBC', 'background-color': '#FDE5DD'});
  197. }
  198. });
  199. $(this).bind("rfuProgress", {'action': settings.onProgress, 'toDisplay': settings.displayData}, function(event, queueID, fileObj, data) {
  200. if (event.data.action(event, queueID, fileObj, data) !== false) {
  201. $("#" + $(this).attr('id') + queueID + "ProgressBar").css('width', data.percentage + '%');
  202. if (event.data.toDisplay == 'percentage') displayData = ' - ' + data.percentage + '%';
  203. if (event.data.toDisplay == 'speed') displayData = ' - ' + data.speed + 'KB/s';
  204. if (event.data.toDisplay == null) displayData = ' ';
  205. $("#" + $(this).attr('id') + queueID + " .percentage").text(displayData);
  206. }
  207. });
  208. $(this).bind("rfuComplete", {'action': settings.onComplete}, function(event, queueID, fileObj, response, data) {
  209. if (event.data.action(event, queueID, fileObj, unescape(response), data) !== false) {
  210. $("#" + $(this).attr('id') + queueID).fadeOut(250, function() { $("#" + $(this).attr('id') + queueID).remove()});
  211. $("#" + $(this).attr('id') + queueID + " .percentage").text(' - Completed');
  212. }
  213. });
  214. if (typeof(settings.onAllComplete) == 'function') {
  215. $(this).bind("rfuAllComplete", settings.onAllComplete);
  216. }
  217. });
  218. }
  219. },
  220. fileUploadSettings:function(settingName, settingValue) {
  221. $(this).each(function() {
  222. document.getElementById($(this).attr('id') + 'Uploader').updateSettings(settingName,settingValue);
  223. });
  224. },
  225. fileUploadStart:function(queueID) {
  226. $(this).each(function() {
  227. document.getElementById($(this).attr('id') + 'Uploader').startFileUpload(queueID, false);
  228. });
  229. },
  230. fileUploadCancel:function(queueID) {
  231. $(this).each(function() {
  232. document.getElementById($(this).attr('id') + 'Uploader').cancelFileUpload(queueID);
  233. });
  234. },
  235. fileUploadClearQueue:function() {
  236. $(this).each(function() {
  237. document.getElementById($(this).attr('id') + 'Uploader').clearFileUploadQueue();
  238. });
  239. }
  240. })
  241. })(jQuery);