2016-01-26 06:14:09 +08:00
|
|
|
_ = require 'underscore'
|
|
|
|
_str = require 'underscore.string'
|
|
|
|
{React, Actions, ExtensionRegistry} = require 'nylas-exports'
|
|
|
|
{Menu, RetinaImg, ButtonDropdown} = require 'nylas-component-kit'
|
|
|
|
|
|
|
|
class SendActionButton extends React.Component
|
|
|
|
@displayName: "SendActionButton"
|
|
|
|
|
|
|
|
@propTypes:
|
|
|
|
draft: React.PropTypes.object
|
2016-02-24 05:42:10 +08:00
|
|
|
style: React.PropTypes.object
|
2016-01-26 06:14:09 +08:00
|
|
|
isValidDraft: React.PropTypes.func
|
|
|
|
|
2016-02-24 05:42:10 +08:00
|
|
|
@defaultProps:
|
|
|
|
style: {}
|
|
|
|
|
2016-01-26 06:14:09 +08:00
|
|
|
@CONFIG_KEY: "core.sending.defaultSendType"
|
|
|
|
|
|
|
|
constructor: (@props) ->
|
|
|
|
@state =
|
|
|
|
actionConfigs: @_actionConfigs(@props)
|
|
|
|
|
2016-02-04 08:27:11 +08:00
|
|
|
componentDidMount: =>
|
2016-01-26 06:14:09 +08:00
|
|
|
@unsub = ExtensionRegistry.Composer.listen(@_onExtensionsChanged)
|
|
|
|
|
2016-02-04 08:27:11 +08:00
|
|
|
componentWillReceiveProps: (newProps) =>
|
2016-01-26 06:14:09 +08:00
|
|
|
@setState actionConfigs: @_actionConfigs(newProps)
|
|
|
|
|
2016-02-04 08:27:11 +08:00
|
|
|
componentWillUnmount: =>
|
2016-01-26 06:14:09 +08:00
|
|
|
@unsub()
|
|
|
|
|
|
|
|
primaryClick: => @_onPrimaryClick()
|
|
|
|
|
2016-02-04 08:27:11 +08:00
|
|
|
_configKeyFromTitle: (title) =>
|
2016-01-26 06:14:09 +08:00
|
|
|
return _str.dasherize(title.toLowerCase())
|
|
|
|
|
|
|
|
_onExtensionsChanged: =>
|
|
|
|
@setState actionConfigs: @_actionConfigs(@props)
|
|
|
|
|
2016-02-04 08:27:11 +08:00
|
|
|
_defaultActionConfig: =>
|
2016-01-26 06:14:09 +08:00
|
|
|
title: "Send"
|
|
|
|
iconUrl: null
|
|
|
|
onSend: ({draft}) -> Actions.sendDraft(draft.clientId)
|
|
|
|
configKey: "send"
|
|
|
|
|
2016-02-04 08:27:11 +08:00
|
|
|
_actionConfigs: (props) =>
|
2016-01-26 06:14:09 +08:00
|
|
|
return [] unless props.draft
|
|
|
|
actionConfigs = [@_defaultActionConfig()]
|
|
|
|
|
|
|
|
for extension in ExtensionRegistry.Composer.extensions()
|
|
|
|
try
|
|
|
|
actionConfig = extension.sendActionConfig?({draft: props.draft})
|
|
|
|
if actionConfig
|
|
|
|
@_verifyConfig(actionConfig, extension)
|
|
|
|
actionConfig.configKey = @_configKeyFromTitle(actionConfig.title)
|
|
|
|
actionConfigs.push(actionConfig)
|
|
|
|
catch err
|
2016-02-04 07:06:52 +08:00
|
|
|
NylasEnv.reportError(err)
|
2016-01-26 06:14:09 +08:00
|
|
|
|
|
|
|
return actionConfigs
|
|
|
|
|
2016-02-04 08:27:11 +08:00
|
|
|
_verifyConfig: (config={}, extension) =>
|
2016-01-26 06:14:09 +08:00
|
|
|
name = extension.name
|
|
|
|
if not _.isString(config.title)
|
|
|
|
throw new Error("#{name}.sendActionConfig must return a string `title`")
|
|
|
|
|
|
|
|
if not _.isFunction(config.onSend)
|
|
|
|
throw new Error("#{name}.sendActionConfig must return a `onSend` function that will be called when the action is selected")
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
2016-02-04 08:27:11 +08:00
|
|
|
render: =>
|
|
|
|
return false unless @props.draft
|
2016-01-26 06:14:09 +08:00
|
|
|
if @state.actionConfigs.length is 1
|
|
|
|
@_renderSingleDefaultButton()
|
|
|
|
else
|
|
|
|
@_renderSendDropdown()
|
|
|
|
|
|
|
|
_onPrimaryClick: =>
|
2016-02-04 08:27:11 +08:00
|
|
|
{preferred} = @_orderedActionConfigs()
|
|
|
|
@_sendWithAction(preferred)
|
|
|
|
|
|
|
|
_renderSingleDefaultButton: =>
|
|
|
|
<button
|
|
|
|
className={"btn btn-toolbar btn-normal btn-emphasis btn-text btn-send"}
|
|
|
|
style={order: -100}
|
|
|
|
onClick={@_onPrimaryClick}>
|
|
|
|
{@_contentForAction(@state.actionConfigs[0])}
|
|
|
|
</button>
|
|
|
|
|
|
|
|
_renderSendDropdown: =>
|
|
|
|
{preferred, rest} = @_orderedActionConfigs()
|
|
|
|
|
2016-01-26 06:14:09 +08:00
|
|
|
<ButtonDropdown
|
2016-01-30 07:51:00 +08:00
|
|
|
className={"btn-send btn-emphasis btn-text"}
|
2016-01-26 06:14:09 +08:00
|
|
|
style={order: -100}
|
2016-02-04 08:27:11 +08:00
|
|
|
primaryItem={@_contentForAction(preferred)}
|
|
|
|
primaryTitle={preferred.title}
|
2016-01-26 06:14:09 +08:00
|
|
|
primaryClick={@_onPrimaryClick}
|
|
|
|
closeOnMenuClick={true}
|
2016-02-04 08:27:11 +08:00
|
|
|
menu={@_dropdownMenu(rest)}/>
|
2016-01-26 06:14:09 +08:00
|
|
|
|
2016-02-04 08:27:11 +08:00
|
|
|
_orderedActionConfigs: =>
|
2016-01-26 06:14:09 +08:00
|
|
|
configKeys = _.pluck(@state.actionConfigs, "configKey")
|
2016-02-04 08:27:11 +08:00
|
|
|
preferredKey = NylasEnv.config.get(SendActionButton.CONFIG_KEY)
|
|
|
|
|
|
|
|
if not preferredKey? or preferredKey not in configKeys
|
|
|
|
preferredKey = @_defaultActionConfig().configKey
|
2016-01-26 06:14:09 +08:00
|
|
|
|
2016-02-04 08:27:11 +08:00
|
|
|
preferred = _.findWhere(@state.actionConfigs, configKey: preferredKey)
|
|
|
|
rest = _.without(@state.actionConfigs, preferred)
|
2016-01-26 06:14:09 +08:00
|
|
|
|
2016-02-04 08:27:11 +08:00
|
|
|
{preferred, rest}
|
2016-01-26 06:14:09 +08:00
|
|
|
|
2016-02-04 08:27:11 +08:00
|
|
|
_sendWithAction: ({onSend}) =>
|
2016-01-26 06:14:09 +08:00
|
|
|
isValidDraft = @props.isValidDraft()
|
|
|
|
if isValidDraft
|
|
|
|
try
|
|
|
|
onSend({draft: @props.draft})
|
|
|
|
catch err
|
2016-02-04 07:06:52 +08:00
|
|
|
NylasEnv.reportError(err)
|
2016-01-26 06:14:09 +08:00
|
|
|
|
2016-02-04 08:27:11 +08:00
|
|
|
_dropdownMenu: (actionConfigs) =>
|
2016-01-26 06:14:09 +08:00
|
|
|
<Menu items={actionConfigs}
|
|
|
|
itemKey={ (actionConfig) -> actionConfig.configKey }
|
2016-02-04 08:27:11 +08:00
|
|
|
itemContent={@_contentForAction}
|
|
|
|
onSelect={@_sendWithAction}
|
2016-01-26 06:14:09 +08:00
|
|
|
/>
|
|
|
|
|
2016-02-04 08:27:11 +08:00
|
|
|
_contentForAction: ({iconUrl}) =>
|
2016-01-26 06:14:09 +08:00
|
|
|
if iconUrl
|
|
|
|
plusHTML = <span> + </span>
|
|
|
|
additionalImg = <RetinaImg url={iconUrl}
|
|
|
|
mode={RetinaImg.Mode.ContentIsMask} />
|
|
|
|
else
|
|
|
|
plusHTML = ""
|
|
|
|
additionalImg = ""
|
|
|
|
|
|
|
|
<span>
|
2016-02-04 08:27:11 +08:00
|
|
|
<RetinaImg name="icon-composer-send.png" mode={RetinaImg.Mode.ContentIsMask} />
|
2016-01-26 06:14:09 +08:00
|
|
|
<span className="text">Send{plusHTML}</span>{additionalImg}
|
|
|
|
</span>
|
|
|
|
|
|
|
|
module.exports = SendActionButton
|