2018-04-12 01:58:04 +03:00
|
|
|
/*
|
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2018-06-25 11:41:28 +03:00
|
|
|
Copyright 2017 Vector Creations Ltd
|
|
|
|
Copyright 2018 New Vector Ltd
|
2018-04-12 01:58:04 +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.
|
|
|
|
*/
|
|
|
|
|
2018-06-25 11:51:23 +03: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 14:57:21 +03:00
|
|
|
import IndicatorScrollbar from './IndicatorScrollbar';
|
2018-04-13 02:43:44 +03:00
|
|
|
import { KeyCode } from '../../Keyboard';
|
2018-06-27 11:49:58 +03:00
|
|
|
import { Group } from 'matrix-js-sdk';
|
2018-06-30 19:06:33 +03:00
|
|
|
import PropTypes from 'prop-types';
|
2019-02-13 16:51:09 +03:00
|
|
|
import RoomTile from "../views/rooms/RoomTile";
|
|
|
|
import LazyRenderList from "../views/elements/LazyRenderList";
|
|
|
|
import * as _ from "lodash";
|
2018-04-12 01:58:04 +03:00
|
|
|
|
|
|
|
// turn this on for drop & drag console debugging galore
|
2018-06-25 11:51:23 +03:00
|
|
|
const debug = false;
|
2018-04-12 01:58:04 +03:00
|
|
|
|
2018-06-25 11:51:23 +03:00
|
|
|
const RoomSubList = React.createClass({
|
2018-04-12 01:58:04 +03:00
|
|
|
displayName: 'RoomSubList',
|
|
|
|
|
|
|
|
debug: debug,
|
|
|
|
|
|
|
|
propTypes: {
|
2018-06-30 19:06:33 +03:00
|
|
|
list: PropTypes.arrayOf(PropTypes.object).isRequired,
|
|
|
|
label: PropTypes.string.isRequired,
|
|
|
|
tagName: PropTypes.string,
|
2018-04-12 01:58:04 +03:00
|
|
|
|
2018-06-30 19:06:33 +03:00
|
|
|
order: PropTypes.string.isRequired,
|
2018-04-12 01:58:04 +03:00
|
|
|
|
|
|
|
// passed through to RoomTile and used to highlight room with `!` regardless of notifications count
|
2018-06-30 19:06:33 +03: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 17:27:17 +03:00
|
|
|
isFiltered: PropTypes.bool,
|
2018-06-30 19:06:33 +03:00
|
|
|
headerItems: PropTypes.node, // content shown in the sublist header
|
|
|
|
extraTiles: PropTypes.arrayOf(PropTypes.node), // extra elements added beneath tiles
|
2018-04-12 01:58:04 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
getInitialState: function() {
|
2019-02-13 16:51:09 +03:00
|
|
|
// throttle updates to LazyRenderList
|
|
|
|
this._onScroll = _.throttle(
|
|
|
|
this._onScroll, 100,
|
|
|
|
{leading: false, trailing: true},
|
|
|
|
);
|
|
|
|
this._updateLazyRenderHeight = _.throttle(
|
|
|
|
this._updateLazyRenderHeight, 100,
|
|
|
|
{leading: false, trailing: true},
|
|
|
|
);
|
2018-04-12 01:58:04 +03:00
|
|
|
return {
|
|
|
|
hidden: this.props.startAsHidden || false,
|
2019-02-13 16:51:09 +03:00
|
|
|
// some values to get LazyRenderList starting
|
|
|
|
scrollerHeight: 800,
|
|
|
|
scrollTop: 0,
|
2018-04-12 01:58:04 +03:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
getDefaultProps: function() {
|
|
|
|
return {
|
2018-06-25 11:51:23 +03:00
|
|
|
onHeaderClick: function() {
|
|
|
|
}, // NOP
|
2018-04-12 01:58:04 +03: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 11:51:23 +03:00
|
|
|
const stuck = this.refs.header.dataset.stuck;
|
2019-01-28 20:28:04 +03:00
|
|
|
if (!this.props.forceExpand && (this.state.hidden || stuck === undefined || stuck === "none")) {
|
2018-04-12 01:58:04 +03: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 11:51:23 +03:00
|
|
|
const isHidden = !this.state.hidden;
|
2018-12-18 12:56:00 +03:00
|
|
|
this.setState({hidden: isHidden}, () => {
|
|
|
|
this.props.onHeaderClick(isHidden);
|
|
|
|
});
|
2018-04-12 01:58:04 +03: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 11:51:23 +03:00
|
|
|
clear_search: (ev && (ev.keyCode === KeyCode.ENTER || ev.keyCode === KeyCode.SPACE)),
|
2018-04-12 01:58:04 +03: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 16:51:09 +03: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-12 01:58:04 +03:00
|
|
|
},
|
|
|
|
|
2018-06-27 11:16:37 +03:00
|
|
|
_onNotifBadgeClick: function(e) {
|
|
|
|
// prevent the roomsublist collapsing
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2019-02-07 16:40:29 +03:00
|
|
|
const room = this.props.list.find(room => RoomNotifs.getRoomHasBadge(room));
|
2019-02-06 13:51:29 +03:00
|
|
|
if (room) {
|
|
|
|
dis.dispatch({
|
|
|
|
action: 'view_room',
|
|
|
|
room_id: room.roomId,
|
|
|
|
});
|
2018-06-27 11:16:37 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-06-27 11:49:58 +03: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 17:27:17 +03:00
|
|
|
if (this.props.list && this.props.list.length > 0) {
|
2018-06-27 11:49:58 +03:00
|
|
|
dis.dispatch({
|
|
|
|
action: 'view_room',
|
2018-11-02 17:27:17 +03:00
|
|
|
room_id: this.props.list[0].roomId,
|
2018-06-27 11:49:58 +03: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 20:28:04 +03:00
|
|
|
_getHeaderJsx: function(isCollapsed) {
|
2018-10-19 13:07:36 +03:00
|
|
|
const AccessibleButton = sdk.getComponent('elements.AccessibleButton');
|
2019-02-06 13:51:29 +03: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-12 01:58:04 +03:00
|
|
|
|
2018-06-25 11:51:23 +03:00
|
|
|
let badge;
|
2018-11-05 16:25:07 +03:00
|
|
|
if (!this.props.collapsed) {
|
2018-10-19 15:50:35 +03: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 18:20:25 +03:00
|
|
|
} else if (this.props.isInvite && this.props.list.length) {
|
2018-10-19 15:50:35 +03:00
|
|
|
// no notifications but highlight anyway because this is an invite badge
|
2019-01-29 18:20:25 +03:00
|
|
|
badge = <div className={badgeClasses} onClick={this._onInviteBadgeClick}>{this.props.list.length}</div>;
|
2018-10-19 15:50:35 +03:00
|
|
|
}
|
2018-04-12 01:58:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// When collapsed, allow a long hover on the header to show user
|
|
|
|
// the full tag name and room count
|
2018-06-25 11:51:23 +03:00
|
|
|
let title;
|
2018-04-12 01:58:04 +03:00
|
|
|
if (this.props.collapsed) {
|
|
|
|
title = this.props.label;
|
|
|
|
}
|
|
|
|
|
2018-06-25 11:51:23 +03:00
|
|
|
let incomingCall;
|
2018-04-12 01:58:04 +03:00
|
|
|
if (this.props.incomingCall) {
|
2018-12-06 21:45:58 +03: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-12 01:58:04 +03:00
|
|
|
}
|
|
|
|
|
2018-10-19 13:07:36 +03:00
|
|
|
let addRoomButton;
|
|
|
|
if (this.props.onAddRoom) {
|
2018-10-19 13:34:55 +03:00
|
|
|
addRoomButton = (
|
2018-11-05 16:25:28 +03:00
|
|
|
<AccessibleButton onClick={ this.props.onAddRoom } className="mx_RoomSubList_addRoom" />
|
2018-10-19 13:34:55 +03:00
|
|
|
);
|
2018-10-19 13:07:36 +03:00
|
|
|
}
|
2018-04-12 01:58:04 +03:00
|
|
|
|
2018-11-02 17:27:17 +03:00
|
|
|
const len = this.props.list.length + this.props.extraTiles.length;
|
2018-10-19 15:33:51 +03:00
|
|
|
let chevron;
|
|
|
|
if (len) {
|
|
|
|
const chevronClasses = classNames({
|
|
|
|
'mx_RoomSubList_chevron': true,
|
2019-01-28 20:28:04 +03:00
|
|
|
'mx_RoomSubList_chevronRight': isCollapsed,
|
|
|
|
'mx_RoomSubList_chevronDown': !isCollapsed,
|
2018-10-19 15:33:51 +03:00
|
|
|
});
|
|
|
|
chevron = (<div className={chevronClasses}></div>);
|
|
|
|
}
|
2018-04-12 01:58:04 +03:00
|
|
|
|
2018-11-02 17:27:17 +03:00
|
|
|
const tabindex = this.props.isFiltered ? "0" : "-1";
|
2018-04-12 01:58:04 +03:00
|
|
|
return (
|
|
|
|
<div className="mx_RoomSubList_labelContainer" title={ title } ref="header">
|
|
|
|
<AccessibleButton onClick={ this.onClick } className="mx_RoomSubList_label" tabIndex={tabindex}>
|
2018-10-19 15:33:51 +03:00
|
|
|
{ chevron }
|
2018-11-06 16:03:01 +03:00
|
|
|
<span>{this.props.label}</span>
|
2018-04-12 01:58:04 +03:00
|
|
|
{ incomingCall }
|
|
|
|
</AccessibleButton>
|
2018-10-22 19:16:36 +03:00
|
|
|
{ badge }
|
2018-10-19 13:07:36 +03:00
|
|
|
{ addRoomButton }
|
2018-04-12 01:58:04 +03:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2018-12-14 19:06:51 +03:00
|
|
|
checkOverflow: function() {
|
|
|
|
if (this.refs.scroller) {
|
|
|
|
this.refs.scroller.checkOverflow();
|
2018-04-12 01:58:04 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-01-24 17:43:49 +03:00
|
|
|
setHeight: function(height) {
|
|
|
|
if (this.refs.subList) {
|
|
|
|
this.refs.subList.style.height = `${height}px`;
|
|
|
|
}
|
2019-02-13 16:51:09 +03:00
|
|
|
this._updateLazyRenderHeight(height);
|
|
|
|
},
|
|
|
|
|
|
|
|
_updateLazyRenderHeight: function(height) {
|
|
|
|
this.setState({scrollerHeight: height});
|
|
|
|
},
|
|
|
|
|
|
|
|
_onScroll: function() {
|
|
|
|
this.setState({scrollTop: this.refs.scroller.getScrollTop()});
|
2019-01-24 17:43:49 +03:00
|
|
|
},
|
|
|
|
|
2018-04-12 01:58:04 +03:00
|
|
|
render: function() {
|
2018-11-02 17:27:17 +03:00
|
|
|
const len = this.props.list.length + this.props.extraTiles.length;
|
2019-01-28 20:28:04 +03:00
|
|
|
const isCollapsed = this.state.hidden && !this.props.forceExpand;
|
2018-10-18 17:25:22 +03:00
|
|
|
if (len) {
|
2018-10-19 15:40:59 +03:00
|
|
|
const subListClasses = classNames({
|
|
|
|
"mx_RoomSubList": true,
|
2019-01-28 20:28:04 +03:00
|
|
|
"mx_RoomSubList_hidden": isCollapsed,
|
|
|
|
"mx_RoomSubList_nonEmpty": len && !isCollapsed,
|
2018-10-19 15:40:59 +03:00
|
|
|
});
|
2019-01-28 20:28:04 +03:00
|
|
|
|
|
|
|
if (isCollapsed) {
|
2019-01-24 17:43:49 +03:00
|
|
|
return <div ref="subList" className={subListClasses}>
|
2019-01-28 20:28:04 +03:00
|
|
|
{this._getHeaderJsx(isCollapsed)}
|
2018-10-18 17:25:22 +03:00
|
|
|
</div>;
|
2018-06-25 11:51:23 +03:00
|
|
|
} else {
|
2019-02-13 16:51:09 +03:00
|
|
|
const items = this.props.list.concat(this.props.extraTiles);
|
2019-01-24 17:43:49 +03:00
|
|
|
return <div ref="subList" className={subListClasses}>
|
2019-01-28 20:28:04 +03:00
|
|
|
{this._getHeaderJsx(isCollapsed)}
|
2019-02-13 16:51:09 +03: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}
|
|
|
|
items={items} />
|
2018-11-12 14:57:21 +03:00
|
|
|
</IndicatorScrollbar>
|
2018-10-18 17:25:22 +03:00
|
|
|
</div>;
|
2018-04-12 01:58:04 +03:00
|
|
|
}
|
2018-06-25 11:51:23 +03:00
|
|
|
} else {
|
|
|
|
const Loader = sdk.getComponent("elements.Spinner");
|
2018-10-19 15:40:59 +03:00
|
|
|
let content;
|
2019-01-28 20:28:04 +03:00
|
|
|
if (this.props.showSpinner && !isCollapsed) {
|
2018-06-26 09:46:33 +03:00
|
|
|
content = <Loader />;
|
2018-06-26 09:54:38 +03:00
|
|
|
}
|
2018-06-26 09:46:33 +03:00
|
|
|
|
2018-04-12 01:58:04 +03:00
|
|
|
return (
|
2019-01-24 17:43:49 +03:00
|
|
|
<div ref="subList" className="mx_RoomSubList">
|
2019-01-28 20:28:04 +03:00
|
|
|
{ this._getHeaderJsx(isCollapsed) }
|
2018-10-19 15:40:59 +03:00
|
|
|
{ content }
|
2018-04-12 01:58:04 +03:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2018-06-25 11:51:23 +03:00
|
|
|
},
|
2018-04-12 01:58:04 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = RoomSubList;
|