Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/webtorrent/webtorrent.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFeross Aboukhadijeh <feross@feross.org>2014-02-23 04:20:12 +0400
committerFeross Aboukhadijeh <feross@feross.org>2014-02-23 04:20:12 +0400
commit458033ae44d218588f6d20b6ef45800055995fb4 (patch)
treef7929018e55ea4c0c02ce512d75010a7d9f192df
parente74637461127d6f9b3a1569b368d96b94d27e37e (diff)
make TorrentManager instance the background page
-rw-r--r--index.js7
-rw-r--r--lib/app.js95
-rw-r--r--lib/background.js33
3 files changed, 98 insertions, 37 deletions
diff --git a/index.js b/index.js
index 068691a..179f822 100644
--- a/index.js
+++ b/index.js
@@ -1,7 +1,10 @@
+var App = require('./lib/app')
+var TorrentManager = require('./lib/torrent-manager')
+
if (window.name === 'app') {
- require('./lib/app')()
+ new App(window.torrentManager)
} else {
- require('./lib/background')()
+ new TorrentManager()
}
diff --git a/lib/app.js b/lib/app.js
index 275aa72..34f508b 100644
--- a/lib/app.js
+++ b/lib/app.js
@@ -1,13 +1,34 @@
+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
+ })
-module.exports = function () {
setupWindow()
+ setupTorrentManager()
}
function setupWindow () {
$('.system .close').on('click', function () {
- console.log('CLOSE')
chrome.app.window.current().close()
})
$('.system .minimize').on('click', function () {
@@ -16,4 +37,74 @@ function setupWindow () {
$('.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)
} \ No newline at end of file
diff --git a/lib/background.js b/lib/background.js
deleted file mode 100644
index a2c42bd..0000000
--- a/lib/background.js
+++ /dev/null
@@ -1,33 +0,0 @@
-var TorrentManager = require('./TorrentManager')
-
-module.exports = function () {
-
- var manager = new TorrentManager()
-
- // manager.add('magnet:?xt=urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36&dn=Leaves+of+Grass+by+Walt+Whitman.epub')
-
- manager.on('error', function (err) {
- console.error(err)
- // TODO: Show error in UI somehow
- })
-
- var mainWindow
-
- chrome.app.runtime.onLaunched.addListener(function () {
- chrome.app.window.create('app.html', {
- bounds: {
- width: 500,
- height: 600
- },
- frame: 'none',
- id: 'app',
- minWidth: 350,
- minHeight: 165
- }, function (_mainWindow) {
- mainWindow = _mainWindow
- mainWindow.contentWindow.name = 'app'
- // TODO: on add/remove torrent call resizeTo to set window size
-
- })
- })
-} \ No newline at end of file