Cleaned TabList up by pulling stuff out into TabListItem, userlist resize listener gets removed on unmount

This commit is contained in:
khlieng 2015-02-06 02:32:25 +01:00
parent 806f9aae27
commit 0a47f2ae4a
6 changed files with 95 additions and 81 deletions

View File

@ -19,22 +19,12 @@ var ChatTitle = React.createClass({
render: function() {
var tab = this.state.selectedTab;
var topic;
var usercount;
if (tab.channel && this.state.channels[tab.server]) {
var channel = this.state.channels[tab.server][tab.channel];
if (channel) {
usercount = channel.users.length;
topic = channel.topic || '';
}
}
var usercount = channelStore.getUsers(tab.server, tab.channel).length;
return (
<div className="chat-title-bar">
<div>
<span className="chat-title">{tab.name}</span>
<span className="chat-topic" title={topic}>{topic}</span>
</div>
<span className="chat-usercount">{usercount}</span>
</div>

View File

@ -34,25 +34,22 @@ var MessageBox = React.createClass({
render: function() {
var tab = this.state.selectedTab;
var dest = tab.channel || tab.server;
var messages;
if (this.state.messages[tab.server] && dest) {
messages = _.map(this.state.messages[tab.server][dest], function(message) {
var messageClass = 'message';
var messages = _.map(messageStore.getMessages(tab.server, dest), function(message) {
var messageClass = 'message';
if (message.type) {
messageClass += ' message-' + message.type;
}
if (message.type) {
messageClass += ' message-' + message.type;
}
return (
<p className={messageClass}>
<span className="message-time">{util.timestamp(message.time)}</span>
{ message.from ? <span className="message-sender">{message.from}</span> : null }
{message.message}
</p>
);
});
}
return (
<p className={messageClass}>
<span className="message-time">{util.timestamp(message.time)}</span>
{ message.from ? <span className="message-sender">{message.from}</span> : null }
{message.message}
</p>
);
});
return (
<div className="messagebox">{messages}</div>

View File

@ -2,27 +2,24 @@ var React = require('react');
var Reflux = require('reflux');
var _ = require('lodash');
var TabListItem = require('./TabListItem.jsx');
var channelStore = require('../stores/channel');
var privateChatStore = require('../stores/privateChat');
var serverStore = require('../stores/server');
var selectedTabStore = require('../stores/selectedTab');
var tabActions = require('../actions/tab');
var routeActions = require('../actions/route');
var TabList = React.createClass({
mixins: [
Reflux.connect(serverStore, 'servers'),
Reflux.connect(channelStore, 'channels'),
Reflux.connect(privateChatStore, 'privateChats'),
Reflux.connect(selectedTabStore, 'selectedTab'),
Reflux.connect(serverStore, 'servers')
Reflux.connect(privateChatStore, 'privateChats')
],
getInitialState: function() {
return {
servers: serverStore.getState(),
channels: channelStore.getState(),
privateChats: privateChatStore.getState(),
selectedTab: selectedTabStore.getState(),
servers: serverStore.getState()
privateChats: privateChatStore.getState()
};
},
@ -36,60 +33,37 @@ var TabList = React.createClass({
render: function() {
var self = this;
var tabClass;
var selected = this.state.selectedTab;
var tabs = _.map(this.state.channels, function(server, address) {
var channels = _.map(server, function(channel, name) {
if (address === selected.server &&
name === selected.channel) {
tabClass = 'selected';
} else {
tabClass = '';
}
var serverTabs = _.map(server, function(channel, name) {
return (
<p
className={tabClass}
onClick={tabActions.select.bind(null, address, name)}>
{name}
</p>
<TabListItem
server={address}
channel={name}
name={name}>
</TabListItem>
);
});
_.each(self.state.privateChats[address], function(chat, nick) {
if (address === selected.server &&
nick === selected.channel) {
tabClass = 'selected';
} else {
tabClass = '';
}
channels.push(
<p
className={tabClass}
onClick={tabActions.select.bind(null, address, nick)}>
{nick}
</p>
serverTabs.push(
<TabListItem
server={address}
channel={nick}
name={nick}>
</TabListItem>
);
});
if (address === selected.server &&
selected.channel === null) {
tabClass = 'tab-server selected';
} else {
tabClass = 'tab-server';
}
channels.unshift(
<p
className={tabClass}
onClick={tabActions.select.bind(null, address, null)}>
{serverStore.getName(address)}
</p>
serverTabs.unshift(
<TabListItem
server={address}
channel={null}
name={serverStore.getName(address)}>
</TabListItem>
);
return channels;
return serverTabs;
});
return (

View File

@ -0,0 +1,41 @@
var React = require('react');
var Reflux = require('reflux');
var selectedTabStore = require('../stores/selectedTab');
var tabActions = require('../actions/tab');
var TabListItem = React.createClass({
mixins: [
Reflux.connect(selectedTabStore, 'selectedTab')
],
getInitialState: function() {
return {
selectedTab: selectedTabStore.getState()
};
},
handleClick: function() {
tabActions.select(this.props.server, this.props.channel);
},
render: function() {
var selected = this.state.selectedTab;
var classes = [];
if (!this.props.channel) {
classes.push('tab-server');
}
if (this.props.server === selected.server &&
this.props.channel === selected.channel) {
classes.push('selected');
}
return (
<p className={classes.join(' ')} onClick={this.handleClick}>{this.props.name}</p>
);
}
});
module.exports = TabListItem;

View File

@ -22,10 +22,15 @@ var UserList = React.createClass({
},
componentDidMount: function() {
var self = this;
window.addEventListener('resize', function() {
self.setState({ height: window.innerHeight - 100 });
});
window.addEventListener('resize', this.handleResize);
},
componentWillUnmount: function() {
window.removeEventListener('resize', this.handleResize);
},
handleResize: function() {
this.setState({ height: window.innerHeight - 100 });
},
render: function() {

View File

@ -77,6 +77,13 @@ var messageStore = Reflux.createStore({
this.trigger(messages);
},
getMessages: function(server, dest) {
if (messages[server] && messages[server][dest]) {
return messages[server][dest];
}
return [];
},
getState: function() {
return messages;
}