Merge pull request #9 from feross/hyperx

try hyperx
This commit is contained in:
Feross Aboukhadijeh
2016-03-04 21:18:30 -08:00
5 changed files with 142 additions and 102 deletions

View File

@@ -16,6 +16,7 @@
"create-torrent": "^3.22.1",
"debug": "^2.2.0",
"drag-drop": "^2.3.1",
"hyperx": "^1.4.0",
"network-address": "^1.1.0",
"pretty-bytes": "^3.0.0",
"throttleit": "^1.0.0",

View File

@@ -1,22 +1,26 @@
module.exports = App
var h = require('virtual-dom/h')
var hyperx = require('hyperx')
var hx = hyperx(h)
var Header = require('./header')
var Player = require('./player')
var TorrentList = require('./torrent-list')
function App (state, dispatch) {
return h('.app', [
Header(state, dispatch),
h('.content', [
(function () {
if (state.view.url === '/') {
return TorrentList(state, dispatch)
} else if (state.view.url === '/player') {
return Player(state, dispatch)
}
})()
])
])
function getView () {
if (state.view.url === '/') {
return TorrentList(state, dispatch)
} else if (state.view.url === '/player') {
return Player(state, dispatch)
}
}
return hx`
<div.app>
${Header(state, dispatch)}
<div.content>${getView()}</div>
</div>
`
}

View File

