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/sidebar.jsx | 89 +++++---------------- web/react/components/unread_channel_indicators.jsx | 93 ++++++++++++++++++++++ 2 files changed, 112 insertions(+), 70 deletions(-) create mode 100644 web/react/components/unread_channel_indicators.jsx (limited to 'web') diff --git a/web/react/components/sidebar.jsx b/web/react/components/sidebar.jsx index 370250e4a..a16a0a8bb 100644 --- a/web/react/components/sidebar.jsx +++ b/web/react/components/sidebar.jsx @@ -11,6 +11,7 @@ var BrowserStore = require('../stores/browser_store.jsx'); var Utils = require('../utils/utils.jsx'); var SidebarHeader = require('./sidebar_header.jsx'); var SearchBox = require('./search_bar.jsx'); +var UnreadChannelIndicators = require('./unread_channel_indicators.jsx'); var Constants = require('../utils/constants.jsx'); var NewChannelFlow = require('./new_channel_flow.jsx'); @@ -19,13 +20,10 @@ export default class Sidebar extends React.Component { super(props); this.badgesActive = false; - this.firstUnreadChannel = null; - this.lastUnreadChannel = null; this.onChange = this.onChange.bind(this); this.onScroll = this.onScroll.bind(this); this.onResize = this.onResize.bind(this); - this.updateUnreadIndicators = this.updateUnreadIndicators.bind(this); this.createChannelElement = this.createChannelElement.bind(this); this.state = this.getStateFromStores(); @@ -149,13 +147,13 @@ export default class Sidebar extends React.Component { $('.nav-pills__container').perfectScrollbar(); this.updateTitle(); - this.updateUnreadIndicators(); + this.refs.unreadIndicators.onParentUpdate(this.refs); $(window).on('resize', this.onResize); } componentDidUpdate() { this.updateTitle(); - this.updateUnreadIndicators(); + this.refs.unreadIndicators.onParentUpdate(this.refs); } componentWillUnmount() { $(window).off('resize', this.onResize); @@ -268,41 +266,12 @@ export default class Sidebar extends React.Component { } } onScroll() { - this.updateUnreadIndicators(); + this.refs.unreadIndicators.onParentUpdate(this.refs); } onResize() { - this.updateUnreadIndicators(); + this.refs.unreadIndicators.onParentUpdate(this.refs); } - updateUnreadIndicators() { - const container = $(React.findDOMNode(this.refs.container)); - const topUnreadIndicator = $(React.findDOMNode(this.refs.topUnreadIndicator)); - const bottomUnreadIndicator = $(React.findDOMNode(this.refs.bottomUnreadIndicator)); - - if (this.firstUnreadChannel) { - var firstUnreadElement = $(React.findDOMNode(this.refs[this.firstUnreadChannel])); - - if (firstUnreadElement.position().top + firstUnreadElement.height() < 0) { - topUnreadIndicator.css('display', 'initial'); - } else { - topUnreadIndicator.css('display', 'none'); - } - } else { - topUnreadIndicator.css('display', 'none'); - } - - if (this.lastUnreadChannel) { - var lastUnreadElement = $(React.findDOMNode(this.refs[this.lastUnreadChannel])); - - if (lastUnreadElement.position().top > container.height()) { - bottomUnreadIndicator.css('display', 'initial'); - } else { - bottomUnreadIndicator.css('display', 'none'); - } - } else { - bottomUnreadIndicator.css('display', 'none'); - } - } - createChannelElement(channel, index) { + createChannelElement(unreadChannels, channel, index) { var members = this.state.members; var activeId = this.state.activeId; var channelMember = members[channel.id]; @@ -324,10 +293,7 @@ export default class Sidebar extends React.Component { titleClass = 'unread-title'; if (channel.id !== activeId) { - if (!this.firstUnreadChannel) { - this.firstUnreadChannel = channel.name; - } - this.lastUnreadChannel = channel.name; + unreadChannels.push(channel); } } @@ -433,24 +399,17 @@ export default class Sidebar extends React.Component { render() { this.badgesActive = false; - // keep track of the first and last unread channels so we can use them to set the unread indicators - this.firstUnreadChannel = null; - this.lastUnreadChannel = null; + // keep track of unread channels so we can use them to set the unread indicators + const unreadChannels = []; // create elements for all 3 types of channels - var channelItems = this.state.channels.filter( - function filterPublicChannels(channel) { - return channel.type === 'O'; - } - ).map(this.createChannelElement); + const publicChannels = this.state.channels.filter((channel) => channel.type === 'O'); + const publicChannelItems = publicChannels.map(this.createChannelElement.bind(this, unreadChannels)); - var privateChannelItems = this.state.channels.filter( - function filterPrivateChannels(channel) { - return channel.type === 'P'; - } - ).map(this.createChannelElement); + const privateChannels = this.state.channels.filter((channel) => channel.type === 'P'); + const privateChannelItems = privateChannels.map(this.createChannelElement.bind(this, unreadChannels)); - var directMessageItems = this.state.showDirectChannels.map(this.createChannelElement); + const directMessageItems = this.state.showDirectChannels.map(this.createChannelElement.bind(this, unreadChannels)); // update the favicon to show if there are any notifications var link = document.createElement('link'); @@ -504,20 +463,10 @@ export default class Sidebar extends React.Component { /> -
- Unread post(s) above -
-
- Unread post(s) below -
+
- {channelItems} + {publicChannelItems}
  • 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