| Current Path : /home/helpink/www/components/com_jbusinessdirectory/assets/libraries/jquery/ |
| Current File : /home/helpink/www/components/com_jbusinessdirectory/assets/libraries/jquery/jbd.upload.js |
/*
* jbd.upload v1.0.0
*
* Copyright (c) 2025 CMSJunkie
* Dual licensed under the MIT and GPL licenses.
*
* http://www.cmsjunkie.com
*
* upload.js is a modernized version that uses XMLHttpRequest
*/
(function($) {
// param function (processes data into an array of key/value pairs)
function param(data) {
if ($.isArray(data)) {
return data;
}
var params = [];
function add(name, value) {
params.push({name:name, value:value});
}
if (typeof data === 'object') {
$.each(data, function(name) {
if ($.isArray(this)) {
$.each(this, function() {
add(name, this);
});
} else {
add(name, $.isFunction(this) ? this() : this);
}
});
} else if (typeof data === 'string') {
$.each(data.split('&'), function() {
var paramEntry = $.map(this.split('='), function(v) {
return decodeURIComponent(v.replace(/\+/g, ' '));
});
add(paramEntry[0], paramEntry[1]);
});
}
return params;
}
// parseXml function (parses a string to an XML document)
function parseXml(text) {
if (window.DOMParser) {
try {
return new DOMParser().parseFromString(text, 'application/xml');
} catch (e) {
// Fall through to ActiveXObject for IE or if DOMParser fails
}
}
// For older IE versions
try {
var xml = new ActiveXObject('Microsoft.XMLDOM');
xml.async = false;
xml.loadXML(text);
return xml;
} catch (e) {
console.error("Error parsing XML:", e);
return null;
}
}
// handleResponse function (processes XHR response based on type)
function handleResponse(responseText, responseXML, type) {
var data;
switch (type) {
case 'xml':
// Prefer responseXML if available and valid, otherwise parse responseText
if (responseXML && responseXML.documentElement && responseXML.documentElement.nodeName !== 'parsererror') {
data = responseXML;
} else {
data = parseXml(responseText);
}
break;
case 'json':
try {
data = JSON.parse(responseText);
} catch (e) {
console.error("Error parsing JSON:", e, "Response text:", responseText);
data = null;
}
break;
case 'script':
// Evaluating script from response is a security risk.
// Included for compatibility with original plugin behavior.
try {
$.globalEval(responseText);
data = responseText; // Or indicate success, e.g., true
} catch (e) {
console.error("Error evaluating script:", e);
data = null;
}
break;
default:
// Default to raw response text if no type or unknown type
data = responseText;
break;
}
return data;
}
$.fn.upload = function(url, data, callback, type) {
var self = this; // Keep 'this' context for the callback
// Adjust arguments if data is omitted and callback is the second argument
if ($.isFunction(data)) {
type = callback;
callback = data;
data = {};
}
var formData = new FormData();
// 1. Append files and other form data from elements in 'self' (the jQuery selection)
self.each(function() {
var el = this;
if (!el.name) { // Skip elements without a 'name' attribute
return;
}
if (el.type === 'file') {
if (el.files) {
for (var i = 0; i < el.files.length; i++) {
formData.append(el.name, el.files[i]);
}
}
} else if (el.type === 'checkbox' || el.type === 'radio') {
if (el.checked) {
formData.append(el.name, $(el).val());
}
} else {
var value = $(el).val();
if (value !== null && value !== undefined) {
if ($.isArray(value)) { // For select-multiple
$.each(value, function(idx, v_item) {
formData.append(el.name, v_item);
});
} else {
formData.append(el.name, value);
}
}
}
});
// 2. Append additional data from the 'data' object argument
var paramsArray = param(data);
$.each(paramsArray, function(i, p) {
formData.append(p.name, p.value);
});
var xhr = new XMLHttpRequest();
xhr.open('POST', url, true);
xhr.onload = function() {
var responseData;
if (xhr.status >= 200 && xhr.status < 300) {
responseData = handleResponse(xhr.responseText, xhr.responseXML, type);
} else {
console.error('Upload failed with status:', xhr.status, xhr.statusText);
// In case of HTTP error, responseData might be null or the error response text
// For consistency, let handleResponse try to parse it, or set to null.
responseData = handleResponse(xhr.responseText, xhr.responseXML, type);
if (xhr.status >= 400 && responseData === xhr.responseText) { // If handleResponse defaulted to text for an error
responseData = null; // Prefer null if it was an error and raw text was returned
}
}
if (callback) {
callback.call(self, responseData);
}
};
xhr.onerror = function() {
console.error('Upload failed due to network error.');
if (callback) {
callback.call(self, null); // Pass null as data for network errors
}
};
xhr.send(formData);
return this; // Maintain jQuery chainability
};
})(jQuery);