From 827e4b4ffd06196d5a64389bd5d075bab14949d5 Mon Sep 17 00:00:00 2001
From: barisusakli <barisusakli@gmail.com>
Date: Fri, 23 Dec 2016 16:35:48 +0300
Subject: [PATCH] refactor post tools

---
 public/src/client/topic/move-post.js |  67 ++++++++++
 public/src/client/topic/postTools.js | 187 ++-------------------------
 public/src/client/topic/votes.js     | 117 +++++++++++++++++
 3 files changed, 198 insertions(+), 173 deletions(-)
 create mode 100644 public/src/client/topic/move-post.js
 create mode 100644 public/src/client/topic/votes.js

diff --git a/public/src/client/topic/move-post.js b/public/src/client/topic/move-post.js
new file mode 100644
index 0000000000..0a2990c4ae
--- /dev/null
+++ b/public/src/client/topic/move-post.js
@@ -0,0 +1,67 @@
+'use strict';
+
+/* globals define, app, socket */
+
+define('forum/topic/move-post', [], function () {
+
+	var MovePost = {};
+
+
+	MovePost.openMovePostModal = function(button) {
+		app.parseAndTranslate('partials/move_post_modal', {}, function (html) {
+			var moveModal = $(html);
+
+			var	moveBtn = moveModal.find('#move_post_commit'),
+				topicId = moveModal.find('#topicId');
+
+			moveModal.on('hidden.bs.modal', function () {
+				moveModal.remove();
+			});
+
+			showMoveModal(moveModal);
+
+			moveModal.find('.close, #move_post_cancel').on('click', function () {
+				moveModal.addClass('hide');
+			});
+
+			topicId.on('keyup change', function () {
+				moveBtn.attr('disabled', !topicId.val());
+			});
+
+			moveBtn.on('click', function () {
+				movePost(button.parents('[data-pid]'), button.parents('[data-pid]').attr('data-pid'), topicId.val(), function () {
+					moveModal.modal('hide');
+					topicId.val('');
+				});
+			});
+
+		});
+	};
+
+	function showMoveModal(modal) {
+		modal.modal('show')
+			.css("position", "fixed")
+			.css("left", Math.max(0, (($(window).width() - modal.outerWidth()) / 2) + $(window).scrollLeft()) + "px")
+			.css("top", "0px")
+			.css("z-index", "2000");
+	}
+
+	function movePost(post, pid, tid, callback) {
+		socket.emit('posts.movePost', {pid: pid, tid: tid}, function (err) {
+			if (err) {
+				app.alertError(err.message);
+				return callback();
+			}
+
+			post.fadeOut(500, function () {
+				post.remove();
+			});
+
+			app.alertSuccess('[[topic:post_moved]]');
+			callback();
+		});
+	}
+
+
+	return MovePost;
+});
diff --git a/public/src/client/topic/postTools.js b/public/src/client/topic/postTools.js
index faa0567878..e3b2e49c14 100644
--- a/public/src/client/topic/postTools.js
+++ b/public/src/client/topic/postTools.js
@@ -2,7 +2,14 @@
 
 /* globals define, app, ajaxify, bootbox, socket, templates, utils, config */
 
