Created
September 7, 2016 16:22
-
-
Save Bobz-zg/def7645ca2ec2b85875f217cfab1e682 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($) { | |
function triggerError($err, $status) { | |
$msg = false; | |
$errors = { | |
'e100' : "Something went wrong, please try again later", | |
'e101' : "Please enter you'r username and password", | |
'e102' : "Please enter you'r email", | |
'e103' : "Please populate all fields marked with *", | |
'e104' : "Only JPEG image format is allowed, please select another image", | |
'e105' : "You have selected photo larger then 2Mb, please select smaller photo" | |
}; | |
for (var k1 in $errors) { | |
if (k1 === $err) { | |
$msg = $errors[$err]; | |
} | |
} | |
if (!$msg) { | |
for (var k2 in $err) { | |
$msg = $err[k2][0]; | |
} | |
} | |
$status.html($msg).attr('class', 'alert-warning'); | |
return; | |
} | |
/** | |
* Upload photo | |
*/ | |
function uploadPhoto($files) { | |
var data = new FormData(); | |
data.append('nonce', tmi.nonce); | |
console.log(data); | |
/** | |
* AJAX Call | |
*/ | |
$.ajax({ | |
url: tmi.ajax_url, | |
data: { | |
action: 'do_upload_photo', | |
nonce: tmi.nonce, | |
}, | |
type: 'POST', | |
cache: false, | |
dataType: 'json', | |
//processData: false, | |
contentType: false, | |
success: function( data, textStatus, XMLHttpRequest ) { | |
// console.log( data ); | |
//console.log( XMLHttpRequest ); | |
}, | |
error: function( MLHttpRequest, textStatus, errorThrown ) { | |
$status.html(errorThrown).attr('class', 'alert-success'); | |
/*console.log( MLHttpRequest ); | |
console.log( textStatus ); | |
console.log( errorThrown );*/ | |
}, | |
complete: function() { | |
$button.removeAttr('disabled'); | |
} | |
}); | |
} | |
/** | |
* Register User profile | |
*/ | |
$('.form-register').submit( function(event) { | |
if(event.preventDefault) { event.preventDefault(); } | |
$error = false; | |
$file = false; | |
$status = $(this).find('#response'); | |
$button = $(document.activeElement); | |
$data = $(this).serializeArray(); | |
$photo = $('#photo'); | |
$(this).find('.error').removeClass('error'); | |
$button.attr('disabled', 'disabled'); | |
$status.html('Please wait').attr('class', 'alert-doing'); | |
/** | |
* Simple verify form | |
*/ | |
$.each( $data, function(index, obj) { | |
if (obj.name !== 'website') { | |
if (!obj.value) { | |
$error = 'e103'; | |
} | |
} | |
}); | |
/** | |
* Verify photo | |
*/ | |
if ($photo[0].files.length) { | |
$file = $photo[0].files[0]; | |
$size = parseInt($file.size/1000); | |
$ext = ['image/jpg', 'image/png', 'image/gif', 'image/jpeg']; | |
$alowed = 2048; | |
if ($.inArray($file.type, $ext) == -1 ){ | |
$error = 'e104'; | |
} | |
if ($size > $alowed) { | |
$error = 'e105'; | |
} | |
// uploadPhoto($photo); | |
/*$preview = '<img src="' + URL.createObjectURL($file) + '" />'; | |
$($preview).insertBefore($photo);*/ | |
// return; | |
} | |
/** | |
* Trigger Error | |
*/ | |
if ($error) { | |
$button.removeAttr('disabled'); | |
triggerError($error, $status); | |
return; | |
} | |
/** | |
* AJAX Call | |
*/ | |
$.ajax({ | |
url: tmi.ajax_url, | |
data: { | |
action: 'do_register_profile', | |
nonce: tmi.nonce, | |
data: $data | |
}, | |
type: 'post', | |
dataType: 'json', | |
success: function( data, textStatus, XMLHttpRequest ) { | |
if ('errors' in data.error) { | |
triggerError(data.error.errors, $status); | |
if ('fields' in data) { | |
$.each(data.fields, function(index, name) { | |
$('input[name='+name+']').addClass('error'); | |
}); | |
} | |
} | |
else if ('status' in data) { | |
if (data.status === 200) { | |
// $status.html(data.message).attr('class', 'alert-success'); | |
} | |
} | |
/*console.log( data ); | |
console.log( XMLHttpRequest );*/ | |
}, | |
error: function( MLHttpRequest, textStatus, errorThrown ) { | |
$status.html(errorThrown).attr('class', 'alert-success'); | |
/*console.log( MLHttpRequest ); | |
console.log( textStatus ); | |
console.log( errorThrown );*/ | |
}, | |
complete: function() { | |
$button.removeAttr('disabled'); | |
} | |
}); | |
}); | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment