feat(perspective): Restore through NylasEnv.savedState

This commit is contained in:
Ben Gotow 2016-02-05 18:36:48 -08:00
parent e6a2bf3f4f
commit ca31ee10bd
11 changed files with 77 additions and 33 deletions

View file

@ -12,8 +12,10 @@ class AccountCommands
@_isSelected: (account, focusedAccounts) => @_isSelected: (account, focusedAccounts) =>
if focusedAccounts.length > 1 if focusedAccounts.length > 1
return account instanceof Array return account instanceof Array
else else if focusedAccounts.length is 1
return account?.id is focusedAccounts[0].id return account?.id is focusedAccounts[0].id
else
return false
@registerCommands: (accounts) -> @registerCommands: (accounts) ->
@_commandsDisposable?.dispose() @_commandsDisposable?.dispose()

View file

@ -1,7 +1,8 @@
Reflux = require 'reflux' Reflux = require 'reflux'
Actions = [ Actions = [
'focusAccounts' 'focusAccounts',
'setKeyCollapsed',
] ]
for idx in Actions for idx in Actions

View file

@ -9,6 +9,7 @@ _str = require 'underscore.string'
Utils} = require 'nylas-exports' Utils} = require 'nylas-exports'
{OutlineViewItem} = require 'nylas-component-kit' {OutlineViewItem} = require 'nylas-component-kit'
SidebarActions = require './sidebar-actions'
idForCategories = (categories) -> idForCategories = (categories) ->
_.pluck(categories, 'id').join('-') _.pluck(categories, 'id').join('-')
@ -27,13 +28,14 @@ isItemDeleted = (perspective) ->
_.any perspective.categories(), (c) -> c.isDeleted _.any perspective.categories(), (c) -> c.isDeleted
isItemCollapsed = (id) -> isItemCollapsed = (id) ->
key = "core.accountSidebarCollapsed.#{id}" if NylasEnv.savedState.sidebarKeysCollapsed[id] isnt undefined
NylasEnv.config.get(key) NylasEnv.savedState.sidebarKeysCollapsed[id]
else
true
toggleItemCollapsed = (item) -> toggleItemCollapsed = (item) ->
return unless item.children.length > 0 return unless item.children.length > 0
key = "core.accountSidebarCollapsed.#{item.id}" SidebarActions.setKeyCollapsed(item.id, not isItemCollapsed(item.id))
NylasEnv.config.set(key, not item.collapsed)
onDeleteItem = (item) -> onDeleteItem = (item) ->
# TODO Delete multiple categories at once # TODO Delete multiple categories at once

View file

