Skip to content

Commit

Permalink
Ajax likes. Woot!
Browse files Browse the repository at this point in the history
  • Loading branch information
ewinslow committed Jul 21, 2012
1 parent c8046fb commit 5c8fd5e
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 3 deletions.
40 changes: 40 additions & 0 deletions hooks/register/menu/entity.php
@@ -0,0 +1,40 @@
<?php

$object = $params['entity'];

if (elgg_is_logged_in() && !elgg_in_context('widgets') && elgg_is_active_plugin('likes')) {
$menu = new EvanMenu($return);

if ($object->canAnnotate(0, 'likes')) {
$hasLiked = elgg_annotation_exists($object->guid, 'likes');

// Always register both. That makes it super easy to toggle with javascript
$menu->registerItem('like', array(
'href' => elgg_add_action_tokens_to_url("/action/likes/add?guid={$object->guid}"),
'text' => elgg_view_icon('thumbs-up'),
'title' => elgg_echo('likes:likethis'),
'item_class' => $hasLiked ? 'hidden' : '',
));
$menu->registerItem('unlike', array(
'href' => elgg_add_action_tokens_to_url("/action/likes/delete?guid={$object->guid}"),
'text' => elgg_view_icon('thumbs-up-alt'),
'title' => elgg_echo('likes:remove'),
'item_class' => $hasLiked ? '' : 'hidden',
));
}

$count = elgg_view('likes/count', array('entity' => $object));

if ($count) {
$menu->registerItem('likes_count', array(
'href' => false,
'priority' => 1001,
'text' => $count,
'title' => elgg_echo('likes:see'),
));
}

return $menu->getItems();
}

return NULL;
54 changes: 54 additions & 0 deletions hooks/register/menu/river.php
@@ -0,0 +1,54 @@
<?php

// The river item
$item = $params['item'];

// only like group creation #3958
if ($item->type == "group" && $item->view != "river/group/create") {
return $return;
}

// don't like users #4116
if ($item->type == "user") {
return $return;
}

$object = $item->getObjectEntity();

if (elgg_is_logged_in() && !elgg_in_context('widgets') && elgg_is_active_plugin('likes')) {
$menu = new EvanMenu($return);

if ($object->canAnnotate(0, 'likes')) {
$hasLiked = elgg_annotation_exists($object->guid, 'likes');

// Always register both. That makes it super easy to toggle with javascript
$menu->registerItem('like', array(
'href' => elgg_add_action_tokens_to_url("/action/likes/add?guid={$object->guid}"),
'text' => elgg_view_icon('thumbs-up'),
'title' => elgg_echo('likes:likethis'),
'item_class' => $hasLiked ? 'hidden' : '',
));
$menu->registerItem('unlike', array(
'href' => elgg_add_action_tokens_to_url("/action/likes/delete?guid={$object->guid}"),
'text' => elgg_view_icon('thumbs-up-alt'),
'title' => elgg_echo('likes:remove'),
'item_class' => $hasLiked ? '' : 'hidden',
));
}

$count = elgg_view('likes/count', array('entity' => $object));

if ($count) {
$menu->registerItem('likes_count', array(
'href' => false,
'priority' => 1001,
'text' => $count,
'title' => elgg_echo('likes:see'),
));
}

return $menu->getItems();
}


return NULL;
4 changes: 4 additions & 0 deletions manifest.xml
Expand Up @@ -11,4 +11,8 @@
<type>elgg_release</type>
<version>1.8.3</version>
</requires>
<requires>
<type>plugin</type>
<plugin>evan</plugin>
</requires>
</plugin_manifest>
15 changes: 12 additions & 3 deletions start.php
Expand Up @@ -7,12 +7,21 @@ function ajaxify_init() {

elgg_register_action('entity/delete', dirname(__FILE__) . "/actions/entities/delete.php");

if (elgg_in_context('admin')) {
elgg_extend_view('page/elements/foot', 'requirejs/config/admin');
}
elgg_extend_view('page/elements/foot', 'requirejs/config/admin');

elgg_extend_view('css/admin', 'css/admin/ajaxify');

elgg_unregister_plugin_hook_handler('register', 'menu:river', 'likes_river_menu_setup');
elgg_unregister_plugin_hook_handler('register', 'menu:entity', 'likes_entity_menu_setup');


elgg_register_simplecache_view('js/elgg/ajaxify/likes');
elgg_register_js('elgg/ajaxify/likes', elgg_get_simplecache_url('js', 'elgg/ajaxify/likes'), 'footer');

if (elgg_is_active_plugin('likes')) {
elgg_load_js('elgg/ajaxify/likes');
}

if (elgg_is_admin_logged_in()) {
$views = array(
'admin/statistics/server',
Expand Down
50 changes: 50 additions & 0 deletions views/default/js/elgg/ajaxify/likes.php
@@ -0,0 +1,50 @@
(function($) {
function showLikeHideUnlike($menu) {
$menu.find('.elgg-menu-item-like').removeClass('hidden');
$menu.find('.elgg-menu-item-unlike').addClass('hidden');
}

function showUnlikeHideLike($menu) {
$menu.find('.elgg-menu-item-unlike').removeClass('hidden');
$menu.find('.elgg-menu-item-like').addClass('hidden');
}

// Handles clicking the like button.
$('.elgg-menu-item-like a').live('click', function() {
var $menu = $(this).closest('.elgg-menu');

// Be optimistic about success
showUnlikeHideLike($menu);

// Send the ajax request
elgg.action($(this).attr('href'), {
error: function() {
// Something went wrong, so undo the optimistic changes
showLikeHideUnlike($menu);
}
});

// Don't want to actually click the link
return false;
});

// Handles clicking the unlike button
$('.elgg-menu-item-unlike a').live('click', function() {
var $menu = $(this).closest('.elgg-menu');

// Be optimistic about success
showLikeHideUnlike($menu);

// Send the ajax request
console.log($menu);
elgg.action($(this).attr('href'), {
error: function() {
// Something went wrong, so undo the optimistic changes
showUnlikeHideLike($menu);
}
});

// Don't want to actually click the link
return false;
});
})(jQuery);
6 changes: 6 additions & 0 deletions views/default/requirejs/config/admin.php
@@ -1,3 +1,9 @@
<?php
if (!elgg_in_context('admin')) {
return true;
}
?>

<script>
/**
* Defines some modules to be reused so they can be refactored transparently later on.
Expand Down

0 comments on commit 5c8fd5e

Please sign in to comment.