2015-03-10 09:25:53 +08:00
|
|
|
_ = require 'underscore-plus'
|
|
|
|
moment = require "moment"
|
|
|
|
React = require 'react'
|
|
|
|
{ListTabular} = require 'ui-components'
|
|
|
|
{timestamp, subject} = require './formatting-utils'
|
|
|
|
{Actions,
|
|
|
|
DraftStore,
|
|
|
|
ComponentRegistry,
|
|
|
|
DatabaseStore} = require 'inbox-exports'
|
|
|
|
|
|
|
|
module.exports =
|
|
|
|
DraftList = React.createClass
|
|
|
|
displayName: 'DraftList'
|
|
|
|
|
|
|
|
mixins: [ComponentRegistry.Mixin]
|
|
|
|
components: ['Participants']
|
|
|
|
|
|
|
|
getInitialState: ->
|
|
|
|
items: DraftStore.items()
|
|
|
|
columns: @_computeColumns()
|
|
|
|
selectedId: null
|
|
|
|
|
|
|
|
componentDidMount: ->
|
2015-03-12 05:20:17 +08:00
|
|
|
@draftStoreUnsubscribe = DraftStore.listen @_onChange
|
|
|
|
@bodyUnsubscriber = atom.commands.add 'body',
|
2015-03-10 09:25:53 +08:00
|
|
|
'application:previous-item': => @_onShiftSelectedIndex(-1)
|
|
|
|
'application:next-item': => @_onShiftSelectedIndex(1)
|
|
|
|
'application:remove-item': @_onDeleteSelected
|
|
|
|
|
|
|
|
componentWillUnmount: ->
|
2015-03-12 05:20:17 +08:00
|
|
|
@draftStoreUnsubscribe()
|
|
|
|
@bodyUnsubscriber.dispose()
|
2015-03-10 09:25:53 +08:00
|
|
|
|
|
|
|
render: ->
|
|
|
|
<div className="thread-list">
|
|
|
|
<ListTabular
|
|
|
|
columns={@state.columns}
|
|
|
|
items={@state.items}
|
|
|
|
selectedId={@state.selectedId}
|
2015-03-24 07:33:28 +08:00
|
|
|
onDoubleClick={@_onDoubleClick}
|
2015-03-10 09:25:53 +08:00
|
|
|
onSelect={@_onSelect} />
|
|
|
|
</div>
|
2015-03-11 04:15:28 +08:00
|
|
|
|
2015-03-10 09:25:53 +08:00
|
|
|
_onSelect: (item) ->
|
|
|
|
@setState
|
|
|
|
selectedId: item.id
|
|
|
|
|
2015-03-24 07:33:28 +08:00
|
|
|
_onDoubleClick: (item) ->
|
2015-03-10 09:25:53 +08:00
|
|
|
DatabaseStore.localIdForModel(item).then (localId) ->
|
|
|
|
Actions.composePopoutDraft(localId)
|
|
|
|
|
|
|
|
_computeColumns: ->
|
2015-03-12 05:20:17 +08:00
|
|
|
snippet = (html) =>
|
|
|
|
@draftSanitizer ?= document.createElement('div')
|
|
|
|
@draftSanitizer.innerHTML = html
|
|
|
|
text = @draftSanitizer.innerText
|
|
|
|
text[0..140]
|
|
|
|
|
2015-03-10 09:25:53 +08:00
|
|
|
c1 = new ListTabular.Column
|
|
|
|
name: "Name"
|
|
|
|
flex: 2
|
|
|
|
resolver: (draft) =>
|
|
|
|
Participants = @state.Participants
|
|
|
|
<div className="participants">
|
|
|
|
<Participants participants={[].concat(draft.to,draft.cc,draft.bcc)}
|
|
|
|
context={'list'} clickable={false} />
|
|
|
|
</div>
|
|
|
|
|
|
|
|
c2 = new ListTabular.Column
|
|
|
|
name: "Subject"
|
|
|
|
flex: 3
|
|
|
|
resolver: (draft) ->
|
|
|
|
<span className="subject">{subject(draft.subject)}</span>
|
|
|
|
|
|
|
|
c3 = new ListTabular.Column
|
|
|
|
name: "Snippet"
|
|
|
|
flex: 4
|
|
|
|
resolver: (draft) ->
|
2015-03-12 05:20:17 +08:00
|
|
|
<span className="snippet">{snippet(draft.body)}</span>
|
2015-03-10 09:25:53 +08:00
|
|
|
|
|
|
|
c4 = new ListTabular.Column
|
|
|
|
name: "Date"
|
|
|
|
flex: 1
|
|
|
|
resolver: (draft) ->
|
|
|
|
<span className="timestamp">{timestamp(draft.date)}</span>
|
|
|
|
|
|
|
|
[c1, c2, c3, c4]
|
|
|
|
|
|
|
|
_onShiftSelectedIndex: (delta) ->
|
|
|
|
item = _.find @state.items, (draft) => draft.id is @state.selectedId
|
2015-03-31 08:25:08 +08:00
|
|
|
return unless item
|
|
|
|
|
2015-03-10 09:25:53 +08:00
|
|
|
index = if item then @state.items.indexOf(item) else -1
|
|
|
|
index = Math.max(0, Math.min(index + delta, @state.items.length-1))
|
|
|
|
@setState
|
|
|
|
selectedId: @state.items[index].id
|
|
|
|
|
|
|
|
_onDeleteSelected: ->
|
|
|
|
item = _.find @state.items, (draft) => draft.id is @state.selectedId
|
2015-03-31 08:25:08 +08:00
|
|
|
return unless item
|
2015-03-10 09:25:53 +08:00
|
|
|
|
|
|
|
DatabaseStore.localIdForModel(item).then (localId) ->
|
|
|
|
Actions.destroyDraft(localId)
|
|
|
|
@_onShiftSelectedIndex(-1)
|
|
|
|
|
|
|
|
_onChange: ->
|
|
|
|
@setState
|
|
|
|
items: DraftStore.items()
|