feat(perspective): Restore through NylasEnv.savedState

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

View file

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

View file

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

View file

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

View file

@ -6,17 +6,17 @@ _ = require 'underscore'
CategoryStore,
Category} = require 'nylas-exports'
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) ->
key = "core.accountSidebarCollapsed.#{id}Section"
collapsed = NylasEnv.config.get(key)
toggleSectionCollapse = (section) ->
key = "core.accountSidebarCollapsed.#{section.title}Section"
toggleSectionCollapsed = (section) ->
return unless section
NylasEnv.config.set(key, not section.collapsed)
SidebarActions.setKeyCollapsed(section.title, not isSectionCollapsed(section.title))
class SidebarSection
@ -130,7 +130,7 @@ class SidebarSection
title ?= account.categoryLabel()
collapsed = isSectionCollapsed(title)
if collapsible
onCollapseToggled = toggleSectionCollapse
onCollapseToggled = toggleSectionCollapsed
return {
title: title

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -1,5 +1,6 @@
_ = require 'underscore'
Utils = require './flux/models/utils'
TaskFactory = require './flux/tasks/task-factory'
AccountStore = require './flux/stores/account-store'
CategoryStore = require './flux/stores/category-store'
@ -44,6 +45,22 @@ class MailboxPerspective
@forInbox: (accountsOrIds) =>
@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
@ -52,6 +69,9 @@ class MailboxPerspective
throw new Error("#{@constructor.name}: You must provide an array of string `accountIds`")
@
toJSON: =>
return {accountIds: @accountIds, type: @constructor.name}
isEqual: (other) =>
return false unless other and @constructor is other.constructor
return false unless other.name is @name
@ -123,6 +143,11 @@ class SearchMailboxPerspective extends MailboxPerspective
@
toJSON: =>
json = super
json.searchQuery = @searchQuery
json
isEqual: (other) =>
super(other) and other.searchQuery is @searchQuery
@ -147,6 +172,9 @@ class DraftsMailboxPerspective extends MailboxPerspective
@drafts = true # The DraftListStore looks for this
@
fromJSON: =>
{type: @constructor.name, accountIds: @accountIds}
threads: =>
null
@ -220,8 +248,13 @@ class CategoryMailboxPerspective extends MailboxPerspective
@
toJSON: =>
json = super
json.serializedCategories = JSON.stringify(@_categories, Utils.registeredObjectReplacer)
json
isEqual: (other) =>
super(other) and _.isEqual(@categories(), other.categories())
super(other) and _.isEqual(_.pluck(@categories(), 'id'), _.pluck(other.categories(), 'id'))
threads: =>
query = DatabaseStore.findAll(Thread)

View file

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