_ = require 'underscore-plus' React = require 'react' {Actions, ContactStore, FileUploadStore, ComponentRegistry} = require 'inbox-exports' FileUploads = require './file-uploads.cjsx' DraftStoreProxy = require './draft-store-proxy' ContenteditableToolbar = require './contenteditable-toolbar.cjsx' ContenteditableComponent = require './contenteditable-component.cjsx' ComposerParticipants = require './composer-participants.cjsx' # The ComposerView is a unique React component because it (currently) is a # singleton. Normally, the React way to do things would be to re-render the # Composer with new props. As an alternative, we can call `setProps` to # simulate the effect of the parent re-rendering us module.exports = ComposerView = React.createClass getInitialState: -> # A majority of the initial state is set in `_setInitialState` because # those getters are asynchronous while `getInitialState` is # synchronous. state = @getComponentRegistryState() _.extend state, populated: false to: undefined cc: undefined bcc: undefined body: undefined subject: undefined state getComponentRegistryState: -> ResizableComponent: ComponentRegistry.findViewByName 'ResizableComponent' AttachmentComponent: ComponentRegistry.findAllViewsByRole 'MessageAttachment' FooterComponents: ComponentRegistry.findAllViewsByRole 'Composer:Footer' componentWillMount: -> @_prepareForDraft() componentWillUnmount: -> @_teardownForDraft() componentWillReceiveProps: (newProps) -> if newProps.localId != @props.localId # When we're given a new draft localId, we have to stop listening to our # current DraftStoreProxy, create a new one and listen to that. The simplest # way to do this is to just re-call registerListeners. @_teardownForDraft() @_prepareForDraft() _prepareForDraft: -> @_proxy = new DraftStoreProxy(@props.localId) @unlisteners = [] @unlisteners.push @_proxy.listen(@_onDraftChanged) @unlisteners.push ComponentRegistry.listen (event) => @setState(@getComponentRegistryState()) _teardownForDraft: -> unlisten() for unlisten in @unlisteners @_proxy.changes.commit() render: -> ResizableComponent = @state.ResizableComponent if @props.mode is "inline" and ResizableComponent?
{@_renderComposer()}
else
{@_renderComposer()}
_wrapClasses: -> "composer-outer-wrap #{@props.containerClass ? ""}" _renderComposer: -> # Do not render the composer unless we have loaded our draft. # Otherwise the Scribe component is initialized with HTML = "" return
if @state.body == undefined
{@_composerTitle()}
@setState {showcc: true}} >Add cc/bcc @setState {showsubject: true}} >Change Subject Popout  
{@_fileComponents()}
{@_footerComponents()}
# TODO, in the future this will be smarter and say useful things like # "Reply" or "Reply All" or "Reply + New Person1, New Person2" _composerTitle: -> "Compose Message" _footerComponents: -> (@state.FooterComponents ? []).map (Component) => _onDraftChanged: -> draft = @_proxy.draft() state = to: draft.to cc: draft.cc bcc: draft.bcc files: draft.files subject: draft.subject body: draft.body if !@state.populated _.extend state, showcc: (not (_.isEmpty(draft.cc) and _.isEmpty(draft.bcc))) showsubject: _.isEmpty(draft.subject) populated: true @setState(state) _popoutComposer: -> Actions.composePopoutDraft @props.localId _onComposeBodyClick: -> @refs.scribe.focus() _onChangeSubject: (event) -> @_proxy.changes.add(subject: event.target.value) _onChangeBody: (event) -> @_proxy.changes.add(body: event.target.value) _participantFunctions: (field) -> remove: (participant) => updates = {} updates[field] = _.without(@state[field], participant) @_proxy.changes.add(updates) add: (participant) => updates = {} updates[field] = _.union (@state[field] ? []), [participant] @_proxy.changes.add(updates) "" _sendDraft: -> @_proxy.changes.commit() Actions.sendDraft(@props.localId) _destroyDraft: -> Actions.destroyDraft(@props.localId) _attachFile: -> Actions.attachFile({messageLocalId: @props.localId}) _fileComponents: -> MessageAttachment = @state.MessageAttachment (@state.files ? []).map (file) =>