ampache_react/app/components/Songs.jsx

248 lines
8.6 KiB
React
Raw Permalink Normal View History

// NPM imports
2016-07-07 23:23:18 +02:00
import React, { Component, PropTypes } from "react";
import { Link} from "react-router";
2016-07-30 01:05:25 +02:00
import CSSModules from "react-css-modules";
import { defineMessages, injectIntl, intlShape, FormattedMessage } from "react-intl";
import FontAwesome from "react-fontawesome";
import Immutable from "immutable";
2016-07-07 23:23:18 +02:00
import Fuse from "fuse.js";
// Local imports
import { formatLength, messagesMap } from "../utils";
// Other components
import DismissibleAlert from "./elements/DismissibleAlert";
2016-07-07 23:23:18 +02:00
import FilterBar from "./elements/FilterBar";
import Pagination from "./elements/Pagination";
// Translations
import commonMessages from "../locales/messagesDescriptors/common";
import messages from "../locales/messagesDescriptors/Songs";
// Styles
2016-07-30 01:05:25 +02:00
import css from "../styles/Songs.scss";
// Define translations
const songsMessages = defineMessages(messagesMap(Array.concat([], commonMessages, messages)));
2016-07-07 23:23:18 +02:00
/**
* A single row for a single song in the songs table.
*/
class SongsTableRowCSSIntl extends Component {
constructor(props) {
super(props);
// Bind this
this.onPlayClick = this.onPlayClick.bind(this);
this.onPlayNextClick = this.onPlayNextClick.bind(this);
}
/**
* Handle click on play button.
*/
onPlayClick() {
$(this.refs.play).blur();
this.props.playAction(this.props.song.get("id"));
}
/**
* Handle click on play next button.
*/
onPlayNextClick() {
$(this.refs.playNext).blur();
if (this.props.playNextAction) {
this.props.playNextAction(this.props.song.get("id"));
}
}
render() {
const { formatMessage } = this.props.intl;
const length = formatLength(this.props.song.get("time"));
const linkToArtist = "/artist/" + this.props.song.getIn(["artist", "id"]) + "-" + encodeURIComponent(this.props.song.getIn(["artist", "name"]));
const linkToAlbum = linkToArtist + "/album/" + this.props.song.getIn(["album", "id"]) + "-" + encodeURIComponent(this.props.song.getIn(["album", "name"]));
let playNextButton = null;
if (this.props.playNextAction) {
playNextButton = (
<button styleName="playNext" title={formatMessage(songsMessages["app.common.playNext"])} onClick={this.onPlayNextClick} ref="playNext">
<span className="sr-only">
<FormattedMessage {...songsMessages["app.common.playNext"]} />
</span>
<FontAwesome name="plus-circle" aria-hidden="true" />
</button>
);
}
2016-07-07 23:23:18 +02:00
return (
<tr>
2016-08-02 13:07:12 +02:00
<td>
<button styleName="play" title={formatMessage(songsMessages["app.common.play"])} onClick={this.onPlayClick} ref="play">
<span className="sr-only">
<FormattedMessage {...songsMessages["app.common.play"]} />
</span>
<FontAwesome name="play-circle-o" aria-hidden="true" />
</button>&nbsp;
{ playNextButton }
2016-08-02 13:07:12 +02:00
</td>
<td className="title">{this.props.song.get("name")}</td>
<td className="artist"><Link to={linkToArtist}>{this.props.song.getIn(["artist", "name"])}</Link></td>
<td className="album"><Link to={linkToAlbum}>{this.props.song.getIn(["album", "name"])}</Link></td>
<td className="genre">{this.props.song.get("genre")}</td>
2016-07-07 23:23:18 +02:00
<td className="length">{length}</td>
</tr>
);
}
}
SongsTableRowCSSIntl.propTypes = {
playAction: PropTypes.func.isRequired,
playNextAction: PropTypes.func,
song: PropTypes.instanceOf(Immutable.Map).isRequired,
intl: intlShape.isRequired,
2016-07-07 23:23:18 +02:00
};
export let SongsTableRow = injectIntl(CSSModules(SongsTableRowCSSIntl, css));
2016-07-07 23:23:18 +02:00
/**
* The songs table.
*/
2016-07-30 01:05:25 +02:00
class SongsTableCSS extends Component {
render() {
// Handle filtering
let displayedSongs = this.props.songs;
2016-07-07 23:23:18 +02:00
if (this.props.filterText) {
// Use Fuse for the filter
displayedSongs = new Fuse(
this.props.songs.toJS(),
2016-07-07 23:23:18 +02:00
{
"keys": ["name"],
"threshold": 0.4,
"include": ["score"],
2016-07-07 23:23:18 +02:00
}).search(this.props.filterText);
// Keep only items in results
displayedSongs = displayedSongs.map(function (item) { return new Immutable.Map(item.item); });
2016-07-07 23:23:18 +02:00
}
// Build song rows
let rows = [];
const { playAction, playNextAction } = this.props;
2016-07-07 23:23:18 +02:00
displayedSongs.forEach(function (song) {
rows.push(<SongsTableRow playAction={playAction} playNextAction={playNextAction} song={song} key={song.get("id")} />);
2016-07-07 23:23:18 +02:00
});
// Handle login icon
let loading = null;
if (this.props.isFetching) {
loading = (
<p className="text-center">
<FontAwesome name="spinner" className="fa-pulse fa-3x fa-fw" aria-hidden="true" />
<span className="sr-only"><FormattedMessage {...songsMessages["app.common.loading"]} /></span>
</p>
);
}
2016-07-07 23:23:18 +02:00
return (
2016-07-27 13:51:30 +02:00
<div className="table-responsive">
2016-07-30 01:05:25 +02:00
<table className="table table-hover" styleName="songs">
2016-07-27 13:51:30 +02:00
<thead>
<tr>
<th></th>
<th>
<FormattedMessage {...songsMessages["app.songs.title"]} />
</th>
<th className="text-capitalize">
<FormattedMessage {...songsMessages["app.common.artist"]} values={{itemCount: 1}} />
</th>
<th className="text-capitalize">
<FormattedMessage {...songsMessages["app.common.album"]} values={{itemCount: 1}} />
</th>
<th>
<FormattedMessage {...songsMessages["app.songs.genre"]} />
</th>
<th>
<FormattedMessage {...songsMessages["app.songs.length"]} />
</th>
2016-07-27 13:51:30 +02:00
</tr>
</thead>
<tbody>{rows}</tbody>
</table>
{loading}
2016-07-27 13:51:30 +02:00
</div>
2016-07-07 23:23:18 +02:00
);
}
}
2016-07-30 01:05:25 +02:00
SongsTableCSS.propTypes = {
playAction: PropTypes.func.isRequired,
playNextAction: PropTypes.func,
songs: PropTypes.instanceOf(Immutable.List).isRequired,
filterText: PropTypes.string,
2016-07-07 23:23:18 +02:00
};
2016-07-30 01:05:25 +02:00
export let SongsTable = CSSModules(SongsTableCSS, css);
2016-07-07 23:23:18 +02:00
/**
* Complete songs table view with filter and pagination
*/
2016-07-07 23:23:18 +02:00
export default class FilterablePaginatedSongsTable extends Component {
constructor(props) {
2016-07-07 23:23:18 +02:00
super(props);
this.state = {
filterText: "", // Initial state, no filter text
2016-07-07 23:23:18 +02:00
};
this.handleUserInput = this.handleUserInput.bind(this); // Bind this on user input handling
2016-07-07 23:23:18 +02:00
}
/**
* Method called whenever the filter input is changed.
*
* Update the state accordingly.
*
* @param filterText Content of the filter input.
*/
handleUserInput(filterText) {
2016-07-07 23:23:18 +02:00
this.setState({
filterText: filterText,
2016-07-07 23:23:18 +02:00
});
}
render() {
// Handle error
let error = null;
if (this.props.error) {
error = (<DismissibleAlert type="danger" text={this.props.error} />);
}
// Set props
const filterProps = {
filterText: this.state.filterText,
onUserInput: this.handleUserInput,
};
const songsTableProps = {
playAction: this.props.playAction,
playNextAction: this.props.playNextAction,
isFetching: this.props.isFetching,
songs: this.props.songs,
filterText: this.state.filterText,
};
2016-07-07 23:23:18 +02:00
return (
<div>
{ error }
<FilterBar {...filterProps} />
<SongsTable {...songsTableProps} />
<Pagination {...this.props.pagination} />
2016-07-07 23:23:18 +02:00
</div>
);
}
}
FilterablePaginatedSongsTable.propTypes = {
playAction: PropTypes.func.isRequired,
playNextAction: PropTypes.func,
isFetching: PropTypes.bool.isRequired,
error: PropTypes.string,
songs: PropTypes.instanceOf(Immutable.List).isRequired,
pagination: PropTypes.object.isRequired,
2016-07-07 23:23:18 +02:00
};