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'
|
2016-02-03 02:50:12 +08:00
|
|
|
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'
|
|
|
|
|
2016-01-20 15:42:50 +08:00
|
|
|
@propTypes:
|
|
|
|
accounts: React.PropTypes.array.isRequired
|
|
|
|
focusedAccounts: React.PropTypes.array.isRequired
|
|
|
|
|
|
|
|
# Helpers
|
2015-09-23 05:43:08 +08:00
|
|
|
|
2016-02-03 02:56:09 +08:00
|
|
|
_makeAccountItem: (account) =>
|
2016-01-22 06:12:56 +08:00
|
|
|
{id, label, emailAddress, provider} = account
|
2016-02-03 02:56:09 +08:00
|
|
|
email = emailAddress
|
|
|
|
iconName = provider
|
|
|
|
accounts = [account]
|
|
|
|
return {id, label, email, iconName, accounts}
|
2015-09-23 05:43:08 +08:00
|
|
|
|
2016-02-03 02:56:09 +08:00
|
|
|
_makeUnifiedItem: =>
|
|
|
|
id = ItemTypes.Unified
|
|
|
|
label = "All Accounts"
|
|
|
|
email = ""
|
|
|
|
iconName = 'unified'
|
|
|
|
accounts = @props.accounts
|
2016-01-20 15:42:50 +08:00
|
|
|
return {id, label, email, iconName, accounts}
|
2015-09-23 05:43:08 +08:00
|
|
|
|
2016-02-03 02:56:09 +08:00
|
|
|
|
2016-01-20 15:42:50 +08:00
|
|
|
_selectedItem: =>
|
|
|
|
if @props.focusedAccounts.length > 1
|
2016-02-03 02:56:09 +08:00
|
|
|
@_makeUnifiedItem()
|
2016-01-20 15:42:50 +08:00
|
|
|
else
|
2016-02-03 02:56:09 +08:00
|
|
|
@_makeAccountItem(@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
|
|
|
|
2016-02-06 01:55:27 +08:00
|
|
|
_makeMenuItem: (item, idx) =>
|
2016-02-05 06:48:15 +08:00
|
|
|
menuItem = {
|
|
|
|
label: item.label,
|
|
|
|
click: @_onSwitchAccount.bind(@, item)
|
|
|
|
accelerator: "CmdOrCtrl+#{idx}"
|
|
|
|
}
|
2015-09-25 10:07:50 +08:00
|
|
|
|
2016-02-05 06:48:15 +08:00
|
|
|
if @_selectedItem().id is item.id
|
|
|
|
menuItem.type = 'checkbox'
|
|
|
|
menuItem.checked = true
|
2016-01-20 15:42:50 +08:00
|
|
|
|
2016-02-05 06:48:15 +08:00
|
|
|
return menuItem
|
2016-01-20 15:42:50 +08:00
|
|
|
|
2016-02-05 06:48:15 +08:00
|
|
|
_makeMenuTemplate: =>
|
|
|
|
template = []
|
|
|
|
items = @props.accounts.map(@_makeAccountItem)
|
2016-01-20 15:42:50 +08:00
|
|
|
|
2016-02-05 06:48:15 +08:00
|
|
|
if @props.accounts.length > 1
|
|
|
|
unifiedItem = @_makeUnifiedItem()
|
|
|
|
template = [
|
2016-02-06 01:55:27 +08:00
|
|
|
@_makeMenuItem(unifiedItem, 1)
|
2016-02-05 06:48:15 +08:00
|
|
|
{type: 'separator'}
|
|
|
|
]
|
2016-01-20 15:42:50 +08:00
|
|
|
|
2016-02-06 01:55:27 +08:00
|
|
|
items.forEach (item, idx) => template.push(@_makeMenuItem(item, idx + 2))
|
2016-01-20 15:42:50 +08:00
|
|
|
|
2016-02-05 06:48:15 +08:00
|
|
|
template = template.concat [
|
|
|
|
{type: 'separator'}
|
|
|
|
{label: 'Manage Accounts...', click: @_onManageAccounts}
|
|
|
|
]
|
|
|
|
return template
|
2015-09-25 10:07:50 +08:00
|
|
|
|
2015-09-23 05:43:08 +08:00
|
|
|
|
2016-02-05 06:48:15 +08:00
|
|
|
# Handlers
|
2015-09-23 05:43:08 +08:00
|
|
|
|
2016-02-05 06:48:15 +08:00
|
|
|
_onSwitchAccount: (item) =>
|
|
|
|
SidebarActions.focusAccounts(item.accounts)
|
2015-09-23 05:43:08 +08:00
|
|
|
|
2016-02-05 06:48:15 +08:00
|
|
|
_onManageAccounts: =>
|
|
|
|
Actions.switchPreferencesTab('Accounts')
|
|
|
|
Actions.openPreferences()
|
2015-09-23 05:43:08 +08:00
|
|
|
|
2016-02-05 06:48:15 +08:00
|
|
|
_onShowMenu: =>
|
|
|
|
remote = require('electron').remote
|
|
|
|
Menu = remote.Menu
|
|
|
|
menu = Menu.buildFromTemplate(@_makeMenuTemplate())
|
|
|
|
menu.popup()
|
2015-09-23 05:43:08 +08:00
|
|
|
|
2016-01-20 15:42:50 +08:00
|
|
|
render: =>
|
2016-02-05 06:48:15 +08:00
|
|
|
<div className="account-switcher" onMouseDown={@_onShowMenu}>
|
|
|
|
<RetinaImg
|
|
|
|
style={width: 13, height: 14}
|
|
|
|
name="account-switcher-dropdown.png"
|
|
|
|
mode={RetinaImg.Mode.ContentDark} />
|
2016-01-20 15:42:50 +08:00
|
|
|
</div>
|
2015-09-23 05:43:08 +08:00
|
|
|
|
|
|
|
|
|
|
|
module.exports = AccountSwitcher
|