summaryrefslogtreecommitdiffstats
path: root/root/static/pinboard_activity.js
blob: 0afee78ee29cb70ab2add46bbf45737daec61a00 (plain) (blame)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
 * Pinboard recent bookmarks feed. Modified from:
 * Github activity feed.
 *
 * Graeme Sutherland, July 2012.
 *
 * Uses .json activity from github to show public commits.
 * Requires jQuery and underscore.js
 *
 */

var PinboardActivity = (function($, _) {

    // {"u":"http://www.h-online.com/developer/news/item/SubGit-1-0-stress-free-Subversion-to-Git-migration-1717931.html","d":"SubGit 1.0: \"stress-free\" Subversion to Git migration - The H Developer: News and Features","n":"RT @_doherty: This could be fun at work:  #git #svn","dt":"2012-09-28T23:48:20Z","a":"doy","t":["git","svn"]}
    var
        self = {},
        default_template = '<li class="action"> \
            <a class="pin-title" href="<%= u %>"><%= d %></a> \
            <% if (n) { %> \
                <br /> \
                <%= n %> \
            <% } %> \
            <% if (t.length > 1 || (t.length == 1 && t[0] != "")) { %> \
                <br /> tags: \
                <% _.each(t, function (item) { %> \
                    <a class="pin-tag" href="https://pinboard.in/u:<%= a %>/t:<%= item %>"><%= item %></a> \
                <% }); %> \
            <% } %> \
        </li>';

    /**
     * Fill in activity into selector from public events for username,
     * with optional template selector tmpl_selector.
     */
    self.show_activity = function (username, selector, tmpl_selector) {
        var
            url = 'https://feeds.pinboard.in/json/v1/u:' + username + '/?count=5&cb=?',
            template = $(tmpl_selector).html() || default_template,
            compiled = _.template(template),
            current_date = '',
            $current_list;

        $.getJSON(url, {}, function (data) {
            $.each(data, function(index, commit) {
                var date = commit.dt.substring(0, 10);
                if (date != current_date) {
                    if (current_date) {
                        var $new_item = $('<li class="date"></li>');
                        $new_item.append('<h5>' + current_date + '</h5>');
                        var $div = $('<div class="actions"></div>');
                        $div.append($current_list);
                        $new_item.append($div);
                        $(selector).append($new_item);
                    }

                    $current_list = $('<ul></ul>');
                    current_date = date;
                }
                var $action = $(compiled(commit));
                if (index % 2) {
                    $action.addClass('odd')
                }
                $current_list.append($action);
            });
            var $new_item = $('<li class="date"></li>');
            $new_item.append('<h5>' + current_date + '</h5>');
            var $div = $('<div class="actions"></div>');
            $div.append($current_list);
            $new_item.append($div);
            $(selector).append($new_item);
        });
    };

    return self;

}(jQuery, _));