2018-04-11 22:58:04 +00:00
|
|
|
/*
|
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2018-06-25 08:41:28 +00:00
|
|
|
Copyright 2017 Vector Creations Ltd
|
|
|
|
Copyright 2018 New Vector Ltd
|
2018-04-11 22:58:04 +00: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.
|
|
|
|
*/
|
|
|
|
|
2018-06-25 08:51:23 +00:00
|
|
|
import React from 'react';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import sdk from '../../index';
|
|
|
|
import dis from '../../dispatcher';
|
|
|
|
import Unread from '../../Unread';
|
|
|
|
import * as RoomNotifs from '../../RoomNotifs';
|
|
|
|
import * as FormattingUtils from '../../utils/FormattingUtils';
|
2018-11-12 11:57:21 +00:00
|
|
|
import IndicatorScrollbar from './IndicatorScrollbar';
|
2018-04-12 23:43:44 +00:00
|
|
|
import { KeyCode } from '../../Keyboard';
|
2018-06-27 08:49:58 +00:00
|
|
|
import { Group } from 'matrix-js-sdk';
|
2018-06-30 16:06:33 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2019-02-13 13:51:09 +00:00
|
|
|
import RoomTile from "../views/rooms/RoomTile";
|
|
|
|
import LazyRenderList from "../views/elements/LazyRenderList";
|
2018-04-11 22:58:04 +00:00
|
|
|
|
|
|
|
// turn this on for drop & drag console debugging galore
|
2018-06-25 08:51:23 +00:00
|
|
|
const debug = false;
|
2018-04-11 22:58:04 +00:00
|
|
|
|
2018-06-25 08:51:23 +00:00
|
|
|
const RoomSubList = React.createClass({
|
2018-04-11 22:58:04 +00:00
|
|
|
displayName: 'RoomSubList',
|
|
|
|
|
|
|
|
debug: debug,
|
|
|
|
|
|
|
|
propTypes: {
|
2018-06-30 16:06:33 +00:00
|
|
|
list: PropTypes.arrayOf(PropTypes.object).isRequired,
|
|
|
|
label: PropTypes.string.isRequired,
|
|
|
|
tagName: PropTypes.string,
|
2018-04-11 22:58:04 +00:00
|
|
|
|
2018-06-30 16:06:33 +00:00
|
|
|
order: PropTypes.string.isRequired,
|
2018-04-11 22:58:04 +00:00
|
|
|
|
|
|
|
// passed through to RoomTile and used to highlight room with `!` regardless of notifications count
|
2018-06-30 16:06:33 +00:00
|
|
|
isInvite: PropTypes.bool,
|
|
|
|
|
|
|
|
startAsHidden: PropTypes.bool,
|
|
|
|
showSpinner: PropTypes.bool, // true to show a spinner if 0 elements when expanded
|
|
|
|
collapsed: PropTypes.bool.isRequired, // is LeftPanel collapsed?
|
|
|
|
onHeaderClick: PropTypes.func,
|
|
|
|
incomingCall: PropTypes.object,
|
2018-11-02 14:27:17 +00:00
|
|
|
isFiltered: PropTypes.bool,
|
2018-06-30 16:06:33 +00:00
|
|
|
headerItems: PropTypes.node, // content shown in the sublist header
|
|
|
|
extraTiles: PropTypes.arrayOf(PropTypes.node), // extra elements added beneath tiles
|
2018-04-11 22:58:04 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
getInitialState: function() {
|
|
|
|
return {
|
|
|
|
hidden: this.props.startAsHidden || false,
|
2019-02-13 13:51:09 +00:00
|
|
|
// some values to get LazyRenderList starting
|
|
|
|
scrollerHeight: 800,
|
|
|
|
scrollTop: 0,
|
2018-04-11 22:58:04 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
getDefaultProps: function() {
|
|
|
|
return {
|
2018-06-25 08:51:23 +00:00
|
|
|
onHeaderClick: function() {
|
|
|
|
}, // NOP
|
2018-04-11 22:58:04 +00:00
|
|
|
extraTiles: [],
|
|
|
|
isInvite: false,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
componentWillMount: function() {
|
|
|
|
this.dispatcherRef = dis.register(this.onAction);
|
|
|
|
},
|
|
|
|
|
|
|
|
componentWillUnmount: function() {
|
|
|
|
dis.unregister(this.dispatcherRef);
|
|
|
|
},
|
|
|
|
|
|
|
|
// The header is collapsable if it is hidden or not stuck
|
|
|
|
// The dataset elements are added in the RoomList _initAndPositionStickyHeaders method
|
|
|
|
isCollapsableOnClick: function() {
|
2018-06-25 08:51:23 +00:00
|
|
|
const stuck = this.refs.header.dataset.stuck;
|
2019-01-28 17:28:04 +00:00
|
|
|
if (!this.props.forceExpand && (this.state.hidden || stuck === undefined || stuck === "none")) {
|
2018-04-11 22:58:04 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
onAction: function(payload) {
|
|
|
|
// XXX: Previously RoomList would forceUpdate whenever on_room_read is dispatched,
|
|
|
|
// but this is no longer true, so we must do it here (and can apply the small
|
|
|
|
// optimisation of checking that we care about the room being read).
|
|
|
|
//
|
|
|
|
// Ultimately we need to transition to a state pushing flow where something
|
|
|
|
// explicitly notifies the components concerned that the notif count for a room
|
|
|
|
// has change (e.g. a Flux store).
|
|
|
|
if (payload.action === 'on_room_read' &&
|
|
|
|
this.props.list.some((r) => r.roomId === payload.roomId)
|
|
|
|
) {
|
|
|
|
this.forceUpdate();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
onClick: function(ev) {
|
|
|
|
if (this.isCollapsableOnClick()) {
|
|
|
|
// The header isCollapsable, so the click is to be interpreted as collapse and truncation logic
|
2018-06-25 08:51:23 +00:00
|
|
|
const isHidden = !this.state.hidden;
|
2018-12-18 09:56:00 +00:00
|
|
|
this.setState({hidden: isHidden}, () => {
|
|
|
|
this.props.onHeaderClick(isHidden);
|
|
|
|
});
|
2018-04-11 22:58:04 +00:00
|
|
|
} else {
|
|
|
|
// The header is stuck, so the click is to be interpreted as a scroll to the header
|
|
|
|
this.props.onHeaderClick(this.state.hidden, this.refs.header.dataset.originalPosition);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
onRoomTileClick(roomId, ev) {
|
|
|
|
dis.dispatch({
|
|
|
|
action: 'view_room',
|
|
|
|
room_id: roomId,
|
2018-06-25 08:51:23 +00:00
|
|
|
clear_search: (ev && (ev.keyCode === KeyCode.ENTER || ev.keyCode === KeyCode.SPACE)),
|
2018-04-11 22:58:04 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
_updateSubListCount: function() {
|
|
|
|
// Force an update by setting the state to the current state
|
|
|
|
// Doing it this way rather than using forceUpdate(), so that the shouldComponentUpdate()
|
|
|
|
// method is honoured
|
|
|
|
this.setState(this.state);
|
|
|
|
},
|
|
|
|
|
2019-02-13 13:51:09 +00:00
|
|
|
makeRoomTile: function(room) {
|
|
|
|
return <RoomTile
|
|
|
|
room={room}
|
|
|
|
roomSubList={this}
|
|
|
|
tagName={this.props.tagName}
|
|
|
|
key={room.roomId}
|
|
|
|
collapsed={this.props.collapsed || false}
|
|
|
|
unread={Unread.doesRoomHaveUnreadMessages(room)}
|
|
|
|
highlight={room.getUnreadNotificationCount('highlight') > 0 || this.props.isInvite}
|
|
|
|
notificationCount={room.getUnreadNotificationCount()}
|
|
|
|
isInvite={this.props.isInvite}
|
|
|
|
refreshSubList={this._updateSubListCount}
|
|
|
|
incomingCall={null}
|
|
|
|
onClick={this.onRoomTileClick}
|
|
|
|
/>;
|
2018-04-11 22:58:04 +00:00
|
|
|
},
|
|
|
|
|
2018-06-27 08:16:37 +00:00
|
|
|
_onNotifBadgeClick: function(e) {
|
|
|
|
// prevent the roomsublist collapsing
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2019-02-07 13:40:29 +00:00
|
|
|
const room = this.props.list.find(room => RoomNotifs.getRoomHasBadge(room));
|
2019-02-06 10:51:29 +00:00
|
|
|
if (room) {
|
|
|
|
dis.dispatch({
|
|
|
|
action: 'view_room',
|
|
|
|
room_id: room.roomId,
|
|
|
|
});
|
2018-06-27 08:16:37 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-06-27 08:49:58 +00:00
|
|
|
_onInviteBadgeClick: function(e) {
|
|
|
|
// prevent the roomsublist collapsing
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
// switch to first room in sortedList as that'll be the top of the list for the user
|
2018-11-02 14:27:17 +00:00
|
|
|
if (this.props.list && this.props.list.length > 0) {
|
2018-06-27 08:49:58 +00:00
|
|
|
dis.dispatch({
|
|
|
|
action: 'view_room',
|
2018-11-02 14:27:17 +00:00
|
|
|
room_id: this.props.list[0].roomId,
|
2018-06-27 08:49:58 +00:00
|
|
|
});
|
|
|
|
} else if (this.props.extraTiles && this.props.extraTiles.length > 0) {
|
|
|
|
// Group Invites are different in that they are all extra tiles and not rooms
|
|
|
|
// XXX: this is a horrible special case because Group Invite sublist is a hack
|
|
|
|
if (this.props.extraTiles[0].props && this.props.extraTiles[0].props.group instanceof Group) {
|
|
|
|
dis.dispatch({
|
|
|
|
action: 'view_group',
|
|
|
|
group_id: this.props.extraTiles[0].props.group.groupId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-01-28 17:28:04 +00:00
|
|
|
_getHeaderJsx: function(isCollapsed) {
|
2018-10-19 10:07:36 +00:00
|
|
|
const AccessibleButton = sdk.getComponent('elements.AccessibleButton');
|
2019-02-06 10:51:29 +00:00
|
|
|
const subListNotifications = !this.props.isInvite ?
|
|
|
|
RoomNotifs.aggregateNotificationCount(this.props.list) :
|
|
|
|
{count: 0, highlight: true};
|
|
|
|
const subListNotifCount = subListNotifications.count;
|
|
|
|
const subListNotifHighlight = subListNotifications.highlight;
|
2018-04-11 22:58:04 +00:00
|
|
|
|
2018-06-25 08:51:23 +00:00
|
|
|
let badge;
|
2018-11-05 13:25:07 +00:00
|
|
|
if (!this.props.collapsed) {
|
2018-10-19 12:50:35 +00:00
|
|
|
const badgeClasses = classNames({
|
|
|
|
'mx_RoomSubList_badge': true,
|
|
|
|
'mx_RoomSubList_badgeHighlight': subListNotifHighlight,
|
|
|
|
});
|
|
|
|
if (subListNotifCount > 0) {
|
|
|
|
badge = <div className={badgeClasses} onClick={this._onNotifBadgeClick}>
|
|
|
|
{ FormattingUtils.formatCount(subListNotifCount) }
|
|
|
|
</div>;
|
2019-01-29 15:20:25 +00:00
|
|
|
} else if (this.props.isInvite && this.props.list.length) {
|
2018-10-19 12:50:35 +00:00
|
|
|
// no notifications but highlight anyway because this is an invite badge
|
2019-01-29 15:20:25 +00:00
|
|
|
badge = <div className={badgeClasses} onClick={this._onInviteBadgeClick}>{this.props.list.length}</div>;
|
2018-10-19 12:50:35 +00:00
|
|
|
}
|
2018-04-11 22:58:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// When collapsed, allow a long hover on the header to show user
|
|
|
|
// the full tag name and room count
|
2018-06-25 08:51:23 +00:00
|
|
|
let title;
|
2018-04-11 22:58:04 +00:00
|
|
|
if (this.props.collapsed) {
|
|
|
|
title = this.props.label;
|
|
|
|
}
|
|
|
|
|
2018-06-25 08:51:23 +00:00
|
|
|
let incomingCall;
|
2018-04-11 22:58:04 +00:00
|
|
|
if (this.props.incomingCall) {
|
2018-12-06 18:45:58 +00:00
|
|
|
// We can assume that if we have an incoming call then it is for this list
|
|
|
|
const IncomingCallBox = sdk.getComponent("voip.IncomingCallBox");
|
|
|
|
incomingCall =
|
|
|
|
<IncomingCallBox className="mx_RoomSubList_incomingCall" incomingCall={this.props.incomingCall} />;
|
2018-04-11 22:58:04 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 10:07:36 +00:00
|
|
|
let addRoomButton;
|
|
|
|
if (this.props.onAddRoom) {
|
2018-10-19 10:34:55 +00:00
|
|
|
addRoomButton = (
|
2018-11-05 13:25:28 +00:00
|
|
|
<AccessibleButton onClick={ this.props.onAddRoom } className="mx_RoomSubList_addRoom" />
|
2018-10-19 10:34:55 +00:00
|
|
|
);
|
2018-10-19 10:07:36 +00:00
|
|
|
}
|
2018-04-11 22:58:04 +00:00
|
|
|
|
2018-11-02 14:27:17 +00:00
|
|
|
const len = this.props.list.length + this.props.extraTiles.length;
|
2018-10-19 12:33:51 +00:00
|
|
|
let chevron;
|
|
|
|
if (len) {
|
|
|
|
const chevronClasses = classNames({
|
|
|
|
'mx_RoomSubList_chevron': true,
|
2019-01-28 17:28:04 +00:00
|
|
|
'mx_RoomSubList_chevronRight': isCollapsed,
|
|
|
|
'mx_RoomSubList_chevronDown': !isCollapsed,
|
2018-10-19 12:33:51 +00:00
|
|
|
});
|
|
|
|
chevron = (<div className={chevronClasses}></div>);
|
|
|
|
}
|
2018-04-11 22:58:04 +00:00
|
|
|
|
2018-11-02 14:27:17 +00:00
|
|
|
const tabindex = this.props.isFiltered ? "0" : "-1";
|
2018-04-11 22:58:04 +00:00
|
|
|
return (
|
|
|
|
<div className="mx_RoomSubList_labelContainer" title={ title } ref="header">
|
|
|
|
<AccessibleButton onClick={ this.onClick } className="mx_RoomSubList_label" tabIndex={tabindex}>
|
2018-10-19 12:33:51 +00:00
|
|
|
{ chevron }
|
2018-11-06 13:03:01 +00:00
|
|
|
<span>{this.props.label}</span>
|
2018-04-11 22:58:04 +00:00
|
|
|
{ incomingCall }
|
|
|
|
</AccessibleButton>
|
2018-10-22 16:16:36 +00:00
|
|
|
{ badge }
|
2018-10-19 10:07:36 +00:00
|
|
|
{ addRoomButton }
|
2018-04-11 22:58:04 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2018-12-14 16:06:51 +00:00
|
|
|
checkOverflow: function() {
|
|
|
|
if (this.refs.scroller) {
|
|
|
|
this.refs.scroller.checkOverflow();
|
2018-04-11 22:58:04 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-01-24 14:43:49 +00:00
|
|
|
setHeight: function(height) {
|
|
|
|
if (this.refs.subList) {
|
|
|
|
this.refs.subList.style.height = `${height}px`;
|
|
|
|
}
|
2019-02-13 13:51:09 +00:00
|
|
|
this._updateLazyRenderHeight(height);
|
|
|
|
},
|
|
|
|
|
|
|
|
_updateLazyRenderHeight: function(height) {
|
|
|
|
this.setState({scrollerHeight: height});
|
|
|
|
},
|
|
|
|
|
|
|
|
_onScroll: function() {
|
|
|
|
this.setState({scrollTop: this.refs.scroller.getScrollTop()});
|
2019-01-24 14:43:49 +00:00
|
|
|
},
|
|
|
|
|
2019-02-13 17:36:46 +00:00
|
|
|
_getRenderItems: function() {
|
|
|
|
// try our best to not create a new array
|
|
|
|
// because LazyRenderList rerender when the items prop
|
|
|
|
// is not the same object as the previous value
|
|
|
|
const {list, extraTiles} = this.props;
|
|
|
|
if (!extraTiles || !extraTiles.length) {
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
if (!list || list.length) {
|
|
|
|
return extraTiles;
|
|
|
|
}
|
|
|
|
return list.concat(extraTiles);
|
|
|
|
},
|
|
|
|
|
2018-04-11 22:58:04 +00:00
|
|
|
render: function() {
|
2018-11-02 14:27:17 +00:00
|
|
|
const len = this.props.list.length + this.props.extraTiles.length;
|
2019-01-28 17:28:04 +00:00
|
|
|
const isCollapsed = this.state.hidden && !this.props.forceExpand;
|
2018-10-18 14:25:22 +00:00
|
|
|
if (len) {
|
2018-10-19 12:40:59 +00:00
|
|
|
const subListClasses = classNames({
|
|
|
|
"mx_RoomSubList": true,
|
2019-01-28 17:28:04 +00:00
|
|
|
"mx_RoomSubList_hidden": isCollapsed,
|
|
|
|
"mx_RoomSubList_nonEmpty": len && !isCollapsed,
|
2018-10-19 12:40:59 +00:00
|
|
|
});
|
2019-01-28 17:28:04 +00:00
|
|
|
|
|
|
|
if (isCollapsed) {
|
2019-01-24 14:43:49 +00:00
|
|
|
return <div ref="subList" className={subListClasses}>
|
2019-01-28 17:28:04 +00:00
|
|
|
{this._getHeaderJsx(isCollapsed)}
|
2018-10-18 14:25:22 +00:00
|
|
|
</div>;
|
2018-06-25 08:51:23 +00:00
|
|
|
} else {
|
2019-01-24 14:43:49 +00:00
|
|
|
return <div ref="subList" className={subListClasses}>
|
2019-01-28 17:28:04 +00:00
|
|
|
{this._getHeaderJsx(isCollapsed)}
|
2019-02-13 13:51:09 +00:00
|
|
|
<IndicatorScrollbar ref="scroller" className="mx_RoomSubList_scroll" onScroll={ this._onScroll }>
|
|
|
|
<LazyRenderList
|
|
|
|
scrollTop={this.state.scrollTop }
|
|
|
|
height={ this.state.scrollerHeight }
|
|
|
|
renderItem={ this.makeRoomTile }
|
|
|
|
itemHeight={34}
|
2019-02-13 17:36:46 +00:00
|
|
|
items={this._getRenderItems()} />
|
2018-11-12 11:57:21 +00:00
|
|
|
</IndicatorScrollbar>
|
2018-10-18 14:25:22 +00:00
|
|
|
</div>;
|
2018-04-11 22:58:04 +00:00
|
|
|
}
|
2018-06-25 08:51:23 +00:00
|
|
|
} else {
|
|
|
|
const Loader = sdk.getComponent("elements.Spinner");
|
2018-10-19 12:40:59 +00:00
|
|
|
let content;
|
2019-01-28 17:28:04 +00:00
|
|
|
if (this.props.showSpinner && !isCollapsed) {
|
2018-06-26 06:46:33 +00:00
|
|
|
content = <Loader />;
|
2018-06-26 06:54:38 +00:00
|
|
|
}
|
2018-06-26 06:46:33 +00:00
|
|
|
|
2018-04-11 22:58:04 +00:00
|
|
|
return (
|
2019-01-24 14:43:49 +00:00
|
|
|
<div ref="subList" className="mx_RoomSubList">
|
2019-01-28 17:28:04 +00:00
|
|
|
{ this._getHeaderJsx(isCollapsed) }
|
2018-10-19 12:40:59 +00:00
|
|
|
{ content }
|
2018-04-11 22:58:04 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2018-06-25 08:51:23 +00:00
|
|
|
},
|
2018-04-11 22:58:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = RoomSubList;
|