2015-09-23 05:43:08 +08:00
|
|
|
React = require 'react'
|
2016-01-09 09:49:27 +08:00
|
|
|
crypto = require 'crypto'
|
2015-09-23 05:43:08 +08:00
|
|
|
classNames = require 'classnames'
|
2016-01-22 06:12:56 +08:00
|
|
|
{Actions} = require 'nylas-exports'
|
|
|
|
{RetinaImg} = require 'nylas-component-kit'
|
|
|
|
SidebarActions = require '../sidebar-actions'
|
2015-09-23 05:43:08 +08:00
|
|
|
|
2016-01-20 15:42:50 +08:00
|
|
|
|
|
|
|
ItemTypes = {
|
|
|
|
"Unified"
|
|
|
|
}
|
|
|
|
|
2015-09-23 05:43:08 +08:00
|
|
|
class AccountSwitcher extends React.Component
|
|
|
|
@displayName: 'AccountSwitcher'
|
|
|
|
|
2015-10-04 15:19:29 +08:00
|
|
|
@containerRequired: false
|
|
|
|
@containerStyles:
|
|
|
|
minWidth: 165
|
|
|
|
maxWidth: 210
|
|
|
|
|
2016-01-20 15:42:50 +08:00
|
|
|
@propTypes:
|
|
|
|
accounts: React.PropTypes.array.isRequired
|
|
|
|
focusedAccounts: React.PropTypes.array.isRequired
|
|
|
|
|
2015-09-23 05:43:08 +08:00
|
|
|
constructor: (@props) ->
|
2016-01-20 15:42:50 +08:00
|
|
|
@state =
|
|
|
|
showing: false
|
2015-09-23 05:43:08 +08:00
|
|
|
|
2016-01-20 15:42:50 +08:00
|
|
|
# Helpers
|
2015-09-23 05:43:08 +08:00
|
|
|
|
2016-01-22 06:12:56 +08:00
|
|
|
_makeItem: (account = {}) =>
|
|
|
|
{id, label, emailAddress, provider} = account
|
2016-01-20 15:42:50 +08:00
|
|
|
id ?= ItemTypes.Unified
|
|
|
|
label ?= "All Accounts"
|
|
|
|
email = emailAddress ? ""
|
|
|
|
iconName = provider ? 'imap'
|
|
|
|
accounts = if id is ItemTypes.Unified
|
|
|
|
@props.accounts
|
|
|
|
else
|
2016-01-22 06:12:56 +08:00
|
|
|
[account]
|
2015-09-23 05:43:08 +08:00
|
|
|
|
2016-01-20 15:42:50 +08:00
|
|
|
return {id, label, email, iconName, accounts}
|
2015-09-23 05:43:08 +08:00
|
|
|
|
2016-01-20 15:42:50 +08:00
|
|
|
_selectedItem: =>
|
|
|
|
if @props.focusedAccounts.length > 1
|
|
|
|
@_makeItem()
|
|
|
|
else
|
|
|
|
@_makeItem(@props.focusedAccounts[0])
|
2015-09-23 05:43:08 +08:00
|
|
|
|
2016-01-20 15:42:50 +08:00
|
|
|
_toggleDropdown: =>
|
|
|
|
@setState showing: !@state.showing
|
2015-09-23 05:43:08 +08:00
|
|
|
|
2015-09-25 10:07:50 +08:00
|
|
|
|
2016-01-20 15:42:50 +08:00
|
|
|
# Handlers
|
|
|
|
|
|
|
|
_onBlur: (e) =>
|
|
|
|
target = e.nativeEvent.relatedTarget
|
|
|
|
if target? and React.findDOMNode(@refs.button).contains(target)
|
|
|
|
return
|
|
|
|
@setState(showing: false)
|
|
|
|
|
|
|
|
_onSwitchAccount: (item) =>
|
2016-01-22 06:12:56 +08:00
|
|
|
SidebarActions.focusAccounts(item.accounts)
|
2016-01-20 15:42:50 +08:00
|
|
|
@setState(showing: false)
|
|
|
|
|
|
|
|
_onManageAccounts: =>
|
|
|
|
Actions.switchPreferencesTab('Accounts')
|
|
|
|
Actions.openPreferences()
|
|
|
|
|
|
|
|
@setState(showing: false)
|
|
|
|
|
|
|
|
_renderItem: (item) =>
|
2015-09-25 10:07:50 +08:00
|
|
|
classes = classNames
|
2016-01-20 15:42:50 +08:00
|
|
|
"active": item.id is @_selectedItem().id
|
2015-09-25 10:07:50 +08:00
|
|
|
"item": true
|
|
|
|
"secondary-item": true
|
|
|
|
|
2016-01-20 15:42:50 +08:00
|
|
|
<div key={item.email} className={classes} onClick={@_onSwitchAccount.bind(@, item)}>
|
|
|
|
{@_renderGravatar(item)}
|
|
|
|
<div className="name" style={lineHeight: "110%"}>{item.label}</div>
|
2015-09-25 10:07:50 +08:00
|
|
|
<div style={clear: "both"}></div>
|
2015-09-23 05:43:08 +08:00
|
|
|
</div>
|
|
|
|
|
2016-01-20 15:42:50 +08:00
|
|
|
_renderManageAccountsItem: =>
|
2015-09-25 10:07:50 +08:00
|
|
|
<div className="item secondary-item new-account-option"
|
2015-10-01 03:29:56 +08:00
|
|
|
onClick={@_onManageAccounts}
|
2015-09-23 05:43:08 +08:00
|
|
|
tabIndex={999}>
|
|
|
|
<div style={float: 'left'}>
|
|
|
|
<RetinaImg name="icon-accounts-addnew.png"
|
|
|
|
fallback="ic-settings-account-imap.png"
|
|
|
|
mode={RetinaImg.Mode.ContentPreserve}
|
|
|
|
style={width: 28, height: 28, marginTop: -10} />
|
|
|
|
</div>
|
|
|
|
<div className="name" style={lineHeight: "110%", textTransform: 'none'}>
|
2015-10-01 03:29:56 +08:00
|
|
|
Manage accounts…
|
2015-09-23 05:43:08 +08:00
|
|
|
</div>
|
2015-09-25 10:07:50 +08:00
|
|
|
<div style={clear: "both"}></div>
|
2015-09-23 05:43:08 +08:00
|
|
|
</div>
|
|
|
|
|
2016-01-20 15:42:50 +08:00
|
|
|
_renderDropdown: (items) =>
|
2015-09-25 10:07:50 +08:00
|
|
|
<div className="dropdown">
|
|
|
|
<div className="inner">
|
2016-01-20 15:42:50 +08:00
|
|
|
{items.map(@_renderItem)}
|
|
|
|
{@_renderManageAccountsItem()}
|
2015-09-25 10:07:50 +08:00
|
|
|
</div>
|
|
|
|
</div>
|
2015-09-23 05:43:08 +08:00
|
|
|
|
2016-01-20 15:42:50 +08:00
|
|
|
_renderGravatar: ({email, iconName}) =>
|
|
|
|
if email
|
|
|
|
hash = crypto.createHash('md5').update(email, 'utf8').digest('hex')
|
|
|
|
url = "url(http://www.gravatar.com/avatar/#{hash}?d=blank&s=56)"
|
|
|
|
else
|
|
|
|
url = ''
|
2015-09-25 10:07:50 +08:00
|
|
|
|
2015-09-25 23:32:42 +08:00
|
|
|
<div style={float: 'left', position: "relative"}>
|
2015-09-25 10:07:50 +08:00
|
|
|
<div className="gravatar" style={backgroundImage:url}></div>
|
2016-01-20 15:42:50 +08:00
|
|
|
<RetinaImg name={"ic-settings-account-#{iconName}@2x.png"}
|
2015-09-25 10:07:50 +08:00
|
|
|
style={width: 28, height: 28, marginTop: -10}
|
|
|
|
fallback="ic-settings-account-imap.png"
|
|
|
|
mode={RetinaImg.Mode.ContentPreserve} />
|
2015-09-23 05:43:08 +08:00
|
|
|
</div>
|
|
|
|
|
2016-01-20 15:42:50 +08:00
|
|
|
_renderPrimaryItem: (item) =>
|
|
|
|
<div className="item primary-item" onClick={@_toggleDropdown}>
|
|
|
|
{@_renderGravatar(item)}
|
|
|
|
<div style={float: 'right', marginTop: -2}>
|
|
|
|
<RetinaImg className="toggle"
|
|
|
|
name="account-switcher-dropdown.png"
|
|
|
|
mode={RetinaImg.Mode.ContentDark} />
|
|
|
|
</div>
|
|
|
|
<div className="name" style={lineHeight: "110%"}>
|
|
|
|
{item.label}
|
|
|
|
</div>
|
|
|
|
<div style={clear: "both"}></div>
|
|
|
|
</div>
|
2015-09-23 05:43:08 +08:00
|
|
|
|
2016-01-20 15:42:50 +08:00
|
|
|
render: =>
|
|
|
|
return <span /> unless @props.focusedAccounts
|
|
|
|
classnames = ""
|
|
|
|
classnames += "open" if @state.showing
|
|
|
|
selected = @_selectedItem()
|
|
|
|
items = [@_makeItem()].concat @props.accounts.map(@_makeItem)
|
2015-11-24 04:20:51 +08:00
|
|
|
|
2016-01-20 15:42:50 +08:00
|
|
|
<div id="account-switcher"
|
|
|
|
tabIndex={-1}
|
|
|
|
onBlur={@_onBlur}
|
|
|
|
ref="button"
|
|
|
|
className={classnames}>
|
|
|
|
{@_renderPrimaryItem(selected)}
|
|
|
|
{@_renderDropdown(items)}
|
|
|
|
</div>
|
2015-09-23 05:43:08 +08:00
|
|
|
|
|
|
|
|
|
|
|
module.exports = AccountSwitcher
|