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
|
module.exports = App
var $ = require('jquery')
var key = require('keymaster')
var handlebars = require('handlebars')
var humanize = require('humanize')
handlebars.registerHelper('humanizeFilesize', function (bytes) {
return humanize.filesize(bytes)
})
/**
* WebTorrent App UI
*/
function App (torrentManager) {
var self = this
if (!(self instanceof App)) return new App(torrentManager)
self.torrentManager = torrentManager
self.torrentManager.on('error', function (err) {
console.error(err)
// TODO: Show error in UI somehow
})
setupWindow()
setupTorrentManager()
}
function setupWindow () {
$('.system .close').on('click', function () {
chrome.app.window.current().close()
})
$('.system .minimize').on('click', function () {
chrome.app.window.current().minimize()
})
$('.system .maximize').on('click', function () {
chrome.app.window.current().maximize()
})
$('#addTorrent').on('submit', function (e) {
e.preventDefault()
var uri = $('#addTorrent input').val().trim()
console.log(uri)
if (uri) {
window.torrentManager.add(uri)
$('#addTorrent input').val('')
}
})
// chrome.contextMenus.onClicked.addListener(function (info) {
// if (!document.hasFocus()) {
// console.log('Ignoring context menu click that happened in another window');
// return;
// }
// console.log('Item selected in A: ' + info.menuItemId);
// })
// window.addEventListener('load', function (e) {
// chrome.contextMenus.create({
// title: 'Save .torrent',
// id: 'saveTorrent',
// contexts: ['all']
// })
// })
}
function setupTorrentManager () {
window.torrentManager.on('addTorrent', function (torrent) {
var $torrent = $(handlebars.templates.torrent(torrent))
$('#torrents').append($torrent)
window.setInterval(function () {
$torrent.replaceWith(handlebars.templates.torrent(torrent))
}, 300)
})
// var t = self.torrents['d2474e86c95b19b8bcfdb92bc12c9d44667cfa36']
// $('.downloadMetadata').click(function () {
// t.downloadMetadata()
// })
window.torrentManager.on('torrent:metadata', function (torrent, metadata) {
})
}
// TODO: show multiple torrents
function updateUI () {
// console.log('Peer ID: ' + this.peerId.toString('utf8'))
// console.log('Node ID: ' + this.nodeId.toString('hex'))
var t = this.torrents['d2474e86c95b19b8bcfdb92bc12c9d44667cfa36']
$('.infoHash span').text(t.infoHash)
$('.displayName span').text(t.displayName)
$('.dhtNodes span').text(Object.keys(this.dht.nodes).length)
$('.dhtPeers span').text(Object.keys(this.dht.peers).length)
var connectedPeers = 0
for (var infoHash in this.torrents) {
var torrent = this.torrents[infoHash]
connectedPeers += torrent.numPeers
}
$('.connectedPeers span').text(connectedPeers)
$('.downloadMetadata').toggleClass('highlight', !!t.metadata)
}
|