Files
Testing/mini-upload-form/assets/js/script_ctp.js
sean 0e8a8b3028
Some checks are pending
deploy-testing / deploy (push) Waiting to run
Initial testing snapshot
2026-11-17 16:15:01 -05:00

229 lines
7.0 KiB
JavaScript

var lastupload="";
var suberror=0;
jQuery(function(){
var ul = jQuery('#upload_ul ul');
jQuery('#drop a').click(function(){
// Simulate a click on the file input button
// to show the file browser dialog
jQuery('#upload_error').css( "display", "none" );
jQuery('#upload_error').html(' ');
jQuery('#upload').find('#upl').click();
});
// Initialize the jQuery File Upload plugin
jQuery('#upload').fileupload({
///This element will accept file drag/drop uploading
dropZone: jQuery(''),
acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
maxFileSize: 5000000, // 5 MB
// This function is called when a file is added to the queue;
// either via the browse button, or via drag/drop:
add: function (e, data) {
// kill any error messages
jQuery('#upload_error').css( "display", "none" );
jQuery('#upload_error').html(' ');
// kill display of previously uploaded ad
//if(jQuery('#display_ad_container').length !=0){
//}
var tpl = jQuery('<li class="working"><input type="text" value="0" data-width="48" data-height="48"'+
' data-fgColor="#0788a5" data-readOnly="1" data-bgColor="#3e4043" /><p></p><span></span></li>');
// tplerror not used
var tplerror=jQuery('<li class="error"><p></p><span></span></li>');
// regex for gif png jpe?g
var thefile=data.files[0].name.match(/(\.|\/)(gif|jpe?g|png)$/i);
var doerror=false;
// not gif jpg or png file
if(null == thefile)
{
tplerror.find('p').text(data.files[0].name) // png jpg gif files only
.append('<i>' + formatFileSize(data.files[0].size) + '</i><strong class="emsg">Error: file type not allowed!</strong>');
doerror=true;
jQuery('#upload_error').fadeIn('slow',function(){
jQuery(this).html('Error: image must be png, jpg, or gif ');
});
return false;
}
// file exceeds 5MB
if(parseFloat(data.files[0].size) > 5000000) // file too big
{
doerror=true;
tplerror.find('p').text(data.files[0].name)
.append('<i>' + formatFileSize(data.files[0].size) + '</i><strong class="emsg">Error: file too big!</strong>');
jQuery('#upload_error').fadeIn('slow',function(){
jQuery(this).html('Error: file too big!');
});
return false;
}
if(doerror) // should not be called any longer
{
data.context = tplerror.appendTo(ul);
tplerror.find('span').click(function(){
tplerror.fadeOut('slow',function(){
tplerror.remove();
});
});
return false;
}
// Append the file name and file size
tpl.find('p').text(data.files[0].name)
.append('<i>' + formatFileSize(data.files[0].size) + '</i>');
// Add the HTML to the UL element
data.context = tpl.appendTo(ul);//context depreciate;
// Initialize the knob plugin
tpl.find('input').knob();
// Listen for clicks on the cancel icon
tpl.find('span').click(function(){
if(tpl.hasClass('working')){
jqXHR.abort();
}
tpl.fadeOut(function(){
tpl.remove();
});
});
// Automatically upload the file once it is added to the queue
var jqXHR = data.submit();
jQuery('#adlink').val("");
},
progress: function(e, data){
// Calculate the completion percentage of the upload
var progress = parseInt(data.loaded / data.total * 100, 10);
// Update the hidden input field and trigger a change
// so that the jQuery knob plugin knows to update the dial
data.context.find('input').val(progress).change();
if(progress == 100){
data.context.removeClass('working');
}
},
fail:function(e, data){
// Something has gone wrong!
data.context.addClass('error');
},
complete: function(e,data){
//alert(data);
},
done: function (e, data) {
//alert(data.result);
var is_json = true;try {var obj = jQuery.parseJSON(data.result);} catch(err) {is_json = false;}
if(is_json){
var obj = jQuery.parseJSON(data.result);
if(obj.status=="success")
{
var iurl=obj.imgurl;
var iwidth=obj.iwidth;
var iheight=obj.iheight;
var upadid=obj.adid;
var iname=obj.iname;
var oext=obj.extension;
var hcid=obj.hcid;
var aimg=iurl+'/'+iname+'_full.'+oext+'?id='+hcid+'';
var timg=iurl+'/'+iname+'_thumbnail.'+oext+'?id='+hcid+'';
var oimg=iurl+'/'+iname+'.'+oext+'?id='+hcid+'';
var nslides=theslider.numSlides;
theslider.appendSlide('<a class="rsImg" data-rsBigImg="'+oimg+'" href="'+aimg+'"><img width="96" height="90" class="rsTmb" src="'+timg+'" style="width:auto;height:72px;margin:0px auto;" /></a>');
theslider.goTo(nslides);
var iimage='<img src="'+iurl+'/'+iname+'_medium.'+oext+'" width="210" height="auto" />';
var viewimage=iimage;
//jQuery('.royalSlider').royalSlider('appendSlide', '<a class="rsSlide" href="'+aimg+'"></a>', 1);
//jQuery('#img_preview').html(viewimage).hide().fadeIn(1000);
jQuery('#sub_success').css('display','block');
if(obj.badurl==1)
{
//jQuery('#upload_error').fadeIn('slow',function(){
//jQuery(this).html("Upload Successful. Warning: Please double check the url you provided to make sure it&#39;s not broken and correct as required.");
//});
}
} //end status eq success
else
{
jQuery('#upload_error').fadeIn('slow',function(){
jQuery(this).html(obj.status);
});
jQuery('#upload_ul ul li:first').remove();
}//end of status not eq success
} //end of is_json;
else
{
jQuery('#upload_error').fadeIn('slow',function(){
jQuery(this).html('Error processing request. Please try again. If the error persists, please contact <a href="http://bateworld.com/help_contact.php" target="_blank">bateworld customer satisfaction</a> !');
});
jQuery('#upload_ul ul li:first').remove();
}
data.jqXHR;
} // end of done
});
// Prevent the default action when a file is dropped on the window
jQuery(document).on('drop dragover', function (e) {
e.preventDefault();
});
// Helper function that formats the file sizes
function formatFileSize(bytes) {
if (typeof bytes !== 'number') {
return '';
}
if (bytes >= 1000000000) {
return (bytes / 1000000000).toFixed(2) + ' GB';
}
if (bytes >= 1000000) {
return (bytes / 1000000).toFixed(2) + ' MB';
}
return (bytes / 1000).toFixed(2) + ' KB';
}
});