2015-09-18 16:34:36 +03:00
|
|
|
/*
|
|
|
|
Copyright 2015 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.
|
|
|
|
*/
|
2015-11-26 15:02:31 +03:00
|
|
|
var React = require('react');
|
2015-11-26 16:49:39 +03:00
|
|
|
var MatrixClientPeg = require('../../../MatrixClientPeg');
|
2015-11-30 17:39:29 +03:00
|
|
|
var Avatar = require('../../../Avatar');
|
2015-09-18 16:34:36 +03:00
|
|
|
|
2015-11-26 15:02:31 +03:00
|
|
|
module.exports = React.createClass({
|
|
|
|
displayName: 'RoomAvatar',
|
|
|
|
|
2015-09-18 16:34:36 +03:00
|
|
|
getDefaultProps: function() {
|
|
|
|
return {
|
2015-10-11 04:08:39 +03:00
|
|
|
width: 36,
|
|
|
|
height: 36,
|
2015-09-18 16:34:36 +03:00
|
|
|
resizeMethod: 'crop'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-10-22 15:08:35 +03:00
|
|
|
getInitialState: function() {
|
2015-10-23 19:34:11 +03:00
|
|
|
this._update();
|
2015-10-22 15:08:35 +03:00
|
|
|
return {
|
|
|
|
imageUrl: this._nextUrl()
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2015-10-23 19:34:11 +03:00
|
|
|
componentWillReceiveProps: function(nextProps) {
|
2015-10-28 18:15:35 +03:00
|
|
|
this.refreshImageUrl();
|
|
|
|
},
|
|
|
|
|
|
|
|
refreshImageUrl: function(nextProps) {
|
|
|
|
// If the list has changed, we start from scratch and re-check, but
|
|
|
|
// don't do so unless the list has changed or we'd re-try fetching
|
|
|
|
// images each time we re-rendered
|
|
|
|
var newList = this.getUrlList();
|
|
|
|
var differs = false;
|
|
|
|
for (var i = 0; i < newList.length && i < this.urlList.length; ++i) {
|
|
|
|
if (this.urlList[i] != newList[i]) differs = true;
|
|
|
|
}
|
|
|
|
if (this.urlList.length != newList.length) differs = true;
|
|
|
|
|
|
|
|
if (differs) {
|
|
|
|
this._update();
|
|
|
|
this.setState({
|
|
|
|
imageUrl: this._nextUrl()
|
|
|
|
});
|
|
|
|
}
|
2015-10-23 19:34:11 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
_update: function() {
|
|
|
|
this.urlList = this.getUrlList();
|
|
|
|
this.urlListIndex = -1;
|
|
|
|
},
|
|
|
|
|
2015-10-22 15:08:35 +03:00
|
|
|
_nextUrl: function() {
|
|
|
|
do {
|
|
|
|
++this.urlListIndex;
|
|
|
|
} while (
|
|
|
|
this.urlList[this.urlListIndex] === null &&
|
|
|
|
this.urlListIndex < this.urlList.length
|
|
|
|
);
|
|
|
|
if (this.urlListIndex < this.urlList.length) {
|
|
|
|
return this.urlList[this.urlListIndex];
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// provided to the view class for convenience
|
|
|
|
roomAvatarUrl: function() {
|
|
|
|
var url = this.props.room.getAvatarUrl(
|
2015-10-20 12:30:58 +03:00
|
|
|
MatrixClientPeg.get().getHomeserverUrl(),
|
2015-09-18 16:34:36 +03:00
|
|
|
this.props.width, this.props.height, this.props.resizeMethod,
|
|
|
|
false
|
|
|
|
);
|
|
|
|
return url;
|
|
|
|
},
|
|
|
|
|
2015-10-22 15:08:35 +03:00
|
|
|
// provided to the view class for convenience
|
|
|
|
getOneToOneAvatar: function() {
|
|
|
|
var userIds = Object.keys(this.props.room.currentState.members);
|
|
|
|
|
|
|
|
if (userIds.length == 2) {
|
|
|
|
var theOtherGuy = null;
|
|
|
|
if (this.props.room.currentState.members[userIds[0]].userId == MatrixClientPeg.get().credentials.userId) {
|
|
|
|
theOtherGuy = this.props.room.currentState.members[userIds[1]];
|
|
|
|
} else {
|
|
|
|
theOtherGuy = this.props.room.currentState.members[userIds[0]];
|
|
|
|
}
|
|
|
|
return theOtherGuy.getAvatarUrl(
|
|
|
|
MatrixClientPeg.get().getHomeserverUrl(),
|
|
|
|
this.props.width, this.props.height, this.props.resizeMethod,
|
|
|
|
false
|
|
|
|
);
|
|
|
|
} else if (userIds.length == 1) {
|
|
|
|
return this.props.room.currentState.members[userIds[0]].getAvatarUrl(
|
|
|
|
MatrixClientPeg.get().getHomeserverUrl(),
|
|
|
|
this.props.width, this.props.height, this.props.resizeMethod,
|
|
|
|
false
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2015-09-18 16:34:36 +03:00
|
|
|
},
|
|
|
|
|
2015-10-22 15:08:35 +03:00
|
|
|
|
2015-09-18 16:34:36 +03:00
|
|
|
onError: function(ev) {
|
|
|
|
this.setState({
|
2015-10-22 15:08:35 +03:00
|
|
|
imageUrl: this._nextUrl()
|
2015-09-18 16:34:36 +03:00
|
|
|
});
|
2015-11-26 15:02:31 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
////////////
|
|
|
|
|
|
|
|
|
|
|
|
getUrlList: function() {
|
|
|
|
return [
|
|
|
|
this.roomAvatarUrl(),
|
|
|
|
this.getOneToOneAvatar(),
|
|
|
|
this.getFallbackAvatar()
|
|
|
|
];
|
|
|
|
},
|
|
|
|
|
|
|
|
getFallbackAvatar: function() {
|
2015-11-30 17:39:29 +03:00
|
|
|
return Avatar.defaultAvatarUrlForString(this.props.room.roomId);
|
2015-11-26 15:02:31 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
render: function() {
|
|
|
|
var style = {
|
|
|
|
width: this.props.width,
|
|
|
|
height: this.props.height,
|
|
|
|
};
|
|
|
|
|
|
|
|
// XXX: recalculates fallback avatar constantly
|
|
|
|
if (this.state.imageUrl === this.getFallbackAvatar()) {
|
|
|
|
var initial;
|
|
|
|
if (this.props.room.name[0])
|
|
|
|
initial = this.props.room.name[0].toUpperCase();
|
|
|
|
if ((initial === '@' || initial === '#') && this.props.room.name[1])
|
|
|
|
initial = this.props.room.name[1].toUpperCase();
|
|
|
|
|
|
|
|
return (
|
|
|
|
<span>
|
|
|
|
<span className="mx_RoomAvatar_initial" aria-hidden="true"
|
2015-11-30 20:15:02 +03:00
|
|
|
style={{ fontSize: (this.props.width * 0.65) + "px",
|
2015-11-26 15:02:31 +03:00
|
|
|
width: this.props.width + "px",
|
2015-11-30 20:15:02 +03:00
|
|
|
lineHeight: this.props.height + "px" }}>{ initial }</span>
|
2015-11-26 15:02:31 +03:00
|
|
|
<img className="mx_RoomAvatar" src={this.state.imageUrl}
|
|
|
|
onError={this.onError} style={style} />
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return <img className="mx_RoomAvatar" src={this.state.imageUrl}
|
|
|
|
onError={this.onError} style={style} />
|
|
|
|
}
|
2015-09-18 16:34:36 +03:00
|
|
|
}
|
2015-11-26 15:02:31 +03:00
|
|
|
});
|