refactoring favourites

v1.18.x
psychobunny 11 years ago
parent 25a6302c01
commit 1e955b769b

@ -612,7 +612,7 @@ define(['composer', 'forum/pagination'], function(composer, pagination) {
});
ajaxify.register_events([
'event:rep_up', 'event:rep_down', 'event:new_post', 'get_users_in_room',
'event:rep_up', 'event:rep_down', 'event:favourited', 'event:unfavourited', 'event:new_post', 'get_users_in_room',
'event:topic_deleted', 'event:topic_restored', 'event:topic:locked',
'event:topic_unlocked', 'event:topic_pinned', 'event:topic_unpinned',
'event:topic_moved', 'event:post_edited', 'event:post_deleted', 'event:post_restored',
@ -714,6 +714,14 @@ define(['composer', 'forum/pagination'], function(composer, pagination) {
adjust_rep(-1, data.pid, data.uid);
});
socket.on('event:favourited', function(data) {
adjust_favourites(1, data.pid, data.uid);
});
socket.on('event:unfavourited', function(data) {
adjust_favourites(-1, data.pid, data.uid);
});
socket.on('event:new_post', function(data) {
if(config.usePagination) {
onNewPostPagination(data);
@ -884,6 +892,15 @@ define(['composer', 'forum/pagination'], function(composer, pagination) {
reputationElements.html(reputation).attr('data-reputation', reputation);
}
function adjust_favourites(value, pid, uid) {
var favourites = $('li[data-pid="' + pid + '"] .favouriteCount'),
currentFavourites = parseInt(favourites.attr('data-favourites'), 10);
currentFavourites += value;
favourites.html(currentFavourites).attr('data-favourites', currentFavourites);
}
function set_locked_state(locked, alert) {
var threadReplyBtn = $('.topic-main-buttons .post_reply'),
postReplyBtns = document.querySelectorAll('#post-container .post_reply'),

@ -72,7 +72,7 @@
<button class="btn btn-sm btn-default flag" type="button" title="[[topic:flag_title]]"><i class="fa fa-flag-o"></i></button>
<button data-favourited="{posts.favourited}" class="favourite favourite-tooltip btn btn-sm btn-default <!-- IF posts.favourited --> btn-warning <!-- ENDIF posts.favourited -->" type="button">
<span class="favourite-text">[[topic:favourite]]</span>
<span class="post_rep_{posts.pid}">{posts.reputation} </span>
<span class="favouriteCount" data-favourites="{posts.reputation}">{posts.reputation}</span>&nbsp;
<!-- IF posts.favourited -->
<i class="fa fa-star"></i>
<!-- ELSE -->

@ -186,7 +186,7 @@ var async = require('async'),
db.sortedSetAdd('uid:' + uid + ':favourites', postData.timestamp, pid);
db.setAdd('pid:' + pid + ':users_favourited', uid, function(err) {
db.setCount('pid:' + pid + ':users_favourited', function(err, count) {
posts.setPostField(pid, 'favourited', count);
posts.setPostField(pid, 'reputation', count);
});
});
@ -218,7 +218,7 @@ var async = require('async'),
db.sortedSetRemove('uid:' + uid + ':favourites', pid);
db.setRemove('pid:' + pid + ':users_favourited', uid, function(err) {
db.setCount('pid:' + pid + ':users_favourited', function(err, count) {
posts.setPostField(pid, 'favourited', count);
posts.setPostField(pid, 'reputation', count);
});
});

Loading…
Cancel
Save