File: facebookclient.js

package info (click to toggle)
php-horde 5.2.1%2Bdebian0-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 12,196 kB
  • sloc: php: 11,089; xml: 6,460; sh: 96; makefile: 33; sql: 1
file content (139 lines) | stat: -rw-r--r-- 3,536 bytes parent folder | download | duplicates (2)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
 * Facebook client javascript.
 *
 * @author     Michael J. Rubinsky <mrubinsk@horde.org>
 * @copyright  2014 Horde LLC
 * @license    http://www.horde.org/licenses/lgpl LGPL-2
 */

var Horde_Facebook = Class.create({

    oldest: '',
    newest: '',
    opts: {},

    /**
     * opts.spinner
     * opts.input
     * opts.refreshrate
     * opts.content
     * opts.endpoint,
     * opts.notifications
     * opts.getmore
     * opts.button
     * opts.instance
     * opts.filter
     * opts.count
     *
     */
    initialize: function(opts)
    {
        this.opts = Object.extend({
            refreshrate: 300,
            count: 10,
            filter: 'nf'
        }, opts);

        this.getNewEntries();
        $(this.opts.getmore).observe('click', function(e) { this.getOlderEntries(); e.stop(); }.bind(this));
        $(this.opts.button).observe('click', function(e) { this.updateStatus(); e.stop(); }.bind(this));
    },

    /**
     * Update FB status.
     *
     * @param string statusText  The new status text.
     * @param string inputNode   The DOM Element for the input box.
     *
     * @return void
     */
    updateStatus: function()
    {
        if (!$F(this.opts.input)) {
            return;
        }
        $(this.opts.spinner).toggle();
        var params = {
            statusText: $F(this.opts.input),
            instance: this.opts.instance
        };
        HordeCore.doAction('facebookUpdateStatus',
            params,
            { callback: this._updateStatusCallback.bind(this) }
        );
    },

    _updateStatusCallback: function(r)
    {
        $(this.opts.input).value = '';
        $(this.opts.spinner).toggle();
        $(this.opts.content).insert({ 'top': r });
    },

    addLike: function(post_id)
    {
        $(this.opts.spinner).toggle();
        var params = {
          post_id: post_id,
          instance: this.opts.instance
        };
        HordeCore.doAction('facebookAddLike',
            params,
            { callback: this._addLikeCallback.curry(post_id).bind(this) }
        );
    },

    _addLikeCallback: function(post_id, r)
    {
        $('fb' + post_id).update(r);
        $(this.opts.spinner).toggle();
    },

    getOlderEntries: function() {
        var params = {
            'newest': this.oldest,
            'instance': this.opts.instance,
            'filter': this.opts.filter
        };
        HordeCore.doAction('facebookGetStream',
            params,
            { callback: this._getOlderEntriesCallback.bind(this) }
        );
    },

    _getOlderEntriesCallback: function(response)
    {
        var content = response.c,
            h = $(this.opts.content).scrollHeight;
        this.oldest = response.o;
        $(this.opts.content).insert(content);
        $(this.opts.content).scrollTop = h;
    },

    getNewEntries: function()
    {
        var params = {
            'notifications': this.opts.notifications,
            'oldest': this.oldest,
            'newest': this.newest,
            'instance': this.opts.instance,
            'filter': this.opts.filter
        };
        HordeCore.doAction('facebookGetStream',
            params,
            { callback: this._getNewEntriesCallback.bind(this) }
        );
    },

    _getNewEntriesCallback: function(response)
    {
        $(this.opts.content).insert({ 'top': response.c });
        $(this.opts.notifications).update(response.nt);

        this.newest = response.n;
        if (!this.oldest) {
            this.oldest = response.o;
        }
    }

});