##// END OF EJS Templates
Get one ban object, not filter them
Get one ban object, not filter them

File last commit:

r1998:8255ca3c default
r2012:d33cae51 default
Show More
form.js
302 lines | 8.6 KiB | application/javascript | JavascriptLexer
neko259
Ability to switch file source field in posting form
r1461 var ITEM_FILE_SOURCE = 'fileSource';
neko259
Fixed sticker autocompletions. Localized 'too many files' message and added max file count there
r1766 var URL_STICKERS = '/api/stickers';
neko259
Image alias autocomplete
r1702 var MIN_INPUT_LENGTH = 3;
neko259
Fixed sticker autocompletions. Localized 'too many files' message and added max file count there
r1766 var URL_DELIMITER = '\n';
neko259
AJAX-based thread creation
r1998 // TODO This needs to be the same for attachment download time limit.
var POST_AJAX_TIMEOUT = 30000;
neko259
Ability to switch file source field in posting form
r1461
neko259
Allow pasting files from clipboard. Alpha release, removing or previewing images in the form not implemented yet
r1985 var pastedImages = [];
neko259
Updated form to a new style. Fixed mark manel
r680 $('input[name=image]').wrap($('<div class="file_wrap"></div>'));
neko259
Added a compact form to the thread.
r676
$('body').on('change', 'input[name=image]', function(event) {
neko259
Added image preview in the forms
r673 var file = event.target.files[0];
if(file.type.match('image.*')) {
var fileReader = new FileReader();
fileReader.addEventListener("load", function(event) {
neko259
Updated form to a new style. Fixed mark manel
r680 var wrapper = $('.file_wrap');
wrapper.find('.file-thumb').remove();
wrapper.append(
$('<div class="file-thumb" style="background-image: url('+event.target.result+')"></div>')
neko259
Added image preview in the forms
r673 );
});
fileReader.readAsDataURL(file);
}
neko259
Post message by ctrl-enter on the text field
r1005 });
var form = $('#form');
$('textarea').keypress(function(event) {
Fixed submitting form by hotkey in Chrome
r1577 if ((event.which == 10 || event.which == 13) && event.ctrlKey) {
neko259
Added PoW instead of 30-second captcha
r1428 form.find('input[type=submit]').click();
neko259
Post message by ctrl-enter on the text field
r1005 }
neko259
Added AJAX text preview to the form.
r1217 });
$('#preview-button').click(function() {
var data = {
neko259
Insert markup only to the text textarea
r1768 raw_text: $('textarea#id_text').val()
neko259
Added AJAX text preview to the form.
r1217 }
var diffUrl = '/api/preview/';
$.post(diffUrl,
data,
function(data) {
var previewTextBlock = $('#preview-text');
previewTextBlock.html(data);
previewTextBlock.show();
neko259
Add reflink popup to post preview text
r1717
addScriptsToPost(previewTextBlock);
neko259
Added AJAX text preview to the form.
r1217 })
neko259
Added PoW instead of 30-second captcha
r1428 });
/**
* Show text in the errors row of the form.
* @param form
* @param text
*/
function showAsErrors(form, text) {
form.children('.form-errors').remove();
if (text.length > 0) {
var errorList = $('<div class="form-errors">' + text + '<div>');
errorList.appendTo(form);
}
}
function addHiddenInput(form, name, value) {
form.find('input[name=' + name + ']').val(value);
}
neko259
Ability to switch file source field in posting form
r1461 function selectFileChoice() {
var file_input = $('#id_file');
var url_input = $('#id_file_url');
var file_input_row = file_input.parent().parent();
var url_input_row = url_input.parent().parent();
file_input_row.toggle();
url_input_row.toggle();
url_input.val('');
file_input.val('');
var source;
if (file_input_row.is(':visible')) {
source = 'file';
} else {
source = 'url';
}
localStorage.setItem(ITEM_FILE_SOURCE, source);
}
neko259
Refactoring of the scripts accessing textarea
r1769 function getPostTextarea() {
return $('textarea#id_text');
}
neko259
Allow pasting files from clipboard. Alpha release, removing or previewing images in the form not implemented yet
r1985 function addOnImagePaste() {
$('#id_file_1').on('paste', function(event) {
var items = (event.clipboardData || event.originalEvent.clipboardData).items;
for (index in items) {
var item = items[index];
if (item.kind === 'file') {
var blob = item.getAsFile();
pastedImages.push(blob);
var pastedImagesList = $('#pasted-images');
if (pastedImagesList.length === 0) {
pastedImagesList = $('<div id="pasted-images" />');
$('#id_file_1').parent().append(pastedImagesList);
}
neko259
Show pasted images in the form instead of a text with their count
r1989 var fr = new FileReader();
fr.onload = function () {
var img = $('<img class="image-preview" />');
img.attr('src', fr.result);
pastedImagesList.append(img);
neko259
Remove pasted images by clicking them
r1991 img.on("click", function() {
// Remove the image from all lists
var itemIndex = $(this).index();
pastedImages.splice(itemIndex, 1);
$(this).remove();
});
neko259
Show pasted images in the form instead of a text with their count
r1989 };
fr.readAsDataURL(blob);
neko259
Allow pasting files from clipboard. Alpha release, removing or previewing images in the form not implemented yet
r1985 }
}
});
}
neko259
AJAX-based thread creation
r1998 /**
* When the form is posted, this method will be run as a callback
*/
function updateOnPost(response, statusText, xhr, form) {
var json = $.parseJSON(response);
var status = json.status;
var url = json.url;
showAsErrors(form, '');
$('.post-form-w').unblock();
if (status === 'ok') {
if (url) {
document.location = url;
} else {
resetForm();
getThreadDiff();
scrollToBottom();
}
} else {
var errors = json.errors;
for (var i = 0; i < errors.length; i++) {
var fieldErrors = errors[i];
var error = fieldErrors.errors;
showAsErrors(form, error);
}
}
}
function initAjaxForm(openingPostId) {
var form = $('#form');
var url = '/api/add_post/';
if (openingPostId) {
url += openingPostId + '/';
}
if (form.length > 0) {
var options = {
beforeSubmit: function(arr, form, options) {
$('.post-form-w').block({ message: gettext('Sending message...') });
$.each(pastedImages, function(i, blob) {
arr.push({
name: "file_0",
value: blob
});
});
},
success: updateOnPost,
error: function(xhr, textStatus, errorString) {
var errorText = gettext('Server error: ') + textStatus;
if (errorString) {
errorText += ' / ' + errorString;
}
showAsErrors(form, errorText);
$('.post-form-w').unblock();
},
url: url,
timeout: POST_AJAX_TIMEOUT
};
form.ajaxForm(options);
}
}
neko259
Added PoW instead of 30-second captcha
r1428 $(document).ready(function() {
var powDifficulty = parseInt($('body').attr('data-pow-difficulty'));
neko259
Check for shared worker support before enabling them
r1465 if (powDifficulty > 0 && typeof SharedWorker != 'undefined') {
neko259
Don't include pow and hasher scripts to both document and worker. Import...
r1468 var worker = new SharedWorker($('.post-form').attr('data-pow-script'));
neko259
Make pow script be a shared worker
r1462 worker.port.onmessage = function(e) {
neko259
Added PoW instead of 30-second captcha
r1428 var form = $('#form');
addHiddenInput(form, 'timestamp', e.data.timestamp);
addHiddenInput(form, 'iteration', e.data.iteration);
addHiddenInput(form, 'guess', e.data.guess);
form.submit();
neko259
Block the form while computing PoW.
r1451 $('.post-form-w').unblock();
neko259
Added PoW instead of 30-second captcha
r1428 };
neko259
Fixed capturing worker errors
r1466 worker.onerror = function(event){
throw new Error(event.message + " (" + event.filename + ":" + event.lineno + ")");
neko259
Add worker js errors to the console
r1464 };
neko259
Make pow script be a shared worker
r1462 worker.port.start();
neko259
Added PoW instead of 30-second captcha
r1428
var form = $('#form');
var submitButton = form.find('input[type=submit]');
submitButton.click(function() {
showAsErrors(form, gettext('Computing PoW...'));
neko259
Block the form while computing PoW.
r1451 $('.post-form-w').block({ message: gettext('Computing PoW...') })
neko259
Added PoW instead of 30-second captcha
r1428
neko259
Insert markup only to the text textarea
r1768 var msg = $('textarea#id_text').val().trim();
neko259
Added PoW instead of 30-second captcha
r1428
var data = {
msg: msg,
difficulty: parseInt($('body').attr('data-pow-difficulty')),
neko259
Don't include pow and hasher scripts to both document and worker. Import...
r1468 hasher: $('.post-form').attr('data-hasher')
neko259
Added PoW instead of 30-second captcha
r1428 };
neko259
Make pow script be a shared worker
r1462 worker.port.postMessage(data);
neko259
Added PoW instead of 30-second captcha
r1428
return false;
});
}
neko259
Ability to switch file source field in posting form
r1461
Opera Mini fixes
r1639 var $fileSourceButton = $('#file-source-button');
if (window.localStorage) {
var source = localStorage.getItem(ITEM_FILE_SOURCE);
if (source == null) {
source = 'file';
}
if (source == 'file') {
$('#id_file_url').parent().parent().hide();
} else {
$('#id_file').parent().parent().hide();
}
$fileSourceButton.click(function() {
selectFileChoice();
});
neko259
Ability to switch file source field in posting form
r1461 } else {
Opera Mini fixes
r1639 $fileSourceButton.hide();
neko259
Ability to switch file source field in posting form
r1461 }
neko259
Image alias autocomplete
r1702
neko259
Allow pasting files from clipboard. Alpha release, removing or previewing images in the form not implemented yet
r1985 addOnImagePaste();
neko259
Fixed sticker autocompletions. Localized 'too many files' message and added max file count there
r1766 // Stickers autocomplete
function split( val ) {
return val.split(URL_DELIMITER);
}
function extractLast( term ) {
return split(term).pop();
}
$('#id_file_1').autocomplete({
neko259
Image alias autocomplete
r1702 source: function( request, response ) {
$.getJSON(URL_STICKERS, {
neko259
Fixed sticker autocompletions. Localized 'too many files' message and added max file count there
r1766 term: extractLast( request.term )
neko259
Image alias autocomplete
r1702 }, response);
},
search: function() {
// custom minLength
neko259
Fixed sticker autocompletions. Localized 'too many files' message and added max file count there
r1766 var term = extractLast( this.value );
neko259
Image alias autocomplete
r1702 if (term.length < MIN_INPUT_LENGTH) {
return false;
}
},
focus: function() {
// prevent value inserted on focus
return false;
},
select: function( event, ui ) {
neko259
Fixed sticker autocompletions. Localized 'too many files' message and added max file count there
r1766 var terms = split( this.value );
// remove the current input
terms.pop();
// add the selected item
terms.push( ui.item.alias );
// add placeholder to get the comma-and-space at the end
terms.push("");
this.value = terms.join(URL_DELIMITER);
neko259
Image alias autocomplete
r1702 return false;
}
})
.autocomplete( "instance" )._renderItem = function( ul, item ) {
return $( "<li>" )
.append( "<div>" + '<img src="' + item.thumb + '">' + '<br />' + item.alias + "</div>" )
.appendTo( ul );
};
neko259
Added PoW instead of 30-second captcha
r1428 });