2016-02-10 14:03:46 +03:00
|
|
|
/*
|
|
|
|
Copyright 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
var React = require('react');
|
2016-02-24 20:26:34 +03:00
|
|
|
var ReactDOM = require("react-dom");
|
2016-02-10 14:03:46 +03:00
|
|
|
var sdk = require('../../index');
|
|
|
|
|
2016-02-15 12:50:12 +03:00
|
|
|
/* (almost) stateless UI component which builds the event tiles in the room timeline.
|
2016-02-10 14:03:46 +03:00
|
|
|
*/
|
|
|
|
module.exports = React.createClass({
|
|
|
|
displayName: 'MessagePanel',
|
|
|
|
|
|
|
|
propTypes: {
|
2016-02-11 18:38:13 +03:00
|
|
|
// true to give the component a 'display: none' style.
|
2016-02-10 14:03:46 +03:00
|
|
|
hidden: React.PropTypes.bool,
|
|
|
|
|
2016-02-24 16:38:55 +03:00
|
|
|
// true to show a spinner at the top of the timeline to indicate
|
|
|
|
// back-pagination in progress
|
|
|
|
backPaginating: React.PropTypes.bool,
|
|
|
|
|
|
|
|
// true to show a spinner at the end of the timeline to indicate
|
|
|
|
// forward-pagination in progress
|
|
|
|
forwardPaginating: React.PropTypes.bool,
|
|
|
|
|
2016-02-10 14:03:46 +03:00
|
|
|
// the list of MatrixEvents to display
|
|
|
|
events: React.PropTypes.array.isRequired,
|
|
|
|
|
|
|
|
// ID of an event to highlight. If undefined, no event will be highlighted.
|
|
|
|
highlightedEventId: React.PropTypes.string,
|
|
|
|
|
|
|
|
// event after which we should show a read marker
|
|
|
|
readMarkerEventId: React.PropTypes.string,
|
|
|
|
|
2016-02-24 20:26:34 +03:00
|
|
|
// whether the read marker should be visible
|
|
|
|
readMarkerVisible: React.PropTypes.bool,
|
|
|
|
|
2016-02-10 14:03:46 +03:00
|
|
|
// the userid of our user. This is used to suppress the read marker
|
|
|
|
// for pending messages.
|
|
|
|
ourUserId: React.PropTypes.string,
|
|
|
|
|
|
|
|
// true to suppress the date at the start of the timeline
|
|
|
|
suppressFirstDateSeparator: React.PropTypes.bool,
|
|
|
|
|
|
|
|
// true if updates to the event list should cause the scroll panel to
|
|
|
|
// scroll down when we are at the bottom of the window. See ScrollPanel
|
|
|
|
// for more details.
|
|
|
|
stickyBottom: React.PropTypes.bool,
|
|
|
|
|
|
|
|
// callback which is called when the panel is scrolled.
|
|
|
|
onScroll: React.PropTypes.func,
|
|
|
|
|
|
|
|
// callback which is called when more content is needed.
|
|
|
|
onFillRequest: React.PropTypes.func,
|
|
|
|
},
|
|
|
|
|
2016-02-15 12:50:12 +03:00
|
|
|
componentWillMount: function() {
|
|
|
|
// the event after which we put a visible unread marker on the last
|
|
|
|
// render cycle; null if readMarkerVisible was false or the RM was
|
|
|
|
// suppressed (eg because it was at the end of the timeline)
|
|
|
|
this.currentReadMarkerEventId = null;
|
|
|
|
|
|
|
|
// the event after which we are showing a disappearing read marker
|
|
|
|
// animation
|
|
|
|
this.currentGhostEventId = null;
|
|
|
|
},
|
|
|
|
|
2016-02-10 14:03:46 +03:00
|
|
|
/* get the DOM node representing the given event */
|
|
|
|
getNodeForEventId: function(eventId) {
|
|
|
|
if (!this.eventNodes) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.eventNodes[eventId];
|
|
|
|
},
|
|
|
|
|
|
|
|
/* return true if the content is fully scrolled down right now; else false.
|
|
|
|
*/
|
|
|
|
isAtBottom: function() {
|
2016-02-12 16:39:38 +03:00
|
|
|
return this.refs.scrollPanel
|
2016-02-10 14:03:46 +03:00
|
|
|
&& this.refs.scrollPanel.isAtBottom();
|
|
|
|
},
|
|
|
|
|
|
|
|
/* get the current scroll state. See ScrollPanel.getScrollState for
|
|
|
|
* details.
|
|
|
|
*
|
|
|
|
* returns null if we are not mounted.
|
|
|
|
*/
|
|
|
|
getScrollState: function() {
|
|
|
|
if (!this.refs.scrollPanel) { return null; }
|
|
|
|
return this.refs.scrollPanel.getScrollState();
|
|
|
|
},
|
|
|
|
|
2016-02-24 20:26:34 +03:00
|
|
|
// returns one of:
|
|
|
|
//
|
|
|
|
// null: there is no read marker
|
|
|
|
// -1: read marker is above the window
|
|
|
|
// 0: read marker is within the window
|
|
|
|
// +1: read marker is below the window
|
|
|
|
getReadMarkerPosition: function() {
|
|
|
|
var readMarker = this.refs.readMarkerNode;
|
|
|
|
var messageWrapper = this.refs.scrollPanel;
|
|
|
|
|
|
|
|
if (!readMarker || !messageWrapper) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
var wrapperRect = ReactDOM.findDOMNode(messageWrapper).getBoundingClientRect();
|
|
|
|
var readMarkerRect = readMarker.getBoundingClientRect();
|
|
|
|
|
|
|
|
// the read-marker pretends to have zero height when it is actually
|
|
|
|
// two pixels high; +2 here to account for that.
|
|
|
|
if (readMarkerRect.bottom + 2 < wrapperRect.top) {
|
|
|
|
return -1;
|
|
|
|
} else if (readMarkerRect.top < wrapperRect.bottom) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-02-10 14:03:46 +03:00
|
|
|
/* jump to the bottom of the content.
|
|
|
|
*/
|
|
|
|
scrollToBottom: function() {
|
|
|
|
if (this.refs.scrollPanel) {
|
|
|
|
this.refs.scrollPanel.scrollToBottom();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/* jump to the given event id.
|
|
|
|
*
|
|
|
|
* pixelOffset gives the number of pixels between the bottom of the node
|
|
|
|
* and the bottom of the container. If undefined, it will put the node
|
2016-02-24 20:26:34 +03:00
|
|
|
* 1/3 of the way down of the container.
|
2016-02-10 14:03:46 +03:00
|
|
|
*/
|
|
|
|
scrollToEvent: function(eventId, pixelOffset) {
|
|
|
|
if (this.refs.scrollPanel) {
|
|
|
|
this.refs.scrollPanel.scrollToToken(eventId, pixelOffset);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/* check the scroll state and send out pagination requests if necessary.
|
|
|
|
*/
|
|
|
|
checkFillState: function() {
|
|
|
|
if (this.refs.scrollPanel) {
|
|
|
|
this.refs.scrollPanel.checkFillState();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_getEventTiles: function() {
|
|
|
|
var EventTile = sdk.getComponent('rooms.EventTile');
|
|
|
|
|
2016-02-12 16:39:38 +03:00
|
|
|
this.eventNodes = {};
|
|
|
|
|
2016-02-23 15:56:54 +03:00
|
|
|
var i;
|
2016-02-10 14:03:46 +03:00
|
|
|
|
2016-02-23 15:56:54 +03:00
|
|
|
// first figure out which is the last event in the list which we're
|
|
|
|
// actually going to show; this allows us to behave slightly
|
|
|
|
// differently for the last event in the list.
|
|
|
|
for (i = this.props.events.length-1; i >= 0; i--) {
|
|
|
|
var mxEv = this.props.events[i];
|
2016-02-10 14:03:46 +03:00
|
|
|
if (!EventTile.haveTileForEvent(mxEv)) {
|
2016-02-23 15:56:54 +03:00
|
|
|
continue;
|
2016-02-10 14:03:46 +03:00
|
|
|
}
|
|
|
|
|
2016-02-23 15:56:54 +03:00
|
|
|
break;
|
2016-02-12 16:39:38 +03:00
|
|
|
}
|
2016-02-23 15:56:54 +03:00
|
|
|
var lastShownEventIndex = i;
|
2016-02-10 14:03:46 +03:00
|
|
|
|
2016-02-12 16:39:38 +03:00
|
|
|
var ret = [];
|
|
|
|
|
|
|
|
var prevEvent = null; // the last event we showed
|
|
|
|
|
2016-02-15 12:50:12 +03:00
|
|
|
// assume there is no read marker until proven otherwise
|
|
|
|
var readMarkerVisible = false;
|
|
|
|
|
2016-02-23 15:56:54 +03:00
|
|
|
for (i = 0; i < this.props.events.length; i++) {
|
|
|
|
var mxEv = this.props.events[i];
|
2016-02-12 16:39:38 +03:00
|
|
|
var wantTile = true;
|
2016-02-23 15:56:54 +03:00
|
|
|
var eventId = mxEv.getId();
|
|
|
|
|
|
|
|
if (!EventTile.haveTileForEvent(mxEv)) {
|
|
|
|
wantTile = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
var last = (i == lastShownEventIndex);
|
|
|
|
|
|
|
|
if (wantTile) {
|
|
|
|
ret.push(this._getTilesForEvent(prevEvent, mxEv, last));
|
2016-02-23 21:43:51 +03:00
|
|
|
prevEvent = mxEv;
|
2016-02-23 15:56:54 +03:00
|
|
|
} else if (!mxEv.status) {
|
|
|
|
// if we aren't showing the event, put in a dummy scroll token anyway, so
|
|
|
|
// that we can scroll to the right place.
|
|
|
|
ret.push(<li key={eventId} data-scroll-token={eventId}/>);
|
|
|
|
}
|
2016-02-12 16:39:38 +03:00
|
|
|
|
2016-02-24 20:26:34 +03:00
|
|
|
if (eventId == this.props.readMarkerEventId) {
|
|
|
|
var visible = this.props.readMarkerVisible;
|
|
|
|
|
|
|
|
// if the read marker comes at the end of the timeline, we don't want
|
|
|
|
// to show it, but we still want to create the <li/> for it so that the
|
|
|
|
// algorithms which depend on its position on the screen aren't confused.
|
|
|
|
if (i >= lastShownEventIndex) {
|
|
|
|
visible = false;
|
2016-02-12 16:39:38 +03:00
|
|
|
}
|
2016-02-24 20:26:34 +03:00
|
|
|
ret.push(this._getReadMarkerTile(visible));
|
|
|
|
readMarkerVisible = visible;
|
2016-02-23 15:56:54 +03:00
|
|
|
} else if (eventId == this.currentReadMarkerEventId && !this.currentGhostEventId) {
|
|
|
|
// there is currently a read-up-to marker at this point, but no
|
|
|
|
// more. Show an animation of it disappearing.
|
|
|
|
ret.push(this._getReadMarkerGhostTile());
|
|
|
|
this.currentGhostEventId = eventId;
|
|
|
|
} else if (eventId == this.currentGhostEventId) {
|
|
|
|
// if we're showing an animation, continue to show it.
|
2016-02-12 16:39:38 +03:00
|
|
|
ret.push(this._getReadMarkerGhostTile());
|
2016-02-10 14:03:46 +03:00
|
|
|
}
|
2016-02-12 16:39:38 +03:00
|
|
|
}
|
|
|
|
|
2016-02-15 12:50:12 +03:00
|
|
|
this.currentReadMarkerEventId = readMarkerVisible ? this.props.readMarkerEventId : null;
|
2016-02-12 16:39:38 +03:00
|
|
|
return ret;
|
|
|
|
},
|
|
|
|
|
|
|
|
_getTilesForEvent: function(prevEvent, mxEv, last) {
|
|
|
|
var EventTile = sdk.getComponent('rooms.EventTile');
|
|
|
|
var DateSeparator = sdk.getComponent('messages.DateSeparator');
|
|
|
|
var ret = [];
|
2016-02-10 14:03:46 +03:00
|
|
|
|
2016-02-12 16:39:38 +03:00
|
|
|
// is this a continuation of the previous message?
|
|
|
|
var continuation = false;
|
|
|
|
if (prevEvent !== null && prevEvent.sender && mxEv.sender
|
|
|
|
&& mxEv.sender.userId === prevEvent.sender.userId
|
|
|
|
&& mxEv.getType() == prevEvent.getType()) {
|
|
|
|
continuation = true;
|
|
|
|
}
|
2016-02-10 14:03:46 +03:00
|
|
|
|
2016-02-12 16:39:38 +03:00
|
|
|
// do we need a date separator since the last event?
|
|
|
|
var ts1 = mxEv.getTs();
|
|
|
|
if ((prevEvent == null && !this.props.suppressFirstDateSeparator) ||
|
|
|
|
(prevEvent != null &&
|
|
|
|
new Date(prevEvent.getTs()).toDateString()
|
|
|
|
!== new Date(ts1).toDateString())) {
|
|
|
|
var dateSeparator = <li key={ts1}><DateSeparator key={ts1} ts={ts1}/></li>;
|
|
|
|
ret.push(dateSeparator);
|
|
|
|
continuation = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
var eventId = mxEv.getId();
|
|
|
|
var highlight = (eventId == this.props.highlightedEventId);
|
|
|
|
|
|
|
|
// we can't use local echoes as scroll tokens, because their event IDs change.
|
|
|
|
// Local echos have a send "status".
|
|
|
|
var scrollToken = mxEv.status ? undefined : eventId;
|
|
|
|
|
|
|
|
ret.push(
|
|
|
|
<li key={eventId}
|
|
|
|
ref={this._collectEventNode.bind(this, eventId)}
|
2016-02-10 14:03:46 +03:00
|
|
|
data-scroll-token={scrollToken}>
|
|
|
|
<EventTile mxEvent={mxEv} continuation={continuation}
|
2016-02-23 16:32:23 +03:00
|
|
|
last={last} isSelectedEvent={highlight}
|
|
|
|
onImageLoad={this._onImageLoad} />
|
2016-02-10 14:03:46 +03:00
|
|
|
</li>
|
2016-02-12 16:39:38 +03:00
|
|
|
);
|
2016-02-10 14:03:46 +03:00
|
|
|
|
2016-02-12 16:39:38 +03:00
|
|
|
return ret;
|
|
|
|
},
|
2016-02-10 14:03:46 +03:00
|
|
|
|
2016-02-24 20:26:34 +03:00
|
|
|
_getReadMarkerTile: function(visible) {
|
2016-02-12 16:39:38 +03:00
|
|
|
var hr;
|
2016-02-24 20:26:34 +03:00
|
|
|
if (visible) {
|
|
|
|
hr = <hr className="mx_RoomView_myReadMarker"
|
|
|
|
style={{opacity: 1, width: '99%'}}
|
|
|
|
/>;
|
|
|
|
}
|
2016-02-12 16:39:38 +03:00
|
|
|
|
|
|
|
return (
|
2016-02-24 20:26:34 +03:00
|
|
|
<li key="_readupto" ref="readMarkerNode"
|
2016-02-12 16:39:38 +03:00
|
|
|
className="mx_RoomView_myReadMarker_container">
|
|
|
|
{hr}
|
|
|
|
</li>
|
|
|
|
);
|
|
|
|
},
|
2016-02-10 14:03:46 +03:00
|
|
|
|
2016-02-12 16:39:38 +03:00
|
|
|
_getReadMarkerGhostTile: function() {
|
2016-02-15 12:50:12 +03:00
|
|
|
// reset the ghostEventId when the animation finishes, so that
|
|
|
|
// we can make a new one (and so that we don't run the
|
|
|
|
// animation code every time we render)
|
|
|
|
var completeFunc = () => {
|
|
|
|
this.currentGhostEventId = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
var hr = <hr className="mx_RoomView_myReadMarker"
|
2016-02-12 16:39:38 +03:00
|
|
|
style={{opacity: 1, width: '99%'}}
|
2016-02-11 18:38:13 +03:00
|
|
|
ref={function(n) {
|
|
|
|
Velocity(n, {opacity: '0', width: '10%'},
|
|
|
|
{duration: 400, easing: 'easeInSine',
|
2016-02-15 12:50:12 +03:00
|
|
|
delay: 1000, complete: completeFunc});
|
2016-02-11 18:38:13 +03:00
|
|
|
}}
|
2016-02-12 16:39:38 +03:00
|
|
|
/>;
|
2016-02-15 12:50:12 +03:00
|
|
|
|
|
|
|
// give it a key which depends on the event id. That will ensure that
|
|
|
|
// we get a new DOM node (restarting the animation) when the ghost
|
|
|
|
// moves to a different event.
|
2016-02-12 16:39:38 +03:00
|
|
|
return (
|
2016-02-15 12:50:12 +03:00
|
|
|
<li key={"_readuptoghost_"+this.currentGhostEventId}
|
2016-02-11 18:38:13 +03:00
|
|
|
className="mx_RoomView_myReadMarker_container">
|
2016-02-12 16:39:38 +03:00
|
|
|
{hr}
|
|
|
|
</li>
|
|
|
|
);
|
2016-02-10 14:03:46 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
_collectEventNode: function(eventId, node) {
|
|
|
|
this.eventNodes[eventId] = node;
|
|
|
|
},
|
2016-02-11 18:38:13 +03:00
|
|
|
|
2016-02-23 16:32:23 +03:00
|
|
|
|
|
|
|
// once images in the events load, make the scrollPanel check the
|
|
|
|
// scroll offsets.
|
|
|
|
_onImageLoad: function() {
|
|
|
|
var scrollPanel = this.refs.messagePanel;
|
|
|
|
if (scrollPanel) {
|
|
|
|
scrollPanel.checkScroll();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-02-11 18:38:13 +03:00
|
|
|
render: function() {
|
|
|
|
var ScrollPanel = sdk.getComponent("structures.ScrollPanel");
|
2016-02-24 16:38:55 +03:00
|
|
|
var Spinner = sdk.getComponent("elements.Spinner");
|
|
|
|
var topSpinner, bottomSpinner;
|
|
|
|
if (this.props.backPaginating) {
|
|
|
|
topSpinner = <li key="_topSpinner"><Spinner /></li>;
|
|
|
|
}
|
|
|
|
if (this.props.forwardPaginating) {
|
|
|
|
bottomSpinner = <li key="_bottomSpinner"><Spinner /></li>;
|
|
|
|
}
|
|
|
|
|
2016-02-11 18:38:13 +03:00
|
|
|
return (
|
|
|
|
<ScrollPanel ref="scrollPanel" className="mx_RoomView_messagePanel"
|
|
|
|
onScroll={ this.props.onScroll }
|
|
|
|
onFillRequest={ this.props.onFillRequest }
|
|
|
|
style={ this.props.hidden ? { display: 'none' } : {} }
|
|
|
|
stickyBottom={ this.props.stickyBottom }>
|
2016-02-24 16:38:55 +03:00
|
|
|
{topSpinner}
|
2016-02-11 18:38:13 +03:00
|
|
|
{this._getEventTiles()}
|
2016-02-24 16:38:55 +03:00
|
|
|
{bottomSpinner}
|
2016-02-11 18:38:13 +03:00
|
|
|
</ScrollPanel>
|
|
|
|
);
|
|
|
|
},
|
2016-02-10 14:03:46 +03:00
|
|
|
});
|