mirror of
https://github.com/Foundry376/Mailspring.git
synced 2025-10-11 05:36:36 +08:00
Fix CategoryStore sorting:
- Also fix Category model specs
This commit is contained in:
parent
fe625d8f6d
commit
c5d4b10c7d
3 changed files with 41 additions and 15 deletions
|
@ -2,12 +2,11 @@
|
||||||
|
|
||||||
describe 'Category', ->
|
describe 'Category', ->
|
||||||
|
|
||||||
describe '_initCategoryTypes', ->
|
describe 'category types', ->
|
||||||
|
|
||||||
it 'assigns type correctly when it is a user category', ->
|
it 'assigns type correctly when it is a user category', ->
|
||||||
cat = new Label
|
cat = new Label
|
||||||
cat.name = undefined
|
cat.name = undefined
|
||||||
cat._initCategoryTypes()
|
|
||||||
expect(cat.isUserCategory()).toBe true
|
expect(cat.isUserCategory()).toBe true
|
||||||
expect(cat.isStandardCategory()).toBe false
|
expect(cat.isStandardCategory()).toBe false
|
||||||
expect(cat.isHiddenCategory()).toBe false
|
expect(cat.isHiddenCategory()).toBe false
|
||||||
|
@ -16,7 +15,6 @@ describe 'Category', ->
|
||||||
it 'assigns type correctly when it is a standard category', ->
|
it 'assigns type correctly when it is a standard category', ->
|
||||||
cat = new Label
|
cat = new Label
|
||||||
cat.name = 'inbox'
|
cat.name = 'inbox'
|
||||||
cat._initCategoryTypes()
|
|
||||||
expect(cat.isUserCategory()).toBe false
|
expect(cat.isUserCategory()).toBe false
|
||||||
expect(cat.isStandardCategory()).toBe true
|
expect(cat.isStandardCategory()).toBe true
|
||||||
expect(cat.isHiddenCategory()).toBe false
|
expect(cat.isHiddenCategory()).toBe false
|
||||||
|
@ -25,7 +23,6 @@ describe 'Category', ->
|
||||||
it 'assigns type for `important` category when should not show important', ->
|
it 'assigns type for `important` category when should not show important', ->
|
||||||
cat = new Label
|
cat = new Label
|
||||||
cat.name = 'important'
|
cat.name = 'important'
|
||||||
cat._initCategoryTypes()
|
|
||||||
expect(cat.isUserCategory()).toBe false
|
expect(cat.isUserCategory()).toBe false
|
||||||
expect(cat.isStandardCategory(false)).toBe false
|
expect(cat.isStandardCategory(false)).toBe false
|
||||||
expect(cat.isHiddenCategory()).toBe true
|
expect(cat.isHiddenCategory()).toBe true
|
||||||
|
@ -34,7 +31,6 @@ describe 'Category', ->
|
||||||
it 'assigns type correctly when it is a hidden category', ->
|
it 'assigns type correctly when it is a hidden category', ->
|
||||||
cat = new Label
|
cat = new Label
|
||||||
cat.name = 'archive'
|
cat.name = 'archive'
|
||||||
cat._initCategoryTypes()
|
|
||||||
expect(cat.isUserCategory()).toBe false
|
expect(cat.isUserCategory()).toBe false
|
||||||
expect(cat.isStandardCategory()).toBe true
|
expect(cat.isStandardCategory()).toBe true
|
||||||
expect(cat.isHiddenCategory()).toBe true
|
expect(cat.isHiddenCategory()).toBe true
|
||||||
|
@ -43,8 +39,7 @@ describe 'Category', ->
|
||||||
it 'assigns type correctly when it is a locked category', ->
|
it 'assigns type correctly when it is a locked category', ->
|
||||||
cat = new Label
|
cat = new Label
|
||||||
cat.name = 'sent'
|
cat.name = 'sent'
|
||||||
cat._initCategoryTypes()
|
|
||||||
expect(cat.isUserCategory()).toBe false
|
expect(cat.isUserCategory()).toBe false
|
||||||
expect(cat.isStandardCategory()).toBe true
|
expect(cat.isStandardCategory()).toBe true
|
||||||
expect(cat.isHiddenCategory()).toBe true
|
expect(cat.isHiddenCategory()).toBe true
|
||||||
expect(cat.isLockedCategory()).toBe false
|
expect(cat.isLockedCategory()).toBe true
|
||||||
|
|
|
@ -9,11 +9,14 @@ class CategoryStore extends NylasStore
|
||||||
|
|
||||||
constructor: ->
|
constructor: ->
|
||||||
@_categoryCache = {}
|
@_categoryCache = {}
|
||||||
|
@_standardCategories = {}
|
||||||
|
@_userCategories = {}
|
||||||
|
@_hiddenCategories = {}
|
||||||
@_registerObservables(AccountStore.accounts())
|
@_registerObservables(AccountStore.accounts())
|
||||||
|
|
||||||
@listenTo AccountStore, @_onAccountsChanged
|
@listenTo AccountStore, @_onAccountsChanged
|
||||||
|
|
||||||
byId: (account, categoryId) -> @categories(account)[categoryId]
|
byId: (account, categoryId) ->
|
||||||
|
@categories(account)[categoryId]
|
||||||
|
|
||||||
# Public: Returns an array of all categories for an account, both
|
# Public: Returns an array of all categories for an account, both
|
||||||
# standard and user generated. The items returned by this function will be
|
# standard and user generated. The items returned by this function will be
|
||||||
|
@ -31,16 +34,21 @@ class CategoryStore extends NylasStore
|
||||||
# Public: Returns all of the standard categories for the current account.
|
# Public: Returns all of the standard categories for the current account.
|
||||||
#
|
#
|
||||||
standardCategories: (account) ->
|
standardCategories: (account) ->
|
||||||
_.values(@categories(account)).filter (cat) -> cat.isStandardCategory()
|
return [] unless account
|
||||||
|
_.compact(
|
||||||
|
StandardCategoryNames.map (name) => @_standardCategories[account.id][name]
|
||||||
|
)
|
||||||
|
|
||||||
hiddenCategories: (account) ->
|
hiddenCategories: (account) ->
|
||||||
_.values(@categories(account)).filter (cat) -> cat.isHiddenCategory()
|
return [] unless account
|
||||||
|
@_hiddenCategories[account.id]
|
||||||
|
|
||||||
# Public: Returns all of the categories that are not part of the standard
|
# Public: Returns all of the categories that are not part of the standard
|
||||||
# category set.
|
# category set.
|
||||||
#
|
#
|
||||||
userCategories: (account) ->
|
userCategories: (account) ->
|
||||||
_.values(@categories(account)).filter (cat) -> cat.isUserCategory()
|
return [] unless account
|
||||||
|
@_userCategories[account.id]
|
||||||
|
|
||||||
# Public: Returns the Folder or Label object for a standard category name and
|
# Public: Returns the Folder or Label object for a standard category name and
|
||||||
# for a given account.
|
# for a given account.
|
||||||
|
@ -71,20 +79,43 @@ class CategoryStore extends NylasStore
|
||||||
@getStandardCategory(account, "trash")
|
@getStandardCategory(account, "trash")
|
||||||
|
|
||||||
_onAccountsChanged: ->
|
_onAccountsChanged: ->
|
||||||
@_registerObservables(AccountStore.accounts())
|
accounts = AccountStore.accounts()
|
||||||
|
@_removeStaleCategories(accounts)
|
||||||
|
@_registerObservables(accounts)
|
||||||
|
|
||||||
_onCategoriesChanged: (accountId, categories) =>
|
_onCategoriesChanged: (accountId, categories) =>
|
||||||
return unless categories
|
return unless categories
|
||||||
@_categoryCache[accountId] = {}
|
@_categoryCache[accountId] = {}
|
||||||
|
@_standardCategories[accountId] = {}
|
||||||
|
@_userCategories[accountId] = []
|
||||||
|
@_hiddenCategories[accountId] = []
|
||||||
|
|
||||||
for category in categories
|
for category in categories
|
||||||
@_categoryCache[accountId][category.id] = category
|
@_categoryCache[accountId][category.id] = category
|
||||||
|
if category.isStandardCategory()
|
||||||
|
@_standardCategories[accountId][category.name] = category
|
||||||
|
if category.isUserCategory()
|
||||||
|
@_userCategories[accountId].push(category)
|
||||||
|
if category.isHiddenCategory()
|
||||||
|
@_hiddenCategories[accountId].push(category)
|
||||||
@trigger()
|
@trigger()
|
||||||
|
|
||||||
|
# Remove any category sets for removed accounts
|
||||||
|
# Will prevent memory leaks
|
||||||
|
_removeStaleCategories: (accounts) ->
|
||||||
|
accountIds = accounts.map (acc) -> acc.id
|
||||||
|
removedAccountIds = _.difference(_.keys(@_categoryCache), accountIds)
|
||||||
|
for accountId in removedAccountIds
|
||||||
|
delete @_categoryCache[accountId]
|
||||||
|
delete @_standardCategories[accountId]
|
||||||
|
delete @_userCategories[accountId]
|
||||||
|
delete @_hiddenCategories[accountId]
|
||||||
|
|
||||||
_registerObservables: (accounts) =>
|
_registerObservables: (accounts) =>
|
||||||
@_disposables ?= []
|
@_disposables ?= []
|
||||||
@_disposables.forEach (disp) -> disp.dispose()
|
@_disposables.forEach (disp) -> disp.dispose()
|
||||||
@_disposables = accounts.map (account) =>
|
@_disposables = accounts.map (account) =>
|
||||||
Categories.forAccount(account)
|
Categories.forAccount(account).sort()
|
||||||
.subscribe(@_onCategoriesChanged.bind(@, account.id))
|
.subscribe(@_onCategoriesChanged.bind(@, account.id))
|
||||||
|
|
||||||
module.exports = new CategoryStore()
|
module.exports = new CategoryStore()
|
||||||
|
|
Loading…
Add table
Reference in a new issue