PageRenderTime 43ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/rmcommon/tags/rmcommon-2.0.95/include/js/jquery.uploadify.js

http://bitcero-modules.googlecode.com/
JavaScript | 259 lines | 234 code | 1 blank | 24 comment | 65 complexity | c4c68fc184c9558cb7fa9038d933214f MD5 | raw file
Possible License(s): LGPL-2.1
  1. /*
  2. Uploadify v2.1.0
  3. Release Date: August 24, 2009
  4. Copyright (c) 2009 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. var queuefiles = new Array();
  22. if(jQuery)(
  23. function(jQuery){
  24. jQuery.extend(jQuery.fn,{
  25. uploadify:function(options) {
  26. jQuery(this).each(function(){
  27. settings = jQuery.extend({
  28. id : jQuery(this).attr('id'), // The ID of the object being Uploadified
  29. uploader : 'uploadify.swf', // The path to the uploadify swf file
  30. script : 'uploadify.php', // The path to the uploadify backend upload script
  31. expressInstall : null, // The path to the express install swf file
  32. folder : '', // The path to the upload folder
  33. height : 30, // The height of the flash button
  34. width : 110, // The width of the flash button
  35. cancelImg : 'cancel.png', // The path to the cancel image for the default file queue item container
  36. wmode : 'opaque', // The wmode of the flash file
  37. scriptAccess : 'sameDomain', // Set to "always" to allow script access across domains
  38. fileDataName : 'Filedata', // The name of the file collection object in the backend upload script
  39. method : 'POST', // The method for sending variables to the backend upload script
  40. queueSizeLimit : 999, // The maximum size of the file queue
  41. simUploadLimit : 1, // The number of simultaneous uploads allowed
  42. queueID : false, // The optional ID of the queue container
  43. displayData : 'percentage', // Set to "speed" to show the upload speed in the default queue item
  44. onInit : function() {}, // Function to run when uploadify is initialized
  45. onSelect : function() {}, // Function to run when a file is selected
  46. onQueueFull : function() {}, // Function to run when the queue reaches capacity
  47. onCheck : function() {}, // Function to run when script checks for duplicate files on the server
  48. onCancel : function() {}, // Function to run when an item is cleared from the queue
  49. onError : function() {}, // Function to run when an upload item returns an error
  50. onProgress : function() {}, // Function to run each time the upload progress is updated
  51. onComplete : function() {}, // Function to run when an upload is completed
  52. onAllComplete : function() {} // Functino to run when all uploads are completed
  53. }, options);
  54. var pagePath = location.pathname;
  55. pagePath = pagePath.split('/');
  56. pagePath.pop();
  57. pagePath = pagePath.join('/') + '/';
  58. var data = {};
  59. data.uploadifyID = settings.id;
  60. data.pagepath = pagePath;
  61. if (settings.buttonImg) data.buttonImg = escape(settings.buttonImg);
  62. if (settings.buttonText) data.buttonText = escape(settings.buttonText);
  63. if (settings.rollover) data.rollover = true;
  64. data.script = settings.script;
  65. data.folder = escape(settings.folder);
  66. if (settings.scriptData) {
  67. var scriptDataString = '';
  68. for (var name in settings.scriptData) {
  69. scriptDataString += '&' + name + '=' + settings.scriptData[name];
  70. }
  71. data.scriptData = escape(scriptDataString.substr(1));
  72. }
  73. data.width = settings.width;
  74. data.height = settings.height;
  75. data.wmode = settings.wmode;
  76. data.method = settings.method;
  77. data.queueSizeLimit = settings.queueSizeLimit;
  78. data.simUploadLimit = settings.simUploadLimit;
  79. if (settings.hideButton) data.hideButton = true;
  80. if (settings.fileDesc) data.fileDesc = settings.fileDesc;
  81. if (settings.fileExt) data.fileExt = settings.fileExt;
  82. if (settings.multi) data.multi = true;
  83. if (settings.auto) data.auto = true;
  84. if (settings.sizeLimit) data.sizeLimit = settings.sizeLimit;
  85. if (settings.checkScript) data.checkScript = settings.checkScript;
  86. if (settings.fileDataName) data.fileDataName = settings.fileDataName;
  87. if (settings.queueID) data.queueID = settings.queueID;
  88. if (settings.onInit() !== false) {
  89. jQuery(this).css('display','none');
  90. jQuery(this).after('<div id="' + jQuery(this).attr('id') + 'Uploader"></div>');
  91. 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});
  92. if (settings.queueID == false) {
  93. jQuery("#" + jQuery(this).attr('id') + "Uploader").after('<div id="' + jQuery(this).attr('id') + 'Queue" class="uploadifyQueue"></div>');
  94. }
  95. }
  96. if (typeof(settings.onOpen) == 'function') {
  97. jQuery(this).bind("uploadifyOpen", settings.onOpen);
  98. }
  99. jQuery(this).bind("uploadifySelect", {'action': settings.onSelect, 'queueID': settings.queueID}, function(event, ID, fileObj) {
  100. if (event.data.action(event, ID, fileObj) !== false) {
  101. var byteSize = Math.round(fileObj.size / 1024 * 100) * .01;
  102. var suffix = 'KB';
  103. if (byteSize > 1000) {
  104. byteSize = Math.round(byteSize *.001 * 100) * .01;
  105. suffix = 'MB';
  106. }
  107. var sizeParts = byteSize.toString().split('.');
  108. if (sizeParts.length > 1) {
  109. byteSize = sizeParts[0] + '.' + sizeParts[1].substr(0,2);
  110. } else {
  111. byteSize = sizeParts[0];
  112. }
  113. if (fileObj.name.length > 20) {
  114. fileName = fileObj.name.substr(0,20) + '...';
  115. } else {
  116. fileName = fileObj.name;
  117. }
  118. queue = '#' + jQuery(this).attr('id') + 'Queue';
  119. if (event.data.queueID) {
  120. queue = '#' + event.data.queueID;
  121. }
  122. jQuery(queue).append('<div id="' + jQuery(this).attr('id') + ID + '" class="uploadifyQueueItem">\
  123. <div class="cancel">\
  124. <a href="javascript:jQuery(\'#' + jQuery(this).attr('id') + '\').uploadifyCancel(\'' + ID + '\')"><img src="' + settings.cancelImg + '" border="0" /></a>\
  125. </div>\
  126. <span class="fileName">' + fileName + ' (' + byteSize + suffix + ')</span><span class="percentage"></span>\
  127. <div class="uploadifyProgress">\
  128. <div id="' + jQuery(this).attr('id') + ID + 'ProgressBar" class="uploadifyProgressBar"><!--Progress Bar--></div>\
  129. </div>\
  130. </div>');
  131. }
  132. });
  133. if (typeof(settings.onSelectOnce) == 'function') {
  134. jQuery(this).bind("uploadifySelectOnce", settings.onSelectOnce);
  135. }
  136. jQuery(this).bind("uploadifyQueueFull", {'action': settings.onQueueFull}, function(event, queueSizeLimit) {
  137. if (event.data.action(event, queueSizeLimit) !== false) {
  138. alert('The queue is full. The max size is ' + queueSizeLimit + '.');
  139. }
  140. });
  141. jQuery(this).bind("uploadifyCheckExist", {'action': settings.onCheck}, function(event, checkScript, fileQueueObj, folder, single) {
  142. var postData = new Object();
  143. postData = fileQueueObj;
  144. postData.folder = pagePath + folder;
  145. if (single) {
  146. for (var ID in fileQueueObj) {
  147. var singleFileID = ID;
  148. }
  149. }
  150. jQuery.post(checkScript, postData, function(data) {
  151. for(var key in data) {
  152. if (event.data.action(event, checkScript, fileQueueObj, folder, single) !== false) {
  153. var replaceFile = confirm("Do you want to replace the file " + data[key] + "?");
  154. if (!replaceFile) {
  155. document.getElementById(jQuery(event.target).attr('id') + 'Uploader').cancelFileUpload(key, true,true);
  156. }
  157. }
  158. }
  159. if (single) {
  160. document.getElementById(jQuery(event.target).attr('id') + 'Uploader').startFileUpload(singleFileID, true);
  161. } else {
  162. document.getElementById(jQuery(event.target).attr('id') + 'Uploader').startFileUpload(null, true);
  163. }
  164. }, "json");
  165. });
  166. jQuery(this).bind("uploadifyCancel", {'action': settings.onCancel}, function(event, ID, fileObj, data, clearFast) {
  167. if (event.data.action(event, ID, fileObj, data, clearFast) !== false) {
  168. var fadeSpeed = (clearFast == true) ? 0 : 250;
  169. jQuery("#" + jQuery(this).attr('id') + ID).fadeOut(fadeSpeed, function() { jQuery(this).remove() });
  170. }
  171. });
  172. if (typeof(settings.onClearQueue) == 'function') {
  173. jQuery(this).bind("uploadifyClearQueue", settings.onClearQueue);
  174. }
  175. var errorArray = [];
  176. jQuery(this).bind("uploadifyError", {'action': settings.onError}, function(event, ID, fileObj, errorObj) {
  177. if (event.data.action(event, ID, fileObj, errorObj) !== false) {
  178. var fileArray = new Array(ID, fileObj, errorObj);
  179. errorArray.push(fileArray);
  180. jQuery("#" + jQuery(this).attr('id') + ID + " .percentage").text(" - " + errorObj.type + " Error");
  181. jQuery("#" + jQuery(this).attr('id') + ID).addClass('uploadifyError');
  182. }
  183. });
  184. jQuery(this).bind("uploadifyProgress", {'action': settings.onProgress, 'toDisplay': settings.displayData}, function(event, ID, fileObj, data) {
  185. if (event.data.action(event, ID, fileObj, data) !== false) {
  186. jQuery("#" + jQuery(this).attr('id') + ID + "ProgressBar").css('width', data.percentage + '%');
  187. if (event.data.toDisplay == 'percentage') displayData = ' - ' + data.percentage + '%';
  188. if (event.data.toDisplay == 'speed') displayData = ' - ' + data.speed + 'KB/s';
  189. if (event.data.toDisplay == null) displayData = ' ';
  190. jQuery("#" + jQuery(this).attr('id') + ID + " .percentage").text(displayData);
  191. }
  192. });
  193. jQuery(this).bind("uploadifyComplete", {'action': settings.onComplete}, function(event, ID, fileObj, response, data) {
  194. if (event.data.action(event, ID, fileObj, unescape(response), data) !== false) {
  195. jQuery("#" + jQuery(this).attr('id') + ID + " .percentage").text(' - Completed');
  196. jQuery("#" + jQuery(this).attr('id') + ID).fadeOut(250, function() { jQuery(this).remove()});
  197. }
  198. });
  199. if (typeof(settings.onAllComplete) == 'function') {
  200. jQuery(this).bind("uploadifyAllComplete", {'action': settings.onAllComplete}, function(event, uploadObj) {
  201. if (event.data.action(event, uploadObj) !== false) {
  202. errorArray = [];
  203. }
  204. });
  205. }
  206. });
  207. },
  208. uploadifySettings:function(settingName, settingValue, resetObject) {
  209. var returnValue = false;
  210. jQuery(this).each(function() {
  211. if (settingName == 'scriptData' && settingValue != null) {
  212. if (resetObject) {
  213. var scriptData = settingValue;
  214. } else {
  215. var scriptData = jQuery.extend(settings.scriptData, settingValue);
  216. }
  217. var scriptDataString = '';
  218. for (var name in scriptData) {
  219. scriptDataString += '&' + name + '=' + escape(scriptData[name]);
  220. }
  221. settingValue = scriptDataString.substr(1);
  222. }
  223. returnValue = document.getElementById(jQuery(this).attr('id') + 'Uploader').updateSettings(settingName, settingValue);
  224. });
  225. if (settingValue == null) {
  226. if (settingName == 'scriptData') {
  227. var returnSplit = unescape(returnValue).split('&');
  228. var returnObj = new Object();
  229. for (var i = 0; i < returnSplit.length; i++) {
  230. var iSplit = returnSplit[i].split('=');
  231. returnObj[iSplit[0]] = iSplit[1];
  232. }
  233. returnValue = returnObj;
  234. }
  235. return returnValue;
  236. }
  237. },
  238. uploadifyUpload:function(ID) {
  239. jQuery(this).each(function() {
  240. document.getElementById(jQuery(this).attr('id') + 'Uploader').startFileUpload(ID, false);
  241. });
  242. },
  243. uploadifyCancel:function(ID) {
  244. jQuery(this).each(function() {
  245. document.getElementById(jQuery(this).attr('id') + 'Uploader').cancelFileUpload(ID, true, false);
  246. });
  247. },
  248. uploadifyClearQueue:function() {
  249. jQuery(this).each(function() {
  250. document.getElementById(jQuery(this).attr('id') + 'Uploader').clearFileUploadQueue(false);
  251. });
  252. }
  253. })
  254. })(jQuery);