From 7e66a3d5be9a928b39c22756a5d83703b99648c4 Mon Sep 17 00:00:00 2001 From: hmhealey Date: Tue, 15 Sep 2015 17:36:48 -0400 Subject: Moved unread channel indicators into their own React component and file --- web/react/components/unread_channel_indicators.jsx | 93 ++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 web/react/components/unread_channel_indicators.jsx (limited to 'web/react/components/unread_channel_indicators.jsx') diff --git a/web/react/components/unread_channel_indicators.jsx b/web/react/components/unread_channel_indicators.jsx new file mode 100644 index 000000000..852e0dbb7 --- /dev/null +++ b/web/react/components/unread_channel_indicators.jsx @@ -0,0 +1,93 @@ +// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved. +// See License.txt for license information. + +// Indicators for the left sidebar which indicate if there's unread posts in a channel that is not shown +// because it is either above or below the screen +export default class UnreadChannelIndicators extends React.Component { + constructor(props) { + super(props); + + this.getFirstLastUnreadChannels = this.getFirstLastUnreadChannels.bind(this); + this.onParentUpdate = this.onParentUpdate.bind(this); + + this.state = this.getFirstLastUnreadChannels(props); + } + + getFirstLastUnreadChannels(props) { + let firstUnreadChannel = null; + let lastUnreadChannel = null; + + for (const unreadChannel of props.unreadChannels) { + if (!firstUnreadChannel) { + firstUnreadChannel = unreadChannel; + } + lastUnreadChannel = unreadChannel; + } + + return { + firstUnreadChannel, + lastUnreadChannel + }; + } + + onParentUpdate(parentRefs) { + const container = $(React.findDOMNode(parentRefs.container)); + const topUnreadIndicator = $(React.findDOMNode(this.refs.topIndicator)); + const bottomUnreadIndicator = $(React.findDOMNode(this.refs.bottomIndicator)); + + if (this.state.firstUnreadChannel) { + var firstUnreadElement = $(React.findDOMNode(parentRefs[this.state.firstUnreadChannel.name])); + + if (firstUnreadElement.position().top + firstUnreadElement.height() < 0) { + topUnreadIndicator.css('display', 'initial'); + } else { + topUnreadIndicator.css('display', 'none'); + } + } else { + topUnreadIndicator.css('display', 'none'); + } + + if (this.state.lastUnreadChannel) { + var lastUnreadElement = $(React.findDOMNode(parentRefs[this.state.lastUnreadChannel.name])); + + if (lastUnreadElement.position().top > container.height()) { + bottomUnreadIndicator.css('display', 'initial'); + } else { + bottomUnreadIndicator.css('display', 'none'); + } + } else { + bottomUnreadIndicator.css('display', 'none'); + } + } + + componentWillReceiveProps(nextProps) { + this.setState(this.getFirstLastUnreadChannels(nextProps)); + } + + render() { + return ( +
+
+ {'Unread post(s) above'} +
+
+ {'Unread post(s) below'} +
+
+ ); + } +} + +UnreadChannelIndicators.propTypes = { + + // a list of the unread channels displayed in the parent + unreadChannels: React.PropTypes.array.isRequired +}; -- cgit v1.2.3-1-g7c22