From 458033ae44d218588f6d20b6ef45800055995fb4 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Sat, 22 Feb 2014 16:20:12 -0800 Subject: make TorrentManager instance the background page --- index.js | 7 ++-- lib/app.js | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- lib/background.js | 33 ------------------- 3 files changed, 98 insertions(+), 37 deletions(-) delete mode 100644 lib/background.js 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 -- cgit v1.2.3