Files
Iris/src/js/components/List.js

128 lines
3.4 KiB
JavaScript
Raw Normal View History

2016-11-01 19:27:59 +13:00
import React, { PropTypes } from 'react'
2017-01-27 16:13:11 +13:00
import { connect } from 'react-redux'
import { createStore, bindActionCreators } from 'redux'
import { hashHistory } from 'react-router'
2016-11-11 23:21:51 +13:00
import FontAwesome from 'react-fontawesome'
2016-11-21 16:26:47 +13:00
import ArtistSentence from './ArtistSentence'
2016-12-22 08:46:08 +13:00
import Dater from './Dater'
import URILink from './URILink'
2017-11-18 08:15:06 +13:00
import ContextMenuTrigger from './ContextMenuTrigger'
2016-11-21 16:26:47 +13:00
import * as helpers from '../helpers'
2017-01-27 16:13:11 +13:00
import * as uiActions from '../services/ui/actions'
2016-11-21 16:26:47 +13:00
2017-01-27 16:13:11 +13:00
class List extends React.Component{
2016-11-01 19:27:59 +13:00
2017-10-10 13:05:28 +13:00
constructor(props){
2016-11-01 19:27:59 +13:00
super(props);
}
2016-11-21 16:54:17 +13:00
handleClick(e, uri){
// make sure we haven't clicked a nested link (ie Artist name)
2017-10-10 13:05:28 +13:00
if (e.target.tagName.toLowerCase() !== 'a'){
hashHistory.push((this.props.link_prefix ? this.props.link_prefix : '') + encodeURIComponent(uri));
}
2016-11-21 16:54:17 +13:00
}
handleContextMenu(e,item){
if (this.props.handleContextMenu){
e.preventDefault()
this.props.handleContextMenu(e,item)
}
2017-01-27 16:13:11 +13:00
}
2016-11-01 19:27:59 +13:00
renderHeader(){
if (!this.props.columns || this.props.noheader) return null
2016-11-01 19:27:59 +13:00
return (
<div className="list-item header cf">
{
2017-10-10 13:05:28 +13:00
this.props.columns.map((col, col_index) => {
var className = 'col '+col.name.replace('.','_')
return <div className={className} key={col_index}>{ col.label ? col.label : col.name }</div>
2016-11-01 19:27:59 +13:00
})
}
</div>
)
}
2017-10-10 13:05:28 +13:00
renderValue(row, key_string){
var key = key_string.split('.')
2016-11-21 16:26:47 +13:00
var value = row
2017-10-10 13:05:28 +13:00
for (var i = 0; i < key.length; i++){
if (typeof(value[key[i]]) === 'undefined'){
return <span>-</span>
2017-10-10 13:05:28 +13:00
} else if (typeof(value[key[i]]) === 'string' && value[key[i]].replace(' ','') == ''){
2016-11-21 16:26:47 +13:00
return <span>-</span>
} else {
value = value[key[i]]
2016-11-21 16:26:47 +13:00
}
}
2016-12-22 08:46:08 +13:00
if (key_string === 'added_at') return <span><Dater type="ago" data={value} /> ago</span>
if (key_string === 'owner') return <URILink type="user" uri={value.uri}>{value.id}</URILink>
if (key[0] === 'artists') return <ArtistSentence artists={value} />
if (value === true) return <FontAwesome name="check" />
if (typeof(value) === 'number') return <span>{value.toLocaleString()}</span>
2016-11-11 23:21:51 +13:00
return <span>{value}</span>
}
2016-11-01 19:27:59 +13:00
render(){
if (!this.props.rows) return null
2017-03-13 13:17:11 +13:00
var className = 'list'
if (this.props.className){
className += ' '+this.props.className
}
2016-11-01 19:27:59 +13:00
return (
2017-03-13 13:17:11 +13:00
<div className={className}>
2016-11-01 19:27:59 +13:00
{ this.renderHeader() }
{
2017-10-10 13:05:28 +13:00
this.props.rows.map((row, row_index) => {
var class_name = 'list-item'
2017-10-10 13:05:28 +13:00
if (row.type ) class_name += ' '+row.type
2016-11-01 19:27:59 +13:00
return (
<div
onClick={e => this.handleClick(e, row.uri)}
onContextMenu={e => this.handleContextMenu(e,row)}
className={class_name}
2017-01-27 16:13:11 +13:00
key={row_index}>
2016-11-01 19:27:59 +13:00
{
2017-10-10 13:05:28 +13:00
this.props.columns.map((col, col_index) => {
var className = 'col '+col.name.replace('.','_')
2016-11-02 21:23:35 +13:00
return (
<div className={className} key={col_index}>
2016-11-21 16:26:47 +13:00
{ this.renderValue(row, col.name) }
2016-11-02 21:23:35 +13:00
</div>
)
2016-11-01 19:27:59 +13:00
})
}
2017-11-18 08:15:06 +13:00
<ContextMenuTrigger onTrigger={e => this.handleContextMenu(e, row)} />
2016-11-21 16:54:17 +13:00
</div>
2016-11-01 19:27:59 +13:00
)
})
}
</div>
);
}
2017-01-27 16:13:11 +13:00
}
const mapStateToProps = (state, ownProps) => {
return {}
}
const mapDispatchToProps = (dispatch) => {
return {
uiActions: bindActionCreators(uiActions, dispatch)
}
}
export default connect(mapStateToProps, mapDispatchToProps)(List)