Custom video controls + button to delete torrents

Also:
* Clean up the state object a bit
* Simplify, factor out torrent-list.js
This commit is contained in:
DC
2016-03-03 08:40:59 -08:00
parent d72999df57
commit 28948e0dbc
4 changed files with 193 additions and 70 deletions

View File

@@ -3,15 +3,29 @@ module.exports = Player
var h = require('virtual-dom/h')
function Player (state, dispatch) {
// Unfortunately, play/pause can't be done just by modifying HTML.
// Instead, grab the DOM node and play/pause it if necessary
var videoElement = document.querySelector('video')
if (videoElement !== null &&
videoElement.paused !== state.video.isPaused) {
if (state.video.isPaused) {
videoElement.pause()
} else {
videoElement.play()
}
}
return h('.player', [
h('video', {
src: state.server.localURL,
autoplay: true,
controls: true,
onloadedmetadata: onLoadedMetadata
})
}),
renderPlayerControls(state, dispatch)
])
// As soon as the video loads far enough to know the dimensions, resize the
// window to match the video resolution
function onLoadedMetadata (e) {
var video = e.target
var dimensions = {
@@ -22,3 +36,37 @@ function Player (state, dispatch) {
}
}
// Renders all video controls: play/pause, scrub, loading bar
// TODO: cast buttons
function renderPlayerControls (state, dispatch) {
return h('.player-controls', [
h('.bottom-bar', [
h('.loading-bar', {
onclick: () => dispatch('playbackJump')
}, renderLoadingBar(state)),
h('.playback-cursor', {
style: {left: '125px'}
}),
h('i.icon.play-pause', {
onclick: () => dispatch('playPause')
}, state.video.isPaused ? 'play_arrow' : 'pause')
])
])
}
// Renders the loading bar. Shows which parts of the torrent are loaded, which
// can be "spongey" / non-contiguous
function renderLoadingBar (state) {
// TODO: get real data from webtorrent
return [
h('.loading-bar-part', {
style: {left: '10px', width: '50px'}
}),
h('.loading-bar-part', {
style: {left: '90px', width: '40px'}
}),
h('.loading-bar-part', {
style: {left: '135px', width: '5px'}
})
]
}

View File

@@ -4,64 +4,66 @@ var h = require('virtual-dom/h')
var prettyBytes = require('pretty-bytes')
function TorrentList (state, dispatch) {
return h('.torrent-list', getList())
var torrents = state.view.client
? state.view.client.torrents
: []
function getList () {
return state.view.client.torrents.map(function (torrent) {
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 + '")'
}
return h('.torrent', {
style: style
}, [
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')
])
]),
h('i.btn.icon.play', {
className: !torrent.ready ? 'disabled' : '',
onclick: openPlayer
}, 'play_arrow'),
(function () {
if (state.view.chromecast) {
return h('i.btn.icon.chromecast', {
className: !torrent.ready ? 'disabled' : '',
onclick: openChromecast
}, 'cast')
}
})(),
(function () {
if (state.view.devices.airplay) {
return h('i.btn.icon.airplay', {
className: !torrent.ready ? 'disabled' : '',
onclick: openAirplay
}, 'airplay')
}
})()
])
function openPlayer () {
dispatch('openPlayer', torrent)
}
function openChromecast () {
dispatch('openChromecast', torrent)
}
function openAirplay () {
dispatch('openAirplay', torrent)
}
})
}
var list = torrents.map((torrent) => renderTorrent(state, dispatch, torrent))
return h('.torrent-list', list)
}
// Renders a torrent in the torrent list
// Includes name, download status, play button, background image
// May be expanded for additional info, including the list of files inside
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 + '")'
}
// 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')
]
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'))
}
return h('.torrent', {style: style}, elements)
}
// 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')
])
])
}