2015-05-20 07:06:59 +08:00
|
|
|
_ = require 'underscore'
|
2015-03-10 09:25:53 +08:00
|
|
|
React = require 'react'
|
2015-04-25 02:33:10 +08:00
|
|
|
classNames = require 'classnames'
|
2016-01-15 06:04:51 +08:00
|
|
|
|
2016-01-16 06:26:07 +08:00
|
|
|
{MultiselectList, FluxContainer} = require 'nylas-component-kit'
|
2016-01-15 06:04:51 +08:00
|
|
|
|
2015-03-26 02:17:57 +08:00
|
|
|
{Actions,
|
2015-04-07 02:46:20 +08:00
|
|
|
Thread,
|
2016-03-08 10:13:53 +08:00
|
|
|
Category,
|
2015-10-22 01:38:00 +08:00
|
|
|
CanvasUtils,
|
|
|
|
TaskFactory,
|
2015-12-15 02:32:45 +08:00
|
|
|
ChangeUnreadTask,
|
2016-03-08 10:13:53 +08:00
|
|
|
ChangeStarredTask,
|
2015-03-26 02:17:57 +08:00
|
|
|
WorkspaceStore,
|
2016-02-20 10:52:49 +08:00
|
|
|
AccountStore,
|
2015-07-22 02:50:08 +08:00
|
|
|
CategoryStore,
|
2015-10-22 01:38:00 +08:00
|
|
|
FocusedContentStore,
|
2016-01-09 06:58:31 +08:00
|
|
|
FocusedPerspectiveStore} = require 'nylas-exports'
|
2015-03-10 09:25:53 +08:00
|
|
|
|
2016-01-15 06:04:51 +08:00
|
|
|
ThreadListColumns = require './thread-list-columns'
|
|
|
|
ThreadListScrollTooltip = require './thread-list-scroll-tooltip'
|
|
|
|
ThreadListStore = require './thread-list-store'
|
2016-01-16 06:26:07 +08:00
|
|
|
FocusContainer = require './focus-container'
|
2015-06-18 04:14:45 +08:00
|
|
|
EmptyState = require './empty-state'
|
2016-02-20 07:35:48 +08:00
|
|
|
ThreadListContextMenu = require './thread-list-context-menu'
|
2016-03-08 10:13:53 +08:00
|
|
|
CategoryRemovalTargetRulesets = require './category-removal-target-rulesets'
|
2015-06-06 02:50:55 +08:00
|
|
|
|
|
|
|
|
2015-05-01 04:08:29 +08:00
|
|
|
class ThreadList extends React.Component
|
|
|
|
@displayName: 'ThreadList'
|
2015-05-09 10:55:32 +08:00
|
|
|
|
feat(unsafe-components): Wrap injected components, catch exceptions, clean up ComponentRegistry
Summary:
This diff gives the ComponentRegistry a cleaner, smaller API. Instead of querying by name, location or role,
it's now just location and role, and you can register components for one or more location and one or more
roles without assigning the entries in the registry separate names.
When you register with the ComponentRegistry, the syntax is also cleaner and uses the component's displayName
instead of requiring you to provide a name. You also provide the actual component when unregistering, ensuring
that you can't unregister someone else's component.
InjectedComponent and InjectedComponentSet now wrap their children in UnsafeComponent, which prevents
render/component lifecycle problems from propogating.
Existing components have been updated:
1. maxWidth / minWidth are now containerStyles.maxWidth/minWidth
2. displayName is now required to use the CR.
3. containerRequired = false can be provided to exempt a component from being wrapped in an UnsafeComponent.
This is useful because it's slightly faster and keeps DOM flat.
This diff also makes the "Show Component Regions" more awesome. It displays column regions, since they now
use the InjectedComponentSet, and also shows for InjectedComponent as well as InjectedComponentSet.
Change ComponentRegistry syntax, lots more work on safely wrapping items. See description.
Fix for inline flexbox scenarios (message actions)
Allow ~/.inbox/packages to be symlinked to a github repo
Test Plan: Run tests!
Reviewers: evan
Reviewed By: evan
Differential Revision: https://review.inboxapp.com/D1457
2015-05-01 07:10:15 +08:00
|
|
|
@containerRequired: false
|
2015-09-05 03:27:05 +08:00
|
|
|
@containerStyles:
|
|
|
|
minWidth: 300
|
2015-09-28 16:44:36 +08:00
|
|
|
maxWidth: 3000
|
2015-03-10 09:25:53 +08:00
|
|
|
|
2015-06-12 09:00:40 +08:00
|
|
|
constructor: (@props) ->
|
|
|
|
@state =
|
|
|
|
style: 'unknown'
|
|
|
|
|
|
|
|
componentDidMount: =>
|
|
|
|
window.addEventListener('resize', @_onResize, true)
|
2016-02-20 07:35:48 +08:00
|
|
|
React.findDOMNode(@).addEventListener('contextmenu', @_onShowContextMenu)
|
2015-06-12 09:00:40 +08:00
|
|
|
@_onResize()
|
|
|
|
|
|
|
|
componentWillUnmount: =>
|
2015-08-04 02:08:15 +08:00
|
|
|
window.removeEventListener('resize', @_onResize, true)
|
2016-02-20 07:35:48 +08:00
|
|
|
React.findDOMNode(@).removeEventListener('contextmenu', @_onShowContextMenu)
|
2015-06-12 09:00:40 +08:00
|
|
|
|
2015-11-07 03:47:06 +08:00
|
|
|
_shift: ({offset, afterRunning}) =>
|
2016-01-15 07:04:17 +08:00
|
|
|
dataSource = ThreadListStore.dataSource()
|
2015-11-07 03:47:06 +08:00
|
|
|
focusedId = FocusedContentStore.focusedId('thread')
|
2016-01-15 07:04:17 +08:00
|
|
|
focusedIdx = Math.min(dataSource.count() - 1, Math.max(0, dataSource.indexOfId(focusedId) + offset))
|
|
|
|
item = dataSource.get(focusedIdx)
|
2015-11-07 03:47:06 +08:00
|
|
|
afterRunning()
|
|
|
|
Actions.setFocus(collection: 'thread', item: item)
|
|
|
|
|
|
|
|
_keymapHandlers: ->
|
2016-03-08 10:13:53 +08:00
|
|
|
'application:remove-from-view': => @_onRemoveFromView
|
|
|
|
'application:gmail-remove-from-view': =>
|
|
|
|
@_onRemoveFromView(CategoryRemovalTargetRulesets.Gmail)
|
2015-11-07 03:47:06 +08:00
|
|
|
'application:archive-item': @_onArchiveItem
|
|
|
|
'application:delete-item': @_onDeleteItem
|
|
|
|
'application:star-item': @_onStarItem
|
2016-01-16 06:26:07 +08:00
|
|
|
'application:mark-important': => @_onSetImportant(true)
|
|
|
|
'application:mark-unimportant': => @_onSetImportant(false)
|
|
|
|
'application:mark-as-unread': => @_onSetUnread(true)
|
|
|
|
'application:mark-as-read': => @_onSetUnread(false)
|
2016-02-02 04:19:18 +08:00
|
|
|
'application:report-as-spam': => @_onMarkAsSpam(false)
|
2015-11-07 03:47:06 +08:00
|
|
|
'application:remove-and-previous': =>
|
|
|
|
@_shift(offset: -1, afterRunning: @_onRemoveFromView)
|
2016-01-23 07:30:00 +08:00
|
|
|
'application:remove-and-next': =>
|
|
|
|
@_shift(offset: 1, afterRunning: @_onRemoveFromView)
|
2016-01-15 06:04:51 +08:00
|
|
|
'thread-list:select-read': @_onSelectRead
|
|
|
|
'thread-list:select-unread': @_onSelectUnread
|
|
|
|
'thread-list:select-starred': @_onSelectStarred
|
|
|
|
'thread-list:select-unstarred': @_onSelectUnstarred
|
2015-11-07 03:47:06 +08:00
|
|
|
|
|
|
|
render: ->
|
2015-06-12 09:00:40 +08:00
|
|
|
if @state.style is 'wide'
|
2016-01-16 06:26:07 +08:00
|
|
|
columns = ThreadListColumns.Wide
|
2016-02-12 07:24:33 +08:00
|
|
|
itemHeight = 36
|
2015-06-12 09:00:40 +08:00
|
|
|
else
|
2016-01-16 06:26:07 +08:00
|
|
|
columns = ThreadListColumns.Narrow
|
2016-02-12 07:24:33 +08:00
|
|
|
itemHeight = 85
|
2016-01-16 06:26:07 +08:00
|
|
|
|
|
|
|
<FluxContainer
|
|
|
|
stores=[ThreadListStore]
|
2016-01-29 06:59:17 +08:00
|
|
|
getStateFromStores={ -> dataSource: ThreadListStore.dataSource() }>
|
2016-01-16 06:26:07 +08:00
|
|
|
<FocusContainer collection="thread">
|
|
|
|
<MultiselectList
|
|
|
|
ref="list"
|
|
|
|
columns={columns}
|
|
|
|
itemPropsProvider={@_threadPropsProvider}
|
|
|
|
itemHeight={itemHeight}
|
|
|
|
className="thread-list thread-list-#{@state.style}"
|
|
|
|
scrollTooltipComponent={ThreadListScrollTooltip}
|
|
|
|
emptyComponent={EmptyState}
|
|
|
|
keymapHandlers={@_keymapHandlers()}
|
|
|
|
onDragStart={@_onDragStart}
|
|
|
|
onDragEnd={@_onDragEnd}
|
|
|
|
draggable="true" />
|
|
|
|
</FocusContainer>
|
|
|
|
</FluxContainer>
|
2015-07-24 02:10:51 +08:00
|
|
|
|
2016-01-15 06:04:51 +08:00
|
|
|
_threadPropsProvider: (item) ->
|
2016-02-20 10:22:28 +08:00
|
|
|
props =
|
|
|
|
className: classNames
|
|
|
|
'unread': item.unread
|
|
|
|
|
2016-03-08 10:13:53 +08:00
|
|
|
props.shouldEnableSwipe = =>
|
|
|
|
perspective = FocusedPerspectiveStore.current()
|
|
|
|
tasks = perspective.tasksForRemovingItems([item], CategoryRemovalTargetRulesets.Default)
|
|
|
|
return tasks.length > 0
|
|
|
|
|
|
|
|
props.onSwipeRightClass = =>
|
|
|
|
perspective = FocusedPerspectiveStore.current()
|
|
|
|
tasks = perspective.tasksForRemovingItems([item], CategoryRemovalTargetRulesets.Default)
|
|
|
|
return null if tasks.length is 0
|
|
|
|
|
|
|
|
# TODO this logic is brittle
|
|
|
|
task = tasks[0]
|
|
|
|
name = if task instanceof ChangeStarredTask
|
|
|
|
'unstar'
|
|
|
|
else if task.categoriesToAdd().length is 1
|
|
|
|
task.categoriesToAdd()[0].name
|
|
|
|
else
|
|
|
|
'remove'
|
|
|
|
|
|
|
|
return "swipe-#{name}"
|
|
|
|
|
|
|
|
props.onSwipeRight = (callback) ->
|
|
|
|
perspective = FocusedPerspectiveStore.current()
|
|
|
|
tasks = perspective.tasksForRemovingItems([item], CategoryRemovalTargetRulesets.Default)
|
|
|
|
callback(false) if tasks.length is 0
|
|
|
|
Actions.closePopover()
|
|
|
|
Actions.queueTasks(tasks)
|
|
|
|
callback(true)
|
2016-02-20 10:22:28 +08:00
|
|
|
|
2016-03-08 10:13:53 +08:00
|
|
|
if FocusedPerspectiveStore.current().isInbox()
|
2016-02-25 07:06:06 +08:00
|
|
|
props.onSwipeLeftClass = 'swipe-snooze'
|
2016-02-25 09:33:20 +08:00
|
|
|
props.onSwipeCenter = =>
|
|
|
|
Actions.closePopover()
|
2016-02-25 07:06:06 +08:00
|
|
|
props.onSwipeLeft = (callback) =>
|
|
|
|
# TODO this should be grabbed from elsewhere
|
|
|
|
{PopoverStore} = require 'nylas-exports'
|
|
|
|
SnoozePopoverBody = require '../../thread-snooze/lib/snooze-popover-body'
|
|
|
|
|
|
|
|
element = document.querySelector("[data-item-id=\"#{item.id}\"]")
|
|
|
|
rect = element.getBoundingClientRect()
|
|
|
|
Actions.openPopover(
|
|
|
|
<SnoozePopoverBody
|
|
|
|
threads={[item]}
|
|
|
|
swipeCallback={callback}
|
|
|
|
closePopover={Actions.closePopover}/>,
|
|
|
|
rect,
|
|
|
|
"right"
|
|
|
|
)
|
2016-02-23 07:48:07 +08:00
|
|
|
|
2016-03-08 10:13:53 +08:00
|
|
|
return props
|
2016-01-15 06:04:51 +08:00
|
|
|
|
2016-02-23 08:08:10 +08:00
|
|
|
_targetItemsForMouseEvent: (event) ->
|
2016-01-16 06:26:07 +08:00
|
|
|
itemThreadId = @refs.list.itemIdAtPoint(event.clientX, event.clientY)
|
2015-07-24 02:10:51 +08:00
|
|
|
unless itemThreadId
|
2016-02-20 07:35:48 +08:00
|
|
|
return null
|
2015-07-24 02:10:51 +08:00
|
|
|
|
2016-01-29 05:27:23 +08:00
|
|
|
dataSource = ThreadListStore.dataSource()
|
|
|
|
if itemThreadId in dataSource.selection.ids()
|
2016-02-23 08:08:10 +08:00
|
|
|
return {
|
|
|
|
threadIds: dataSource.selection.ids()
|
|
|
|
accountIds: _.uniq(_.pluck(dataSource.selection.items(), 'accountId'))
|
|
|
|
}
|
2015-07-24 02:10:51 +08:00
|
|
|
else
|
2016-02-23 08:08:10 +08:00
|
|
|
thread = dataSource.getById(itemThreadId)
|
|
|
|
return null unless thread
|
|
|
|
return {
|
|
|
|
threadIds: [thread.id]
|
|
|
|
accountIds: [thread.accountId]
|
|
|
|
}
|
2016-01-29 05:27:23 +08:00
|
|
|
|
2016-02-20 07:35:48 +08:00
|
|
|
_onShowContextMenu: (event) =>
|
2016-02-23 08:08:10 +08:00
|
|
|
data = @_targetItemsForMouseEvent(event)
|
2016-02-20 07:35:48 +08:00
|
|
|
if not data
|
|
|
|
event.preventDefault()
|
|
|
|
return
|
|
|
|
(new ThreadListContextMenu(data)).displayMenu()
|
|
|
|
|
|
|
|
_onDragStart: (event) =>
|
2016-02-23 08:08:10 +08:00
|
|
|
data = @_targetItemsForMouseEvent(event)
|
|
|
|
if not data
|
2016-02-20 07:35:48 +08:00
|
|
|
event.preventDefault()
|
|
|
|
return
|
|
|
|
|
2015-07-24 02:10:51 +08:00
|
|
|
event.dataTransfer.effectAllowed = "move"
|
|
|
|
event.dataTransfer.dragEffect = "move"
|
|
|
|
|
2016-02-23 08:08:10 +08:00
|
|
|
canvas = CanvasUtils.canvasWithThreadDragImage(data.threadIds.length)
|
2015-07-24 02:10:51 +08:00
|
|
|
event.dataTransfer.setDragImage(canvas, 10, 10)
|
2016-02-23 08:08:10 +08:00
|
|
|
event.dataTransfer.setData('nylas-threads-data', JSON.stringify(data))
|
2015-07-24 02:10:51 +08:00
|
|
|
return
|
|
|
|
|
|
|
|
_onDragEnd: (event) =>
|
|
|
|
|
2015-06-12 09:00:40 +08:00
|
|
|
_onResize: (event) =>
|
|
|
|
current = @state.style
|
|
|
|
desired = if React.findDOMNode(@).offsetWidth < 540 then 'narrow' else 'wide'
|
|
|
|
if current isnt desired
|
|
|
|
@setState(style: desired)
|
2015-03-10 09:25:53 +08:00
|
|
|
|
2015-10-24 05:45:28 +08:00
|
|
|
_threadsForKeyboardAction: ->
|
2016-01-15 07:04:17 +08:00
|
|
|
return null unless ThreadListStore.dataSource()
|
2015-10-22 01:38:00 +08:00
|
|
|
focused = FocusedContentStore.focused('thread')
|
2015-10-24 05:45:28 +08:00
|
|
|
if focused
|
|
|
|
return [focused]
|
2016-01-15 07:04:17 +08:00
|
|
|
else if ThreadListStore.dataSource().selection.count() > 0
|
|
|
|
return ThreadListStore.dataSource().selection.items()
|
2015-06-27 05:47:14 +08:00
|
|
|
else
|
2015-10-24 05:45:28 +08:00
|
|
|
return null
|
2015-06-27 05:47:14 +08:00
|
|
|
|
2015-10-24 05:45:28 +08:00
|
|
|
_onStarItem: =>
|
|
|
|
threads = @_threadsForKeyboardAction()
|
|
|
|
return unless threads
|
2015-10-22 01:38:00 +08:00
|
|
|
task = TaskFactory.taskForInvertingStarred({threads})
|
|
|
|
Actions.queueTask(task)
|
|
|
|
|
2016-01-16 06:26:07 +08:00
|
|
|
_onSetImportant: (important) =>
|
2015-12-08 04:05:40 +08:00
|
|
|
threads = @_threadsForKeyboardAction()
|
|
|
|
return unless threads
|
2016-01-09 01:31:24 +08:00
|
|
|
return unless NylasEnv.config.get('core.workspace.showImportant')
|
2016-02-17 03:41:15 +08:00
|
|
|
|
2015-12-08 04:05:40 +08:00
|
|
|
if important
|
2016-02-17 03:41:15 +08:00
|
|
|
tasks = TaskFactory.tasksForApplyingCategories
|
|
|
|
threads: threads
|
|
|
|
categoriesToRemove: (accountId) -> []
|
2016-03-08 10:13:53 +08:00
|
|
|
categoriesToAdd: (accountId) ->
|
|
|
|
[CategoryStore.getStandardCategory(accountId, 'important')]
|
2016-02-17 03:41:15 +08:00
|
|
|
|
2015-12-08 04:05:40 +08:00
|
|
|
else
|
2016-02-17 03:41:15 +08:00
|
|
|
tasks = TaskFactory.tasksForApplyingCategories
|
|
|
|
threads: threads
|
|
|
|
categoriesToRemove: (accountId) ->
|
|
|
|
important = CategoryStore.getStandardCategory(accountId, 'important')
|
|
|
|
return [important] if important
|
|
|
|
return []
|
2015-12-08 04:05:40 +08:00
|
|
|
|
2016-02-17 03:41:15 +08:00
|
|
|
Actions.queueTasks(tasks)
|
2015-12-08 04:05:40 +08:00
|
|
|
|
2016-01-16 06:26:07 +08:00
|
|
|
_onSetUnread: (unread) =>
|
2015-12-15 02:32:45 +08:00
|
|
|
threads = @_threadsForKeyboardAction()
|
|
|
|
return unless threads
|
2016-02-23 08:08:10 +08:00
|
|
|
Actions.queueTask(new ChangeUnreadTask({threads, unread}))
|
2015-12-15 02:32:45 +08:00
|
|
|
Actions.popSheet()
|
|
|
|
|
2016-02-02 04:19:18 +08:00
|
|
|
_onMarkAsSpam: =>
|
|
|
|
threads = @_threadsForKeyboardAction()
|
|
|
|
return unless threads
|
2016-02-23 08:08:10 +08:00
|
|
|
tasks = TaskFactory.tasksForMarkingAsSpam
|
|
|
|
threads: threads
|
|
|
|
fromPerspective: FocusedPerspectiveStore.current()
|
2016-02-02 04:19:18 +08:00
|
|
|
Actions.queueTasks(tasks)
|
|
|
|
|
2016-03-08 10:13:53 +08:00
|
|
|
_onRemoveFromView: (ruleset = CategoryRemovalTargetRulesets.Default) =>
|
2015-10-24 05:45:28 +08:00
|
|
|
threads = @_threadsForKeyboardAction()
|
2016-02-23 08:08:10 +08:00
|
|
|
return unless threads
|
|
|
|
current = FocusedPerspectiveStore.current()
|
2016-03-08 10:13:53 +08:00
|
|
|
tasks = current.tasksForRemovingItems(threads, ruleset)
|
|
|
|
Actions.queueTasks(tasks)
|
2016-02-23 08:08:10 +08:00
|
|
|
Actions.popSheet()
|
2015-10-22 01:38:00 +08:00
|
|
|
|
2015-10-24 05:45:28 +08:00
|
|
|
_onArchiveItem: =>
|
|
|
|
threads = @_threadsForKeyboardAction()
|
|
|
|
if threads
|
2016-01-22 05:46:04 +08:00
|
|
|
tasks = TaskFactory.tasksForArchiving
|
2015-10-24 05:45:28 +08:00
|
|
|
threads: threads
|
2016-01-15 07:04:17 +08:00
|
|
|
fromPerspective: FocusedPerspectiveStore.current()
|
2016-01-22 05:46:04 +08:00
|
|
|
Actions.queueTasks(tasks)
|
2015-10-24 05:45:28 +08:00
|
|
|
Actions.popSheet()
|
2015-10-22 01:38:00 +08:00
|
|
|
|
2015-10-24 05:45:28 +08:00
|
|
|
_onDeleteItem: =>
|
|
|
|
threads = @_threadsForKeyboardAction()
|
|
|
|
if threads
|
2016-01-22 05:46:04 +08:00
|
|
|
tasks = TaskFactory.tasksForMovingToTrash
|
2015-10-24 05:45:28 +08:00
|
|
|
threads: threads
|
2016-01-15 07:04:17 +08:00
|
|
|
fromPerspective: FocusedPerspectiveStore.current()
|
2016-01-22 05:46:04 +08:00
|
|
|
Actions.queueTasks(tasks)
|
2015-10-24 05:45:28 +08:00
|
|
|
Actions.popSheet()
|
2015-05-01 04:08:29 +08:00
|
|
|
|
2016-01-15 06:04:51 +08:00
|
|
|
_onSelectRead: =>
|
2016-01-15 07:04:17 +08:00
|
|
|
dataSource = ThreadListStore.dataSource()
|
|
|
|
items = dataSource.itemsCurrentlyInViewMatching (item) -> not item.unread
|
|
|
|
dataSource.selection.set(items)
|
2016-01-15 06:04:51 +08:00
|
|
|
|
|
|
|
_onSelectUnread: =>
|
2016-01-15 07:04:17 +08:00
|
|
|
dataSource = ThreadListStore.dataSource()
|
|
|
|
items = dataSource.itemsCurrentlyInViewMatching (item) -> item.unread
|
|
|
|
dataSource.selection.set(items)
|
2016-01-15 06:04:51 +08:00
|
|
|
|
|
|
|
_onSelectStarred: =>
|
2016-01-15 07:04:17 +08:00
|
|
|
dataSource = ThreadListStore.dataSource()
|
|
|
|
items = dataSource.itemsCurrentlyInViewMatching (item) -> item.starred
|
|
|
|
dataSource.selection.set(items)
|
2016-01-15 06:04:51 +08:00
|
|
|
|
|
|
|
_onSelectUnstarred: =>
|
2016-01-15 07:04:17 +08:00
|
|
|
dataSource = ThreadListStore.dataSource()
|
|
|
|
items = dataSource.itemsCurrentlyInViewMatching (item) -> not item.starred
|
|
|
|
dataSource.selection.set(items)
|
2015-05-01 04:08:29 +08:00
|
|
|
|
|
|
|
module.exports = ThreadList
|