import React, { PropTypes } from 'react'
import { connect } from 'react-redux'
import { createStore, bindActionCreators } from 'redux'
import { hashHistory } from 'react-router'
import FontAwesome from 'react-fontawesome'
import ArtistSentence from './ArtistSentence'
import Dater from './Dater'
import URILink from './URILink'
import ContextMenuTrigger from './ContextMenuTrigger'
import * as helpers from '../helpers'
import * as uiActions from '../services/ui/actions'
class List extends React.Component{
constructor(props){
super(props);
}
handleClick(e, uri){
// make sure we haven't clicked a nested link (ie Artist name)
if (e.target.tagName.toLowerCase() !== 'a'){
hashHistory.push((this.props.link_prefix ? this.props.link_prefix : '') + encodeURIComponent(uri));
}
}
handleContextMenu(e,item){
if (this.props.handleContextMenu){
e.preventDefault()
this.props.handleContextMenu(e,item)
}
}
renderHeader(){
if (!this.props.columns || this.props.noheader) return null
return (
{
this.props.columns.map((col, col_index) => {
var className = 'col '+col.name.replace('.','_')
return
{ col.label ? col.label : col.name }
})
}
)
}
renderValue(row, key_string){
var key = key_string.split('.')
var value = row
for (var i = 0; i < key.length; i++){
if (typeof(value[key[i]]) === 'undefined'){
return -
} else if (typeof(value[key[i]]) === 'string' && value[key[i]].replace(' ','') == ''){
return -
} else {
value = value[key[i]]
}
}
if (key_string === 'added_at') return ago
if (key_string === 'owner') return {value.id}
if (key[0] === 'artists') return
if (value === true) return
if (typeof(value) === 'number') return {value.toLocaleString()}
return {value}
}
render(){
if (!this.props.rows) return null
var className = 'list'
if (this.props.className){
className += ' '+this.props.className
}
return (
{ this.renderHeader() }
{
this.props.rows.map((row, row_index) => {
var class_name = 'list-item'
if (row.type ) class_name += ' '+row.type
return (
this.handleClick(e, row.uri)}
onContextMenu={e => this.handleContextMenu(e,row)}
className={class_name}
key={row_index}>
{
this.props.columns.map((col, col_index) => {
var className = 'col '+col.name.replace('.','_')
return (
{ this.renderValue(row, col.name) }
)
})
}
this.handleContextMenu(e, row)} />
)
})
}
);
}
}
const mapStateToProps = (state, ownProps) => {
return {}
}
const mapDispatchToProps = (dispatch) => {
return {
uiActions: bindActionCreators(uiActions, dispatch)
}
}
export default connect(mapStateToProps, mapDispatchToProps)(List)