(function($){
$(document).on('click', '#showCreatePost', function(){
$(this).hide();
$('#add_post_holder').show();
InitRedactor($('#add_post_text'));
return false;
});
$(document).on('click', '#more_file', function(){
$('#picture_url').hide();
$('#more_url').show();
$('#picture').show();
$('#more_file').hide();
return false;
});
$(document).on('click', '#more_url', function(){
$('#picture').hide();
$('#more_file').show();
$('#picture_url').show();
$('#more_url').hide();
return false;
});
$(document).on('click', '#more_poll', function(){
$('#add_poll').toggle();
return false;
});
$(document).on('click', '#poll_add_response', function(){
var $addPoll = $('#add_poll');
if($addPoll.find('input:hidden:first').show().length > 0)
return false;
var cnt = $addPoll.find('input').length;
if(cnt > 100){
return false;
}
$addPoll.find('.polls_vars_right').append('
');
return false;
});
$(document).on('click', '#poll_del_response', function(){
var $visible = $('#add_poll').find('input:visible');
if($visible.length > 3){ // 3 = 2 responses + 1 question
$visible.last().val('').hide();
}
return false;
});
$(document).on('click', '#more_editor', function(){
$(this).hide();
$('#add_post_text').val('' + $('#add_post_text').val() + '
');
InitRedactor($('#add_post_text'));
return false;
});
$(document).on('submit', '#add_post', function(e){
e.preventDefault();
var $formFlash = $('#add_post_flash');
$formFlash.text('').hide();
var $textarea = $(this).find('textarea');
let text = jQuery.trim($textarea.val());
if($textarea.data('redactor')){
// redactor is running - remove new lines
text = text.replace(/\r?\n|\r/g, "");
$textarea.val(text);
}
const $form = $('#add_post');
const $tag = $form.find('input[name=tag]');
if (text.substring(0, 1) !== '#' && text.substring(0, 1) !== '*' && $tag.hasClass('tip')) {
$formFlash.text(window.tag_choose).show('fast');
return;
}
$("#submit_button").attr("disabled", true);
$("#add_post_uploading").show();
$("#add_post").hide();
// old submit
if (!Joyreactor.api_auth){
// disable old form as joyreactor.com migrated
return;
}
// submit via graphql api
const pictureUrl = $('#picture_url').val();
if (pictureUrl) {
text += ``
}
const header = $('#add_post input[name=header]').val();
if (header !== header_default) {
text = `${header}
${text}`;
}
const pollQuestion = $form.find('input[name=poll_question]').val();
const data = {
query:
"mutation PostFormMutation($text: String!, $tags: [String!]!, $files: [Upload!], $poll: PollInput) {" +
" post(text: $text, tags: $tags, files: $files, poll:$poll) {" +
" post {" +
" id" +
" }" +
" }" +
"}",
variables: {
text: text,
tags: $tag.val().split(','),
poll: pollQuestion
? {
question: pollQuestion,
answers: $.map($('.polls_vars input'), (element) => $(element).val()),
}
: null
},
}
const formData = new FormData();
formData.append(
'operations',
JSON.stringify(data),
);
const files = $('#picture')[0].files;
if (files.length){
formData.append('0', files[0]);
formData.append('map', JSON.stringify({0: ['variables.files.0']}));
} else {
formData.append('map', JSON.stringify({}));
}
graphQLRequest(
formData,
function (data) {
if (data.data?.post?.post?.id){
// success
const postId = fromGlobalId(data.data.post.post.id).id;
// redirect to post after 2 seconds timeout to mitigate replication lag
setTimeout(() => {
window.location = `/post/${postId}`;
}, 2000);
return;
}
onSubmitError();
},
onSubmitError
);
});
$(document).on('click', '.post_form_unregistered', function(){
showLoginDialog();
return false;
});
$(document).on('input', '#add_post input[name=tag]', UpdateAddedTags);
var lastInputtedTags = false;
var lastInputTimer = false;
function UpdateAddedTags()
{
clearTimeout(lastInputTimer);
var $input = $('#add_post').find('input[name=tag]');
var inputValue = $input.val();
var inputtedTags = inputValue.substring(0, inputValue.lastIndexOf(','));
if(lastInputtedTags !== false && inputtedTags == lastInputtedTags){
lastInputTimer = setTimeout(function(){
if($input.val() != inputValue)
return;
lastInputtedTags = false;
UpdateAddedTags();
}, 2000);
return;
}
lastInputtedTags = inputtedTags;
if(inputValue == window.tag_default){
inputValue = '';
}
$.ajax({
url: "/post/autotag",
data: "tags=" + encodeURIComponent(inputValue),
cache: false,
success:function (data) {
if(data){
$('#add_post_tags_auto').text(data);
$('#add_post_tags_auto_holder').show();
} else {
$('#add_post_tags_auto_holder').hide();
}
}
});
}
$(document).on('click', '.super-tag', function(){
var $tagInput = $('#add_post').find('input[name=tag]');
$tagInput.focus();
var tags = jQuery.trim($tagInput.val());
tags = tags ? tags.split(',') : [];
var tagname = $(this).text();
for (var i = 0; i < tags.length; i++) {
var tag = jQuery.trim(tags[i]);
tags[i] = tag;
if (tag != '')
continue;
tags.splice(i, 1);
i--;
}
if ($(this).hasClass('active')) {
$(this).removeClass('active');
for (i = 0; i < tags.length; i++) {
tag = jQuery.trim(tags[i]);
if (tag != tagname)
continue;
tags.splice(i, 1);
i--;
}
} else {
$(this).addClass('active');
tags.push(tagname);
}
tags.push('');
$tagInput.val(tags.join(', '));
$tagInput.blur().trigger('input');
return false;
});
$(document).on('focus', '#add_post input[name=tag]', function () {
if($(this).val() == tag_default)
$(this).val('').removeClass('tip');
});
$(document).on('blur', '#add_post input[name=tag]', function () {
if (jQuery.trim($(this).val()) == '' || $(this).val() == tag_default)
$(this).val(tag_default).addClass('tip');
});
$(document).on('focus', '#add_post input[name=header]', function () {
if($(this).val() == header_default)
$(this).val('').removeClass('tip');
});
$(document).on('blur', '#add_post input[name=header]', function () {
if (jQuery.trim($(this).val()) == '' || $(this).val() == header_default)
$(this).val(header_default).addClass('tip');
});
$(document).on('DOMUpdate', function(e){
$(e.target).find('#add_post input[name=tag]').blur();
$(e.target).find('#add_post input[name=header]').blur();
$(e.target).find('.textarea_redactor').each(function(){
InitRedactor($(this));
});
});
function showLoginDialog() {
$('#SignInOrSignUpDialog').dialog({
draggable: false,
modal: true,
resizable: false
});
}
function onSubmitError(){
// ошибка
$("#add_post").show();
$("#add_post_uploading").hide();
$("#add_post_flash").html(window.add_post_internal_error).show('fast');
$("#submit_button").removeAttr("disabled");
}
function InitRedactor($element){
// stop all shortcuts
$.Shortcuts.stop();
$element.redactor({
imageUpload: '/diy/upload',
fixed: true,
formattingTags: ['p', 'h3', 'h4'],
lang: 'ru',
minHeight: 500,
observeImages: false,
buttons: [
'html', '|', 'formatting', '|', 'bold', 'italic', 'deleted', '|', 'video', 'table', 'link', 'image'
],
modal_link: String() +
'' +
'' +
'
' +
''
});
}
function escapeHtml(unsafe)
{
return unsafe
.replace(/&/g, "&")
.replace(//g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
})(jQuery);