2016-01-15 15:02:28 +03:00
|
|
|
/*
|
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var React = require('react');
|
|
|
|
var AvatarLogic = require("../../../Avatar");
|
|
|
|
|
|
|
|
module.exports = React.createClass({
|
|
|
|
displayName: 'BaseAvatar',
|
|
|
|
|
|
|
|
propTypes: {
|
2016-01-15 19:13:25 +03:00
|
|
|
name: React.PropTypes.string.isRequired, // The name (first initial used as default)
|
2016-01-15 15:02:28 +03:00
|
|
|
idName: React.PropTypes.string, // ID for generating hash colours
|
2016-01-15 19:13:25 +03:00
|
|
|
title: React.PropTypes.string, // onHover title text
|
2016-01-15 20:05:05 +03:00
|
|
|
url: React.PropTypes.string, // highest priority of them all, shortcut to set in urls[0]
|
2016-01-15 15:02:28 +03:00
|
|
|
urls: React.PropTypes.array, // [highest_priority, ... , lowest_priority]
|
|
|
|
width: React.PropTypes.number,
|
|
|
|
height: React.PropTypes.number,
|
|
|
|
resizeMethod: React.PropTypes.string,
|
2016-01-15 20:05:05 +03:00
|
|
|
defaultToInitialLetter: React.PropTypes.bool // true to add default url
|
2016-01-15 15:02:28 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
getDefaultProps: function() {
|
|
|
|
return {
|
|
|
|
width: 40,
|
|
|
|
height: 40,
|
|
|
|
resizeMethod: 'crop',
|
|
|
|
defaultToInitialLetter: true
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
getInitialState: function() {
|
2016-01-15 20:05:05 +03:00
|
|
|
return this._getState(this.props);
|
|
|
|
},
|
|
|
|
|
|
|
|
componentWillReceiveProps: function(nextProps) {
|
|
|
|
// work out if we need to call setState (if the image URLs array has changed)
|
|
|
|
var newState = this._getState(nextProps);
|
|
|
|
var newImageUrls = newState.imageUrls;
|
|
|
|
var oldImageUrls = this.state.imageUrls;
|
|
|
|
if (newImageUrls.length !== oldImageUrls.length) {
|
|
|
|
this.setState(newState); // detected a new entry
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// check each one to see if they are the same
|
|
|
|
for (var i = 0; i < newImageUrls.length; i++) {
|
|
|
|
if (oldImageUrls[i] !== newImageUrls[i]) {
|
|
|
|
this.setState(newState); // detected a diff
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_getState: function(props) {
|
|
|
|
// work out the full set of urls to try to load. This is formed like so:
|
|
|
|
// imageUrls: [ props.url, props.urls, default image ]
|
|
|
|
|
|
|
|
var urls = props.urls || [];
|
|
|
|
if (props.url) {
|
|
|
|
urls.unshift(props.url); // put in urls[0]
|
|
|
|
}
|
|
|
|
|
2016-01-15 15:02:28 +03:00
|
|
|
var defaultImageUrl = null;
|
2016-01-15 20:05:05 +03:00
|
|
|
if (props.defaultToInitialLetter) {
|
2016-01-15 15:02:28 +03:00
|
|
|
defaultImageUrl = AvatarLogic.defaultAvatarUrlForString(
|
2016-01-15 20:05:05 +03:00
|
|
|
props.idName || props.name
|
2016-01-15 15:02:28 +03:00
|
|
|
);
|
2016-01-15 20:05:05 +03:00
|
|
|
urls.push(defaultImageUrl); // lowest priority
|
2016-01-15 15:02:28 +03:00
|
|
|
}
|
|
|
|
return {
|
2016-01-15 20:05:05 +03:00
|
|
|
imageUrls: urls,
|
2016-01-15 15:02:28 +03:00
|
|
|
defaultImageUrl: defaultImageUrl,
|
|
|
|
urlsIndex: 0
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2016-01-15 20:05:05 +03:00
|
|
|
onError: function(ev) {
|
|
|
|
var nextIndex = this.state.urlsIndex + 1;
|
|
|
|
if (nextIndex < this.state.imageUrls.length) {
|
|
|
|
// try the next one
|
2016-01-15 15:02:28 +03:00
|
|
|
this.setState({
|
2016-01-15 20:05:05 +03:00
|
|
|
urlsIndex: nextIndex
|
2016-01-15 15:02:28 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_getInitialLetter: function() {
|
|
|
|
var name = this.props.name;
|
|
|
|
var initial = name[0];
|
2016-01-15 19:13:25 +03:00
|
|
|
if ((initial === '@' || initial === '#') && name[1]) {
|
2016-01-15 15:02:28 +03:00
|
|
|
initial = name[1];
|
|
|
|
}
|
|
|
|
return initial.toUpperCase();
|
|
|
|
},
|
|
|
|
|
|
|
|
render: function() {
|
|
|
|
var name = this.props.name;
|
|
|
|
|
2016-01-15 20:05:05 +03:00
|
|
|
var imageUrl = this.state.imageUrls[this.state.urlsIndex];
|
|
|
|
|
|
|
|
if (imageUrl === this.state.defaultImageUrl) {
|
2016-01-15 15:02:28 +03:00
|
|
|
var initialLetter = this._getInitialLetter();
|
|
|
|
return (
|
2016-01-15 19:13:25 +03:00
|
|
|
<span className="mx_BaseAvatar" {...this.props}>
|
|
|
|
<span className="mx_BaseAvatar_initial" aria-hidden="true"
|
2016-01-15 15:02:28 +03:00
|
|
|
style={{ fontSize: (this.props.width * 0.65) + "px",
|
|
|
|
width: this.props.width + "px",
|
|
|
|
lineHeight: this.props.height + "px" }}>
|
|
|
|
{ initialLetter }
|
|
|
|
</span>
|
2016-01-15 20:05:05 +03:00
|
|
|
<img className="mx_BaseAvatar_image" src={imageUrl}
|
2016-01-15 15:02:28 +03:00
|
|
|
title={this.props.title} onError={this.onError}
|
|
|
|
width={this.props.width} height={this.props.height} />
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return (
|
2016-01-15 20:05:05 +03:00
|
|
|
<img className="mx_BaseAvatar mx_BaseAvatar_image" src={imageUrl}
|
2016-01-15 15:02:28 +03:00
|
|
|
onError={this.onError}
|
|
|
|
width={this.props.width} height={this.props.height}
|
|
|
|
title={this.props.title}
|
|
|
|
{...this.props} />
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|