| 
									
										
										
										
											2014-11-18 14:33:14 -05:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-17 13:46:24 -04:00
										 |  |  | /* globals config, app, ajaxify, components, define, socket, utils */ | 
					
						
							| 
									
										
										
										
											2014-11-18 14:33:14 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | define('forum/topic/posts', [ | 
					
						
							|  |  |  | 	'forum/pagination', | 
					
						
							|  |  |  | 	'forum/infinitescroll', | 
					
						
							|  |  |  | 	'forum/topic/postTools', | 
					
						
							|  |  |  | 	'navigator' | 
					
						
							|  |  |  | ], function(pagination, infinitescroll, postTools, navigator) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var Posts = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Posts.onNewPost = function(data) { | 
					
						
							|  |  |  | 		var tid = ajaxify.variables.get('topic_id'); | 
					
						
							| 
									
										
										
										
											2015-02-01 20:10:51 -05:00
										 |  |  | 		if (data && data.posts && data.posts.length && parseInt(data.posts[0].tid, 10) !== parseInt(tid, 10)) { | 
					
						
							| 
									
										
										
										
											2014-11-18 14:33:14 -05:00
										 |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-01 20:10:51 -05:00
										 |  |  | 		if (config.usePagination) { | 
					
						
							| 
									
										
										
										
											2014-11-18 14:33:14 -05:00
										 |  |  | 			return onNewPostPagination(data); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		for (var i=0; i<data.posts.length; ++i) { | 
					
						
							|  |  |  | 			var postcount = $('.user_postcount_' + data.posts[i].uid); | 
					
						
							|  |  |  | 			postcount.html(parseInt(postcount.html(), 10) + 1); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-17 13:59:25 -04:00
										 |  |  | 		createNewPosts(data, components.get('post').not('[data-index=0]'), function(html) { | 
					
						
							| 
									
										
										
										
											2015-02-26 15:14:27 -05:00
										 |  |  | 			if (html) { | 
					
						
							|  |  |  | 				html.addClass('new'); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-11-18 14:33:14 -05:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	function onNewPostPagination(data) { | 
					
						
							| 
									
										
										
										
											2015-02-17 18:52:54 -05:00
										 |  |  | 		function scrollToPost() { | 
					
						
							|  |  |  | 			navigator.scrollBottom(data.posts[0].index); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-18 14:33:14 -05:00
										 |  |  | 		var posts = data.posts; | 
					
						
							| 
									
										
										
										
											2015-02-17 18:52:54 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		pagination.pageCount = Math.max(1, Math.ceil((posts[0].topic.postcount - 1) / config.postsPerPage)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-01 20:10:51 -05:00
										 |  |  | 		if (pagination.currentPage === pagination.pageCount) { | 
					
						
							| 
									
										
										
										
											2015-03-17 13:59:25 -04:00
										 |  |  | 			createNewPosts(data, components.get('post').not('[data-index=0]'), scrollToPost); | 
					
						
							| 
									
										
										
										
											2015-02-17 18:52:54 -05:00
										 |  |  | 		} else if (parseInt(posts[0].uid, 10) === parseInt(app.user.uid, 10)) { | 
					
						
							|  |  |  | 			pagination.loadPage(pagination.pageCount, scrollToPost); | 
					
						
							| 
									
										
										
										
											2015-02-01 20:10:51 -05:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-11-18 14:33:14 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-27 15:18:44 -05:00
										 |  |  | 	function createNewPosts(data, repliesSelector, callback) { | 
					
						
							| 
									
										
										
										
											2014-11-18 14:33:14 -05:00
										 |  |  | 		callback = callback || function() {}; | 
					
						
							|  |  |  | 		if(!data || (data.posts && !data.posts.length)) { | 
					
						
							| 
									
										
										
										
											2015-02-26 15:14:27 -05:00
										 |  |  | 			return callback(); | 
					
						
							| 
									
										
										
										
											2014-11-18 14:33:14 -05:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		function removeAlreadyAddedPosts() { | 
					
						
							|  |  |  | 			data.posts = data.posts.filter(function(post) { | 
					
						
							| 
									
										
										
										
											2015-03-17 13:38:18 -04:00
										 |  |  | 				return components.get('post', 'pid', post.pid).length === 0; | 
					
						
							| 
									
										
										
										
											2014-11-18 14:33:14 -05:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		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); | 
					
						
							| 
									
										
										
										
											2015-02-27 15:18:44 -05:00
										 |  |  | 			var replies = $(repliesSelector); | 
					
						
							| 
									
										
										
										
											2015-02-26 15:14:27 -05:00
										 |  |  | 			var firstReply = replies.first(); | 
					
						
							|  |  |  | 			var lastReply = replies.last(); | 
					
						
							| 
									
										
										
										
											2014-11-18 14:33:14 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			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(); | 
					
						
							| 
									
										
										
										
											2015-02-26 15:14:27 -05:00
										 |  |  | 		if (!data.posts.length) { | 
					
						
							|  |  |  | 			return callback(); | 
					
						
							| 
									
										
										
										
											2014-11-18 14:33:14 -05:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		findInsertionPoint(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		data.title = $('<div></div>').text(ajaxify.variables.get('topic_name')).html(); | 
					
						
							|  |  |  | 		data.viewcount = ajaxify.variables.get('viewcount'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		infinitescroll.parseAndTranslate('topic', 'posts', data, function(html) { | 
					
						
							| 
									
										
										
										
											2015-02-26 15:14:27 -05:00
										 |  |  | 			if (after) { | 
					
						
							| 
									
										
										
										
											2014-11-18 14:33:14 -05:00
										 |  |  | 				html.insertAfter(after); | 
					
						
							| 
									
										
										
										
											2015-02-26 15:14:27 -05:00
										 |  |  | 			} else if (before) { | 
					
						
							| 
									
										
										
										
											2014-11-18 14:33:14 -05:00
										 |  |  | 				// Save document height and position for future reference (about 5 lines down)
 | 
					
						
							|  |  |  | 				var height = $(document).height(), | 
					
						
							|  |  |  | 					scrollTop = $(document).scrollTop(), | 
					
						
							| 
									
										
										
										
											2015-03-17 13:59:25 -04:00
										 |  |  | 					originalPostEl = components.get('post', 'index', 0); | 
					
						
							| 
									
										
										
										
											2014-11-18 14:33:14 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				// 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 { | 
					
						
							| 
									
										
										
										
											2015-03-17 13:38:18 -04:00
										 |  |  | 				components.get('topic').append(html); | 
					
						
							| 
									
										
										
										
											2014-11-18 14:33:14 -05:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			html.hide().fadeIn('slow'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-24 18:48:34 -05:00
										 |  |  | 			var pids = []; | 
					
						
							|  |  |  | 			for(var i=0; i<data.posts.length; ++i) { | 
					
						
							|  |  |  | 				pids.push(data.posts[i].pid); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-16 10:43:56 +03:00
										 |  |  | 			$(window).trigger('action:posts.loaded', {posts: data.posts}); | 
					
						
							| 
									
										
										
										
											2015-01-24 18:48:34 -05:00
										 |  |  | 			onNewPostsLoaded(html, pids); | 
					
						
							| 
									
										
										
										
											2015-02-26 15:14:27 -05:00
										 |  |  | 			callback(html); | 
					
						
							| 
									
										
										
										
											2014-11-18 14:33:14 -05:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-24 18:48:34 -05:00
										 |  |  | 	function onNewPostsLoaded(html, pids) { | 
					
						
							| 
									
										
										
										
											2015-02-11 12:54:54 -05:00
										 |  |  | 		if (app.user.uid) { | 
					
						
							| 
									
										
										
										
											2014-12-10 00:43:35 -05:00
										 |  |  | 			socket.emit('posts.getPrivileges', pids, function(err, privileges) { | 
					
						
							|  |  |  | 				if(err) { | 
					
						
							|  |  |  | 					return app.alertError(err.message); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2014-11-18 14:33:14 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-24 18:48:34 -05:00
										 |  |  | 				for(var i=0; i<pids.length; ++i) { | 
					
						
							| 
									
										
										
										
											2014-12-10 00:43:35 -05:00
										 |  |  | 					toggleModTools(pids[i], privileges[i]); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2015-01-24 18:48:34 -05:00
										 |  |  | 			for(var i=0; i<pids.length; ++i) { | 
					
						
							|  |  |  | 				toggleModTools(pids[i], {editable: false, move: false}); | 
					
						
							| 
									
										
										
										
											2014-11-18 14:33:14 -05:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-12-10 00:43:35 -05:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-11-18 14:33:14 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		Posts.processPage(html); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	function toggleModTools(pid, privileges) { | 
					
						
							| 
									
										
										
										
											2015-03-17 13:59:25 -04:00
										 |  |  | 		var postEl = components.get('post', 'pid', pid); | 
					
						
							| 
									
										
										
										
											2014-11-18 14:33:14 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if (!privileges.editable) { | 
					
						
							|  |  |  | 			postEl.find('.edit, .delete, .purge').remove(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if (!privileges.move) { | 
					
						
							|  |  |  | 			postEl.find('.move').remove(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		postEl.find('.reply, .quote').toggleClass('hidden', !$('.post_reply').length); | 
					
						
							| 
									
										
										
										
											2015-02-11 12:54:54 -05:00
										 |  |  | 		var isSelfPost = parseInt(postEl.attr('data-uid'), 10) === parseInt(app.user.uid, 10); | 
					
						
							|  |  |  | 		postEl.find('.chat, .flag').toggleClass('hidden', isSelfPost || !app.user.uid); | 
					
						
							| 
									
										
										
										
											2014-11-18 14:33:14 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Posts.loadMorePosts = function(direction) { | 
					
						
							| 
									
										
										
										
											2015-03-17 13:38:18 -04:00
										 |  |  | 		if (!components.get('topic').length || navigator.scrollActive) { | 
					
						
							| 
									
										
										
										
											2014-11-18 14:33:14 -05:00
										 |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		var reverse = config.topicPostSort === 'newest_to_oldest' || config.topicPostSort === 'most_votes'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-17 13:38:18 -04:00
										 |  |  | 		infinitescroll.calculateAfter(direction, components.get('topic').find('[data-index][data-index!="0"]:not(.new)'), config.postsPerPage, reverse, function(after, offset, el) { | 
					
						
							| 
									
										
										
										
											2014-11-18 14:33:14 -05:00
										 |  |  | 			loadPostsAfter(after); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	function loadPostsAfter(after) { | 
					
						
							|  |  |  | 		var tid = ajaxify.variables.get('topic_id'); | 
					
						
							| 
									
										
										
										
											2015-03-17 13:38:18 -04:00
										 |  |  | 		if (!utils.isNumber(tid) || !utils.isNumber(after) || (after === 0 && components.get('post', 'index', 1).length)) { | 
					
						
							| 
									
										
										
										
											2014-11-18 14:33:14 -05:00
										 |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		var indicatorEl = $('.loading-indicator'); | 
					
						
							|  |  |  | 		if (!indicatorEl.is(':animated')) { | 
					
						
							|  |  |  | 			indicatorEl.fadeIn(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		infinitescroll.loadMore('topics.loadMore', { | 
					
						
							|  |  |  | 			tid: tid, | 
					
						
							|  |  |  | 			after: after | 
					
						
							|  |  |  | 		}, function (data, done) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			indicatorEl.fadeOut(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (data && data.posts && data.posts.length) { | 
					
						
							| 
									
										
										
										
											2015-03-17 13:59:25 -04:00
										 |  |  | 				createNewPosts(data, components.get('post').not('[data-index=0]').not('.new'), done); | 
					
						
							| 
									
										
										
										
											2014-11-18 14:33:14 -05:00
										 |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2015-02-11 12:54:54 -05:00
										 |  |  | 				if (app.user.uid) { | 
					
						
							| 
									
										
										
										
											2014-11-29 02:07:19 -05:00
										 |  |  | 					socket.emit('topics.markAsRead', [tid]); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2014-11-18 14:33:14 -05:00
										 |  |  | 				navigator.update(); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Posts.processPage = function(element) { | 
					
						
							|  |  |  | 		app.createUserTooltips(); | 
					
						
							|  |  |  | 		app.replaceSelfLinks(element.find('a')); | 
					
						
							|  |  |  | 		utils.addCommasToNumbers(element.find('.formatted-number')); | 
					
						
							|  |  |  | 		utils.makeNumbersHumanReadable(element.find('.human-readable-number')); | 
					
						
							| 
									
										
										
										
											2015-03-16 17:22:44 -04:00
										 |  |  | 		element.find('.timeago').timeago(); | 
					
						
							| 
									
										
										
										
											2015-03-17 14:16:41 -04:00
										 |  |  | 		element.find('[component="post/content"] img:not(.emoji), .post-content img:not(.emoji)').addClass('img-responsive').each(function() { | 
					
						
							| 
									
										
										
										
											2014-11-18 14:33:14 -05:00
										 |  |  | 			var $this = $(this); | 
					
						
							|  |  |  | 			if (!$this.parent().is('a')) { | 
					
						
							|  |  |  | 				$this.wrap('<a href="' + $this.attr('src') + '" target="_blank">'); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 		postTools.updatePostCount(); | 
					
						
							| 
									
										
										
										
											2015-03-17 14:16:41 -04:00
										 |  |  | 		addBlockquoteEllipses(element.find('[component="post/content"] > blockquote, .post-content > blockquote')); | 
					
						
							| 
									
										
										
										
											2014-11-18 14:33:14 -05:00
										 |  |  | 		hidePostToolsForDeletedPosts(element); | 
					
						
							|  |  |  | 		showBottomPostBar(); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	function showBottomPostBar() { | 
					
						
							| 
									
										
										
										
											2015-03-17 13:38:18 -04:00
										 |  |  | 		if(components.get('post', 'index').length > 1 || !components.get('post', 'index', 0).length) { | 
					
						
							| 
									
										
										
										
											2014-11-18 14:33:14 -05:00
										 |  |  | 			$('.bottom-post-bar').removeClass('hide'); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	function hidePostToolsForDeletedPosts(element) { | 
					
						
							| 
									
										
										
										
											2015-03-17 13:59:25 -04:00
										 |  |  | 		element.find('[data-pid].deleted').each(function() { | 
					
						
							| 
									
										
										
										
											2014-11-18 14:33:14 -05:00
										 |  |  | 			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('<i class="fa fa-angle-down pointer toggle"></i>'); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return Posts; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-25 19:09:39 -05:00
										 |  |  | }); |