function showFullPost(uuid, username) { let defaultUrl = window.location.pathname; if(window.location.pathname.indexOf('user') === -1) defaultUrl += window.location.search; $.ajax({ url: "/user/single_post_data/" + username + "/" + uuid, beforeSend: () => { const origin = encodeURI(btoa(window.location.href.split('/post')[0])); window.history.pushState('', '', '/user/' + username + '/post/' + uuid + '?o=' + origin); $('body').append(` `); const modal = $('#postFullviewModal'); modal.modal('show'); modal.on('hidden.bs.modal', () => { $('.postFullviewModal').remove(); window.history.replaceState('', '', defaultUrl); }); }, success: (data) => { $('#postFullviewModal .modal-body').empty().append(data); registerPostEvents(); } }); } function showFullscreenImage(imageURL) { $('body').append(` `); $('#imageFullviewModal').modal('show').on('hidden.bs.modal', function () { $(this).remove(); }); $('.postFullviewModal').modal('hide'); } function registerPostEvents() { $('.action-btn.like-button').click((e) => { e.preventDefault(); addPostLike($(e.currentTarget)); }); $('.action-btn.reply-button').click((e) => { e.preventDefault(); const target = $(e.currentTarget); $('.postFullviewModal').modal('hide'); $('#postModal #replyTo').val(target.data('uuid')); $('#postModal').modal('show'); }); $('.action-btn.more-options-button').click((e) => { e.preventDefault(); }); $('.post-item').click(function (e) { const target = e.target; const forbiddenElements = ['BUTTON', 'A', 'INPUT', 'IMG', 'I', 'SPAN']; if(!forbiddenElements.includes(target.tagName) && !target.classList.contains('post-media')) { e.preventDefault(); const uuid = $(this).data('uuid'); const username = $(this).data('username'); showFullPost(uuid, username); } }); $('.post-media').click(function () { showFullscreenImage($(this).data('full-image')); }); addVideoListeners(); $(window).scroll(() => { const pageOffsetTop = $(window).scrollTop(); const pageOffsetBottom = pageOffsetTop + $(window).height(); $('.post-media.post-video').each(function () { const elemTop = $(this).offset().top; const elemBottom = elemTop + $(this).height(); if(elemBottom <= pageOffsetBottom || elemTop >= pageOffsetTop) { $(this)[0].play(); } else { $(this)[0].pause(); } }); }); } registerPostEvents(); const pendingRequests = []; function addPostLike(el) { const uuid = el.data('uuid'); if(pendingRequests.indexOf(uuid) !== -1) return; const icon = $('.fa-heart', el); const text = $('span', el); const likeCount = parseInt(text.text()); $.ajax({ url: '/posts/addPostLike', method: 'post', data: { postUUID: uuid }, success: (result) => { if(result.success) { text.text(result.likeCount); if(result.isLiked) { icon.removeClass('far').addClass('fas'); icon.parent().addClass('active'); } else { icon.removeClass('fas').addClass('far'); icon.parent().removeClass('active'); } } else { text.text(likeCount); addSnackbar('warning', result.message); icon.toggleClass('far').toggleClass('fas'); icon.parent().toggleClass('active'); } pendingRequests.splice(pendingRequests.indexOf(uuid), 1); }, beforeSend: () => { pendingRequests.push(uuid); if(icon.hasClass('far')) { text.text(likeCount + 1); } else { text.text(likeCount - 1); } icon.toggleClass('far').toggleClass('fas'); icon.parent().toggleClass('active'); }, }); } function copyToClipboard(text) { const $temp = $(''); $('body').append($temp); $temp.val(text).select(); document.execCommand('copy'); $temp.remove(); $('.snackbar-container').append(''); } function openPostReportModal(uuid) { $('body').append(` `); $('#postReportModal').modal('show'); $('#postReportModal').on('hidden.bs.modal', function () { $('#postReportModal').remove(); }); loadPostReportModal(uuid); } function loadPostReportModal(uuid) { $.ajax({ url: '/posts/getReportModal', data: { uuid }, success: (data) => { $('#postReportModal .modal-dialog').html(data); $('#postReportForm').submit((e) => { e.preventDefault(); $('#postReportForm').slideUp(); setTimeout(() => { $('#postReportBody').addClass('text-center').append(''); }, 200); submitReportForm(uuid, $('#postReportReason').val(), $('#postReportText').val()); }); }, error: () => { $('#postReportBody').html('