Skip to content

Instantly share code, notes, and snippets.

@nikolab
Created September 17, 2020 15:34
Show Gist options
  • Save nikolab/d3005d38bf1593609a2104451e371efe to your computer and use it in GitHub Desktop.
Save nikolab/d3005d38bf1593609a2104451e371efe to your computer and use it in GitHub Desktop.
<script>
(function($){
$(document).on('click', 'button.close', function(e){
e.preventDefault();
window.location.hash = '';
});
$(window).on('hashchange', function(){
$('#update_passport, #uploader, #register, #login, #challenge_login, #challenge_uploader').removeClass('active');
if(window.location.hash === '#uploader'){
if(<?php echo get_current_user_id(); ?>){
$('#uploader').addClass('active');
}
else {
window.location.hash = '#login'
}
}
else if(window.location.hash === '#challenge_uploader'){
if(<?php echo get_current_user_id(); ?>){
$('#challenge_uploader').addClass('active');
}
else {
window.location.hash = '#challenge_login'
}
}
else if(window.location.hash === '#challenge_login'){
$('#challenge_login').addClass('active');
}
else if(window.location.hash === '#register'){
$('#register').addClass('active');
}
else if(window.location.hash === '#login'){
$('#login').addClass('active');
}
else if(window.location.hash === '#update-passport'){
$('#update_passport').addClass('active');
}
}).trigger('hashchange');
function makeDroppable(element, callback) {
var input = document.createElement('input');
input.setAttribute('type', 'file');
input.setAttribute('multiple', false);
input.style.display = 'none';
input.addEventListener('change', triggerCallback);
element.appendChild(input);
element.addEventListener('dragover', function(e) {
e.preventDefault();
e.stopPropagation();
element.classList.add('dragover');
});
element.addEventListener('dragleave', function(e) {
e.preventDefault();
e.stopPropagation();
element.classList.remove('dragover');
});
element.addEventListener('drop', function(e) {
e.preventDefault();
e.stopPropagation();
element.classList.remove('dragover');
triggerCallback(e);
});
element.addEventListener('click', function() {
input.value = null;
input.click();
});
function triggerCallback(e) {
var files;
if(e.dataTransfer) {
files = e.dataTransfer.files;
} else if(e.target) {
files = e.target.files;
}
callback.call(null, files);
}
}
var element = document.querySelector('.profile-picture');
function callback(files) {
var formData = new FormData();
formData.append("files", files);
formData.append('action', 'upload-attachment');
formData.append('async-upload', files[0]);
formData.append('name', files[0].name);
formData.append('_wpnonce', '<?php echo wp_create_nonce('media-form') ?>');
$.ajax({
url: '<?php echo admin_url('async-upload.php'); ?>',
method: 'post',
data: formData,
processData: false,
contentType: false,
dataType: 'json',
beforeSend: function(){
$('.btn.btn-submit').prop('disabled', true);
$('.profile-picture').addClass('is-loading with-image');
},
success: function(response) {
$('.btn.btn-submit').prop('disabled', false);
$('.profile-picture').removeClass('is-loading');
if(response.success === true) {
$('[name=profile_photo]').val(response.data.id);
$('.profile-picture').find('.cover').remove();
$('.profile-picture').css({
background: 'url('+URL.createObjectURL(files[0])+')'
}).append('<div class="cover" />');
}
else {
// Error
}
}
});
}
makeDroppable(element, callback);
var element = document.querySelector('.cover-picture');
function coverCallback(files) {
var formData = new FormData();
formData.append("files", files);
formData.append('action', 'upload-attachment');
formData.append('async-upload', files[0]);
formData.append('name', files[0].name);
formData.append('_wpnonce', '<?php echo wp_create_nonce('media-form') ?>');
$.ajax({
url: '<?php echo admin_url('async-upload.php'); ?>',
method: 'post',
data: formData,
processData: false,
contentType: false,
dataType: 'json',
beforeSend: function(){
$('.btn.btn-submit').prop('disabled', true);
$('.cover-picture').addClass('is-loading with-image');
},
success: function(response) {
$('.btn.btn-submit').prop('disabled', false);
$('.cover-picture').removeClass('is-loading');
if(response.success === true) {
$('[name=cover_photo]').val(response.data.id);
$('.cover-picture').find('.cover').remove();
$('.cover-picture').addClass('with-image').css({
background: 'url('+URL.createObjectURL(files[0])+')'
}).append('<div class="cover" />');
}
else {
// Error
}
}
});
}
makeDroppable(element, coverCallback);
})(jQuery)
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment