// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved. // See License.txt for license information. var utils = require('../utils/utils.jsx'); var client = require('../utils/client.jsx'); var AsyncClient = require('../utils/async_client.jsx'); var UserStore = require('../stores/user_store.jsx'); var ChannelStore = require('../stores/channel_store.jsx'); var TeamStore = require('../stores/team_store.jsx'); var UserProfile = require('./user_profile.jsx'); var MessageWrapper = require('./message_wrapper.jsx'); var Constants = require('../utils/constants.jsx'); var ActionTypes = Constants.ActionTypes; var AppDispatcher = require('../dispatcher/app_dispatcher.jsx'); function getCountsStateFromStores() { var count = 0; var channels = ChannelStore.getAll(); var members = ChannelStore.getAllMembers(); channels.forEach(function(channel) { var channelMember = members[channel.id]; if (channel.type === 'D') { count += channel.total_msg_count - channelMember.msg_count; } else { if (channelMember.mention_count > 0) { count += channelMember.mention_count; } else if (channelMember.notify_level !== "quiet" && channel.total_msg_count - channelMember.msg_count > 0) { count += 1; } } }); return { count: count }; } var NotifyCounts = React.createClass({ componentDidMount: function() { ChannelStore.addChangeListener(this._onChange); }, componentWillUnmount: function() { ChannelStore.removeChangeListener(this._onChange); }, _onChange: function() { var newState = getCountsStateFromStores(); if (!utils.areStatesEqual(newState, this.state)) { this.setState(newState); } }, getInitialState: function() { return getCountsStateFromStores(); }, render: function() { if (this.state.count) { return { this.state.count }; } else { return null; } } }); function getStateFromStores() { return { channel: ChannelStore.getCurrent(), member: ChannelStore.getCurrentMember(), users: ChannelStore.getCurrentExtraInfo().members }; } module.exports = React.createClass({ displayName: 'Navbar', componentDidMount: function() { ChannelStore.addChangeListener(this._onChange); ChannelStore.addExtraInfoChangeListener(this._onChange); $('.inner__wrap').click(this.hideSidebars); $('body').on('click.infopopover', function(e) { if ($(e.target).attr('data-toggle') !== 'popover' && $(e.target).parents('.popover.in').length === 0) { $('.info-popover').popover('hide'); } }); }, componentWillUnmount: function() { ChannelStore.removeChangeListener(this._onChange); }, handleSubmit: function(e) { e.preventDefault(); }, handleLeave: function(e) { client.leaveChannel(this.state.channel.id, function(data, text, req) { AsyncClient.getChannels(true); window.location.href = TeamStore.getCurrentTeamUrl() + '/channels/town-square'; }.bind(this), function(err) { AsyncClient.dispatchError(err, "handleLeave"); } ); }, hideSidebars: function(e) { var windowWidth = $(window).outerWidth(); if(windowWidth <= 768) { AppDispatcher.handleServerAction({ type: ActionTypes.RECIEVED_SEARCH, results: null }); AppDispatcher.handleServerAction({ type: ActionTypes.RECIEVED_POST_SELECTED, results: null }); if (e.target.className != 'navbar-toggle' && e.target.className != 'icon-bar') { $('.inner__wrap').removeClass('move--right move--left move--left-small'); $('.sidebar--left').removeClass('move--right'); $('.sidebar--right').removeClass('move--left'); $('.sidebar--menu').removeClass('move--left'); } } }, toggleLeftSidebar: function() { $('.inner__wrap').toggleClass('move--right'); $('.sidebar--left').toggleClass('move--right'); }, toggleRightSidebar: function() { $('.inner__wrap').toggleClass('move--left-small'); $('.sidebar--menu').toggleClass('move--left'); }, _onChange: function() { this.setState(getStateFromStores()); $("#navbar .navbar-brand .description").popover({placement : 'bottom', trigger: 'click', html: true}); }, getInitialState: function() { return getStateFromStores(); }, render: function() { var currentId = UserStore.getCurrentId(); var popoverContent = ""; var channelTitle = this.props.teamDisplayName; var isAdmin = false; var isDirect = false; var description = "" var channel = this.state.channel; if (channel) { description = utils.textToJsx(channel.description, {"singleline": true, "noMentionHighlight": true}); popoverContent = React.renderToString(); isAdmin = this.state.member.roles.indexOf("admin") > -1; if (channel.type === 'O') { channelTitle = channel.display_name; } else if (channel.type === 'P') { channelTitle = channel.display_name; } else if (channel.type === 'D') { isDirect = true; if (this.state.users.length > 1) { if (this.state.users[0].id === currentId) { channelTitle = ; } else { channelTitle = ; } } } if (channel.description.length == 0) { popoverContent = React.renderToString(No channel description yet. Click here to add one.); } } var navbar_collapse_button = currentId != null ? null : Toggle sidebar ; var sidebar_collapse_button = currentId == null ? null : Toggle sidebar ; var right_sidebar_collapse_button= currentId == null ? null : ; return ( { navbar_collapse_button } { sidebar_collapse_button } { right_sidebar_collapse_button } { !isDirect && channel ? {channelTitle} { !ChannelStore.isDefault(channel) ? Add Members : null } { isAdmin && !ChannelStore.isDefault(channel) ? Manage Members : null } Set Channel Description... Notification Preferences { isAdmin && !ChannelStore.isDefault(channel) ? Rename Channel... : null } { isAdmin && !ChannelStore.isDefault(channel) ? Delete Channel... : null } { !ChannelStore.isDefault(channel) ? Leave Channel : null } : null } { isDirect && channel ? { channelTitle } : null } { !channel ? { channelTitle } : "" } ); } });