Mailspring/internal_packages/account-sidebar/lib/account-sidebar-store.coffee
Ben Gotow df38008c56 fix(*): Small fixes from Lake Tahoe. See Summary.
Summary:
This diff includes a few small things:

- Menu: Don't select the first item until the user taps down arrow, and allow the user to use the arrow keys to move up and down through Menu items.

- Menu: Make scroll code from MultiselectList re-usable, use in Menu. Now if you use the keys to move to an item that is offscreen it will follow.

- Popover: Tapping the button that opened popover should close it

- Make sure buttons in toolbars are at least standard height

- Re-enable Markdown processing via `grunt docs`

- A bit of initial inline documentation for crosjdoc. Need to evaluate whether this is worth doing everywhere.

- New `search-playground` package for experimenting with search and search weights.

- Swap itemClassProvider for more generic itemPropProvider

- Add crojsdoc config file

- Export React, because third party packages can't require things from our app

- [FEATURE] Bring back static file support in third party packages via `nylas://translate/IMG_20150417_124142.jpg`

- Fix invariant error with search bar

- [FEATURE] "Show Original" under Message actions

- Fix DatabaseView so that many archives at once don't cause problems

Test Plan: Run specs

Reviewers: evan

Reviewed By: evan

Differential Revision: https://review.inboxapp.com/D1426
2015-04-22 16:41:29 -07:00

130 lines
3.6 KiB
CoffeeScript

Reflux = require 'reflux'
_ = require 'underscore-plus'
{DatabaseStore,
NamespaceStore,
WorkspaceStore,
Actions,
Tag,
Message,
FocusedTagStore,
Thread} = require 'inbox-exports'
AccountSidebarStore = Reflux.createStore
init: ->
@_setStoreDefaults()
@_registerListeners()
@_populate()
########### PUBLIC #####################################################
sections: ->
@_sections
selected: ->
if WorkspaceStore.rootSheet() is WorkspaceStore.Sheet.Threads
FocusedTagStore.tag()
else
WorkspaceStore.rootSheet()
########### PRIVATE ####################################################
_setStoreDefaults: ->
@_sections = []
_registerListeners: ->
@listenTo DatabaseStore, @_onDataChanged
@listenTo NamespaceStore, @_onNamespaceChanged
@listenTo WorkspaceStore, @_onWorkspaceChanged
@listenTo FocusedTagStore, @_onFocusChange
_populate: ->
namespace = NamespaceStore.current()
return unless namespace
DatabaseStore.findAll(Tag, namespaceId: namespace.id).then (tags) =>
# Collect the built-in tags we want to display, and the user tags
# (which can be identified by having non-hardcoded IDs)
# We ignore the server drafts so we can use our own localDrafts
tags = _.reject tags, (tag) -> tag.id is "drafts"
# We ignore the trash tag because you can't trash anything
tags = _.reject tags, (tag) -> tag.id is "trash"
mainTagIDs = ['inbox', 'drafts', 'sent', 'archive']
mainTags = _.filter tags, (tag) -> _.contains(mainTagIDs, tag.id)
userTags = _.reject tags, (tag) -> _.contains(mainTagIDs, tag.id)
# Sort the main tags so they always appear in a standard order
mainTags = _.sortBy mainTags, (tag) -> mainTagIDs.indexOf(tag.id)
mainTags.push new Tag(name: 'All Mail', id: '*')
# Sort user tags by name
userTags = _.sortBy(userTags, 'name')
# Find root views, add the Views section
rootSheets = _.filter WorkspaceStore.Sheet, (sheet) -> sheet.root and sheet.name
lastSections = @_sections
@_sections = [
{ label: 'Mailboxes', items: mainTags, type: 'tag' },
{ label: 'Views', items: rootSheets, type: 'sheet' },
{ label: 'Tags', items: userTags, type: 'tag' },
]
@trigger(@)
_populateInboxCount: ->
namespace = NamespaceStore.current()
return unless namespace
# Make a web request for unread count
atom.inbox.makeRequest
method: 'GET'
path: "/n/#{namespace.id}/tags/inbox"
returnsModel: true
_populateDraftCount: ->
namespace = NamespaceStore.current()
return unless namespace
DatabaseStore.count(Message, draft: true).then (count) =>
#TODO: Save Draft Count
@trigger(@)
_refetchFromAPI: ->
namespace = NamespaceStore.current()
return unless namespace
atom.inbox.getCollection(namespace.id, 'tags')
# Inbound Events
_onNamespaceChanged: ->
@_refetchFromAPI()
@_populateInboxCount()
@_populate()
_onWorkspaceChanged: ->
@_populate()
_onFocusChange: ->
@trigger(@)
_onDataChanged: (change) ->
@populateInboxCountDebounced ?= _.debounce ->
@_populateInboxCount()
, 1000
@populateDraftCountDebounced ?= _.debounce ->
@_populateDraftCount()
, 1000
if change.objectClass is Tag.name
@_populate()
if change.objectClass is Thread.name
@populateInboxCountDebounced()
if change.objectClass is Message.name
return unless _.some change.objects, (msg) -> msg.draft
@populateDraftCountDebounced()
module.exports = AccountSidebarStore