2016-01-19 15:18:19 +08:00
|
|
|
_ = require 'underscore'
|
2016-05-20 03:04:13 +08:00
|
|
|
NylasStore = require 'nylas-store'
|
2016-01-22 06:12:56 +08:00
|
|
|
{Actions,
|
2016-01-19 15:18:19 +08:00
|
|
|
AccountStore,
|
|
|
|
ThreadCountsStore,
|
|
|
|
WorkspaceStore,
|
2016-02-05 06:14:24 +08:00
|
|
|
OutboxStore,
|
2016-01-19 15:18:19 +08:00
|
|
|
FocusedPerspectiveStore,
|
|
|
|
CategoryStore} = require 'nylas-exports'
|
|
|
|
|
|
|
|
SidebarSection = require './sidebar-section'
|
2016-02-03 02:50:12 +08:00
|
|
|
SidebarActions = require './sidebar-actions'
|
2016-02-06 06:24:07 +08:00
|
|
|
AccountCommands = require './account-commands'
|
2016-01-19 15:18:19 +08:00
|
|
|
|
|
|
|
Sections = {
|
|
|
|
"Standard",
|
|
|
|
"User"
|
|
|
|
}
|
|
|
|
|
|
|
|
class SidebarStore extends NylasStore
|
|
|
|
|
|
|
|
constructor: ->
|
2016-02-06 10:36:48 +08:00
|
|
|
NylasEnv.savedState.sidebarKeysCollapsed ?= {}
|
|
|
|
|
2016-01-19 15:18:19 +08:00
|
|
|
@_sections = {}
|
2016-01-20 15:42:50 +08:00
|
|
|
@_sections[Sections.Standard] = {}
|
|
|
|
@_sections[Sections.User] = []
|
2016-06-02 08:13:07 +08:00
|
|
|
@_focusedAccounts = FocusedPerspectiveStore.sidebarAccounts()
|
2016-02-06 06:24:07 +08:00
|
|
|
@_registerCommands()
|
|
|
|
@_registerMenuItems()
|
2016-01-19 15:18:19 +08:00
|
|
|
@_registerListeners()
|
|
|
|
@_updateSections()
|
|
|
|
|
2016-01-20 15:42:50 +08:00
|
|
|
accounts: ->
|
|
|
|
AccountStore.accounts()
|
|
|
|
|
|
|
|
focusedAccounts: ->
|
2016-01-22 06:12:56 +08:00
|
|
|
@_focusedAccounts
|
2016-01-20 15:42:50 +08:00
|
|
|
|
2016-01-19 15:18:19 +08:00
|
|
|
standardSection: ->
|
|
|
|
@_sections[Sections.Standard]
|
|
|
|
|
|
|
|
userSections: ->
|
|
|
|
@_sections[Sections.User]
|
|
|
|
|
|
|
|
_registerListeners: ->
|
2016-05-20 03:04:13 +08:00
|
|
|
@listenTo Actions.setCollapsedSidebarItem, @_onSetCollapsedByName
|
|
|
|
@listenTo SidebarActions.setKeyCollapsed, @_onSetCollapsedByKey
|
2016-01-30 02:21:16 +08:00
|
|
|
@listenTo AccountStore, @_onAccountsChanged
|
2016-02-03 02:56:09 +08:00
|
|
|
@listenTo FocusedPerspectiveStore, @_onFocusedPerspectiveChanged
|
2016-01-19 15:18:19 +08:00
|
|
|
@listenTo WorkspaceStore, @_updateSections
|
2016-02-05 06:14:24 +08:00
|
|
|
@listenTo OutboxStore, @_updateSections
|
2016-01-19 15:18:19 +08:00
|
|
|
@listenTo ThreadCountsStore, @_updateSections
|
|
|
|
@listenTo CategoryStore, @_updateSections
|
2016-01-26 08:36:56 +08:00
|
|
|
|
2016-01-29 16:31:10 +08:00
|
|
|
@configSubscription = NylasEnv.config.onDidChange(
|
2016-01-19 15:18:19 +08:00
|
|
|
'core.workspace.showUnreadForAllCategories',
|
|
|
|
@_updateSections
|
|
|
|
)
|
2016-02-06 10:36:48 +08:00
|
|
|
|
2016-01-19 15:18:19 +08:00
|
|
|
return
|
|
|
|
|
2016-05-20 03:04:13 +08:00
|
|
|
_onSetCollapsedByKey: (itemKey, collapsed) =>
|
|
|
|
currentValue = NylasEnv.savedState.sidebarKeysCollapsed[itemKey]
|
|
|
|
if currentValue isnt collapsed
|
|
|
|
NylasEnv.savedState.sidebarKeysCollapsed[itemKey] = collapsed
|
|
|
|
@_updateSections()
|
|
|
|
|
|
|
|
_onSetCollapsedByName: (itemName, collapsed) =>
|
|
|
|
item = _.findWhere(@standardSection().items, {name: itemName})
|
|
|
|
if not item
|
|
|
|
for section in @userSections()
|
|
|
|
item = _.findWhere(section.items, {name: itemName})
|
|
|
|
break if item
|
|
|
|
return unless item
|
|
|
|
@_onSetCollapsedByKey(item.id, collapsed)
|
2016-02-06 10:36:48 +08:00
|
|
|
|
2016-02-06 06:24:07 +08:00
|
|
|
_registerCommands: (accounts = AccountStore.accounts()) =>
|
|
|
|
AccountCommands.registerCommands(accounts)
|
|
|
|
|
|
|
|
_registerMenuItems: (accounts = AccountStore.accounts()) =>
|
|
|
|
AccountCommands.registerMenuItems(accounts, @_focusedAccounts)
|
|
|
|
|
2016-06-02 08:13:07 +08:00
|
|
|
# TODO Refactor this
|
|
|
|
# Listen to changes on the account store only for when the account label
|
|
|
|
# changes. When accounts or added or removed, those changes will come in
|
|
|
|
# through the FocusedPerspectiveStore
|
2016-01-30 02:21:16 +08:00
|
|
|
_onAccountsChanged: =>
|
|
|
|
@_updateSections()
|
|
|
|
|
2016-06-02 08:13:07 +08:00
|
|
|
# TODO Refactor this
|
|
|
|
# The FocusedPerspectiveStore tells this store the accounts that should be
|
|
|
|
# displayed in the sidebar (i.e. unified inbox vs single account) and will
|
|
|
|
# trigger whenever an account is added or removed, as well as when a
|
|
|
|
# perspective is focused.
|
|
|
|
# However, when udpating the SidebarSections, we also depend on the actual
|
|
|
|
# accounts in the AccountStore. The problem is that the FocusedPerspectiveStore
|
|
|
|
# triggers before the AccountStore is actually updated, so we need to wait for
|
|
|
|
# the AccountStore to get updated (via `defer`) before updateing our sidebar
|
|
|
|
# sections
|
2016-02-03 02:56:09 +08:00
|
|
|
_onFocusedPerspectiveChanged: =>
|
2016-06-02 08:13:07 +08:00
|
|
|
_.defer =>
|
|
|
|
@_focusedAccounts = FocusedPerspectiveStore.sidebarAccounts()
|
|
|
|
@_registerCommands()
|
2016-02-06 06:24:07 +08:00
|
|
|
@_registerMenuItems()
|
2016-06-02 08:13:07 +08:00
|
|
|
@_updateSections()
|
2016-02-03 02:56:09 +08:00
|
|
|
|
2016-01-29 06:59:17 +08:00
|
|
|
_updateSections: =>
|
2016-01-22 06:12:56 +08:00
|
|
|
accounts = @_focusedAccounts
|
2016-01-21 09:09:05 +08:00
|
|
|
multiAccount = accounts.length > 1
|
2016-01-20 15:42:50 +08:00
|
|
|
|
2016-01-19 15:18:19 +08:00
|
|
|
@_sections[Sections.Standard] = SidebarSection.standardSectionForAccounts(accounts)
|
|
|
|
@_sections[Sections.User] = accounts.map (acc) ->
|
2016-01-21 09:09:05 +08:00
|
|
|
opts = {}
|
|
|
|
if multiAccount
|
|
|
|
opts.title = acc.label
|
|
|
|
opts.collapsible = true
|
|
|
|
SidebarSection.forUserCategories(acc, opts)
|
2016-01-19 15:18:19 +08:00
|
|
|
@trigger()
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = new SidebarStore()
|