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