aboutsummaryrefslogtreecommitdiff
path: root/mod/elgg-favorites/views/default/js/favorites.php
blob: cc364f2edda8b38ac0297d3a76942f6f6bc501b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
/**
 * Favorites JS. 
 */
?>

elgg.provide('elgg.favorites');

elgg.favorites.init = function() {
    $('.elgg-menu-item-favorite .favorites-add').live('click', elgg.favorites.add);
    $('.elgg-menu-item-favorite .favorites-remove').live('click', elgg.favorites.remove);
};

elgg.favorites.add = function(event) {
    event.preventDefault();
    var $link = $(this);
    var $actionParam = $link.attr("href").split("/").pop();
    var $postGuid = $link.attr("href").match(/guid=([^&]+)/)[1];

    elgg.action('favorites/add', {
        data: {
            guid: $postGuid
        },
        success: function(json) {
            $link.attr('href', elgg.config.wwwroot + 'action/favorites/remove/' + $actionParam);
            $link.attr('class', '.elgg-menu-item-favorite .favorites-remove');
            $link.children('span').attr('class','elgg-icon elgg-icon-star');
            $link.unbind('click');
            $link.click(elgg.favorites.remove);
        }
    });
};

elgg.favorites.remove = function(event) {
    event.preventDefault();
    var $link = $(this);
    var $actionParam = $link.attr("href").split("/").pop();
    var $postGuid = $link.attr("href").match(/guid=([^&]+)/)[1];

    elgg.action('favorites/remove', {
        data: {
            guid: $postGuid
        },
        success: function(json) {
            $link.attr("href", elgg.config.wwwroot + 'action/favorites/add/' + $actionParam);
            $link.attr('class', '.elgg-menu-item-favorite .favorites-add');
            $link.children('span').attr('class','elgg-icon elgg-icon-star-empty');
            $link.unbind('click');
            $link.click(elgg.favorites.add);
        }
    });
};

elgg.register_hook_handler('init', 'system', elgg.favorites.init);