File: transfersView.js

package info (click to toggle)
filetea 0.1.16-4
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 2,500 kB
  • ctags: 402
  • sloc: sh: 11,465; ansic: 1,862; makefile: 120
file content (253 lines) | stat: -rw-r--r-- 8,450 bytes parent folder | download
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
/*
 * transfersView.js
 *
 * FileTea, low-friction file sharing <http://filetea.net>
 *
 * Copyright (C) 2011, Igalia S.L.
 *
 * Authors:
 *   Eduardo Lima Mitev <elima@igalia.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public License
 * version 3, or (at your option) any later version as published by
 * the Free Software Foundation.
 *
 * This library 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
 * Affero General Public License at http://www.gnu.org/licenses/agpl.html
 * for more details.
 */

// TransfersView
var TransfersView = new Evd.Constructor ();
TransfersView.prototype = new Evd.Object ();

Evd.Object.extend (TransfersView.prototype, {

    _init: function (args) {
        this._parentElement = args.parentElement;
        this._transfers = args.transferManager;

        var self = this;

        this._items = {};
        this._dlItemCount = 0;
        this._ulItemCount = 0;

        this._dlList = document.getElementById ("downloads-list");
        this._ulList = document.getElementById ("uploads-list");

        this._dlListEmptyNote = this._newContainer (this._dlList,
                                                      "No transfers",
                                                      "list-empty-note");
        this._ulListEmptyNote = this._newContainer (this._ulList,
                                                      "No transfers",
                                                      "list-empty-note");

        require (["../common/utils"], function (Utils) {
                     self._utils = Utils;
                 });

        this._transfers.addEventListener ("transfer-started",
            function (transfer) {
                self.add (transfer);
            });

        this._transfers.addEventListener ("transfer-status",
            function (transfer) {
                self._onTransferStatus (transfer);
            });

        this._transfers.addEventListener ("transfer-finished",
            function (transfer) {
                self._onTransferFinished (transfer);
            });

        this._classByStatus = [
            "not-started",
            "active",
            "paused",
            "completed",
            "aborted",
            "aborted"
        ];
        this._cancelDialog = $ ("#transfer-list-confirm-cancel");
        this._cancelDialog.dialog({
                modal: true,
                title: "Cancel transfer",
                autoOpen: false,
                buttons: {
                    "Yes": function () {
                        var id = $ (this).dialog("option", "transferId");
                        self._transfers.cancel ([id]);

                        $ (this).dialog ("close");
                    },
                    "No": function () {
                        $ (this).dialog ("close");
                    }
                }
        });

    },

    _newContainer: function (parent, inner, className) {
        var el = document.createElement ("div");
        el.innerHTML = inner;
        el.className = className;
        parent.appendChild (el);
        return el;
    },

    _getProgressLabel: function (item, transfer) {
        return this._utils.humanizeFileSize (transfer.transferred) + " / " + item.totalSizeSt;
    },

    add: function (transfer) {
        var self = this;

        var id = transfer.id;
        var name = transfer.fileName;

        var className = "transfer-item " + this._classByStatus[transfer.status];
        var item = this._newContainer (this._parentElement, null, className);
        item.isDownload = transfer.isDownload;

        item.totalSizeSt = this._utils.humanizeFileSize (transfer.fileSize);

        item.thumbEl = document.createElement ("img");
        item.thumbEl.className = "transfer-file-thumb";
        item.thumbEl.src = "../common/mime-type-icon-default.png";
        item.appendChild (item.thumbEl);

        item.nameEl = this._newContainer (item, name, "transfer-file-name");

        item.progLabelEl = this._newContainer (item,
                                               this._getProgressLabel (item, transfer),
                                               "transfer-progress-label");

        item.progBarEl = this._newContainer (item,
                                             "",
                                             "transfer-progress-bar");
        item.progBarInnerEl = this._newContainer (item.progBarEl,
                                                  "&nbsp;",
                                                  "transfer-progress-bar-inner");
        item.progBarLabelEl = this._newContainer (item.progBarEl,
                                                  "&nbsp;",
                                                  "transfer-progress-bar-label");

        item.timeLabelEl = this._newContainer (item,
                                               "",
                                               "transfer-time-label");

        item.bwLabelEl = this._newContainer (item, "", "transfer-bw-label");

        item.delEl = this._newContainer (item, "", "transfer-cancel-btn");
        item.delEl.title = "Cancel transfer of '" + name + "'";
        item.delEl.onclick = function (e) {
            self.cancel (id);
        };

        this._items[id] = item;

        if (transfer.isDownload) {
            if (this._dlItemCount == 0)
                this._dlList.removeChild (this._dlListEmptyNote);

            this._dlItemCount++;
            this._dlList.insertBefore (item, this._dlList.childNodes.item (0));
        }
        else {
            if (this._ulItemCount == 0)
                this._ulList.removeChild (this._ulListEmptyNote);

            this._ulItemCount++;
            this._ulList.insertBefore (item, this._ulList.childNodes.item (0));
        }

        this._onTransferStatus (transfer);

        this._fireEvent ("item-added", []);

        this._fireEvent ("have-updates", []);
    },

    isEmpty: function () {
        return this._itemCount == 0;
    },

    cancel: function (id) {
        var item = this._items[id];
        if (! item)
            return;

        var self = this;
        if (item.status == TransferManager.Status.ACTIVE) {
            self._cancelDialog.dialog("option", "transferId", id);
            self._cancelDialog.dialog('open');
        }
        else {
            delete (this._items[id]);

            item.parentNode.removeChild (item);

            if (item.isDownload) {
                this._dlItemCount--;
                if (this._dlItemCount == 0)
                    this._dlList.appendChild (this._dlListEmptyNote);
            }
            else {
                this._ulItemCount--;
                if (this._ulItemCount == 0)
                    this._ulList.appendChild (this._ulListEmptyNote);
            }
        }
    },

    _onTransferStatus: function (transfer) {
        var item = this._items[transfer.id];
        if (! item)
            return;

        item.status = transfer.status;

        var progPercent = Math.floor ((transfer.transferred / transfer.fileSize) * 100 * 100) / 100;
        item.progBarInnerEl.style.width = progPercent + "%";
        item.progBarLabelEl.innerHTML = progPercent + "%";

        if (transfer.bandwidth > 0) {
            item.timeLabelEl.visibility = "visible";
            var time = (transfer.fileSize - transfer.transferred) / (transfer.bandwidth * 1024);
            item.timeLabelEl.innerHTML = this._utils.humanizeTime (time);
        }
        else {
            item.timeLabelEl.visibility = "hidden";
        }

        item.progLabelEl.innerHTML = this._getProgressLabel (item, transfer);

        item.className = "transfer-item " + this._classByStatus[transfer.status];

        item.bwLabelEl.innerHTML = this._utils.humanizeFileSize (Math.round (transfer.bandwidth * 1024)) + "/s";
    },

    _onTransferFinished: function (transfer) {
        this._cancelDialog.dialog("close");

        this._onTransferStatus (transfer);

        var item = this._items[transfer.id];
        if (! item)
            return;

        item.delEl.title = "Remove from list";

        this._fireEvent ("have-updates", []);
    }
});

define (function () {
    return TransfersView;
});