summaryrefslogtreecommitdiffstats
path: root/deluge/ui/webui/templates/ajax/static/js/deluge-bars.js
blob: 9ab48572152e8bf55b7ff7d37a9a2a17b1b615d0 (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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
Script: deluge-bars.js
    Contains the various bars (Sidebar, Toolbar, Statusbar) used within Deluge.

 *
 * Copyright (C) Damien Churchill 2008 <damoxc@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, write to:
 *     The Free Software Foundation, Inc.,
 *     51 Franklin Street, Fifth Floor
 *     Boston, MA  02110-1301, USA.
 *


    Class: Deluge.Widgets.Toolbar
        Manages the top toolbar handling clicks and actions.

    Example:
        toolbar = new Deluge.Widgets.Toolbar();

    Returns:
        An instance of the class wrapped about the toolbar.
*/

Deluge.Widgets.Toolbar = new Class({
    Implements: Events,
    Extends: Widgets.Base,

    initialize: function() {
        this.parent($('toolbar'));
        this.buttons = this.element.getFirst();
        this.buttons.getElements('li').each(function(el) {
            el.addEvent('click', function(e) {
                e.action = el.id;
                this.fireEvent('buttonClick', e);
            }.bind(this));
        }, this);
    }
});


/*
    Class: Deluge.Widgets.StatusBar
        Class to manage the bottom status bar

    Example:
        status = new Deluge.Widgets.StatusBar();

    Returns:
        An instance of the class wrapped about the status div
*/
Deluge.Widgets.StatusBar = new Class({
    Extends: Widgets.Base,

    initialize: function() {
        this.parent($('status'));
        this.bound = {
            onContextMenu: this.onContextMenu.bindWithEvent(this)
        };

        this.element.getElements('li').each(function(el) {
            this[el.id] = el;
        }, this);
        this.incoming_connections.setStyle('display', 'none');

        this.connections.addEvent('contextmenu', this.bound.onContextMenu);
        var menu = new Widgets.PopupMenu();
        menu.add(Deluge.Menus.Connections);
        menu.addEvent('action', this.onMenuAction);
        this.connections.store('menu', menu);

        this.downspeed.addEvent('contextmenu', this.bound.onContextMenu);
        menu = new Widgets.PopupMenu();
        menu.add(Deluge.Menus.Download);
        menu.addEvent('action', this.onMenuAction);
        this.downspeed.store('menu', menu);

        this.upspeed.addEvent('contextmenu', this.bound.onContextMenu);
        menu = new Widgets.PopupMenu();
        menu.add(Deluge.Menus.Upload);
        menu.addEvent('action', this.onMenuAction);
        this.upspeed.store('menu', menu);
    },

    /*
        Property: update
            Takes thes stats part of the update_ui rpc call and
            performs the required changes on the statusbar.

        Arguments:
            stats - A dictionary of the returned stats

        Example:
            statusbar.update(data['stats']);
    */
    update: function(stats) {
        this.connections.set('text', stats.num_connections + ' (' + stats.max_num_connections + ')');
        this.downspeed.set('text', stats.download_rate.toSpeed() + ' (' + stats.max_download + ' KiB/s)');
        this.upspeed.set('text', stats.upload_rate.toSpeed() + ' (' + stats.max_upload + ' KiB/s)');
        this.dht.set('text', stats.dht_nodes);
        this.free_space.set('text', stats.free_space.toBytes());
        if (stats.has_incoming_connections) {
            this.incoming_connections.setStyle('display', 'none');
        } else {
            this.incoming_connections.setStyle('display', 'inline');
        }
    },

    /*
        Property: onContextMenu
            Event handler for when certain parts of the statusbar have been
            right clicked.

        Arguments:
            e - The event args

        Example:
            el.addEvent('contextmenu', this.onContextMenu.bindWithEvent(this));
    */
    onContextMenu: function(e) {
        e.stop();
        var menu = e.target.retrieve('menu');
        if (menu) menu.show(e);
    },

    /*
        Property: onMenuAction
            Event handler for when an item in one of the menus is clicked.
            Note that it does not need to be bound as it doesn't use `this`
            anywhere within the method.

        Arguments:
            e - The event args

        Example:
            menu.addEvent('action', this.onMenuAction);
    */
    onMenuAction: function(e) {
        if (e.action == 'max_connections') e.action = 'max_connections_global';
        config = {}
        config[e.action] = e.value;
        Deluge.Client.set_config(config);
        Deluge.UI.update();
    }
});


/*
    Class: Deluge.Wdigets.Labels
        Class to manage the filtering labels in the sidebar

    Example:
        labels = new Deluge.Widgets.Labels();

    Returns:
        An instance of the class wrapped about the labels div
*/
Deluge.Widgets.Labels = new Class({

    Extends: Widgets.Base,

    initialize: function() {
        this.parent($('labels'));
        this.bound = {
            labelClicked: this.labelClicked.bindWithEvent(this)
        };
        this.filters = {};
    },

    /*
        Property: update
            Takes thes filters part of the update_ui rpc call and
            performs the required changes on the filtering

        Arguments:
            filters - A dictionary of the available filters

        Example:
            labels.update({'state': [['All', '3'], ['Downloading', '2']]);
    */
    update: function(filters) {
        $each(filters, function(values, name) {
            if ($defined(this.filters[name])) {
                this.filters[name].update(values);
            } else {
                this.filters[name] = new Deluge.Widgets.LabelSection(name);
                this.filters[name].addEvent('labelClicked', this.bound.labelClicked);
                this.element.grab(this.filters[name]);
                this.filters[name].update(values);
                if (!this.filterType && !this.filterName) {
                    var el = this.filters[name].list.getElements('li')[0];
                    this.currentFilter = el;
                    this.filterType = name;
                    this.filterName = el.retrieve('filterName');
                    this.currentFilter.addClass('activestate');
                }
            }
        }, this);
    },

    /*
        Property: labelClicked

        Arguments:
            e - The event args

        Example:
            labelSection.addEvent('labelClicked', this.bound.labelClicked);
    */
    labelClicked: function(e) {
        this.currentFilter.removeClass('activestate');
        this.filterType = e.name;
        this.filterName = e.filter;
        this.currentFilter = e.target;
        e.target.addClass('activestate');
        this.fireEvent('filterChanged');
    }
});

/*
    Class: Deluge.Widgets.LabelSection
        Class to manage a section of filters within the labels block

    Arguments:
        string (the name of the section)

    Returns:
        A widget with the ability to manage the filters
*/
Deluge.Widgets.LabelSection = new Class({

    Extends: Widgets.Base,

    regex: /([\w]+)\s\((\d)\)/,

    initialize: function(name) {
        this.parent(new Element('div'));
        this.name = name;
        this.bound = {
            'clicked': this.clicked.bindWithEvent(this)
        }

        name = name.replace('_', ' ');
        parts = name.split(' ');
        name = '';
        parts.each(function(part) {
            firstLetter = part.substring(0, 1);
            firstLetter = firstLetter.toUpperCase();
            part = firstLetter + part.substring(1);
            name += part + ' ';
        });

        this.header = new Element('h3').set('text', name);
        this.list = new Element('ul');

        this.element.grab(this.header);
        this.element.grab(this.list);
    },

    /*
        Property: update
            Updates the filters list

        Arguments:
            values - a list of name/count values for the filters

        Example:
            labelSection.update([['All', '3'], ['Downloading', '2']]);
    */
    update: function(values) {
        names = new Array();
        $each(values, function(value) {
            var name = value[0], count = value[1], lname = name.toLowerCase();
            lname = lname.replace('.', '_');
            names.include(lname);
            var el = this.list.getElement('li.' + lname);
            if (!el) {
                el = new Element('li').addClass(lname);
                el.store('filterName', name)
                el.addEvent('click', this.bound.clicked);
                if (this.name == 'tracker_host') {
                    var icon = 'url(/tracker/icon/' + name + ')';
                    el.setStyle('background-image', icon);
                };
                this.list.grab(el);
            }
            el.set('text', name + ' (' + count +')');
        }, this);

        // Clean out any labels that are no longer returned
        this.list.getElements('li').each(function(el) {
            var hasName = false;
            names.each(function(name) {
                if (hasName) return;
                hasName = el.hasClass(name);
            });

            if (!hasName) {
                el.destroy();
            }
        });
    },

    /*
        Property: clicked
            Event handler for when a list item is clicked

        Arguments:
            e - The event args

        Example:
            listItem.addEvent('click', this.clicked.bindWithEvent(this));
    */
    clicked: function(e) {
        e.filter = e.target.retrieve('filterName');
        e.name = this.name
        this.fireEvent('labelClicked', e);
    }
});