@@ -1,32 +1,34 @@
module.exports = Header
var h = require('virtual-dom/h')
var hyperx = require('hyperx')
var hx = hyperx(h)
function Header (state, dispatch) {
return h('.header', [
(function () {
if (process.platform === 'darwin') {
return h('.title', state.view.title)
}
})(),
h('.nav.left', [
h('i.icon.back', {
onclick: onBack
}, 'chevron_left'),
h('i.icon.forward', {
onclick: onForward
}, 'chevron_right')
]),
(function () {
if (state.view.url !== '/player') {
return h('.nav.right', [
h('i.icon.add', {
onclick: onAddTorrent
}, 'add')
])
}
})()
])
return hx`
<div.header>
${getTitle()}
<div.nav.left>
<i.icon.back onclick=${onBack}>chevron_left</i>
<i.icon.forward onclick=${onForward}>chevron_right</i>
</div>
<div.nav.right>
${plusButton()}
</div>
</div>
`
function getTitle () {
if (process.platform === 'darwin') {
return hx`<div.title>${state.view.title}</div>`
}
}
function plusButton () {
if (state.view.url !== '/player') {
return hx`<i.icon.add onclick=${onAddTorrent}>add</i>`
}
}
function onBack (e) {
dispatch('back')

View File

@@ -1,6 +1,8 @@
module.exports = Player
var h = require('virtual-dom/h')
var hyperx = require('hyperx')
var hx = hyperx(h)
var electron = require('electron')
function Player (state, dispatch) {
@@ -23,14 +25,16 @@ function Player (state, dispatch) {
}
// Show the video as large as will fit in the window, play immediately
return h('.player', [
h('video', {
src: state.server.localURL,
autoplay: true,
onloadedmetadata: onLoadedMetadata
}),
renderPlayerControls(state, dispatch)
])
return hx`
<div.player>
<video
src="${state.server.localURL}"
onloadedmetadata="${onLoadedMetadata}"
autoplay="true">
</video>
${renderPlayerControls(state, dispatch)}
</div>
`
// As soon as the video loads far enough to know the dimensions, resize the
// window to match the video resolution
@@ -48,22 +52,22 @@ function Player (state, dispatch) {
// TODO: cast buttons
function renderPlayerControls (state, dispatch) {
var positionPercent = 100 * state.video.currentTime / state.video.duration
return h('.player-controls', [
h('.scrub-bar', {
onclick: handleScrub,
ondrag: handleScrub
}, [
h('.loading-bar', renderLoadingBar(state)),
h('.playback-cursor', {
style: {
left: 'calc(' + positionPercent + '% - 4px)'
}
})
]),
h('i.icon.play-pause', {
onclick: () => dispatch('playPause')
}, state.video.isPaused ? 'play_arrow' : 'pause')
])
var playbackCursorStyle = { left: 'calc(' + positionPercent + '% - 4px)' }
return hx`
<div.player-controls>
<div.scrub-bar
draggable="true"
onclick=${handleScrub},
ondrag=${handleScrub}>
${renderLoadingBar(state)}
<div.playback-cursor style=${playbackCursorStyle}></div>
</div>
<i.icon.play-pause onclick=${() => dispatch('playPause')}>
${state.video.isPaused ? 'play_arrow' : 'pause'}
</i>
</div>
`
// Handles a click or drag to scrub (jump to another position in the video)
function handleScrub (e) {
@@ -99,12 +103,16 @@ function renderLoadingBar (state) {
}
// Output an list of rectangles to show loading progress
return parts.map(function (part) {
return h('.loading-bar-part', {
style: {
left: (100 * part.start / numParts) + '%',
width: (100 * part.count / numParts) + '%'
}
})
})
return hx`
<div.loading-bar>
${parts.map(function (part) {
var style = {
left: (100 * part.start / numParts) + '%',
width: (100 * part.count / numParts) + '%'
}
return hx`<div.loading-bar-part style=${style}></div>`
})}
</div>
`
}

View File

@@ -1,6 +1,8 @@
module.exports = TorrentList
var h = require('virtual-dom/h')
var hyperx = require('hyperx')
var hx = hyperx(h)
var prettyBytes = require('pretty-bytes')
function TorrentList (state, dispatch) {
@@ -9,7 +11,7 @@ function TorrentList (state, dispatch) {
: []
var list = torrents.map((torrent) => renderTorrent(state, dispatch, torrent))
return h('.torrent-list', list)
return hx`<div.torrent-list>${list}</div>`
}
// Renders a torrent in the torrent list
@@ -19,51 +21,74 @@ function renderTorrent (state, dispatch, torrent) {
// Background image: show some nice visuals, like a frame from the movie, if possible
var style = {}
if (torrent.posterURL) {
style['background-image'] = 'linear-gradient(to bottom, rgba(0, 0, 0, 0.5) 0%, rgba(0, 0, 0, 0) 100%), url("' + torrent.posterURL + '")'
style['background-image'] = `linear-gradient(to bottom, rgba(0, 0, 0, 0.5) 0%, rgba(0, 0, 0, 0) 100%), url("${torrent.posterURL}")`
}
// Foreground: name of the torrent, basic info like size, play button,
// cast buttons if available, and delete
var elements = [
renderTorrentMetadata(torrent),
h('i.icon.delete', {
onclick: () => dispatch('deleteTorrent', torrent)
}, 'close'),
h('i.btn.icon.play', {
className: !torrent.ready ? 'disabled' : '',
onclick: () => dispatch('openPlayer', torrent)
}, 'play_arrow')
hx`
<i.icon.delete
onclick=${() => dispatch('deleteTorrent', torrent)}>
close
</i>
`,
hx`
<i.btn.icon.play
className="${!torrent.ready ? 'disabled' : ''}"
onclick=${() => dispatch('openPlayer', torrent)}>
play_arrow
</i>
`
]
if (state.view.chromecast) {
elements.push(h('i.btn.icon.chromecast', {
className: !torrent.ready ? 'disabled' : '',
onclick: () => dispatch('openChromecast', torrent)
}, 'cast'))
}
if (state.view.devices.airplay) {
elements.push(h('i.btn.icon.airplay', {
className: !torrent.ready ? 'disabled' : '',
onclick: () => dispatch('openAirplay', torrent)
}, 'airplay'))
elements.push(hx`
<i.btn.icon.chromecast
className="${!torrent.ready ? 'disabled' : ''}"
onclick=${() => dispatch('openChromecast', torrent)}>
cast
</i>
`)
}
return h('.torrent', {style: style}, elements)
if (state.view.devices.airplay) {
elements.push(hx`
<i.btn.icon.airplay
className="${!torrent.ready ? 'disabled' : ''}"
onclick=${() => dispatch('openAirplay', torrent)}>
airplay
</i>
`)
}
return hx`<div.torrent style=${style}>${elements}</div>`
}
// Renders the torrent name and download progress
function renderTorrentMetadata (torrent) {
return h('.metadata', [
h('.name.ellipsis', torrent.name || 'Loading torrent...'),
h('.status', [
h('span.progress', Math.floor(100 * torrent.progress) + '%'),
(function () {
if (torrent.ready && torrent.files.length > 1) {
return h('span.files', torrent.files.length + ' files')
}
})(),
h('span', torrent.numPeers + ' ' + (torrent.numPeers === 1 ? 'peer' : 'peers')),
h('span', prettyBytes(torrent.downloadSpeed) + '/s'),
h('span', prettyBytes(torrent.uploadSpeed) + '/s')
])
])
return hx`
<div.metadata>
<div.name.ellipsis>${torrent.name || 'Loading torrent...'}</div>
<div.status>
<span.progress>${Math.floor(100 * torrent.progress)}%</span>
</div>
${getFilesLength()}
<span>${getPeers()}</span>
<span>${prettyBytes(torrent.downloadSpeed)}/s</span>
<span>${prettyBytes(torrent.uploadSpeed)}/s</span>
</div>
`
function getPeers () {
var count = torrent.numPeers === 1 ? 'peer' : 'peers'
return `${torrent.numPeers} ${count}`
}
function getFilesLength () {
if (torrent.ready && torrent.files.length > 1) {
return hx`<span.files>${torrent.files.length} files</span>`
}
}
}