-define('forum/topic/postTools', ['share', 'navigator', 'components', 'translator'], function (share, navigator, components, translator) {
+define('forum/topic/postTools', [
+	'share',
+	'navigator',
+	'components',
+	'translator',
+	'forum/topic/votes',
+	'forum/topic/move-post'
+], function (share, navigator, components, translator, votes, movePost) {
 
 	var PostTools = {};
 
@@ -17,7 +24,7 @@ define('forum/topic/postTools', ['share', 'navigator', 'components', 'translator
 
 		share.addShareHandlers(ajaxify.data.titleRaw);
 
-		addVoteHandler();
+		votes.addVoteHandler();
 
 		PostTools.updatePostCount(ajaxify.data.postcount);
 	};
@@ -69,62 +76,6 @@ define('forum/topic/postTools', ['share', 'navigator', 'components', 'translator
 		navigator.setCount(postCount);
 	};
 
-	function addVoteHandler() {
-		components.get('topic').on('mouseenter', '[data-pid] [component="post/vote-count"]', loadDataAndCreateTooltip);
-		components.get('topic').on('mouseout', '[data-pid] [component="post/vote-count"]', function () {
-			var el = $(this).parent();
-			el.on('shown.bs.tooltip', function () {
-				$('.tooltip').tooltip('destroy');
-				el.off('shown.bs.tooltip');
-			});
-
-			$('.tooltip').tooltip('destroy');
-		});
-	}
-
-	function loadDataAndCreateTooltip(e) {
-		e.stopPropagation();
-
-		var $this = $(this);
-		var el = $this.parent();
-		var pid = el.parents('[data-pid]').attr('data-pid');
-
-		$('.tooltip').tooltip('destroy');
-		$this.off('mouseenter', loadDataAndCreateTooltip);
-
-		socket.emit('posts.getUpvoters', [pid], function (err, data) {
-			if (err) {
-				return app.alertError(err.message);
-			}
-
-			if (data.length) {
-				createTooltip(el, data[0]);
-			}
-			$this.off('mouseenter').on('mouseenter', loadDataAndCreateTooltip);
-		});
-		return false;
-	}
-
-	function createTooltip(el, data) {
-		function doCreateTooltip(title) {
-			el.attr('title', title).tooltip('fixTitle').tooltip('show');
-		}
-		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, ',');
-				doCreateTooltip(translated);
-			});
-		} else {
-			usernames = usernames.join(', ');
-			doCreateTooltip(usernames);
-		}
-	}
-
 	function addPostHandlers(tid) {
 		var postContainer = components.get('topic');
 
@@ -150,19 +101,19 @@ define('forum/topic/postTools', ['share', 'navigator', 'components', 'translator
 		});
 
 		postContainer.on('click', '[component="post/bookmark"]', function () {
-			bookmarkPost($(this), getData($(this), 'data-pid'));
+			return bookmarkPost($(this), getData($(this), 'data-pid'));
 		});
 
 		postContainer.on('click', '[component="post/upvote"]', function () {
-			return toggleVote($(this), '.upvoted', 'posts.upvote');
+			return votes.toggleVote($(this), '.upvoted', 'posts.upvote');
 		});
 
 		postContainer.on('click', '[component="post/downvote"]', function () {
-			return toggleVote($(this), '.downvoted', 'posts.downvote');
+			return votes.toggleVote($(this), '.downvoted', 'posts.downvote');
 		});
 
 		postContainer.on('click', '[component="post/vote-count"]', function () {
-			showVotes(getData($(this), 'data-pid'));
+			votes.showVotes(getData($(this), 'data-pid'));
 		});
 
 		postContainer.on('click', '[component="post/flag"]', function () {
@@ -237,7 +188,7 @@ define('forum/topic/postTools', ['share', 'navigator', 'components', 'translator
 		});
 
 		postContainer.on('click', '[component="post/move"]', function () {
-			openMovePostModal($(this));
+			movePost.openMovePostModal($(this));
 		});
 
 		postContainer.on('click', '[component="post/chat"]', function () {
@@ -347,55 +298,6 @@ define('forum/topic/postTools', ['share', 'navigator', 'components', 'translator
 		return false;
 	}
 
-	function toggleVote(button, className, method) {
-		var post = button.parents('[data-pid]'),
-			currentState = post.find(className).length;
-
-		socket.emit(currentState ? 'posts.unvote' : method , {
-			pid: post.attr('data-pid'),
-			room_id: app.currentRoom
-		}, function (err) {
-			if (err) {
-				if (err.message === 'self-vote') {
-					showVotes(post.attr('data-pid'));
-				} else {
-					app.alertError(err.message);
-				}
-			}
-		});
-
-		return false;
-	}
-
-	function showVotes(pid) {
-		socket.emit('posts.getVoters', {pid: pid, cid: ajaxify.data.cid}, function (err, data) {
-			if (err) {
-				if (err.message === '[[error:no-privileges]]') {
-					return;
-				}
-
-				// Only show error if it's an unexpected error.
-				return app.alertError(err.message);
-			}
-
-			templates.parse('partials/modals/votes_modal', data, function (html) {
-				translator.translate(html, function (translated) {
-					var dialog = bootbox.dialog({
-						title: 'Voters',
-						message: translated,
-						className: 'vote-modal',
-						show: true
-					});
-
-					dialog.on('click', function () {
-						dialog.modal('hide');
-					});
-
-				});
-			});
-		});
-	}
-
 	function getData(button, data) {
 		return button.parents('[data-pid]').attr(data);
 	}
@@ -449,67 +351,6 @@ define('forum/topic/postTools', ['share', 'navigator', 'components', 'translator
 		});
 	}
 
-	function openMovePostModal(button) {
-		parseMoveModal(function (html) {
-			var moveModal = $(html);
-
-			var	moveBtn = moveModal.find('#move_post_commit'),
-				topicId = moveModal.find('#topicId');
-
-			moveModal.on('hidden.bs.modal', function () {
-				moveModal.remove();
-			});
-
-			showMoveModal(moveModal);
-
-			moveModal.find('.close, #move_post_cancel').on('click', function () {
-				moveModal.addClass('hide');
-			});
-
-			topicId.on('keyup change', function () {
-				moveBtn.attr('disabled', !topicId.val());
-			});
-
-			moveBtn.on('click', function () {
-				movePost(button.parents('[data-pid]'), getData(button, 'data-pid'), topicId.val(), function () {
-					moveModal.modal('hide');
-					topicId.val('');
-				});
-			});
-
-		});
-	}
-
-	function parseMoveModal(callback) {
-		templates.parse('partials/move_post_modal', {}, function (html) {
-			translator.translate(html, callback);
-		});
-	}
-
-	function showMoveModal(modal) {
-		modal.modal('show')
-			.css("position", "fixed")
-			.css("left", Math.max(0, (($(window).width() - modal.outerWidth()) / 2) + $(window).scrollLeft()) + "px")
-			.css("top", "0px")
-			.css("z-index", "2000");
-	}
-
-	function movePost(post, pid, tid, callback) {
-		socket.emit('posts.movePost', {pid: pid, tid: tid}, function (err) {
-			if (err) {
-				app.alertError(err.message);
-				return callback();
-			}
-
-			post.fadeOut(500, function () {
-				post.remove();
-			});
-
-			app.alertSuccess('[[topic:post_moved]]');
-			callback();
-		});
-	}
-
 	function openChat(button) {
 		var post = button.parents('[data-pid]');
 
diff --git a/public/src/client/topic/votes.js b/public/src/client/topic/votes.js
new file mode 100644
index 0000000000..8bd1b30b6b
--- /dev/null
+++ b/public/src/client/topic/votes.js
@@ -0,0 +1,117 @@
+'use strict';
+
+/* globals define, app, socket, ajaxify, templates, bootbox */
+
+define('forum/topic/votes', ['components', 'translator'], function (components, translator) {
+
+	var Votes = {};
+
+	Votes.addVoteHandler = function () {
+		components.get('topic').on('mouseenter', '[data-pid] [component="post/vote-count"]', loadDataAndCreateTooltip);
+		components.get('topic').on('mouseout', '[data-pid] [component="post/vote-count"]', function () {
+			var el = $(this).parent();
+			el.on('shown.bs.tooltip', function () {
+				$('.tooltip').tooltip('destroy');
+				el.off('shown.bs.tooltip');
+			});
+
+			$('.tooltip').tooltip('destroy');
+		});
+	};
+
+	function loadDataAndCreateTooltip(e) {
+		e.stopPropagation();
+
+		var $this = $(this);
+		var el = $this.parent();
+		var pid = el.parents('[data-pid]').attr('data-pid');
+
+		$('.tooltip').tooltip('destroy');
+		$this.off('mouseenter', loadDataAndCreateTooltip);
+
+		socket.emit('posts.getUpvoters', [pid], function (err, data) {
+			if (err) {
+				return app.alertError(err.message);
+			}
+
+			if (data.length) {
+				createTooltip(el, data[0]);
+			}
+			$this.off('mouseenter').on('mouseenter', loadDataAndCreateTooltip);
+		});
+		return false;
+	}
+
+	function createTooltip(el, data) {
+		function doCreateTooltip(title) {
+			el.attr('title', title).tooltip('fixTitle').tooltip('show');
+		}
+		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, ',');
+				doCreateTooltip(translated);
+			});
+		} else {
+			usernames = usernames.join(', ');
+			doCreateTooltip(usernames);
+		}
+	}
+
+
+	Votes.toggleVote = function (button, className, method) {
+		var post = button.parents('[data-pid]'),
+			currentState = post.find(className).length;
+
+		socket.emit(currentState ? 'posts.unvote' : method , {
+			pid: post.attr('data-pid'),
+			room_id: app.currentRoom
+		}, function (err) {
+			if (err) {
+				if (err.message === 'self-vote') {
+					Votes.showVotes(post.attr('data-pid'));
+				} else {
+					app.alertError(err.message);
+				}
+			}
+		});
+
+		return false;
+	};
+
+	Votes.showVotes = function (pid) {
+		socket.emit('posts.getVoters', {pid: pid, cid: ajaxify.data.cid}, function (err, data) {
+			if (err) {
+				if (err.message === '[[error:no-privileges]]') {
+					return;
+				}
+
+				// Only show error if it's an unexpected error.
+				return app.alertError(err.message);
+			}
+
+			templates.parse('partials/modals/votes_modal', data, function (html) {
+				translator.translate(html, function (translated) {
+					var dialog = bootbox.dialog({
+						title: 'Voters',
+						message: translated,
+						className: 'vote-modal',
+						show: true
+					});
+
+					dialog.on('click', function () {
+						dialog.modal('hide');
+					});
+
+				});
+			});
+		});
+	};
+
+
+	return Votes;
+});