PageRenderTime 53ms CodeModel.GetById 20ms app.highlight 28ms RepoModel.GetById 2ms app.codeStats 0ms

/BlogEngine/BlogEngine.NET/admin/uploadify/jquery.uploadify.v2.1.4.js

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