Permalink
Browse files

fix(likes): count is updated after liking/unliking

Also removes focus from the icon so its state is more apparent.

Fixes #9100
  • Loading branch information...
mrclay committed Nov 25, 2015
1 parent c52bd2f commit dae30cb71e8d1900bac8730e594ca8d5ea8d0154
@@ -83,5 +83,19 @@
system_message(elgg_echo("likes:likes"));
if (elgg_is_xhr()) {
$num_of_likes = likes_count($entity);
if ($num_of_likes == 1) {
$likes_string = elgg_echo('likes:userlikedthis', array($num_of_likes));
} else {
$likes_string = elgg_echo('likes:userslikedthis', array($num_of_likes));
}
echo json_encode([
'text' => $likes_string,
'selector' => "[data-likes-guid={$entity->guid}]",
'num_likes' => $num_of_likes,
]);
}
// Forward back to the page where the user 'liked' the object
forward(REFERER);
@@ -22,8 +22,24 @@
}
if ($like && $like->canEdit()) {
$entity = $like->getEntity();
$like->delete();
system_message(elgg_echo("likes:deleted"));
if ($entity && elgg_is_xhr()) {
$num_of_likes = likes_count($entity);
if ($num_of_likes == 1) {
$likes_string = elgg_echo('likes:userlikedthis', array($num_of_likes));
} else {
$likes_string = elgg_echo('likes:userslikedthis', array($num_of_likes));
}
echo json_encode([
'text' => $likes_string,
'selector' => "[data-likes-guid={$entity->guid}]",
'num_likes' => $num_of_likes,
]);
}
forward(REFERER);
}
@@ -8,26 +8,30 @@
$num_of_likes = \Elgg\Likes\DataService::instance()->getNumLikes($vars['entity']);
$guid = $vars['entity']->guid;
if ($num_of_likes) {
elgg_load_js('lightbox');
elgg_load_css('lightbox');
// display the number of likes
if ($num_of_likes == 1) {
$likes_string = elgg_echo('likes:userlikedthis', array($num_of_likes));
} else {
$likes_string = elgg_echo('likes:userslikedthis', array($num_of_likes));
}
$params = array(
'text' => $likes_string,
'title' => elgg_echo('likes:see'),
'class' => 'elgg-lightbox elgg-non-link',
'href' => '#',
'data-colorbox-opts' => json_encode([
'maxHeight' => '85%',
'href' => elgg_normalize_url("ajax/view/likes/popup?guid=$guid")
]),
);
echo elgg_view('output/url', $params);
elgg_load_js('lightbox');
elgg_load_css('lightbox');
// display the number of likes
if ($num_of_likes == 1) {
$likes_string = elgg_echo('likes:userlikedthis', array($num_of_likes));
} else {
$likes_string = elgg_echo('likes:userslikedthis', array($num_of_likes));
}
$class = 'elgg-lightbox elgg-non-link';
if (!$num_of_likes) {
$class .= ' hidden';
}
$params = array(
'text' => $likes_string,
'title' => elgg_echo('likes:see'),
'class' => $class,
'href' => '#',
'data-likes-guid' => $vars['entity']->guid,
'data-colorbox-opts' => json_encode([
'maxHeight' => '85%',
'href' => elgg_normalize_url("ajax/view/likes/popup?guid=$guid")
]),
);
echo elgg_view('output/url', $params);
@@ -3,6 +3,7 @@
* Likes JavaScript extension for elgg.js
*/
?>
//<script>
/**
* Repositions the likes popup
@@ -24,4 +25,39 @@
};
elgg.register_hook_handler('getOptions', 'ui.popup', elgg.ui.likesPopupHandler);
elgg.ui.registerTogglableMenuItems('likes', 'unlike');
!function() {
function setupHandlers(nameA, nameB) {
$(document).on('click', '.elgg-menu-item-' + nameA + ' a', function() {
var $menu = $(this).closest('.elgg-menu');
// Be optimistic about success
elgg.ui.toggleMenuItems($menu, nameB, nameA);
$menu.find('.elgg-menu-item-' + nameB + ' a').blur();
// Send the ajax request
elgg.action($(this).attr('href'), {
success: function(data) {
if (data.system_messages.error.length) {
// Something went wrong, so undo the optimistic changes
elgg.ui.toggleMenuItems($menu, nameA, nameB);
}
var func_name = data.output.num_likes > 0 ? 'removeClass' : 'addClass';
$(data.output.selector).text(data.output.text)[func_name]('hidden');
},
error: function() {
// Something went wrong, so undo the optimistic changes
elgg.ui.toggleMenuItems($menu, nameA, nameB);
}
});
// Don't want to actually click the link
return false;
});
}
setupHandlers('likes', 'unlike');
setupHandlers('unlike', 'likes');
}();

0 comments on commit dae30cb

Please sign in to comment.