szurubooru/public_html/media/js/upload.js

224 lines
4.7 KiB
JavaScript
Raw Normal View History

2013-10-07 00:44:17 +02:00
$(function()
{
2013-10-25 09:40:33 +02:00
$('.tabs nav a').click(function(e)
{
e.preventDefault();
var className = $(this).parents('li').attr('class').replace('selected', '').replace(/^\s+|\s+$/, '');
$('.tabs nav li').removeClass('selected');
$(this).parents('li').addClass('selected');
$('.tab').hide();
$('.tab.' + className).show();
});
var tags = [];
$.getJSON('/tags?json', function(data)
{
tags = data['tags'];
});
2013-10-07 00:44:17 +02:00
2013-10-25 09:40:33 +02:00
$('#file-handler').on('dragenter', function(e)
2013-10-07 00:44:17 +02:00
{
$(this).addClass('active');
2013-10-25 09:40:33 +02:00
}).on('dragleave', function(e)
2013-10-07 00:44:17 +02:00
{
$(this).removeClass('active');
2013-10-25 09:40:33 +02:00
}).on('dragover', function(e)
2013-10-07 00:44:17 +02:00
{
e.preventDefault();
2013-10-25 09:40:33 +02:00
}).on('drop', function(e)
2013-10-07 00:44:17 +02:00
{
e.preventDefault();
handleFiles(e.originalEvent.dataTransfer.files);
$(this).trigger('dragleave');
2013-10-25 09:40:33 +02:00
}).on('click', function(e)
2013-10-07 00:44:17 +02:00
{
$(':file').show().focus().trigger('click').hide();
});
$(':file').change(function(e)
{
handleFiles(this.files);
});
2013-10-25 09:40:33 +02:00
$('#url-handler-wrapper button').click(function(e)
{
var urls = $('#url-handler-wrapper textarea').val().split(/\s+/);
2013-10-25 09:56:55 +02:00
$('#url-handler-wrapper textarea').val('');
2013-10-25 09:40:33 +02:00
handleURLs(urls);
});
2013-10-19 16:16:09 +02:00
$('.post .move-down-trigger, .post .move-up-trigger').on('click', function()
{
var dir = $(this).hasClass('move-down-trigger') ? 'd' : 'u';
var post = $(this).parents('.post');
if (dir == 'u')
post.insertBefore(post.prev('.post'));
else
post.insertAfter(post.next('.post'));
});
2013-10-07 00:44:17 +02:00
$('.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-25 09:40:33 +02:00
2013-10-19 16:00:47 +02:00
function sendNextPost()
2013-10-07 00:44:17 +02:00
{
2013-10-09 00:58:49 +02:00
var posts = $('#upload-step2 .post');
2013-10-07 00:44:17 +02:00
if (posts.length == 0)
{
2013-10-19 16:00:47 +02:00
uploadFinished();
2013-10-07 00:44:17 +02:00
return;
}
2013-10-19 16:00:47 +02:00
var postDom = posts.first();
var url = postDom.find('form').attr('action') + '?json';
2013-10-25 09:40:33 +02:00
var sourceFile = postDom.data('file');
var sourceUrl = postDom.data('url');
2013-10-19 16:00:47 +02:00
var tags = postDom.find('[name=tags]').val();
var safety = postDom.find('[name=safety]:checked').val();
var source = postDom.find('[name=source]').val();
2013-10-19 16:00:47 +02:00
var fd = new FormData();
2013-10-25 09:40:33 +02:00
fd.append('file', sourceFile);
fd.append('url', sourceUrl);
2013-10-19 16:00:47 +02:00
fd.append('tags', tags);
fd.append('safety', safety);
fd.append('source', source);
2013-10-21 23:50:30 +02:00
fd.append('submit', 1);
2013-10-19 16:00:47 +02:00
var ajaxData =
2013-10-09 00:58:49 +02:00
{
2013-10-19 16:00:47 +02:00
url: url,
data: fd,
processData: false,
contentType: false,
dataType: 'json',
type: 'POST',
success: function(data)
2013-10-09 00:58:49 +02:00
{
if (data['success'])
{
postDom.slideUp(function()
2013-10-07 00:44:17 +02:00
{
2013-10-19 16:00:47 +02:00
postDom.remove();
sendNextPost();
2013-10-09 00:58:49 +02:00
});
2013-10-07 00:44:17 +02:00
}
2013-10-09 00:58:49 +02:00
else
{
2013-10-19 15:19:47 +02:00
postDom.find('.alert').html(data['errorHtml']).slideDown();
2013-10-19 16:00:47 +02:00
enableUpload();
2013-10-09 00:58:49 +02:00
}
}
2013-10-19 16:00:47 +02:00
};
2013-10-07 00:44:17 +02:00
2013-10-19 16:00:47 +02:00
$.ajax(ajaxData);
}
function uploadFinished()
{
window.location.href = $('#upload-step2').attr('data-redirect-url');
}
function disableUpload()
{
var theSubmit = $('#the-submit');
theSubmit.addClass('inactive');
var posts = $('#upload-step2 .post');
posts.find(':input').attr('readonly', true);
posts.addClass('inactive');
}
function enableUpload()
{
var theSubmit = $('#the-submit');
theSubmit.removeClass('inactive');
var posts = $('#upload-step2 .post');
posts.removeClass('inactive');
posts.find(':input').attr('readonly', false);
}
$('#the-submit').click(function(e)
{
e.preventDefault();
var theSubmit = $(this);
disableUpload();
sendNextPost();
2013-10-07 00:44:17 +02:00
});
function handleFiles(files)
2013-10-25 09:40:33 +02:00
{
handleInputs(files, function(postDom, file)
{
postDom.data('file', file);
$('.file-name strong', postDom).text(file.name);
if (file.type.match('image.*'))
{
var img = postDom.find('img')
var reader = new FileReader();
reader.onload = (function(theFile, img)
{
return function(e)
{
img.css('background-image', 'none');
img.attr('src', e.target.result);
};
})(file, img);
reader.readAsDataURL(file);
}
});
}
function handleURLs(urls)
{
handleInputs(urls, function(postDom, url)
{
postDom.data('url', url);
$('.file-name strong', postDom).text(url);
$('[name=source]', postDom).val(url);
var img = postDom.find('img');
img.css('background-image', 'none');
img.attr('src', url);
});
}
function handleInputs(inputs, callback)
2013-10-07 00:44:17 +02:00
{
2013-10-25 09:56:55 +02:00
for (var i = 0; i < inputs.length; i ++)
2013-10-07 00:44:17 +02:00
{
2013-10-25 09:56:55 +02:00
var input = inputs[i];
var postDom = $('#post-template').clone(true);
postDom.find('form').submit(false);
postDom.removeAttr('id');
2013-10-25 09:40:33 +02:00
2013-10-25 09:56:55 +02:00
$('.posts').append(postDom);
2013-10-07 00:44:17 +02:00
2013-10-25 09:56:55 +02:00
postDom.show();
var tagItOptions = getTagItOptions();
tagItOptions.availableTags = tags;
tagItOptions.placeholderText = $('.tags input').attr('placeholder');
$('.tags input', postDom).tagit(tagItOptions);
2013-10-25 09:56:55 +02:00
callback(postDom, input);
}
$('#upload-step2').fadeIn(function()
{
2013-10-07 00:44:17 +02:00
});
}
});