Files
Iris/src/js/components/Thumbnail.js

98 lines
2.8 KiB
JavaScript
Raw Normal View History

import React, { PropTypes } from 'react'
2017-01-24 09:04:26 +13:00
import { connect } from 'react-redux'
import { createStore, bindActionCreators } from 'redux'
import FontAwesome from 'react-fontawesome'
2016-10-25 17:01:37 +13:00
import * as helpers from '../helpers'
2017-01-24 09:04:26 +13:00
import * as uiActions from '../services/ui/actions'
2017-01-24 09:04:26 +13:00
class Thumbnail extends React.Component{
constructor(props) {
super(props);
}
// TODO: ascertain whether this is improving or hindering performance
// The UI appears to work perfectly fine without this
shouldComponentUpdate(nextProps, nextState){
// no images at all, and we already know it
2017-01-15 21:06:24 +13:00
if (!nextProps.image && !this.props.image && !nextProps.images && !this.props.images) return false
// image changed
if( !this.props.image && nextProps.image ) return true
if( this.props.image && nextProps.image ) return true
if( this.props.image != nextProps.image ) return true
// images array changed
if( typeof(this.props.images) === 'undefined' && nextProps.images ) return true
if( this.props.images && typeof(nextProps.images) === 'undefined' ) return true
if( this.props.images.length != nextProps.images.length ) return true
// image item changed
var size = 'medium'
var images = helpers.sizedImages( nextProps.images )
if( this.props.size ) size = this.props.size
if( this.props.images[size] != images[size] ) return true
return false
2016-10-24 21:26:01 +13:00
}
mapImageSizes( props = this.props ){
// no images
if( !this.props.image && !this.props.images ){
return require('../../assets/no-image.svg')
2016-11-28 08:24:01 +13:00
// single image
}else if( this.props.image ){
return this.props.image
2016-10-25 17:01:37 +13:00
2016-11-28 08:24:01 +13:00
// multiple images
}else if( this.props.images && this.props.images.length > 0 ){
var images = helpers.sizedImages( this.props.images )
var size = 'medium'
if( this.props.size ) size = this.props.size
return images[size]
2016-11-28 08:24:01 +13:00
}
}
2017-01-24 09:04:26 +13:00
zoom(e, image){
e.preventDefault()
this.props.uiActions.openModal('image_zoom', {url: image} )
}
render(){
var image = this.mapImageSizes()
var style = { backgroundImage: 'url("'+image+'")' }
2017-05-31 08:40:55 +12:00
var class_name = 'thumbnail '
if( this.props.size ) class_name += ' '+this.props.size
if( this.props.circle ) class_name += ' circle'
if( this.props.className ) class_name += ' '+this.props.className
2016-10-25 17:01:37 +13:00
2017-01-24 09:04:26 +13:00
var zoom_icon = null
if (this.props.canZoom){
2017-01-24 13:12:12 +13:00
zoom_icon = <span className="zoom" onClick={e => this.zoom(e,image)}><FontAwesome name="search" /></span>
2017-01-24 09:04:26 +13:00
}
return (
2017-01-24 09:04:26 +13:00
<div className={class_name}>
2016-10-25 17:01:37 +13:00
<div className="image" style={style}></div>
2017-01-24 09:04:26 +13:00
{zoom_icon}
2016-10-25 17:01:37 +13:00
</div>
);
}
2017-01-24 09:04:26 +13:00
}
const mapStateToProps = (state, ownProps) => {
return {}
}
const mapDispatchToProps = (dispatch) => {
return {
uiActions: bindActionCreators(uiActions, dispatch)
}
}
export default connect(mapStateToProps, mapDispatchToProps)(Thumbnail)