_ = require 'underscore' React = require "react" classnames = require 'classnames' {Actions, Contact} = require 'nylas-exports' {Menu, MenuItem} = require('electron').remote MAX_COLLAPSED = 5 class MessageParticipants extends React.Component @displayName: 'MessageParticipants' @propTypes: to: React.PropTypes.array cc: React.PropTypes.array bcc: React.PropTypes.array from: React.PropTypes.array onClick: React.PropTypes.func isDetailed: React.PropTypes.bool @defaultProps: to: [] cc: [] bcc: [] from: [] # Helpers _allToParticipants: => _.union(@props.to, @props.cc, @props.bcc) _selectText: (e) => textNode = e.currentTarget.childNodes[0] range = document.createRange() range.setStart(textNode, 0) range.setEnd(textNode, textNode.length) selection = document.getSelection() selection.removeAllRanges() selection.addRange(range) _shortNames: (contacts = [], max = MAX_COLLAPSED) => names = _.map(contacts, (c) -> c.displayName(includeAccountLabel: true, compact: true)) if names.length > max extra = names.length - max names = names.slice(0, max) names.push("and #{extra} more") names.join(", ") _onContactContextMenu: (contact) => menu = new Menu() menu.append(new MenuItem({role: 'copy'})) menu.append(new MenuItem({ label: "Email #{contact.email}", click: => Actions.composeNewDraftToRecipient(contact) })) menu.popup(NylasEnv.getCurrentWindow()) # Renderers _renderFullContacts: (contacts = []) => _.map(contacts, (c, i) => if contacts.length is 1 then comma = "" else if i is contacts.length-1 then comma = "" else comma = "," if c.name?.length > 0 and c.name isnt c.email
{c.fullName()}
{" <"} @_onContactContextMenu(c)} > {c.email} {">#{comma}"}
else
@_onContactContextMenu(c)} > {c.email} {comma}
) _renderExpandedField: (name, field, {includeLabel} = {}) => includeLabel ?= true
{ if includeLabel
{name}: 
else undefined }
{@_renderFullContacts(field)}
_renderExpanded: => expanded = [] if @props.from.length > 0 expanded.push( @_renderExpandedField('from', @props.from, includeLabel: false) ) if @props.to.length > 0 expanded.push( @_renderExpandedField('to', @props.to) ) if @props.cc.length > 0 expanded.push( @_renderExpandedField('cc', @props.cc) ) if @props.bcc.length > 0 expanded.push( @_renderExpandedField('bcc', @props.bcc) )
{expanded}
_renderCollapsed: => childSpans = [] toParticipants = @_allToParticipants() if @props.from.length > 0 childSpans.push( {@_shortNames(@props.from)} ) if toParticipants.length > 0 childSpans.push( To:  {@_shortNames(toParticipants)} ) {childSpans} render: => classSet = classnames "participants": true "message-participants": true "collapsed": not @props.isDetailed "from-participants": @props.from.length > 0 "to-participants": @_allToParticipants().length > 0
{if @props.isDetailed then @_renderExpanded() else @_renderCollapsed()}
module.exports = MessageParticipants