form.js
136 lines
| 3.8 KiB
| application/javascript
|
JavascriptLexer
neko259
|
r1461 | var ITEM_FILE_SOURCE = 'fileSource'; | ||
neko259
|
r680 | $('input[name=image]').wrap($('<div class="file_wrap"></div>')); | ||
neko259
|
r676 | |||
$('body').on('change', 'input[name=image]', function(event) { | ||||
neko259
|
r673 | var file = event.target.files[0]; | ||
if(file.type.match('image.*')) { | ||||
var fileReader = new FileReader(); | ||||
fileReader.addEventListener("load", function(event) { | ||||
neko259
|
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
|
r673 | ); | ||
}); | ||||
fileReader.readAsDataURL(file); | ||||
} | ||||
neko259
|
r1005 | }); | ||
var form = $('#form'); | ||||
$('textarea').keypress(function(event) { | ||||
r1577 | if ((event.which == 10 || event.which == 13) && event.ctrlKey) { | |||
neko259
|
r1428 | form.find('input[type=submit]').click(); | ||
neko259
|
r1005 | } | ||
neko259
|
r1217 | }); | ||
$('#preview-button').click(function() { | ||||
var data = { | ||||
raw_text: $('textarea').val() | ||||
} | ||||
var diffUrl = '/api/preview/'; | ||||
$.post(diffUrl, | ||||
data, | ||||
function(data) { | ||||
var previewTextBlock = $('#preview-text'); | ||||
previewTextBlock.html(data); | ||||
previewTextBlock.show(); | ||||
}) | ||||
neko259
|
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
|
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
|
r1428 | $(document).ready(function() { | ||
var powDifficulty = parseInt($('body').attr('data-pow-difficulty')); | ||||
neko259
|
r1465 | if (powDifficulty > 0 && typeof SharedWorker != 'undefined') { | ||
neko259
|
r1468 | var worker = new SharedWorker($('.post-form').attr('data-pow-script')); | ||
neko259
|
r1462 | worker.port.onmessage = function(e) { | ||
neko259
|
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
|
r1451 | $('.post-form-w').unblock(); | ||
neko259
|
r1428 | }; | ||
neko259
|
r1466 | worker.onerror = function(event){ | ||
throw new Error(event.message + " (" + event.filename + ":" + event.lineno + ")"); | ||||
neko259
|
r1464 | }; | ||
neko259
|
r1462 | worker.port.start(); | ||
neko259
|
r1428 | |||
var form = $('#form'); | ||||
var submitButton = form.find('input[type=submit]'); | ||||
submitButton.click(function() { | ||||
showAsErrors(form, gettext('Computing PoW...')); | ||||
neko259
|
r1451 | $('.post-form-w').block({ message: gettext('Computing PoW...') }) | ||
neko259
|
r1428 | |||
var msg = $('textarea').val().trim(); | ||||
var data = { | ||||
msg: msg, | ||||
difficulty: parseInt($('body').attr('data-pow-difficulty')), | ||||
neko259
|
r1468 | hasher: $('.post-form').attr('data-hasher') | ||
neko259
|
r1428 | }; | ||
neko259
|
r1462 | worker.port.postMessage(data); | ||
neko259
|
r1428 | |||
return false; | ||||
}); | ||||
} | ||||
neko259
|
r1461 | |||
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(); | ||||
} | ||||
$('#file-source-button').click(function() { | ||||
selectFileChoice(); | ||||
}); | ||||
neko259
|
r1428 | }); | ||