| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-13 16:36:43 -04:00
										 |  |  | /* globals define, app, ajaxify, bootbox, socket, templates, utils, config */ | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-23 16:35:48 +03:00
										 |  |  | define('forum/topic/postTools', [ | 
					
						
							|  |  |  | 	'share', | 
					
						
							|  |  |  | 	'navigator', | 
					
						
							|  |  |  | 	'components', | 
					
						
							|  |  |  | 	'translator', | 
					
						
							|  |  |  | 	'forum/topic/votes', | 
					
						
							|  |  |  | 	'forum/topic/move-post' | 
					
						
							|  |  |  | ], function (share, navigator, components, translator, votes, movePost) { | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-18 18:32:08 +02:00
										 |  |  | 	var PostTools = {}; | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-30 11:38:33 +03:00
										 |  |  | 	var staleReplyAnyway = false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	PostTools.init = function (tid) { | 
					
						
							| 
									
										
										
										
											2016-06-30 11:38:33 +03:00
										 |  |  | 		staleReplyAnyway = false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-24 20:50:43 -04:00
										 |  |  | 		renderMenu(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-02 22:34:23 -04:00
										 |  |  | 		addPostHandlers(tid); | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-06 15:49:34 -05:00
										 |  |  | 		share.addShareHandlers(ajaxify.data.titleRaw); | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-23 16:35:48 +03:00
										 |  |  | 		votes.addVoteHandler(); | 
					
						
							| 
									
										
										
										
											2015-10-20 18:08:59 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		PostTools.updatePostCount(ajaxify.data.postcount); | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-24 20:50:43 -04:00
										 |  |  | 	function renderMenu() { | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		$('[component="topic"]').on('show.bs.dropdown', '.moderator-tools', function () { | 
					
						
							| 
									
										
										
										
											2015-10-24 20:50:43 -04:00
										 |  |  | 			var $this = $(this); | 
					
						
							|  |  |  | 			var dropdownMenu = $this.find('.dropdown-menu'); | 
					
						
							|  |  |  | 			if (dropdownMenu.html()) { | 
					
						
							|  |  |  | 				return; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			var postEl = $this.parents('[data-pid]'); | 
					
						
							|  |  |  | 			var pid = postEl.attr('data-pid'); | 
					
						
							|  |  |  | 			var index = parseInt(postEl.attr('data-index'), 10); | 
					
						
							| 
									
										
										
										
											2016-03-01 18:16:51 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			socket.emit('posts.loadPostTools', {pid: pid, cid: ajaxify.data.cid}, function (err, data) { | 
					
						
							| 
									
										
										
										
											2015-10-24 20:50:43 -04:00
										 |  |  | 				if (err) { | 
					
						
							| 
									
										
										
										
											2016-08-10 22:53:30 +03:00
										 |  |  | 					return app.alertError(err.message); | 
					
						
							| 
									
										
										
										
											2015-10-24 20:50:43 -04:00
										 |  |  | 				} | 
					
						
							|  |  |  | 				data.posts.display_move_tools = data.posts.display_move_tools && index !== 0; | 
					
						
							| 
									
										
										
										
											2016-03-08 14:38:44 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 				templates.parse('partials/topic/post-menu-list', data, function (html) { | 
					
						
							|  |  |  | 					translator.translate(html, function (html) { | 
					
						
							| 
									
										
										
										
											2015-10-24 20:50:43 -04:00
										 |  |  | 						dropdownMenu.html(html); | 
					
						
							| 
									
										
										
										
											2016-03-01 18:16:51 -05:00
										 |  |  | 						$(window).trigger('action:post.tools.load'); | 
					
						
							| 
									
										
										
										
											2015-10-24 20:50:43 -04:00
										 |  |  | 					}); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-08 19:09:48 +03:00
										 |  |  | 		postEl.find('[component="post/quote"], [component="post/bookmark"], [component="post/reply"], [component="post/flag"], [component="user/chat"]') | 
					
						
							| 
									
										
										
										
											2015-03-17 16:15:12 -04:00
										 |  |  | 			.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); | 
					
						
							| 
									
										
										
										
											2016-07-06 15:15:51 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-29 15:51:46 +03:00
										 |  |  | 		postEl.find('[component="post/tools"] .dropdown-menu').html(''); | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	PostTools.updatePostCount = function (postCount) { | 
					
						
							| 
									
										
										
										
											2015-10-20 18:08:59 -04:00
										 |  |  | 		var postCountEl = components.get('topic/post-count'); | 
					
						
							|  |  |  | 		postCountEl.html(postCount).attr('title', postCount); | 
					
						
							|  |  |  | 		utils.makeNumbersHumanReadable(postCountEl); | 
					
						
							|  |  |  | 		navigator.setCount(postCount); | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-02 22:34:23 -04:00
										 |  |  | 	function addPostHandlers(tid) { | 
					
						
							| 
									
										
										
										
											2015-03-17 14:09:08 -04:00
										 |  |  | 		var postContainer = components.get('topic'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		postContainer.on('click', '[component="post/quote"]', function () { | 
					
						
							| 
									
										
										
										
											2016-02-18 18:32:08 +02:00
										 |  |  | 			onQuoteClicked($(this), tid); | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		postContainer.on('click', '[component="post/reply"]', function () { | 
					
						
							| 
									
										
										
										
											2016-02-18 18:32:08 +02:00
										 |  |  | 			onReplyClicked($(this), tid); | 
					
						
							| 
									
										
										
										
											2015-03-20 16:13:34 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		$('.topic').on('click', '[component="topic/reply"]', function () { | 
					
						
							| 
									
										
										
										
											2016-02-18 18:32:08 +02:00
										 |  |  | 			onReplyClicked($(this), tid); | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		$('.topic').on('click', '[component="topic/reply-as-topic"]', function () { | 
					
						
							|  |  |  | 			translator.translate('[[topic:link_back, ' + ajaxify.data.titleRaw + ', ' + config.relative_path + '/topic/' + ajaxify.data.slug + ']]', function (body) { | 
					
						
							| 
									
										
										
										
											2016-02-23 12:55:46 -05:00
										 |  |  | 				$(window).trigger('action:composer.topic.new', { | 
					
						
							|  |  |  | 					cid: ajaxify.data.cid, | 
					
						
							|  |  |  | 					body: body | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2015-12-21 12:51:49 +02:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		postContainer.on('click', '[component="post/bookmark"]', function () { | 
					
						
							| 
									
										
										
										
											2016-12-23 16:35:48 +03:00
										 |  |  | 			return bookmarkPost($(this), getData($(this), 'data-pid')); | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		postContainer.on('click', '[component="post/upvote"]', function () { | 
					
						
							| 
									
										
										
										
											2016-12-23 16:35:48 +03:00
										 |  |  | 			return votes.toggleVote($(this), '.upvoted', 'posts.upvote'); | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		postContainer.on('click', '[component="post/downvote"]', function () { | 
					
						
							| 
									
										
										
										
											2016-12-23 16:35:48 +03:00
										 |  |  | 			return votes.toggleVote($(this), '.downvoted', 'posts.downvote'); | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		postContainer.on('click', '[component="post/vote-count"]', function () { | 
					
						
							| 
									
										
										
										
											2016-12-23 16:35:48 +03:00
										 |  |  | 			votes.showVotes(getData($(this), 'data-pid')); | 
					
						
							| 
									
										
										
										
											2014-11-15 12:37:22 -05:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		postContainer.on('click', '[component="post/flag"]', function () { | 
					
						
							| 
									
										
										
										
											2015-10-13 16:36:43 -04:00
										 |  |  | 			var pid = getData($(this), 'data-pid'); | 
					
						
							| 
									
										
										
										
											2016-10-21 13:39:14 -04:00
										 |  |  | 			var username = getData($(this), 'data-username'); | 
					
						
							|  |  |  | 			var userslug = getData($(this), 'data-userslug'); | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			require(['forum/topic/flag'], function (flag) { | 
					
						
							| 
									
										
										
										
											2016-10-21 13:39:14 -04:00
										 |  |  | 				flag.showFlagModal(pid, username, userslug); | 
					
						
							| 
									
										
										
										
											2015-10-13 16:36:43 -04:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		postContainer.on('click', '[component="post/edit"]', function () { | 
					
						
							| 
									
										
										
										
											2015-04-01 23:28:21 -04:00
										 |  |  | 			var btn = $(this); | 
					
						
							| 
									
										
										
										
											2016-05-25 15:48:53 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-21 19:26:06 +03:00
										 |  |  | 			var timestamp = parseInt(getData(btn, 'data-timestamp'), 10); | 
					
						
							|  |  |  | 			var postEditDuration = parseInt(ajaxify.data.postEditDuration, 10); | 
					
						
							| 
									
										
										
										
											2016-06-28 12:34:09 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			if (checkDuration(postEditDuration, timestamp, 'post-edit-duration-expired')) { | 
					
						
							|  |  |  | 				$(window).trigger('action:composer.post.edit', { | 
					
						
							|  |  |  | 					pid: getData(btn, 'data-pid') | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		postContainer.on('click', '[component="post/delete"]', function () { | 
					
						
							| 
									
										
										
										
											2016-06-28 12:34:09 +03:00
										 |  |  | 			var btn = $(this); | 
					
						
							|  |  |  | 			var timestamp = parseInt(getData(btn, 'data-timestamp'), 10); | 
					
						
							|  |  |  | 			var postDeleteDuration = parseInt(ajaxify.data.postDeleteDuration, 10); | 
					
						
							|  |  |  | 			if (checkDuration(postDeleteDuration, timestamp, 'post-delete-duration-expired')) { | 
					
						
							|  |  |  | 				togglePostDelete($(this), tid); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		function checkDuration(duration, postTimestamp, languageKey) { | 
					
						
							|  |  |  | 			if (!ajaxify.data.privileges.isAdminOrMod && duration && Date.now() - postTimestamp > duration * 1000) { | 
					
						
							|  |  |  | 				var numDays = Math.floor(duration / 86400); | 
					
						
							|  |  |  | 				var numHours = Math.floor((duration % 86400) / 3600); | 
					
						
							|  |  |  | 				var numMinutes = Math.floor(((duration % 86400) % 3600) / 60); | 
					
						
							|  |  |  | 				var numSeconds = ((duration % 86400) % 3600) % 60; | 
					
						
							|  |  |  | 				var msg = '[[error:' + languageKey + ', ' + duration + ']]'; | 
					
						
							| 
									
										
										
										
											2016-05-21 19:26:06 +03:00
										 |  |  | 				if (numDays) { | 
					
						
							|  |  |  | 					if (numHours) { | 
					
						
							| 
									
										
										
										
											2016-06-28 12:34:09 +03:00
										 |  |  | 						msg = '[[error:' + languageKey + '-days-hours, ' + numDays + ', ' + numHours + ']]'; | 
					
						
							| 
									
										
										
										
											2016-05-21 19:26:06 +03:00
										 |  |  | 					} else { | 
					
						
							| 
									
										
										
										
											2016-06-28 12:34:09 +03:00
										 |  |  | 						msg = '[[error:' + languageKey + '-days, ' + numDays + ']]'; | 
					
						
							| 
									
										
										
										
											2016-05-25 15:48:53 +03:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2016-05-21 19:26:06 +03:00
										 |  |  | 				} else if (numHours) { | 
					
						
							|  |  |  | 					if (numMinutes) { | 
					
						
							| 
									
										
										
										
											2016-06-28 12:34:09 +03:00
										 |  |  | 						msg = '[[error:' + languageKey + '-hours-minutes, ' + numHours + ', ' + numMinutes + ']]'; | 
					
						
							| 
									
										
										
										
											2016-05-21 19:26:06 +03:00
										 |  |  | 					} else { | 
					
						
							| 
									
										
										
										
											2016-06-28 12:34:09 +03:00
										 |  |  | 						msg = '[[error:' + languageKey + '-hours, ' + numHours + ']]'; | 
					
						
							| 
									
										
										
										
											2016-05-25 15:48:53 +03:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2016-05-21 19:26:06 +03:00
										 |  |  | 				} else if (numMinutes) { | 
					
						
							|  |  |  | 					if (numSeconds) { | 
					
						
							| 
									
										
										
										
											2016-06-28 12:34:09 +03:00
										 |  |  | 						msg = '[[error:' + languageKey + '-minutes-seconds, ' + numMinutes + ', ' + numSeconds + ']]'; | 
					
						
							| 
									
										
										
										
											2016-05-21 19:26:06 +03:00
										 |  |  | 					} else { | 
					
						
							| 
									
										
										
										
											2016-06-28 12:34:09 +03:00
										 |  |  | 						msg = '[[error:' + languageKey + '-minutes, ' + numMinutes + ']]'; | 
					
						
							| 
									
										
										
										
											2016-05-25 15:48:53 +03:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2016-05-21 19:26:06 +03:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2016-06-28 12:34:09 +03:00
										 |  |  | 				app.alertError(msg); | 
					
						
							|  |  |  | 				return false; | 
					
						
							| 
									
										
										
										
											2016-05-21 19:26:06 +03:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2016-06-28 12:34:09 +03:00
										 |  |  | 			return true; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-04-02 22:54:09 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		postContainer.on('click', '[component="post/restore"]', function () { | 
					
						
							| 
									
										
										
										
											2015-04-02 22:54:09 -04:00
										 |  |  | 			togglePostDelete($(this), tid); | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		postContainer.on('click', '[component="post/purge"]', function () { | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 			purgePost($(this), tid); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		postContainer.on('click', '[component="post/move"]', function () { | 
					
						
							| 
									
										
										
										
											2016-12-23 16:35:48 +03:00
										 |  |  | 			movePost.openMovePostModal($(this)); | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		postContainer.on('click', '[component="post/chat"]', function () { | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 			openChat($(this)); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-18 18:32:08 +02:00
										 |  |  | 	function onReplyClicked(button, tid) { | 
					
						
							| 
									
										
										
										
											2017-02-16 15:01:06 +03:00
										 |  |  | 		var selectedNode = getSelectedNode(); | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		showStaleWarning(function () { | 
					
						
							| 
									
										
										
										
											2016-06-21 13:37:17 +03:00
										 |  |  | 			var username = getUserName(button); | 
					
						
							|  |  |  | 			if (getData(button, 'data-uid') === '0' || !getData(button, 'data-userslug')) { | 
					
						
							|  |  |  | 				username = ''; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2015-10-05 15:37:16 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-21 13:37:17 +03:00
										 |  |  | 			var toPid = button.is('[component="post/reply"]') ? getData(button, 'data-pid') : null; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-16 15:01:06 +03:00
										 |  |  | 			if (selectedNode.text && (!toPid || !selectedNode.pid || toPid === selectedNode.pid)) { | 
					
						
							|  |  |  | 				username = username || selectedNode.username; | 
					
						
							| 
									
										
										
										
											2016-06-21 13:37:17 +03:00
										 |  |  | 				$(window).trigger('action:composer.addQuote', { | 
					
						
							|  |  |  | 					tid: tid, | 
					
						
							|  |  |  | 					pid: toPid, | 
					
						
							|  |  |  | 					topicName: ajaxify.data.titleRaw, | 
					
						
							|  |  |  | 					username: username, | 
					
						
							| 
									
										
										
										
											2017-02-16 15:01:06 +03:00
										 |  |  | 					text: selectedNode.text, | 
					
						
							|  |  |  | 					selectedPid: selectedNode.pid | 
					
						
							| 
									
										
										
										
											2016-06-21 13:37:17 +03:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				$(window).trigger('action:composer.post.new', { | 
					
						
							|  |  |  | 					tid: tid, | 
					
						
							|  |  |  | 					pid: toPid, | 
					
						
							|  |  |  | 					topicName: ajaxify.data.titleRaw, | 
					
						
							|  |  |  | 					text: username ? username + ' ' : '' | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2015-10-07 01:38:27 -04:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-18 18:32:08 +02:00
										 |  |  | 	function onQuoteClicked(button, tid) { | 
					
						
							| 
									
										
										
										
											2017-02-16 15:01:06 +03:00
										 |  |  | 		var selectedNode = getSelectedNode(); | 
					
						
							| 
									
										
										
										
											2016-06-30 11:38:33 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		showStaleWarning(function () { | 
					
						
							| 
									
										
										
										
											2015-10-06 05:28:05 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-20 13:06:08 +03:00
										 |  |  | 			function quote(text) { | 
					
						
							|  |  |  | 				$(window).trigger('action:composer.addQuote', { | 
					
						
							|  |  |  | 					tid: tid, | 
					
						
							| 
									
										
										
										
											2017-02-16 15:01:06 +03:00
										 |  |  | 					pid: toPid, | 
					
						
							| 
									
										
										
										
											2016-06-20 13:06:08 +03:00
										 |  |  | 					username: username, | 
					
						
							|  |  |  | 					topicName: ajaxify.data.titleRaw, | 
					
						
							|  |  |  | 					text: text | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-21 13:37:17 +03:00
										 |  |  | 			var username = getUserName(button); | 
					
						
							| 
									
										
										
										
											2017-02-16 15:01:06 +03:00
										 |  |  | 			var toPid = getData(button, 'data-pid'); | 
					
						
							| 
									
										
										
										
											2016-06-30 11:38:33 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-16 15:01:06 +03:00
										 |  |  | 			if (selectedNode.text && toPid && toPid === selectedNode.pid) { | 
					
						
							|  |  |  | 				return quote(selectedNode.text); | 
					
						
							| 
									
										
										
										
											2016-06-21 13:37:17 +03:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-02-16 15:01:06 +03:00
										 |  |  | 			socket.emit('posts.getRawPost', toPid, function (err, post) { | 
					
						
							| 
									
										
										
										
											2016-06-21 13:37:17 +03:00
										 |  |  | 				if (err) { | 
					
						
							|  |  |  | 					return app.alertError(err.message); | 
					
						
							| 
									
										
										
										
											2016-06-20 13:06:08 +03:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2015-07-03 16:41:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-21 13:37:17 +03:00
										 |  |  | 				quote(post); | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-16 15:01:06 +03:00
										 |  |  | 	function getSelectedNode() { | 
					
						
							|  |  |  | 		var selectedText = ''; | 
					
						
							|  |  |  | 		var selectedPid; | 
					
						
							|  |  |  | 		var username = ''; | 
					
						
							| 
									
										
										
										
											2016-06-20 13:06:08 +03:00
										 |  |  | 		var selection = window.getSelection ? window.getSelection() : document.selection.createRange(); | 
					
						
							| 
									
										
										
										
											2017-02-16 15:01:06 +03:00
										 |  |  | 		var postContents = $('[component="post"] [component="post/content"]'); | 
					
						
							|  |  |  | 		var content; | 
					
						
							|  |  |  | 		postContents.each(function(index, el) { | 
					
						
							|  |  |  | 			if (selection && selection.containsNode && el && selection.containsNode(el, true)) { | 
					
						
							|  |  |  | 				content = el; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2016-06-20 13:06:08 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-16 15:01:06 +03:00
										 |  |  | 		if (content) { | 
					
						
							| 
									
										
										
										
											2016-06-20 13:06:08 +03:00
										 |  |  | 			var bounds = document.createRange(); | 
					
						
							|  |  |  | 			bounds.selectNodeContents(content); | 
					
						
							|  |  |  | 			var range = selection.getRangeAt(0).cloneRange(); | 
					
						
							|  |  |  | 			if (range.compareBoundaryPoints(Range.START_TO_START, bounds) < 0) { | 
					
						
							|  |  |  | 				range.setStart(bounds.startContainer, bounds.startOffset); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			if (range.compareBoundaryPoints(Range.END_TO_END, bounds) > 0) { | 
					
						
							|  |  |  | 				range.setEnd(bounds.endContainer, bounds.endOffset); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			bounds.detach(); | 
					
						
							| 
									
										
										
										
											2017-02-16 15:01:06 +03:00
										 |  |  | 			selectedText = range.toString(); | 
					
						
							|  |  |  | 			var postEl = $(content).parents('[component="post"]'); | 
					
						
							|  |  |  | 			selectedPid = postEl.attr('data-pid'); | 
					
						
							|  |  |  | 			username = getUserName($(content)); | 
					
						
							| 
									
										
										
										
											2016-06-20 13:06:08 +03:00
										 |  |  | 			range.detach(); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-02-16 15:01:06 +03:00
										 |  |  | 		return {text: selectedText, pid: selectedPid, username: username}; | 
					
						
							| 
									
										
										
										
											2016-06-20 13:06:08 +03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-08 19:09:48 +03:00
										 |  |  | 	function bookmarkPost(button, pid) { | 
					
						
							|  |  |  | 		var method = button.attr('data-bookmarked') === 'false' ? 'posts.bookmark' : 'posts.unbookmark'; | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		socket.emit(method, { | 
					
						
							|  |  |  | 			pid: pid, | 
					
						
							|  |  |  | 			room_id: app.currentRoom | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		}, function (err) { | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 			if (err) { | 
					
						
							|  |  |  | 				app.alertError(err.message); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	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) { | 
					
						
							| 
									
										
										
										
											2016-03-08 14:38:44 +02:00
										 |  |  | 		var username = ''; | 
					
						
							|  |  |  | 		var post = button.parents('[data-pid]'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-26 18:29:27 -04:00
										 |  |  | 		if (button.attr('component') === 'topic/reply') { | 
					
						
							|  |  |  | 			return username; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-03-08 14:38:44 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							| 
									
										
										
										
											2016-06-28 12:34:09 +03:00
										 |  |  | 		var pid = getData(button, 'data-pid'); | 
					
						
							|  |  |  | 		var postEl = components.get('post', 'pid', pid); | 
					
						
							|  |  |  | 		var action = !postEl.hasClass('deleted') ? 'delete' : 'restore'; | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		postAction(action, pid, tid); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	function purgePost(button, tid) { | 
					
						
							|  |  |  | 		postAction('purge', getData(button, 'data-pid'), tid); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	function postAction(action, pid, tid) { | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		translator.translate('[[topic:post_' + action + '_confirm]]', function (msg) { | 
					
						
							|  |  |  | 			bootbox.confirm(msg, function (confirm) { | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 				if (!confirm) { | 
					
						
							|  |  |  | 					return; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				socket.emit('posts.' + action, { | 
					
						
							|  |  |  | 					pid: pid, | 
					
						
							|  |  |  | 					tid: tid | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 				}, 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 openChat(button) { | 
					
						
							| 
									
										
										
										
											2015-04-02 17:11:55 +08:00
										 |  |  | 		var post = button.parents('[data-pid]'); | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-16 13:35:24 +02:00
										 |  |  | 		app.newChat(post.attr('data-uid')); | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 		button.parents('.btn-group').find('.dropdown-toggle').click(); | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-07 01:38:27 -04:00
										 |  |  | 	function showStaleWarning(callback) { | 
					
						
							| 
									
										
										
										
											2016-06-30 11:38:33 +03:00
										 |  |  | 		if (staleReplyAnyway || ajaxify.data.lastposttime >= (Date.now() - (1000 * 60 * 60 * 24 * ajaxify.data.topicStaleDays))) { | 
					
						
							| 
									
										
										
										
											2016-06-21 13:37:17 +03:00
										 |  |  | 			return callback(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		translator.translate('[[topic:stale.warning]]', function (translated) { | 
					
						
							| 
									
										
										
										
											2016-06-21 13:37:17 +03:00
										 |  |  | 			var warning = bootbox.dialog({ | 
					
						
							|  |  |  | 					title: '[[topic:stale.title]]', | 
					
						
							|  |  |  | 					message: translated, | 
					
						
							|  |  |  | 					buttons: { | 
					
						
							|  |  |  | 						reply: { | 
					
						
							|  |  |  | 							label: '[[topic:stale.reply_anyway]]', | 
					
						
							|  |  |  | 							className: 'btn-link', | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 							callback: function () { | 
					
						
							| 
									
										
										
										
											2016-06-30 11:38:33 +03:00
										 |  |  | 								staleReplyAnyway = true; | 
					
						
							| 
									
										
										
										
											2016-06-21 13:37:17 +03:00
										 |  |  | 								callback(); | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 						}, | 
					
						
							|  |  |  | 						create: { | 
					
						
							|  |  |  | 							label: '[[topic:stale.create]]', | 
					
						
							|  |  |  | 							className: 'btn-primary', | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 							callback: function () { | 
					
						
							|  |  |  | 								translator.translate('[[topic:link_back, ' + ajaxify.data.title + ', ' + config.relative_path + '/topic/' + ajaxify.data.slug + ']]', function (body) { | 
					
						
							| 
									
										
										
										
											2016-06-21 13:37:17 +03:00
										 |  |  | 									$(window).trigger('action:composer.topic.new', { | 
					
						
							|  |  |  | 										cid: ajaxify.data.cid, | 
					
						
							|  |  |  | 										body: body | 
					
						
							| 
									
										
										
										
											2015-11-13 16:22:27 -05:00
										 |  |  | 									}); | 
					
						
							| 
									
										
										
										
											2016-06-21 13:37:17 +03:00
										 |  |  | 								}); | 
					
						
							| 
									
										
										
										
											2015-11-13 16:22:27 -05:00
										 |  |  | 							} | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2016-06-21 13:37:17 +03:00
										 |  |  | 					} | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2015-10-07 01:38:27 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-21 13:37:17 +03:00
										 |  |  | 			warning.modal(); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2015-10-06 05:28:05 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-08 15:36:47 -04:00
										 |  |  | 	return PostTools; | 
					
						
							|  |  |  | }); |