Files
Testing/mini-upload-form/assets/js/script.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

269 lines
8.5 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
if(document.getElementById("adlink").value =="")
{
jQuery('#upload_error').fadeIn('slow',function(){
jQuery(this).html('Please enter a URL for a website or page !');
jQuery('#adlink').focus();
});
return false;
}
jQuery('#upload_error').css( "display", "none" );
jQuery('#upload_error').html(' ');
if(document.getElementById("adlink").value != "")
{
var checkurl = RegExp(/(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!-\/]))?/);
var adlink = jQuery('#adlink').val();
if (!checkurl.test(adlink) ){
jQuery('#upload_error').fadeIn('slow',function(){
jQuery(this).html('Please provide a valid URL, <i>http://www.example.com</i> !');
jQuery('#adlink').focus();
});
return false;
}
jQuery('#upload').find('#upl').click();
}
});
jQuery('#ms_instructions a').click(function(){
jQuery("#upload_form").fadeIn(500);
});
// 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('&nbsp;');
// kill display of previously uploaded ad
//if(jQuery('#display_ad_container').length !=0){
jQuery("#ms_ad_preview").fadeOut(10, function() { jQuery("#ms_ad_preview").html(); });
//}
// website link input empty //////////////
var input = jQuery('#adlink');
data.formData = {adlink: input.val()};
if (!data.formData.adlink) {
jQuery('#upload_error').fadeIn('slow',function(){
jQuery(this).html('Please provide a link to a website !');
input.focus();
suberror=1;
});
return false;
}
// website link input contains invalid URL //////////////////
var checkurl = RegExp(/(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!-\/]))?/);
var adlink = jQuery('#adlink').val();
if (!checkurl.test(adlink) ){
jQuery('#upload_error').fadeIn('slow',function(){
jQuery(this).html('Please provide a valid URL, <i>http://www.example.com</i> !');
input.focus();
});
return false;
}
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) {
var is_json = true;try {var obj = jQuery.parseJSON(data.result);} catch(err) {is_json = false;}
//alert(data.result);
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 iimage='<img src="'+iurl+'" width="'+iwidth+'" height="'+iheight+'" />';
var viewimage='<div id="display_ad_container_'+upadid+'"><div class="spacer" style="height:20px;">&nbsp;</div><h3>This is what your ad will look like.</h3><div id="display_ad" style="text-align:center;">'+iimage+'</div></div>';
jQuery('#ms_ad_preview').html(viewimage).hide().fadeIn(1000);
//jQuery(viewimage)
//.appendTo('#member_services')
//.hide().fadeIn(1000);
jQuery('#userads_tab tbody').prepend(obj.userad);
document.getElementById('userads').style.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';
}
});