szurubooru/public_html/media/js/upload.js

191 lines
3.7 KiB
JavaScript
Raw Normal View History

2013-10-07 00:44:17 +02:00
$(function()
{
var tags = [];
$.getJSON('/tags?json', function(data)
{
tags = data['tags'];
});
2013-10-07 00:44:17 +02:00
var handler = $('#file-handler');
2013-10-07 00:44:17 +02:00
handler.on('dragenter', function(e)
{
$(this).addClass('active');
});
handler.on('dragleave', function(e)
{
$(this).removeClass('active');
});
handler.on('dragover', function(e)
{
e.preventDefault();
});
handler.on('drop', function(e)
{
e.preventDefault();
handleFiles(e.originalEvent.dataTransfer.files);
$(this).trigger('dragleave');
});
handler.on('click', function(e)
{
$(':file').show().focus().trigger('click').hide();
});
$(':file').change(function(e)
{
handleFiles(this.files);
});
$('.post .remove-trigger').on('click', function()
{
$(this).parents('.post').slideUp(function()
{
$(this).remove();
});
2013-10-09 00:58:49 +02:00
if ($('#upload-step2 .post').length == 1)
{
$('#upload-step2').slideUp();
$('#upload-no-posts').slideDown();
}
2013-10-07 00:44:17 +02:00
});
2013-10-09 00:58:49 +02:00
$('#the-submit').click(function(e)
2013-10-07 00:44:17 +02:00
{
e.preventDefault();
2013-10-09 00:58:49 +02:00
var theSubmit = $(this);
theSubmit.addClass('inactive');
var posts = $('#upload-step2 .post');
2013-10-07 00:44:17 +02:00
if (posts.length == 0)
{
2013-10-09 00:58:49 +02:00
//shouldn't happen
2013-10-07 00:44:17 +02:00
alert('No posts to upload!');
return;
}
2013-10-09 00:58:49 +02:00
var ajaxCalls = [];
2013-10-07 00:44:17 +02:00
posts.each(function()
{
var postDom = $(this);
2013-10-09 00:58:49 +02:00
var url = postDom.find('form').attr('action') + '?json';
2013-10-07 00:44:17 +02:00
var file = postDom.data('file');
var tags = postDom.find('[name=tags]').val();
2013-10-07 23:13:41 +02:00
var safety = postDom.find('[name=safety]:checked').val();
2013-10-07 00:44:17 +02:00
var fd = new FormData();
fd.append('file', file);
fd.append('tags', tags);
fd.append('safety', safety);
2013-10-09 00:58:49 +02:00
postDom.find(':input').attr('readonly', true);
postDom.addClass('inactive');
var ajaxData =
2013-10-07 00:44:17 +02:00
{
url: url,
data: fd,
processData: false,
contentType: false,
type: 'POST',
2013-10-09 00:58:49 +02:00
context: postDom
};
var defer = $.ajax(ajaxData)
.then(function(data)
2013-10-07 00:44:17 +02:00
{
2013-10-09 00:58:49 +02:00
data.postDom = $(this);
return data;
}).promise();
ajaxCalls.push(defer);
});
$.when.all(ajaxCalls).then(function(allData)
{
var errors = false;
for (var i in allData)
{
var data = allData[i];
var postDom = data.postDom;
if (data['success'])
{
postDom.slideUp(function()
2013-10-07 00:44:17 +02:00
{
2013-10-09 00:58:49 +02:00
$(this).remove();
});
2013-10-07 00:44:17 +02:00
}
2013-10-09 00:58:49 +02:00
else
{
postDom.removeClass('inactive');
postDom.find(':input').attr('readonly', false);
postDom.find('.alert').text(data['errorMessage']).slideDown();
errors = true;
}
}
2013-10-07 00:44:17 +02:00
2013-10-09 00:58:49 +02:00
if (errors)
{
theSubmit.removeClass('inactive');
}
else
{
window.location.href = $('#upload-step2').attr('data-redirect-url');
}
2013-10-07 00:44:17 +02:00
});
});
function handleFiles(files)
{
$('#upload-step1').fadeOut(function()
{
for (var i = 0; i < files.length; i ++)
{
var file = files[i];
var postDom = $('#post-template').clone(true);
2013-10-09 00:58:49 +02:00
postDom.find('form').submit(false);
2013-10-07 00:44:17 +02:00
postDom.removeAttr('id');
postDom.data('file', file);
$('.file-name strong', postDom).text(file.name);
$('.posts').append(postDom);
postDom.show();
var tagItOptions =
2013-10-13 12:28:16 +02:00
{
caseSensitive: true,
availableTags: tags,
placeholderText: $('.tags input').attr('placeholder')
};
$('.tags input', postDom).tagit(tagItOptions);
2013-10-07 00:44:17 +02:00
if (!file.type.match('image.*'))
{
continue;
}
var img = postDom.find('img')
2013-10-07 00:44:17 +02:00
var reader = new FileReader();
reader.onload = (function(theFile, img)
2013-10-07 00:44:17 +02:00
{
return function(e)
{
/*img.css('max-width', img.css('width'));
img.css('max-height', img.css('height'));
img.css('width', 'auto');
img.css('height', 'auto');*/
img.css('background-image', 'none');
img.attr('src', e.target.result);
};
})(file, img);
2013-10-07 00:44:17 +02:00
reader.readAsDataURL(file);
}
$('#upload-step2').fadeIn(function()
{
});
});
}
});