2016-01-19 15:18:19 +08:00
|
|
|
_ = require 'underscore'
|
|
|
|
{WorkspaceStore,
|
|
|
|
MailboxPerspective,
|
|
|
|
FocusedPerspectiveStore,
|
|
|
|
DraftCountStore,
|
|
|
|
DestroyCategoryTask,
|
|
|
|
Actions} = require 'nylas-exports'
|
|
|
|
{OutlineViewItem} = require 'nylas-component-kit'
|
|
|
|
|
|
|
|
|
|
|
|
idForCategories = (categories) ->
|
|
|
|
categories.map((cat) -> cat.id).join('-')
|
|
|
|
|
|
|
|
countForItem = (perspective) ->
|
|
|
|
unreadCountEnabled = NylasEnv.config.get('core.workspace.showUnreadForAllCategories')
|
|
|
|
if perspective.isInbox() or unreadCountEnabled
|
|
|
|
return perspective.threadUnreadCount()
|
|
|
|
return 0
|
|
|
|
|
|
|
|
isItemSelected = (perspective) ->
|
|
|
|
(WorkspaceStore.rootSheet() is WorkspaceStore.Sheet.Threads and
|
|
|
|
FocusedPerspectiveStore.current().isEqual(perspective))
|
|
|
|
|
|
|
|
isItemDeleted = (perspective) ->
|
|
|
|
_.any perspective.categories(), (c) -> c.isDeleted
|
|
|
|
|
|
|
|
isItemCollapsed = (id) ->
|
|
|
|
key = "core.accountSidebarCollapsed.#{id}"
|
|
|
|
NylasEnv.config.get(key)
|
|
|
|
|
2016-01-20 02:50:33 +08:00
|
|
|
toggleItemCollapsed = (item) ->
|
|
|
|
return unless item.children.length > 0
|
|
|
|
key = "core.accountSidebarCollapsed.#{item.id}"
|
|
|
|
NylasEnv.config.set(key, not item.collapsed)
|
|
|
|
|
2016-01-19 15:18:19 +08:00
|
|
|
|
|
|
|
class SidebarItem
|
|
|
|
|
|
|
|
@forPerspective: (id, perspective, {children, deletable, name} = {}) ->
|
|
|
|
children ?= []
|
|
|
|
counterStyle = OutlineViewItem.CounterStyles.Alt if perspective.isInbox()
|
|
|
|
dataTransferType = 'nylas-thread-ids'
|
|
|
|
|
|
|
|
if deletable
|
|
|
|
onDeleteItem = (item) ->
|
|
|
|
# TODO Delete multiple categories at once
|
|
|
|
return if item.perspective.categories.length > 1
|
|
|
|
return if item.deleted is true
|
|
|
|
category = item.perspective.categories[0]
|
|
|
|
Actions.queueTask(new DestroyCategoryTask({category: category}))
|
|
|
|
|
|
|
|
return {
|
|
|
|
id: id
|
|
|
|
name: name ? perspective.name
|
|
|
|
count: countForItem(perspective)
|
|
|
|
iconName: perspective.iconName
|
|
|
|
children: children
|
|
|
|
perspective: perspective
|
|
|
|
selected: isItemSelected(perspective)
|
2016-01-21 11:22:33 +08:00
|
|
|
collapsed: isItemCollapsed(id) ? true
|
2016-01-19 15:18:19 +08:00
|
|
|
deleted: isItemDeleted(perspective)
|
|
|
|
counterStyle: counterStyle
|
|
|
|
dataTransferType: dataTransferType
|
|
|
|
onDelete: onDeleteItem
|
2016-01-20 02:50:33 +08:00
|
|
|
onToggleCollapsed: toggleItemCollapsed
|
2016-01-19 15:18:19 +08:00
|
|
|
onDrop: (item, ids) ->
|
|
|
|
return unless ids
|
|
|
|
item.perspective.applyToThreads(ids)
|
|
|
|
shouldAcceptDrop: (item, event) ->
|
|
|
|
perspective = item.perspective
|
|
|
|
return false unless perspective
|
|
|
|
return false if perspective.isEqual(FocusedPerspectiveStore.current())
|
|
|
|
return false unless perspective.canApplyToThreads()
|
|
|
|
item.dataTransferType in event.dataTransfer.types
|
|
|
|
onSelect: (item) ->
|
|
|
|
Actions.selectRootSheet(WorkspaceStore.Sheet.Threads)
|
|
|
|
Actions.focusMailboxPerspective(item.perspective)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@forCategories: (categories = [], opts = {}) ->
|
|
|
|
id = idForCategories(categories)
|
|
|
|
perspective = MailboxPerspective.forCategories(categories)
|
|
|
|
@forPerspective(id, perspective, opts)
|
|
|
|
|
|
|
|
@forStarred: (accountIds, opts = {}) ->
|
|
|
|
perspective = MailboxPerspective.forStarred(accountIds)
|
|
|
|
id = 'Starred'
|
|
|
|
id += "-#{opts.name}" if opts.name
|
|
|
|
@forPerspective(id, perspective, opts)
|
|
|
|
|
2016-01-21 11:22:33 +08:00
|
|
|
@forSheet: (id, name, iconName, sheet, count, collapsed, children = []) ->
|
2016-01-19 15:18:19 +08:00
|
|
|
return {
|
|
|
|
id,
|
|
|
|
name,
|
|
|
|
iconName,
|
|
|
|
count,
|
|
|
|
sheet,
|
|
|
|
children,
|
2016-01-21 11:22:33 +08:00
|
|
|
collapsed: isItemCollapsed(id) ? true
|
2016-01-20 02:50:33 +08:00
|
|
|
onToggleCollapsed: toggleItemCollapsed
|
2016-01-19 15:18:19 +08:00
|
|
|
onSelect: (item) ->
|
|
|
|
Actions.selectRootSheet(item.sheet)
|
|
|
|
}
|
|
|
|
|
2016-01-21 11:22:33 +08:00
|
|
|
@forDrafts: ({accountId, name, children, collapsed} = {}) ->
|
2016-01-20 02:50:33 +08:00
|
|
|
id = 'Drafts'
|
|
|
|
id += "-#{name}" if name
|
2016-01-19 15:18:19 +08:00
|
|
|
sheet = WorkspaceStore.Sheet.Drafts
|
|
|
|
iconName = 'drafts.png'
|
|
|
|
count = if accountId?
|
|
|
|
DraftCountStore.count(accountId)
|
|
|
|
else
|
|
|
|
DraftCountStore.totalCount()
|
2016-01-21 11:22:33 +08:00
|
|
|
@forSheet(id, name ? id, iconName, sheet, count, collapsed, children)
|
2016-01-19 15:18:19 +08:00
|
|
|
|
|
|
|
|
|
|
|
module.exports = SidebarItem
|