parent
ec399d8b3b
commit
d43d363a7a
@ -0,0 +1,123 @@
|
||||
|
||||
|
||||
'use strict';
|
||||
|
||||
/* globals define, app, translator, config, socket, ajaxify */
|
||||
|
||||
define(function() {
|
||||
|
||||
var Browsing = {};
|
||||
|
||||
Browsing.onUpdateUsersInRoom = function(data) {
|
||||
if(data && data.room.indexOf('topic') !== -1) {
|
||||
var activeEl = $('.thread_active_users');
|
||||
|
||||
// remove users that are no longer here
|
||||
activeEl.find('a').each(function(index, element) {
|
||||
if(element) {
|
||||
var uid = $(element).attr('data-uid');
|
||||
var absent = data.users.every(function(user) {
|
||||
return parseInt(user.uid, 10) !== parseInt(uid, 10);
|
||||
});
|
||||
|
||||
if (absent) {
|
||||
$(element).remove();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
var i=0, icon;
|
||||
// add self
|
||||
for(i = 0; i<data.users.length; ++i) {
|
||||
if(parseInt(data.users[i].uid, 10) === parseInt(app.uid, 10)) {
|
||||
icon = createUserIcon(data.users[i].uid, data.users[i].picture, data.users[i].userslug, data.users[i].username);
|
||||
activeEl.prepend(icon);
|
||||
data.users.splice(i, 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
// add other users
|
||||
for(i=0; i<data.users.length; ++i) {
|
||||
icon = createUserIcon(data.users[i].uid, data.users[i].picture, data.users[i].userslug, data.users[i].username);
|
||||
activeEl.append(icon);
|
||||
if(activeEl.children().length > 8) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
activeEl.find('a[data-uid] img').tooltip({
|
||||
placement: 'top'
|
||||
});
|
||||
|
||||
var remainingUsers = data.users.length - 9;
|
||||
remainingUsers = remainingUsers < 0 ? 0 : remainingUsers;
|
||||
var anonymousCount = parseInt(data.anonymousCount, 10);
|
||||
activeEl.find('.anonymous-box').remove();
|
||||
if(anonymousCount || remainingUsers) {
|
||||
|
||||
var anonLink = $('<div class="anonymous-box inline-block"><i class="fa fa-user"></i></div>');
|
||||
activeEl.append(anonLink);
|
||||
|
||||
var title = '';
|
||||
if(remainingUsers && anonymousCount) {
|
||||
title = '[[topic:more_users_and_guests, ' + remainingUsers + ', ' + anonymousCount + ']]';
|
||||
} else if(remainingUsers) {
|
||||
title = '[[topic:more_users, ' + remainingUsers + ']]';
|
||||
} else {
|
||||
title = '[[topic:more_guests, ' + anonymousCount + ']]';
|
||||
}
|
||||
|
||||
translator.translate(title, function(translated) {
|
||||
$('.anonymous-box').tooltip({
|
||||
placement: 'top',
|
||||
title: translated
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
getReplyingUsers();
|
||||
}
|
||||
|
||||
app.populateOnlineUsers();
|
||||
};
|
||||
|
||||
Browsing.onUserOnline = function(err, data) {
|
||||
app.populateOnlineUsers();
|
||||
|
||||
updateBrowsingUsers(data);
|
||||
};
|
||||
|
||||
function updateBrowsingUsers(data) {
|
||||
var activeEl = $('.thread_active_users');
|
||||
var user = activeEl.find('a[data-uid="'+ data.uid + '"]');
|
||||
if (user.length && !data.online) {
|
||||
user.parent().remove();
|
||||
} else if(!user.length && data.online) {
|
||||
user = createUserIcon(data.uid, data.picture, data.userslug, data.username);
|
||||
activeEl.append(user);
|
||||
activeEl.find('a[data-uid] img').tooltip({
|
||||
placement: 'top'
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function createUserIcon(uid, picture, userslug, username) {
|
||||
if(!$('.thread_active_users').find('[data-uid="' + uid + '"]').length) {
|
||||
return $('<div class="inline-block"><a data-uid="' + uid + '" href="' + config.relative_path + '/user/' + userslug + '"><img title="' + username + '" src="'+ picture +'"/></a></div>');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function getReplyingUsers() {
|
||||
var activeEl = $('.thread_active_users');
|
||||
socket.emit('modules.composer.getUsersByTid', ajaxify.variables.get('topic_id'), function(err, uids) {
|
||||
if (uids && uids.length) {
|
||||
for(var x=0;x<uids.length;x++) {
|
||||
activeEl.find('[data-uid="' + uids[x] + '"]').addClass('replying');
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return Browsing;
|
||||
});
|
@ -0,0 +1,152 @@
|
||||
|
||||
'use strict';
|
||||
|
||||
/* globals app, ajaxify, define, socket */
|
||||
|
||||
define(['forum/topic/browsing', 'forum/topic/postTools', 'forum/topic/threadTools'], function(browsing, postTools, threadTools) {
|
||||
|
||||
var Events = {};
|
||||
|
||||
var events = {
|
||||
'event:update_users_in_room': browsing.onUpdateUsersInRoom,
|
||||
'user.isOnline': browsing.onUserOnline,
|
||||
'event:voted': updatePostVotesAndUserReputation,
|
||||
'event:favourited': updateFavouriteCount,
|
||||
|
||||
'event:topic_deleted': toggleTopicDeleteState,
|
||||
'event:topic_restored': toggleTopicDeleteState,
|
||||
|
||||
'event:topic_locked': toggleTopicLockedState,
|
||||
'event:topic_unlocked': toggleTopicLockedState,
|
||||
|
||||
'event:topic_pinned': toggleTopicPinnedState,
|
||||
'event:topic_unpinned': toggleTopicPinnedState,
|
||||
|
||||
'event:topic_moved': onTopicMoved,
|
||||
|
||||
'event:post_edited': onPostEdited,
|
||||
|
||||
'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.toggleReply': toggleReply,
|
||||
};
|
||||
|
||||
Events.init = function() {
|
||||
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 toggleTopicLockedState(data) {
|
||||
threadTools.setLockedState(data);
|
||||
|
||||
app.alertSuccess(data.isLocked ? '[[topic:topic_lock_success]]' : '[[topic:topic_unlock_success]]');
|
||||
}
|
||||
|
||||
function toggleTopicPinnedState(data) {
|
||||
threadTools.setPinnedState(data);
|
||||
|
||||
app.alertSuccess(data.isPinned ? '[[topic:topic_pin_success]]' : '[[topic:topic_unpin_success]]');
|
||||
}
|
||||
|
||||
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);
|
||||
editedPostTitle.fadeIn(250);
|
||||
});
|
||||
}
|
||||
|
||||
editedPostEl.fadeOut(250, function() {
|
||||
editedPostEl.html(data.content);
|
||||
editedPostEl.find('img').addClass('img-responsive');
|
||||
editedPostEl.fadeIn(250);
|
||||
});
|
||||
}
|
||||
|
||||
function togglePostDeleteState(data) {
|
||||
var postEl = $('#post-container li[data-pid="' + data.pid + '"]');
|
||||
|
||||
if (postEl.length) {
|
||||
postEl.toggleClass('deleted');
|
||||
|
||||
postTools.toggle(data.pid, postEl.hasClass('deleted'));
|
||||
|
||||
postTools.updatePostCount();
|
||||
}
|
||||
}
|
||||
|
||||
function togglePostFavourite(data) {
|
||||
|
||||
var favBtn = $('li[data-pid="' + data.post.pid + '"] .favourite');
|
||||
if (favBtn.length) {
|
||||
favBtn.addClass('btn-warning')
|
||||
.attr('data-favourited', data.isFavourited);
|
||||
|
||||
var icon = favBtn.find('i');
|
||||
var className = icon.attr('class');
|
||||
|
||||
if (data.isFavourited ? className.indexOf('-o') !== -1 : className.indexOf('-o') === -1) {
|
||||
icon.attr('class', data.isFavourited ? className.replace('-o', '') : className + '-o');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function togglePostVote(data) {
|
||||
var post = $('li[data-pid="' + data.post.pid + '"]');
|
||||
|
||||
post.find('.upvote').toggleClass('btn-primary upvoted', data.upvote);
|
||||
post.find('.downvote').toggleClass('btn-primary downvoted', data.downvote);
|
||||
}
|
||||
|
||||
function toggleReply(data) {
|
||||
$('.thread_active_users [data-uid="' + data.uid + '"]').toggleClass('replying', data.isReplying);
|
||||
}
|
||||
|
||||
return Events;
|
||||
|
||||
});
|
Loading…
Reference in New Issue