summaryrefslogtreecommitdiffstats
path: root/deluge/ui/webui/templates/ajax/static/js/deluge-torrent-grid.js
blob: 6e5161329eb655598a81e1d7c53828a8cf7425c5 (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
/*
Script: deluge-torrent-grid.js
    Contains the Deluge torrent grid.

License:
    General Public License v3

Copyright:
    Damien Churchill (c) 2008 <damoxc@gmail.com>


    Class: Deluge.Widgets.TorrentGrid
        Extending Widgest.DataGrid to manage the torrents in the main
        grid.

    Example:
        grid = new Deluge.Widgets.TorrentGrid('torrentGrid');

    Returns:
        An instance of the class wrapped about the torrent grid.
*/
Deluge.Widgets.TorrentGrid = new Class({
    Extends: Widgets.DataGrid,
    
    options: {
        columns: [
            {name: 'number',text: '#',type:'number',width: 20},
            {name: 'name',text: 'Name',type:'icon',width: 350},
            {name: 'size',text: 'Size',type:'bytes',width: 80},
            {name: 'progress',text: 'Progress',type:'progress',width: 180},
            {name: 'seeders',text: 'Seeders',type:'text',width: 80},
            {name: 'peers',text: 'Peers',type:'text',width: 80},
            {name: 'down',text: 'Down Speed',type:'speed',width: 100},
            {name: 'up',text: 'Up Speed',type:'speed',width: 100},
            {name: 'eta',text: 'ETA',type:'time',width: 80},
            {name: 'ratio',text: 'Ratio',type:'number',width: 60},
            {name: 'avail',text: 'Avail.',type:'number',width: 60}
        ]
    },
    
    icons: {
        'Downloading': '/pixmaps/downloading16.png',
        'Seeding': '/pixmaps/seeding16.png',
        'Queued': '/pixmaps/queued16.png',
        'Paused': '/pixmaps/inactive16.png',
        'Error': '/pixmaps/alert16.png',
        'Checking': '/pixmaps/checking16.png'
    },
    
    /*
        Property: getSelectedTorrentIds
            Helper function to quickly return the torrent ids of the currently
            selected torrents in the grid.
        
        Example:
            var ids = '';
            grid.getSelectedTorrentIds.each(function(id) {
                ids += id + '\n';
            });
            alert(ids);
        
        Returns:
            A list containing the currently selected torrent ids.
    */
    getSelectedTorrentIds: function() {
        var torrentIds = [];
        this.getSelected().each(function(row) {
            torrentIds.include(row.id);
        });
        return torrentIds;
    },
    
    /*
        Property: updateTorrents
            Event handler for when a list item is clicked
        
        Arguments:
            e - The event args
        
        Example:
            listItem.addEvent('click', this.clicked.bindWithEvent(this));
    */
    updateTorrents: function(torrents) {
        torrents.each(function(torrent, id) {
            torrent.queue = (torrent.queue > -1) ? torrent.queue + 1 : '';
            torrent.icon = this.icons[torrent.state];
            row = {
                id: id,
                data: {
                    number: torrent.queue,
                    name: {text: torrent.name, icon: torrent.icon},
                    size: torrent.total_size,
                    progress: {percent: torrent.progress, text: torrent.state + ' ' + torrent.progress.toFixed(2) + '%'},
                    seeders: torrent.num_seeds + ' (' + torrent.total_seeds + ')',
                    peers: torrent.num_peers + ' (' + torrent.total_peers + ')',
                    down: torrent.download_payload_rate,
                    up: torrent.upload_payload_rate,
                    eta: torrent.eta,
                    ratio: torrent.ratio.toFixed(3),
                    avail: torrent.distributed_copies.toFixed(3)
                },
                torrent: torrent
            };
            if (this.has(row.id)) {
                this.updateRow(row, true);
            } else {
                this.addRow(row, true);
            };
        }, this);
        
        // remove any torrents no longer in the grid.
        this.rows.each(function(row) {
            if (!torrents.has(row.id)) {
                if (this.selectedRow && this.selectedRow.id == row.id) {
                    this.deselectRow(row);
                };
                delete this.rows[this.rows.indexOf(row)];
            };
        }, this);
        this.render();
    }
});