| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-31 15:11:38 -04:00
										 |  |  | /* globals define, app, ajaxify, bootbox, socket, templates, utils */ | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-01 23:28:21 -04:00
										 |  |  | define('forum/topic/postTools', ['share', 'navigator', 'components', 'translator'], function(share, navigator, components, translator) { | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	var PostTools = {}, | 
					
						
							|  |  |  | 		topicName; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-02 22:34:23 -04:00
										 |  |  | 	PostTools.init = function(tid) { | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 		topicName = ajaxify.variables.get('topic_name'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-02 22:34:23 -04:00
										 |  |  | 		addPostHandlers(tid); | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		share.addShareHandlers(topicName); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		addVoteHandler(); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	PostTools.toggle = function(pid, isDeleted) { | 
					
						
							| 
									
										
										
										
											2015-03-17 13:38:18 -04:00
										 |  |  | 		var postEl = components.get('post', 'pid', pid); | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-17 16:15:12 -04:00
										 |  |  | 		postEl.find('[component="post/quote"], [component="post/favourite"], [component="post/reply"], [component="post/flag"], [component="user/chat"]') | 
					
						
							|  |  |  | 			.toggleClass('hidden', isDeleted); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-02 22:54:09 -04:00
										 |  |  | 		postEl.find('[component="post/delete"]').toggleClass('hidden', isDeleted); | 
					
						
							|  |  |  | 		postEl.find('[component="post/restore"]').toggleClass('hidden', !isDeleted); | 
					
						
							| 
									
										
										
										
											2015-03-17 16:15:12 -04:00
										 |  |  | 		postEl.find('[component="post/purge"]').toggleClass('hidden', !isDeleted); | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	PostTools.updatePostCount = function() { | 
					
						
							|  |  |  | 		socket.emit('topics.postcount', ajaxify.variables.get('topic_id'), function(err, postCount) { | 
					
						
							|  |  |  | 			if (!err) { | 
					
						
							| 
									
										
										
										
											2015-04-03 18:22:18 -04:00
										 |  |  | 				var postCountEl = components.get('topic/post-count'); | 
					
						
							| 
									
										
										
										
											2014-11-25 23:20:29 -05:00
										 |  |  | 				postCountEl.html(postCount).attr('title', postCount); | 
					
						
							|  |  |  | 				utils.makeNumbersHumanReadable(postCountEl); | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 				navigator.setCount(postCount); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	function addVoteHandler() { | 
					
						
							| 
									
										
										
										
											2015-03-17 13:59:25 -04:00
										 |  |  | 		components.get('topic').on('mouseenter', '[data-pid] .votes', function() { | 
					
						
							| 
									
										
										
										
											2015-01-19 17:06:43 -05:00
										 |  |  | 			loadDataAndCreateTooltip($(this)); | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-19 17:06:43 -05:00
										 |  |  | 	function loadDataAndCreateTooltip(el) { | 
					
						
							| 
									
										
										
										
											2015-03-17 13:59:25 -04:00
										 |  |  | 		var pid = el.parents('[data-pid]').attr('data-pid'); | 
					
						
							| 
									
										
										
										
											2015-01-19 17:06:43 -05:00
										 |  |  | 		socket.emit('posts.getUpvoters', [pid], function(err, data) { | 
					
						
							|  |  |  | 			if (!err && data.length) { | 
					
						
							|  |  |  | 				createTooltip(el, data[0]); | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	function createTooltip(el, data) { | 
					
						
							|  |  |  | 		var usernames = data.usernames; | 
					
						
							|  |  |  | 		if (!usernames.length) { | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if (usernames.length + data.otherCount > 6) { | 
					
						
							|  |  |  | 			usernames = usernames.join(', ').replace(/,/g, '|'); | 
					
						
							|  |  |  | 			translator.translate('[[topic:users_and_others, ' + usernames + ', ' + data.otherCount + ']]', function(translated) { | 
					
						
							|  |  |  | 				translated = translated.replace(/\|/g, ','); | 
					
						
							|  |  |  | 				el.attr('title', translated).tooltip('destroy').tooltip('show'); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			usernames = usernames.join(', '); | 
					
						
							|  |  |  | 			el.attr('title', usernames).tooltip('destroy').tooltip('show'); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-02 22:34:23 -04:00
										 |  |  | 	function addPostHandlers(tid) { | 
					
						
							| 
									
										
										
										
											2015-03-17 14:09:08 -04:00
										 |  |  | 		var postContainer = components.get('topic'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-17 16:15:12 -04:00
										 |  |  | 		postContainer.on('click', '[component="post/quote"]', function() { | 
					
						
							| 
									
										
										
										
											2015-04-02 22:34:23 -04:00
										 |  |  | 			onQuoteClicked($(this), tid, topicName); | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-17 16:15:12 -04:00
										 |  |  | 		postContainer.on('click', '[component="post/reply"]', function() { | 
					
						
							| 
									
										
										
										
											2015-04-02 22:34:23 -04:00
										 |  |  | 			onReplyClicked($(this), tid, topicName); | 
					
						
							| 
									
										
										
										
											2015-03-20 16:13:34 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		components.get('topic/reply').on('click', function() { | 
					
						
							| 
									
										
										
										
											2015-04-02 22:34:23 -04:00
										 |  |  | 			onReplyClicked($(this), tid, topicName); | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-17 16:15:12 -04:00
										 |  |  | 		postContainer.on('click', '[component="post/favourite"]', function() { | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 			favouritePost($(this), getData($(this), 'data-pid')); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-17 16:15:12 -04:00
										 |  |  | 		postContainer.on('click', '[component="post/upvote"]', function() { | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 			return toggleVote($(this), '.upvoted', 'posts.upvote'); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-17 16:15:12 -04:00
										 |  |  | 		postContainer.on('click', '[component="post/downvote"]', function() { | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 			return toggleVote($(this), '.downvoted', 'posts.downvote'); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-17 16:15:12 -04:00
										 |  |  | 		postContainer.on('click', '[component="post/vote-count"]', function() { | 
					
						
							| 
									
										
										
										
											2014-11-15 12:37:22 -05:00
										 |  |  | 			showVotes(getData($(this), 'data-pid')); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-17 16:15:12 -04:00
										 |  |  | 		postContainer.on('click', '[component="post/flag"]', function() { | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 			flagPost(getData($(this), 'data-pid')); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-17 16:15:12 -04:00
										 |  |  | 		postContainer.on('click', '[component="post/edit"]', function(e) { | 
					
						
							| 
									
										
										
										
											2015-04-01 23:28:21 -04:00
										 |  |  | 			var btn = $(this); | 
					
						
							| 
									
										
										
										
											2015-07-03 16:41:21 -04:00
										 |  |  | 			$(window).trigger('action:composer.post.edit', { | 
					
						
							|  |  |  | 				pid: getData(btn, 'data-pid') | 
					
						
							|  |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-17 16:15:12 -04:00
										 |  |  | 		postContainer.on('click', '[component="post/delete"]', function(e) { | 
					
						
							| 
									
										
										
										
											2015-04-02 22:54:09 -04:00
										 |  |  | 			togglePostDelete($(this), tid); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		postContainer.on('click', '[component="post/restore"]', function(e) { | 
					
						
							|  |  |  | 			togglePostDelete($(this), tid); | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-17 16:15:12 -04:00
										 |  |  | 		postContainer.on('click', '[component="post/purge"]', function(e) { | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 			purgePost($(this), tid); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-17 16:15:12 -04:00
										 |  |  | 		postContainer.on('click', '[component="post/move"]', function(e) { | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 			openMovePostModal($(this)); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-02 17:11:55 +08:00
										 |  |  | 		postContainer.on('click', '[component="post/chat"]', function(e) { | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 			openChat($(this)); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	function onReplyClicked(button, tid, topicName) { | 
					
						
							| 
									
										
										
										
											2015-07-03 16:41:21 -04:00
										 |  |  | 		var selectionText = '', | 
					
						
							|  |  |  | 			selection = window.getSelection ? window.getSelection() : document.selection.createRange(); | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-03 16:41:21 -04:00
										 |  |  | 		if ($(selection.baseNode).parents('[component="post/content"]').length > 0) { | 
					
						
							|  |  |  | 			var snippet = selection.toString(); | 
					
						
							|  |  |  | 			if (snippet.length) { | 
					
						
							|  |  |  | 				selectionText = '> ' + snippet.replace(/\n/g, '\n> ') + '\n\n'; | 
					
						
							| 
									
										
										
										
											2015-04-01 23:28:21 -04:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2015-07-03 16:41:21 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-03 16:41:21 -04:00
										 |  |  | 		var username = getUserName(selectionText ? $(selection.baseNode) : button); | 
					
						
							|  |  |  | 		if (getData(button, 'data-uid') === '0') { | 
					
						
							|  |  |  | 			username = ''; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if (selectionText.length) { | 
					
						
							|  |  |  | 			$(window).trigger('action:composer.addQuote', { | 
					
						
							|  |  |  | 				tid: tid, | 
					
						
							|  |  |  | 				slug: ajaxify.variables.get('topic_slug'), | 
					
						
							|  |  |  | 				index: getData(button, 'data-index'), | 
					
						
							|  |  |  | 				pid: getData(button, 'data-pid'), | 
					
						
							|  |  |  | 				topicName: topicName, | 
					
						
							|  |  |  | 				username: username, | 
					
						
							|  |  |  | 				text: selectionText | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			$(window).trigger('action:composer.post.new', { | 
					
						
							|  |  |  | 				tid: tid, | 
					
						
							|  |  |  | 				pid: ajaxify.variables.get('pid'), | 
					
						
							|  |  |  | 				topicName: topicName, | 
					
						
							|  |  |  | 				text: username + ' ' || '' | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	function onQuoteClicked(button, tid, topicName) { | 
					
						
							| 
									
										
										
										
											2015-07-03 16:41:21 -04:00
										 |  |  | 		var username = getUserName(button), | 
					
						
							|  |  |  | 			pid = getData(button, 'data-pid'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		socket.emit('posts.getRawPost', pid, function(err, post) { | 
					
						
							|  |  |  | 			if(err) { | 
					
						
							|  |  |  | 				return app.alertError(err.message); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			var quoted = ''; | 
					
						
							|  |  |  | 			if(post) { | 
					
						
							|  |  |  | 				quoted = '> ' + post.replace(/\n/g, '\n> ') + '\n\n'; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			$(window).trigger('action:composer.addQuote', { | 
					
						
							|  |  |  | 				tid: tid, | 
					
						
							|  |  |  | 				slug: ajaxify.variables.get('topic_slug'), | 
					
						
							|  |  |  | 				index: getData(button, 'data-index'), | 
					
						
							|  |  |  | 				pid: pid, | 
					
						
							|  |  |  | 				username: username, | 
					
						
							|  |  |  | 				topicName: topicName, | 
					
						
							|  |  |  | 				text: quoted | 
					
						
							| 
									
										
										
										
											2015-04-01 23:28:21 -04:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	function favouritePost(button, pid) { | 
					
						
							|  |  |  | 		var method = button.attr('data-favourited') === 'false' ? 'posts.favourite' : 'posts.unfavourite'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		socket.emit(method, { | 
					
						
							|  |  |  | 			pid: pid, | 
					
						
							|  |  |  | 			room_id: app.currentRoom | 
					
						
							|  |  |  | 		}, function(err) { | 
					
						
							|  |  |  | 			if (err) { | 
					
						
							|  |  |  | 				app.alertError(err.message); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	function toggleVote(button, className, method) { | 
					
						
							| 
									
										
										
										
											2015-03-17 13:59:25 -04:00
										 |  |  | 		var post = button.parents('[data-pid]'), | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 			currentState = post.find(className).length; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		socket.emit(currentState ? 'posts.unvote' : method , { | 
					
						
							|  |  |  | 			pid: post.attr('data-pid'), | 
					
						
							|  |  |  | 			room_id: app.currentRoom | 
					
						
							|  |  |  | 		}, function(err) { | 
					
						
							|  |  |  | 			if (err) { | 
					
						
							|  |  |  | 				app.alertError(err.message); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-15 12:37:22 -05:00
										 |  |  | 	function showVotes(pid) { | 
					
						
							| 
									
										
										
										
											2014-12-02 14:59:29 -05:00
										 |  |  | 		socket.emit('posts.getVoters', {pid: pid, cid: ajaxify.variables.get('category_id')}, function(err, data) { | 
					
						
							| 
									
										
										
										
											2014-11-15 12:37:22 -05:00
										 |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return app.alertError(err.message); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			templates.parse('partials/modals/votes_modal', data, function(html) { | 
					
						
							|  |  |  | 				var dialog = bootbox.dialog({ | 
					
						
							|  |  |  | 					title: 'Voters', | 
					
						
							|  |  |  | 					message: html, | 
					
						
							|  |  |  | 					className: 'vote-modal', | 
					
						
							|  |  |  | 					show: true | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				dialog.on('click', function() { | 
					
						
							|  |  |  | 					dialog.modal('hide'); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 	function getData(button, data) { | 
					
						
							| 
									
										
										
										
											2015-03-17 13:59:25 -04:00
										 |  |  | 		return button.parents('[data-pid]').attr(data); | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	function getUserName(button) { | 
					
						
							|  |  |  | 		var username = '', | 
					
						
							| 
									
										
										
										
											2015-04-02 23:01:47 -04:00
										 |  |  | 			post = button.parents('[data-pid]'); | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if (post.length) { | 
					
						
							|  |  |  | 			username = post.attr('data-username').replace(/\s/g, '-'); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if (post.length && post.attr('data-uid') !== '0') { | 
					
						
							|  |  |  | 			username = '@' + username; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return username; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-02 22:54:09 -04:00
										 |  |  | 	function togglePostDelete(button, tid) { | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 		var pid = getData(button, 'data-pid'), | 
					
						
							| 
									
										
										
										
											2015-03-17 13:38:18 -04:00
										 |  |  | 			postEl = components.get('post', 'pid', pid), | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 			action = !postEl.hasClass('deleted') ? 'delete' : 'restore'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		postAction(action, pid, tid); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	function purgePost(button, tid) { | 
					
						
							|  |  |  | 		postAction('purge', getData(button, 'data-pid'), tid); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	function postAction(action, pid, tid) { | 
					
						
							|  |  |  | 		translator.translate('[[topic:post_' + action + '_confirm]]', function(msg) { | 
					
						
							|  |  |  | 			bootbox.confirm(msg, function(confirm) { | 
					
						
							|  |  |  | 				if (!confirm) { | 
					
						
							|  |  |  | 					return; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				socket.emit('posts.' + action, { | 
					
						
							|  |  |  | 					pid: pid, | 
					
						
							|  |  |  | 					tid: tid | 
					
						
							|  |  |  | 				}, function(err) { | 
					
						
							| 
									
										
										
										
											2015-04-02 23:01:47 -04:00
										 |  |  | 					if (err) { | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 						app.alertError(err.message); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	function openMovePostModal(button) { | 
					
						
							|  |  |  | 		var moveModal = $('#move-post-modal'), | 
					
						
							|  |  |  | 			moveBtn = moveModal.find('#move_post_commit'), | 
					
						
							|  |  |  | 			topicId = moveModal.find('#topicId'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		showMoveModal(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		moveModal.find('.close,#move_post_cancel').on('click', function() { | 
					
						
							|  |  |  | 			moveModal.addClass('hide'); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		topicId.on('change', function() { | 
					
						
							|  |  |  | 			if(topicId.val().length) { | 
					
						
							|  |  |  | 				moveBtn.removeAttr('disabled'); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				moveBtn.attr('disabled', true); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		moveBtn.on('click', function() { | 
					
						
							| 
									
										
										
										
											2015-03-17 13:59:25 -04:00
										 |  |  | 			movePost(button.parents('[data-pid]'), getData(button, 'data-pid'), topicId.val()); | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	function showMoveModal() { | 
					
						
							|  |  |  | 		$('#move-post-modal').removeClass('hide') | 
					
						
							|  |  |  | 			.css("position", "fixed") | 
					
						
							|  |  |  | 			.css("left", Math.max(0, (($(window).width() - $($('#move-post-modal')).outerWidth()) / 2) + $(window).scrollLeft()) + "px") | 
					
						
							|  |  |  | 			.css("top", "0px") | 
					
						
							|  |  |  | 			.css("z-index", "2000"); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	function movePost(post, pid, tid) { | 
					
						
							|  |  |  | 		socket.emit('topics.movePost', {pid: pid, tid: tid}, function(err) { | 
					
						
							|  |  |  | 			$('#move-post-modal').addClass('hide'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-02 23:01:47 -04:00
										 |  |  | 			if (err) { | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 				$('#topicId').val(''); | 
					
						
							|  |  |  | 				return app.alertError(err.message); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			post.fadeOut(500, function() { | 
					
						
							|  |  |  | 				post.remove(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			$('#topicId').val(''); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			app.alertSuccess('[[topic:post_moved]]'); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	function flagPost(pid) { | 
					
						
							|  |  |  | 		translator.translate('[[topic:flag_confirm]]', function(message) { | 
					
						
							|  |  |  | 			bootbox.confirm(message, function(confirm) { | 
					
						
							| 
									
										
										
										
											2015-04-02 23:01:47 -04:00
										 |  |  | 				if (!confirm) { | 
					
						
							|  |  |  | 					return; | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2015-04-02 23:01:47 -04:00
										 |  |  | 				socket.emit('posts.flag', pid, function(err) { | 
					
						
							|  |  |  | 					if (err) { | 
					
						
							|  |  |  | 						return app.alertError(err.message); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					app.alertSuccess('[[topic:flag_success]]'); | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	function openChat(button) { | 
					
						
							| 
									
										
										
										
											2015-04-02 17:11:55 +08:00
										 |  |  | 		var post = button.parents('[data-pid]'); | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		app.openChat(post.attr('data-username'), post.attr('data-uid')); | 
					
						
							|  |  |  | 		button.parents('.btn-group').find('.dropdown-toggle').click(); | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return PostTools; | 
					
						
							|  |  |  | }); |