@ -6,17 +6,17 @@ _ = require 'underscore'
CategoryStore, CategoryStore,
Category} = require 'nylas-exports' Category} = require 'nylas-exports'
SidebarItem = require './sidebar-item' SidebarItem = require './sidebar-item'
SidebarActions = require './sidebar-actions'
isSectionCollapsed = (title) ->
if NylasEnv.savedState.sidebarKeysCollapsed[title] isnt undefined
NylasEnv.savedState.sidebarKeysCollapsed[title]
else
false
isSectionCollapsed = (id) -> toggleSectionCollapsed = (section) ->
key = "core.accountSidebarCollapsed.#{id}Section"
collapsed = NylasEnv.config.get(key)
toggleSectionCollapse = (section) ->
key = "core.accountSidebarCollapsed.#{section.title}Section"
return unless section return unless section
NylasEnv.config.set(key, not section.collapsed) SidebarActions.setKeyCollapsed(section.title, not isSectionCollapsed(section.title))
class SidebarSection class SidebarSection
@ -130,7 +130,7 @@ class SidebarSection
title ?= account.categoryLabel() title ?= account.categoryLabel()
collapsed = isSectionCollapsed(title) collapsed = isSectionCollapsed(title)
if collapsible if collapsible
onCollapseToggled = toggleSectionCollapse onCollapseToggled = toggleSectionCollapsed
return { return {
title: title title: title

View file

@ -20,10 +20,13 @@ Sections = {
class SidebarStore extends NylasStore class SidebarStore extends NylasStore
constructor: -> constructor: ->
NylasEnv.savedState.sidebarKeysCollapsed ?= {}
@_sections = {} @_sections = {}
@_sections[Sections.Standard] = {} @_sections[Sections.Standard] = {}
@_sections[Sections.User] = [] @_sections[Sections.User] = []
@_focusedAccounts = @accounts() @_focusedAccounts = FocusedPerspectiveStore.current().accountIds.map (id) ->
AccountStore.accountForId(id)
@_registerCommands() @_registerCommands()
@_registerMenuItems() @_registerMenuItems()
@_registerListeners() @_registerListeners()
@ -43,6 +46,7 @@ class SidebarStore extends NylasStore
_registerListeners: -> _registerListeners: ->
@listenTo SidebarActions.focusAccounts, @_onAccountsFocused @listenTo SidebarActions.focusAccounts, @_onAccountsFocused
@listenTo SidebarActions.setKeyCollapsed, @_onSetCollapsed
@listenTo AccountStore, @_onAccountsChanged @listenTo AccountStore, @_onAccountsChanged
@listenTo FocusedPerspectiveStore, @_onFocusedPerspectiveChanged @listenTo FocusedPerspectiveStore, @_onFocusedPerspectiveChanged
@listenTo WorkspaceStore, @_updateSections @listenTo WorkspaceStore, @_updateSections
@ -54,12 +58,13 @@ class SidebarStore extends NylasStore
'core.workspace.showUnreadForAllCategories', 'core.workspace.showUnreadForAllCategories',
@_updateSections @_updateSections
) )
@configSubscription = NylasEnv.config.onDidChange(
'core.accountSidebarCollapsed',
@_updateSections
)
return return
_onSetCollapsed: (key, collapsed) =>
NylasEnv.savedState.sidebarKeysCollapsed[key] = collapsed
@_updateSections()
_registerCommands: (accounts = AccountStore.accounts()) => _registerCommands: (accounts = AccountStore.accounts()) =>
AccountCommands.registerCommands(accounts) AccountCommands.registerCommands(accounts)

View file

@ -14,10 +14,9 @@ class SearchBar extends React.Component
@displayName = 'SearchBar' @displayName = 'SearchBar'
constructor: (@props) -> constructor: (@props) ->
@state = @state = _.extend({}, @_getStateFromStores(), {
query: ""
focused: false focused: false
suggestions: [] },)
componentDidMount: => componentDidMount: =>
@usub = [] @usub = []

View file

@ -19,7 +19,7 @@ SearchActions = require './search-actions'
class SearchSuggestionStore extends NylasStore class SearchSuggestionStore extends NylasStore
constructor: -> constructor: ->
@_searchQuery = "" @_searchQuery = FocusedPerspectiveStore.current().searchQuery ? ""
@_searchSuggestionsVersion = 1 @_searchSuggestionsVersion = 1
@_clearResults() @_clearResults()
@ -47,8 +47,11 @@ class SearchSuggestionStore extends NylasStore
Actions.focusMailboxPerspective(next) Actions.focusMailboxPerspective(next)
else if FocusedPerspectiveStore.current().searchQuery else if FocusedPerspectiveStore.current().searchQuery
Actions.focusMailboxPerspective(@_perspectiveBeforeSearch) if @_perspectiveBeforeSearch
@_perspectiveBeforeSearch = null Actions.focusMailboxPerspective(@_perspectiveBeforeSearch)
@_perspectiveBeforeSearch = null
else
Actions.focusDefaultMailboxPerspectiveForAccounts(AccountStore.accounts())
@_clearResults() @_clearResults()

View file

@ -3,8 +3,6 @@ Model = require './models/model'
DatabaseStore = require './stores/database-store' DatabaseStore = require './stores/database-store'
Utils = require './models/utils' Utils = require './models/utils'
TaskRegistry = require '../task-registry'
DatabaseObjectRegistry = require '../database-object-registry'
Role = Role =
WORK: 'work', WORK: 'work',

View file

@ -8,15 +8,14 @@ Actions = require '../actions'
class FocusedPerspectiveStore extends NylasStore class FocusedPerspectiveStore extends NylasStore
constructor: -> constructor: ->
@_current = @_defaultPerspective() if NylasEnv.savedState.perspective
@_current = MailboxPerspective.fromJSON(NylasEnv.savedState.perspective)
@_current ?= @_defaultPerspective()
@listenTo CategoryStore, @_onCategoryStoreChanged @listenTo CategoryStore, @_onCategoryStoreChanged
@listenTo Actions.focusMailboxPerspective, @_onFocusPerspective @listenTo Actions.focusMailboxPerspective, @_onFocusPerspective
@listenTo Actions.focusDefaultMailboxPerspectiveForAccounts, @_onFocusAccounts @listenTo Actions.focusDefaultMailboxPerspectiveForAccounts, @_onFocusAccounts
@_onCategoryStoreChanged()
# Inbound Events # Inbound Events
_onCategoryStoreChanged: -> _onCategoryStoreChanged: ->
@ -45,6 +44,7 @@ class FocusedPerspectiveStore extends NylasStore
_setPerspective: (perspective) -> _setPerspective: (perspective) ->
return if perspective?.isEqual(@_current) return if perspective?.isEqual(@_current)
NylasEnv.savedState.perspective = perspective.toJSON()
@_current = perspective @_current = perspective
@trigger() @trigger()

View file

@ -1,5 +1,6 @@
_ = require 'underscore' _ = require 'underscore'
Utils = require './flux/models/utils'
TaskFactory = require './flux/tasks/task-factory' TaskFactory = require './flux/tasks/task-factory'
AccountStore = require './flux/stores/account-store' AccountStore = require './flux/stores/account-store'
CategoryStore = require './flux/stores/category-store' CategoryStore = require './flux/stores/category-store'
@ -44,6 +45,22 @@ class MailboxPerspective
@forInbox: (accountsOrIds) => @forInbox: (accountsOrIds) =>
@forStandardCategories(accountsOrIds, 'inbox') @forStandardCategories(accountsOrIds, 'inbox')
@fromJSON: (json) =>
try
if json.type is CategoryMailboxPerspective.name
categories = JSON.parse(json.serializedCategories, Utils.registeredObjectReviver)
return @forCategories(categories)
else if json.type is SearchMailboxPerspective.name
return @forSearch(json.accountIds, json.searchQuery)
else if json.type is StarredMailboxPerspective.name
return @forStarred(json.accountIds)
else if json.type is DraftsMailboxPerspective.name
return @forDrafts(json.accountIds)
else
return null
catch error
NylasEnv.reportError(new Error("Could not restore mailbox perspective: #{error}"))
return null
# Instance Methods # Instance Methods
@ -52,6 +69,9 @@ class MailboxPerspective
throw new Error("#{@constructor.name}: You must provide an array of string `accountIds`") throw new Error("#{@constructor.name}: You must provide an array of string `accountIds`")
@ @
toJSON: =>
return {accountIds: @accountIds, type: @constructor.name}
isEqual: (other) => isEqual: (other) =>
return false unless other and @constructor is other.constructor return false unless other and @constructor is other.constructor
return false unless other.name is @name return false unless other.name is @name
@ -123,6 +143,11 @@ class SearchMailboxPerspective extends MailboxPerspective
@ @
toJSON: =>
json = super
json.searchQuery = @searchQuery
json
isEqual: (other) => isEqual: (other) =>
super(other) and other.searchQuery is @searchQuery super(other) and other.searchQuery is @searchQuery
@ -147,6 +172,9 @@ class DraftsMailboxPerspective extends MailboxPerspective
@drafts = true # The DraftListStore looks for this @drafts = true # The DraftListStore looks for this
@ @
fromJSON: =>
{type: @constructor.name, accountIds: @accountIds}
threads: => threads: =>
null null
@ -220,8 +248,13 @@ class CategoryMailboxPerspective extends MailboxPerspective
@ @
toJSON: =>
json = super
json.serializedCategories = JSON.stringify(@_categories, Utils.registeredObjectReplacer)
json
isEqual: (other) => isEqual: (other) =>
super(other) and _.isEqual(@categories(), other.categories()) super(other) and _.isEqual(_.pluck(@categories(), 'id'), _.pluck(other.categories(), 'id'))
threads: => threads: =>
query = DatabaseStore.findAll(Thread) query = DatabaseStore.findAll(Thread)

View file

@ -45,6 +45,7 @@
display: none; display: none;
min-width: 30px; min-width: 30px;
margin-left: auto; margin-left: auto;
margin-right: 8px;
} }
&:hover { &:hover {