'use strict'; /* globals config, app, ajaxify, define, socket, utils */ define('forum/topic/posts', [ 'forum/pagination', 'forum/infinitescroll', 'forum/topic/postTools', 'navigator', 'components' ], function(pagination, infinitescroll, postTools, navigator, components) { var Posts = {}; Posts.onNewPost = function(data) { var tid = ajaxify.variables.get('topic_id'); if (data && data.posts && data.posts.length && parseInt(data.posts[0].tid, 10) !== parseInt(tid, 10)) { return; } if (config.usePagination) { return onNewPostPagination(data); } for (var i=0; i 1) { data.posts.forEach(function(post) { components.get('post', 'pid', post.pid).remove(); }); } else { data.posts = data.posts.filter(function(post) { return components.get('post', 'pid', post.pid).length === 0; }); } } var after = null, before = null; function findInsertionPoint() { var firstPostTimestamp = parseInt(data.posts[0].timestamp, 10); var firstPostVotes = parseInt(data.posts[0].votes, 10); var firstPostIndex = parseInt(data.posts[0].index, 10); var replies = $(repliesSelector); var firstReply = replies.first(); var lastReply = replies.last(); if (config.topicPostSort === 'oldest_to_newest') { if (firstPostTimestamp < parseInt(firstReply.attr('data-timestamp'), 10)) { before = firstReply; } else if(firstPostTimestamp >= parseInt(lastReply.attr('data-timestamp'), 10)) { after = lastReply; } } else if(config.topicPostSort === 'newest_to_oldest') { if (firstPostTimestamp > parseInt(firstReply.attr('data-timestamp'), 10)) { before = firstReply; } else if(firstPostTimestamp <= parseInt(lastReply.attr('data-timestamp'), 10)) { after = lastReply; } } else if(config.topicPostSort === 'most_votes') { if (firstPostVotes > parseInt(firstReply.attr('data-votes'), 10)) { before = firstReply; } else if(firstPostVotes < parseInt(firstReply.attr('data-votes'), 10)) { after = lastReply; } else { if (firstPostIndex > parseInt(firstReply.attr('data-index'), 10)) { before = firstReply; } else if(firstPostIndex <= parseInt(firstReply.attr('data-index'), 10)) { after = lastReply; } } } } removeAlreadyAddedPosts(); if (!data.posts.length) { return callback(); } findInsertionPoint(); data.title = $('
').text(ajaxify.variables.get('topic_name')).html(); data.viewcount = ajaxify.variables.get('viewcount'); infinitescroll.parseAndTranslate('topic', 'posts', data, function(html) { if (after) { html.insertAfter(after); } else if (before) { // Save document height and position for future reference (about 5 lines down) var height = $(document).height(), scrollTop = $(document).scrollTop(), originalPostEl = components.get('post', 'index', 0); // Insert the new post html.insertBefore(before); // If the user is not at the top of the page... (or reasonably so...) if (scrollTop > originalPostEl.offset().top) { // Now restore the relative position the user was on prior to new post insertion $(document).scrollTop(scrollTop + ($(document).height() - height)); } } else { components.get('topic').append(html); } html.hide().fadeIn('slow'); var pids = []; for(var i=0; i'); } }); postTools.updatePostCount(); addBlockquoteEllipses(element.find('[component="post/content"] > blockquote')); hidePostToolsForDeletedPosts(element); showBottomPostBar(); }; function showBottomPostBar() { if(components.get('post').length > 1 || !components.get('post', 'index', 0).length) { $('.bottom-post-bar').removeClass('hidden'); } } function hidePostToolsForDeletedPosts(element) { element.find('[data-pid].deleted').each(function() { postTools.toggle($(this).attr('data-pid'), true); }); } function addBlockquoteEllipses(blockquotes) { blockquotes.each(function() { var $this = $(this); if ($this.find(':hidden:not(br)').length && !$this.find('.toggle').length) { $this.append(''); } }); } return Posts; });