2015-11-27 14:50:33 +03:00
|
|
|
/*
|
2016-01-07 07:06:39 +03:00
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2017-09-11 18:59:09 +03:00
|
|
|
Copyright 2017 New Vector Ltd
|
2015-11-27 14:50:33 +03:00
|
|
|
|
|
|
|
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';
|
|
|
|
|
2017-10-11 19:56:17 +03:00
|
|
|
const React = require('react');
|
|
|
|
const ReactDOM = require("react-dom");
|
2017-12-26 04:03:18 +03:00
|
|
|
import PropTypes from 'prop-types';
|
2017-10-11 19:56:17 +03:00
|
|
|
const classNames = require('classnames');
|
|
|
|
const MatrixClientPeg = require('../../../MatrixClientPeg');
|
2016-10-02 14:57:45 +03:00
|
|
|
import DMRoomMap from '../../../utils/DMRoomMap';
|
2017-10-11 19:56:17 +03:00
|
|
|
const sdk = require('../../../index');
|
|
|
|
const ContextualMenu = require('../../structures/ContextualMenu');
|
|
|
|
const RoomNotifs = require('../../../RoomNotifs');
|
|
|
|
const FormattingUtils = require('../../../utils/FormattingUtils');
|
2017-01-25 01:41:52 +03:00
|
|
|
import AccessibleButton from '../elements/AccessibleButton';
|
2017-09-11 18:59:09 +03:00
|
|
|
import ActiveRoomObserver from '../../../ActiveRoomObserver';
|
|
|
|
import RoomViewStore from '../../../stores/RoomViewStore';
|
2015-11-27 14:50:33 +03:00
|
|
|
|
|
|
|
module.exports = React.createClass({
|
|
|
|
displayName: 'RoomTile',
|
|
|
|
|
|
|
|
propTypes: {
|
2017-12-26 04:03:18 +03:00
|
|
|
onClick: PropTypes.func,
|
2015-11-27 14:50:33 +03:00
|
|
|
|
2017-12-26 04:03:18 +03:00
|
|
|
room: PropTypes.object.isRequired,
|
|
|
|
collapsed: PropTypes.bool.isRequired,
|
|
|
|
unread: PropTypes.bool.isRequired,
|
|
|
|
highlight: PropTypes.bool.isRequired,
|
|
|
|
isInvite: PropTypes.bool.isRequired,
|
|
|
|
incomingCall: PropTypes.object,
|
2015-11-27 14:50:33 +03:00
|
|
|
},
|
|
|
|
|
2016-09-09 18:15:01 +03:00
|
|
|
getDefaultProps: function() {
|
|
|
|
return {
|
|
|
|
isDragging: false,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2015-11-27 14:50:33 +03:00
|
|
|
getInitialState: function() {
|
2017-11-16 16:19:36 +03:00
|
|
|
return ({
|
2017-10-11 19:56:17 +03:00
|
|
|
hover: false,
|
|
|
|
badgeHover: false,
|
2017-03-09 20:03:57 +03:00
|
|
|
menuDisplayed: false,
|
2016-08-18 16:00:14 +03:00
|
|
|
notifState: RoomNotifs.getRoomNotifsState(this.props.room.roomId),
|
2017-09-11 18:59:09 +03:00
|
|
|
selected: this.props.room.roomId === RoomViewStore.getRoomId(),
|
2016-07-18 18:10:07 +03:00
|
|
|
});
|
2015-11-27 14:50:33 +03:00
|
|
|
},
|
|
|
|
|
2016-08-18 13:58:27 +03:00
|
|
|
_shouldShowNotifBadge: function() {
|
2016-08-18 15:44:58 +03:00
|
|
|
const showBadgeInStates = [RoomNotifs.ALL_MESSAGES, RoomNotifs.ALL_MESSAGES_LOUD];
|
2016-08-18 16:00:14 +03:00
|
|
|
return showBadgeInStates.indexOf(this.state.notifState) > -1;
|
2016-08-17 20:26:37 +03:00
|
|
|
},
|
|
|
|
|
2016-08-18 13:58:27 +03:00
|
|
|
_shouldShowMentionBadge: function() {
|
2016-08-18 16:00:14 +03:00
|
|
|
return this.state.notifState != RoomNotifs.MUTE;
|
2016-08-18 13:58:27 +03:00
|
|
|
},
|
|
|
|
|
2016-09-09 16:36:51 +03:00
|
|
|
_isDirectMessageRoom: function(roomId) {
|
2017-10-11 19:56:17 +03:00
|
|
|
const dmRooms = DMRoomMap.shared().getUserIdForRoomId(roomId);
|
2016-09-09 16:36:51 +03:00
|
|
|
if (dmRooms) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-05-16 18:11:01 +03:00
|
|
|
onAccountData: function(accountDataEvent) {
|
|
|
|
if (accountDataEvent.getType() == 'm.push_rules') {
|
|
|
|
this.setState({
|
|
|
|
notifState: RoomNotifs.getRoomNotifsState(this.props.room.roomId),
|
|
|
|
});
|
|
|
|
}
|
2017-04-19 02:13:01 +03:00
|
|
|
},
|
|
|
|
|
2017-09-11 18:59:09 +03:00
|
|
|
_onActiveRoomChange: function() {
|
|
|
|
this.setState({
|
|
|
|
selected: this.props.room.roomId === RoomViewStore.getRoomId(),
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-05-16 18:11:01 +03:00
|
|
|
componentWillMount: function() {
|
|
|
|
MatrixClientPeg.get().on("accountData", this.onAccountData);
|
2017-09-11 18:59:09 +03:00
|
|
|
ActiveRoomObserver.addListener(this.props.room.roomId, this._onActiveRoomChange);
|
2017-04-18 21:28:24 +03:00
|
|
|
},
|
|
|
|
|
2017-05-16 18:11:01 +03:00
|
|
|
componentWillUnmount: function() {
|
2017-10-11 19:56:17 +03:00
|
|
|
const cli = MatrixClientPeg.get();
|
2017-05-16 18:11:01 +03:00
|
|
|
if (cli) {
|
|
|
|
MatrixClientPeg.get().removeListener("accountData", this.onAccountData);
|
|
|
|
}
|
2017-09-11 18:59:09 +03:00
|
|
|
ActiveRoomObserver.removeListener(this.props.room.roomId, this._onActiveRoomChange);
|
2017-04-17 22:58:43 +03:00
|
|
|
},
|
|
|
|
|
2017-04-20 16:16:45 +03:00
|
|
|
onClick: function(ev) {
|
2017-03-03 16:48:37 +03:00
|
|
|
if (this.props.onClick) {
|
2017-04-20 16:16:45 +03:00
|
|
|
this.props.onClick(this.props.room.roomId, ev);
|
2017-03-03 16:48:37 +03:00
|
|
|
}
|
2015-11-27 14:50:33 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
onMouseEnter: function() {
|
2017-10-11 19:56:17 +03:00
|
|
|
this.setState( { hover: true });
|
2016-09-11 02:30:43 +03:00
|
|
|
this.badgeOnMouseEnter();
|
2015-11-27 14:50:33 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
onMouseLeave: function() {
|
2017-10-11 19:56:17 +03:00
|
|
|
this.setState( { hover: false });
|
2016-09-11 02:30:43 +03:00
|
|
|
this.badgeOnMouseLeave();
|
2015-11-27 14:50:33 +03:00
|
|
|
},
|
|
|
|
|
2016-07-18 18:10:07 +03:00
|
|
|
badgeOnMouseEnter: function() {
|
2016-09-11 02:30:43 +03:00
|
|
|
// Only allow non-guests to access the context menu
|
2016-08-02 16:46:47 +03:00
|
|
|
// and only change it if it needs to change
|
|
|
|
if (!MatrixClientPeg.get().isGuest() && !this.state.badgeHover) {
|
2017-10-11 19:56:17 +03:00
|
|
|
this.setState( { badgeHover: true } );
|
2016-07-27 13:58:40 +03:00
|
|
|
}
|
2016-07-18 18:10:07 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
badgeOnMouseLeave: function() {
|
2017-10-11 19:56:17 +03:00
|
|
|
this.setState( { badgeHover: false } );
|
2016-07-18 18:10:07 +03:00
|
|
|
},
|
|
|
|
|
2016-07-21 19:44:31 +03:00
|
|
|
onBadgeClicked: function(e) {
|
2016-07-27 13:58:40 +03:00
|
|
|
// Only allow none guests to access the context menu
|
|
|
|
if (!MatrixClientPeg.get().isGuest()) {
|
2016-07-29 13:10:16 +03:00
|
|
|
// If the badge is clicked, then no longer show tooltip
|
|
|
|
if (this.props.collapsed) {
|
|
|
|
this.setState({ hover: false });
|
|
|
|
}
|
|
|
|
|
2017-10-11 19:56:17 +03:00
|
|
|
const RoomTileContextMenu = sdk.getComponent('context_menus.RoomTileContextMenu');
|
|
|
|
const elementRect = e.target.getBoundingClientRect();
|
2017-03-09 20:03:57 +03:00
|
|
|
|
2016-07-27 13:58:40 +03:00
|
|
|
// The window X and Y offsets are to adjust position when zoomed in to page
|
2017-03-09 20:03:57 +03:00
|
|
|
const x = elementRect.right + window.pageXOffset + 3;
|
|
|
|
const chevronOffset = 12;
|
|
|
|
let y = (elementRect.top + (elementRect.height / 2) + window.pageYOffset);
|
|
|
|
y = y - (chevronOffset + 8); // where 8 is half the height of the chevron
|
|
|
|
|
2017-10-11 19:56:17 +03:00
|
|
|
const self = this;
|
2017-03-09 20:03:57 +03:00
|
|
|
ContextualMenu.createMenu(RoomTileContextMenu, {
|
|
|
|
chevronOffset: chevronOffset,
|
2016-07-27 13:58:40 +03:00
|
|
|
left: x,
|
|
|
|
top: y,
|
|
|
|
room: this.props.room,
|
|
|
|
onFinished: function() {
|
2017-03-09 20:03:57 +03:00
|
|
|
self.setState({ menuDisplayed: false });
|
2016-08-23 14:40:28 +03:00
|
|
|
self.props.refreshSubList();
|
2017-10-11 19:56:17 +03:00
|
|
|
},
|
2016-07-27 13:58:40 +03:00
|
|
|
});
|
2017-03-09 20:03:57 +03:00
|
|
|
this.setState({ menuDisplayed: true });
|
2016-07-27 13:58:40 +03:00
|
|
|
}
|
2016-08-04 18:25:09 +03:00
|
|
|
// Prevent the RoomTile onClick event firing as well
|
|
|
|
e.stopPropagation();
|
2016-07-21 19:44:31 +03:00
|
|
|
},
|
|
|
|
|
2015-11-27 14:50:33 +03:00
|
|
|
render: function() {
|
2017-10-11 19:56:17 +03:00
|
|
|
const myUserId = MatrixClientPeg.get().credentials.userId;
|
|
|
|
const me = this.props.room.currentState.members[myUserId];
|
2016-01-22 20:22:49 +03:00
|
|
|
|
2017-10-11 19:56:17 +03:00
|
|
|
const notificationCount = this.props.room.getUnreadNotificationCount();
|
2016-01-22 20:22:49 +03:00
|
|
|
// var highlightCount = this.props.room.getUnreadNotificationCount("highlight");
|
|
|
|
|
2016-08-18 15:44:58 +03:00
|
|
|
const notifBadges = notificationCount > 0 && this._shouldShowNotifBadge();
|
2017-05-16 18:11:01 +03:00
|
|
|
const mentionBadges = this.props.highlight && this._shouldShowMentionBadge();
|
2016-08-18 15:44:58 +03:00
|
|
|
const badges = notifBadges || mentionBadges;
|
2016-08-18 13:58:27 +03:00
|
|
|
|
2017-10-11 19:56:17 +03:00
|
|
|
const classes = classNames({
|
2015-11-27 14:50:33 +03:00
|
|
|
'mx_RoomTile': true,
|
2017-09-11 18:59:09 +03:00
|
|
|
'mx_RoomTile_selected': this.state.selected,
|
2017-05-16 18:11:01 +03:00
|
|
|
'mx_RoomTile_unread': this.props.unread,
|
2016-08-18 17:19:24 +03:00
|
|
|
'mx_RoomTile_unreadNotify': notifBadges,
|
|
|
|
'mx_RoomTile_highlight': mentionBadges,
|
2015-11-27 14:50:33 +03:00
|
|
|
'mx_RoomTile_invited': (me && me.membership == 'invite'),
|
2017-03-09 20:03:57 +03:00
|
|
|
'mx_RoomTile_menuDisplayed': this.state.menuDisplayed,
|
2016-08-18 13:58:27 +03:00
|
|
|
'mx_RoomTile_noBadges': !badges,
|
2015-11-27 14:50:33 +03:00
|
|
|
});
|
|
|
|
|
2017-10-11 19:56:17 +03:00
|
|
|
const avatarClasses = classNames({
|
2016-07-28 19:24:58 +03:00
|
|
|
'mx_RoomTile_avatar': true,
|
|
|
|
});
|
|
|
|
|
2017-10-11 19:56:17 +03:00
|
|
|
const badgeClasses = classNames({
|
2016-07-28 19:24:58 +03:00
|
|
|
'mx_RoomTile_badge': true,
|
2017-03-09 20:03:57 +03:00
|
|
|
'mx_RoomTile_badgeButton': this.state.badgeHover || this.state.menuDisplayed,
|
2016-07-28 19:24:58 +03:00
|
|
|
});
|
|
|
|
|
2015-12-08 13:21:40 +03:00
|
|
|
// XXX: We should never display raw room IDs, but sometimes the
|
|
|
|
// room name js sdk gives is undefined (cannot repro this -- k)
|
2017-10-11 19:56:17 +03:00
|
|
|
let name = this.props.room.name || this.props.room.roomId;
|
2015-11-27 14:50:33 +03:00
|
|
|
name = name.replace(":", ":\u200b"); // add a zero-width space to allow linewrapping after the colon
|
2016-07-21 16:33:54 +03:00
|
|
|
|
2017-10-11 19:56:17 +03:00
|
|
|
let badge;
|
|
|
|
let badgeContent;
|
2016-07-20 14:47:32 +03:00
|
|
|
|
2017-03-09 20:03:57 +03:00
|
|
|
if (this.state.badgeHover || this.state.menuDisplayed) {
|
2016-07-20 19:12:41 +03:00
|
|
|
badgeContent = "\u00B7\u00B7\u00B7";
|
2016-08-18 13:58:27 +03:00
|
|
|
} else if (badges) {
|
2017-10-11 19:56:17 +03:00
|
|
|
const limitedCount = FormattingUtils.formatCount(notificationCount);
|
2016-07-21 20:50:07 +03:00
|
|
|
badgeContent = notificationCount ? limitedCount : '!';
|
2016-07-20 14:47:32 +03:00
|
|
|
} else {
|
|
|
|
badgeContent = '\u200B';
|
|
|
|
}
|
|
|
|
|
2017-10-11 19:56:17 +03:00
|
|
|
badge = <div className={badgeClasses} onClick={this.onBadgeClicked}>{ badgeContent }</div>;
|
2016-07-20 14:47:32 +03:00
|
|
|
|
2016-08-11 05:25:12 +03:00
|
|
|
const EmojiText = sdk.getComponent('elements.EmojiText');
|
2017-10-11 19:56:17 +03:00
|
|
|
let label;
|
|
|
|
let tooltip;
|
2015-11-27 14:50:33 +03:00
|
|
|
if (!this.props.collapsed) {
|
2017-10-11 19:56:17 +03:00
|
|
|
const nameClasses = classNames({
|
2016-07-28 19:24:58 +03:00
|
|
|
'mx_RoomTile_name': true,
|
|
|
|
'mx_RoomTile_invite': this.props.isInvite,
|
2017-03-09 20:03:57 +03:00
|
|
|
'mx_RoomTile_badgeShown': badges || this.state.badgeHover || this.state.menuDisplayed,
|
2016-07-28 19:24:58 +03:00
|
|
|
});
|
|
|
|
|
2017-09-11 18:59:09 +03:00
|
|
|
if (this.state.selected) {
|
2017-10-11 19:56:17 +03:00
|
|
|
const nameSelected = <EmojiText>{ name }</EmojiText>;
|
2016-07-22 16:58:09 +03:00
|
|
|
|
2017-10-11 19:56:17 +03:00
|
|
|
label = <div title={name} className={nameClasses} dir="auto">{ nameSelected }</div>;
|
2016-07-05 07:46:17 +03:00
|
|
|
} else {
|
2017-10-11 19:56:17 +03:00
|
|
|
label = <EmojiText element="div" title={name} className={nameClasses} dir="auto">{ name }</EmojiText>;
|
2016-04-14 23:11:10 +03:00
|
|
|
}
|
2016-09-09 08:57:30 +03:00
|
|
|
} else if (this.state.hover) {
|
2017-10-11 19:56:17 +03:00
|
|
|
const RoomTooltip = sdk.getComponent("rooms.RoomTooltip");
|
2017-06-03 18:52:45 +03:00
|
|
|
tooltip = <RoomTooltip className="mx_RoomTile_tooltip" room={this.props.room} dir="auto" />;
|
2015-11-27 14:50:33 +03:00
|
|
|
}
|
|
|
|
|
2016-09-15 16:39:34 +03:00
|
|
|
//var incomingCallBox;
|
|
|
|
//if (this.props.incomingCall) {
|
|
|
|
// var IncomingCallBox = sdk.getComponent("voip.IncomingCallBox");
|
|
|
|
// incomingCallBox = <IncomingCallBox incomingCall={ this.props.incomingCall }/>;
|
|
|
|
//}
|
2015-12-17 05:49:09 +03:00
|
|
|
|
2017-10-11 19:56:17 +03:00
|
|
|
const RoomAvatar = sdk.getComponent('avatars.RoomAvatar');
|
2015-11-27 14:50:33 +03:00
|
|
|
|
2017-10-11 19:56:17 +03:00
|
|
|
let directMessageIndicator;
|
2016-09-26 20:02:14 +03:00
|
|
|
if (this._isDirectMessageRoom(this.props.room.roomId)) {
|
2017-10-11 19:56:17 +03:00
|
|
|
directMessageIndicator = <img src="img/icon_person.svg" className="mx_RoomTile_dm" width="11" height="13" alt="dm" />;
|
2016-09-26 20:02:14 +03:00
|
|
|
}
|
2016-09-09 16:36:51 +03:00
|
|
|
|
2018-01-18 20:52:50 +03:00
|
|
|
return <AccessibleButton className={classes} tabIndex="0" onClick={this.onClick} onMouseEnter={this.onMouseEnter} onMouseLeave={this.onMouseLeave}>
|
|
|
|
<div className={avatarClasses}>
|
|
|
|
<div className="mx_RoomTile_avatar_container">
|
|
|
|
<RoomAvatar room={this.props.room} width={24} height={24} />
|
|
|
|
{ directMessageIndicator }
|
2015-11-27 14:50:33 +03:00
|
|
|
</div>
|
2017-01-13 19:25:26 +03:00
|
|
|
</div>
|
2018-01-18 20:52:50 +03:00
|
|
|
<div className="mx_RoomTile_nameContainer">
|
|
|
|
{ label }
|
|
|
|
{ badge }
|
|
|
|
</div>
|
|
|
|
{ /* { incomingCallBox } */ }
|
|
|
|
{ tooltip }
|
|
|
|
</AccessibleButton>;
|
2017-10-11 19:56:17 +03:00
|
|
|
},
|
2015-11-27 14:50:33 +03:00
|
|
|
});
|