2016-01-19 15:18:19 +08:00
|
|
|
_ = require 'underscore'
|
|
|
|
{Actions,
|
2016-01-20 15:42:50 +08:00
|
|
|
AccountStore,
|
2016-01-19 15:18:19 +08:00
|
|
|
SyncbackCategoryTask,
|
|
|
|
DestroyCategoryTask,
|
|
|
|
CategoryStore,
|
|
|
|
Category} = require 'nylas-exports'
|
|
|
|
SidebarItem = require './sidebar-item'
|
2016-02-06 10:36:48 +08:00
|
|
|
SidebarActions = require './sidebar-actions'
|
2016-01-19 15:18:19 +08:00
|
|
|
|
2016-02-06 10:36:48 +08:00
|
|
|
isSectionCollapsed = (title) ->
|
|
|
|
if NylasEnv.savedState.sidebarKeysCollapsed[title] isnt undefined
|
|
|
|
NylasEnv.savedState.sidebarKeysCollapsed[title]
|
|
|
|
else
|
|
|
|
false
|
2016-01-19 15:18:19 +08:00
|
|
|
|
2016-02-06 10:36:48 +08:00
|
|
|
toggleSectionCollapsed = (section) ->
|
2016-01-21 11:22:33 +08:00
|
|
|
return unless section
|
2016-02-06 10:36:48 +08:00
|
|
|
SidebarActions.setKeyCollapsed(section.title, not isSectionCollapsed(section.title))
|
2016-01-21 11:22:33 +08:00
|
|
|
|
2016-01-19 15:18:19 +08:00
|
|
|
class SidebarSection
|
|
|
|
|
2016-01-29 06:59:17 +08:00
|
|
|
@empty: (title) ->
|
2016-01-19 15:18:19 +08:00
|
|
|
return {
|
|
|
|
title,
|
|
|
|
items: []
|
|
|
|
}
|
|
|
|
|
|
|
|
@standardSectionForAccount: (account) ->
|
2016-01-28 03:14:11 +08:00
|
|
|
if not account
|
|
|
|
throw new Error("standardSectionForAccount: You must pass an account.")
|
|
|
|
|
2016-01-19 15:18:19 +08:00
|
|
|
cats = CategoryStore.standardCategories(account)
|
2016-02-05 06:48:15 +08:00
|
|
|
return @empty(account.label) if cats.length is 0
|
2016-01-28 03:14:11 +08:00
|
|
|
|
2016-01-19 15:18:19 +08:00
|
|
|
items = _
|
|
|
|
.reject(cats, (cat) -> cat.name is 'drafts')
|
2016-01-29 08:20:46 +08:00
|
|
|
.map (cat) => SidebarItem.forCategories([cat], editable: false, deletable: false)
|
2016-01-19 15:18:19 +08:00
|
|
|
|
|
|
|
starredItem = SidebarItem.forStarred([account.id])
|
2016-01-26 09:08:38 +08:00
|
|
|
draftsItem = SidebarItem.forDrafts([account.id])
|
2016-02-25 02:50:21 +08:00
|
|
|
snoozedItem = SidebarItem.forSnoozed([account.id])
|
2016-01-19 15:18:19 +08:00
|
|
|
|
|
|
|
# Order correctly: Inbox, Starred, rest... , Drafts
|
2016-02-25 02:50:21 +08:00
|
|
|
items.splice(1, 0, starredItem, snoozedItem)
|
2016-01-19 15:18:19 +08:00
|
|
|
items.push(draftsItem)
|
|
|
|
|
|
|
|
return {
|
2016-02-05 06:48:15 +08:00
|
|
|
title: account.label
|
2016-01-19 15:18:19 +08:00
|
|
|
items: items
|
|
|
|
}
|
|
|
|
|
|
|
|
@standardSectionForAccounts: (accounts) ->
|
2016-02-05 06:48:15 +08:00
|
|
|
return @empty('All Accounts') if not accounts or accounts.length is 0
|
|
|
|
return @empty('All Accounts') if CategoryStore.categories().length is 0
|
2016-01-19 15:18:19 +08:00
|
|
|
return @standardSectionForAccount(accounts[0]) if accounts.length is 1
|
|
|
|
|
2016-01-20 15:42:50 +08:00
|
|
|
standardNames = [
|
|
|
|
'inbox',
|
|
|
|
'sent',
|
|
|
|
['archive', 'all'],
|
|
|
|
'trash'
|
|
|
|
]
|
|
|
|
items = []
|
|
|
|
|
|
|
|
for names in standardNames
|
|
|
|
names = if Array.isArray(names) then names else [names]
|
|
|
|
categories = CategoryStore.getStandardCategories(accounts, names...)
|
|
|
|
continue if categories.length is 0
|
|
|
|
|
2016-01-28 03:14:11 +08:00
|
|
|
children = []
|
|
|
|
accounts.forEach (acc) ->
|
2016-01-20 15:42:50 +08:00
|
|
|
cat = _.first(_.compact(
|
|
|
|
names.map((name) -> CategoryStore.getStandardCategory(acc, name))
|
|
|
|
))
|
2016-01-28 03:14:11 +08:00
|
|
|
return unless cat
|
2016-01-29 08:20:46 +08:00
|
|
|
children.push(SidebarItem.forCategories([cat], name: acc.label, editable: false, deletable: false))
|
2016-01-20 15:42:50 +08:00
|
|
|
|
2016-01-29 08:20:46 +08:00
|
|
|
items.push SidebarItem.forCategories(categories, {children, editable: false, deletable: false})
|
2016-01-20 15:42:50 +08:00
|
|
|
|
2016-02-25 02:50:21 +08:00
|
|
|
accountIds = _.pluck(accounts, 'id')
|
|
|
|
|
|
|
|
starredItem = SidebarItem.forStarred(accountIds,
|
2016-01-19 15:18:19 +08:00
|
|
|
children: accounts.map (acc) -> SidebarItem.forStarred([acc.id], name: acc.label)
|
|
|
|
)
|
2016-02-25 02:50:21 +08:00
|
|
|
draftsItem = SidebarItem.forDrafts(accountIds,
|
2016-01-26 08:36:56 +08:00
|
|
|
children: accounts.map (acc) -> SidebarItem.forDrafts([acc.id], name: acc.label)
|
2016-01-19 15:18:19 +08:00
|
|
|
)
|
2016-02-25 02:50:21 +08:00
|
|
|
snoozedItem = SidebarItem.forSnoozed(accountIds,
|
|
|
|
children: accounts.map (acc) -> SidebarItem.forSnoozed([acc.id], name: acc.label)
|
|
|
|
)
|
2016-01-19 15:18:19 +08:00
|
|
|
|
2016-01-20 15:42:50 +08:00
|
|
|
# Order correctly: Inbox, Starred, rest... , Drafts
|
2016-02-25 02:50:21 +08:00
|
|
|
items.splice(1, 0, starredItem, snoozedItem)
|
2016-01-20 15:42:50 +08:00
|
|
|
items.push(draftsItem)
|
2016-01-19 15:18:19 +08:00
|
|
|
|
|
|
|
return {
|
2016-02-05 06:48:15 +08:00
|
|
|
title: 'All Accounts'
|
2016-01-19 15:18:19 +08:00
|
|
|
items: items
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-21 09:09:05 +08:00
|
|
|
@forUserCategories: (account, {title, collapsible} = {}) ->
|
2016-01-19 15:18:19 +08:00
|
|
|
return unless account
|
|
|
|
# Compute hierarchy for user categories using known "path" separators
|
|
|
|
# NOTE: This code uses the fact that userCategoryItems is a sorted set, eg:
|
|
|
|
#
|
|
|
|
# Inbox
|
|
|
|
# Inbox.FolderA
|
|
|
|
# Inbox.FolderA.FolderB
|
|
|
|
# Inbox.FolderB
|
|
|
|
#
|
|
|
|
items = []
|
|
|
|
seenItems = {}
|
|
|
|
for category in CategoryStore.userCategories(account)
|
|
|
|
# https://regex101.com/r/jK8cC2/1
|
|
|
|
itemKey = category.displayName.replace(/[./\\]/g, '/')
|
|
|
|
|
|
|
|
parent = null
|
|
|
|
parentComponents = itemKey.split('/')
|
|
|
|
for i in [parentComponents.length..1] by -1
|
|
|
|
parentKey = parentComponents[0...i].join('/')
|
|
|
|
parent = seenItems[parentKey]
|
|
|
|
break if parent
|
|
|
|
|
|
|
|
if parent
|
|
|
|
itemDisplayName = category.displayName.substr(parentKey.length+1)
|
|
|
|
item = SidebarItem.forCategories([category], name: itemDisplayName)
|
|
|
|
parent.children.push(item)
|
|
|
|
else
|
|
|
|
item = SidebarItem.forCategories([category])
|
|
|
|
items.push(item)
|
|
|
|
seenItems[itemKey] = item
|
|
|
|
|
2016-01-21 09:09:05 +08:00
|
|
|
|
|
|
|
title ?= account.categoryLabel()
|
2016-01-21 11:22:33 +08:00
|
|
|
collapsed = isSectionCollapsed(title)
|
2016-01-21 09:09:05 +08:00
|
|
|
if collapsible
|
2016-02-06 10:36:48 +08:00
|
|
|
onCollapseToggled = toggleSectionCollapsed
|
2016-01-21 09:09:05 +08:00
|
|
|
|
2016-01-19 15:18:19 +08:00
|
|
|
return {
|
2016-01-21 09:09:05 +08:00
|
|
|
title: title
|
|
|
|
iconName: account.categoryIcon()
|
2016-01-19 15:18:19 +08:00
|
|
|
items: items
|
2016-01-21 09:09:05 +08:00
|
|
|
collapsed: collapsed
|
2016-01-29 07:26:58 +08:00
|
|
|
onCollapseToggled: onCollapseToggled
|
2016-01-28 15:16:17 +08:00
|
|
|
onItemCreated: (displayName) ->
|
2016-01-28 16:02:55 +08:00
|
|
|
return unless displayName
|
2016-01-19 15:18:19 +08:00
|
|
|
category = new Category
|
|
|
|
displayName: displayName
|
|
|
|
accountId: account.id
|
|
|
|
Actions.queueTask(new SyncbackCategoryTask({category}))
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = SidebarSection
|