'use strict'; /* globals app, ajaxify, define, socket, translator, templates */ define('forum/topic/events', ['forum/topic/browsing', 'forum/topic/postTools', 'forum/topic/threadTools'], function(browsing, postTools, threadTools) { var Events = {}; var events = { 'event:update_users_in_room': browsing.onUpdateUsersInRoom, 'event:user_enter': browsing.onUserEnter, 'event:user_leave': browsing.onUserLeave, 'event:user_status_change': browsing.onUserStatusChange, 'event:voted': updatePostVotesAndUserReputation, 'event:favourited': updateFavouriteCount, 'event:topic_deleted': toggleTopicDeleteState, 'event:topic_restored': toggleTopicDeleteState, 'event:topic_purged': onTopicPurged, 'event:topic_locked': threadTools.setLockedState, 'event:topic_unlocked': threadTools.setLockedState, 'event:topic_pinned': threadTools.setPinnedState, 'event:topic_unpinned': threadTools.setPinnedState, 'event:topic_moved': onTopicMoved, 'event:post_edited': onPostEdited, 'event:post_purged': onPostPurged, 'event:post_deleted': togglePostDeleteState, 'event:post_restored': togglePostDeleteState, 'posts.favourite': togglePostFavourite, 'posts.unfavourite': togglePostFavourite, 'posts.upvote': togglePostVote, 'posts.downvote': togglePostVote, 'posts.unvote': togglePostVote, 'event:topic.notifyTyping': onNotifyTyping, 'event:topic.stopNotifyTyping': onStopNotifyTyping }; Events.init = function() { Events.removeListeners(); for(var eventName in events) { if (events.hasOwnProperty(eventName)) { socket.on(eventName, events[eventName]); } } }; Events.removeListeners = function() { for(var eventName in events) { if (events.hasOwnProperty(eventName)) { socket.removeListener(eventName, events[eventName]); } } }; function updatePostVotesAndUserReputation(data) { var votes = $('li[data-pid="' + data.post.pid + '"] .votes'), reputationElements = $('.reputation[data-uid="' + data.post.uid + '"]'); votes.html(data.post.votes).attr('data-votes', data.post.votes); reputationElements.html(data.user.reputation).attr('data-reputation', data.user.reputation); } function updateFavouriteCount(data) { $('li[data-pid="' + data.post.pid + '"] .favouriteCount').html(data.post.reputation).attr('data-favourites', data.post.reputation); } function toggleTopicDeleteState(data) { threadTools.setLockedState(data); threadTools.setDeleteState(data); } function onTopicPurged(tid) { ajaxify.go('category/' + ajaxify.variables.get('category_id')); } function onTopicMoved(data) { if (data && data.tid > 0) { ajaxify.go('topic/' + data.tid); } } function onPostEdited(data) { var editedPostEl = $('#content_' + data.pid), editedPostTitle = $('#topic_title_' + data.pid); if (editedPostTitle.length) { editedPostTitle.fadeOut(250, function() { editedPostTitle.html(data.title).fadeIn(250); }); } editedPostEl.fadeOut(250, function() { editedPostEl.html(data.content); editedPostEl.find('img').addClass('img-responsive'); app.replaceSelfLinks(editedPostEl.find('a')); editedPostEl.fadeIn(250); $(window).trigger('action:posts.edited'); }); if (data.tags && tagsUpdated(data.tags)) { templates.parse('partials/post_bar', 'tags', {tags: data.tags}, function(html) { var tags = $('.tags'); tags.fadeOut(250, function() { tags.html(html).fadeIn(250); }); }); } } function tagsUpdated(tags) { if (tags.length !== $('.tags').first().children().length) { return true; } for (var i=0; i