Mailspring/internal_packages/message-templates/lib/template-picker.cjsx
Ben Gotow eb6cc11a83 feature(templates): Pick templates, fill variable regions, Draft extensions
Summary:
fix(keymappings): Enter to focus item, logout works now

Minor fix for some problems with activity bar

Fix tabindex = 1 where tabindex should be =-1

Remove idgen that was causing footers to be replaced

WIP

Draft store extension hooks

Test Plan: Run tests

Reviewers: evan

Reviewed By: evan

Differential Revision: https://review.inboxapp.com/D1320
2015-03-20 10:23:50 -07:00

80 lines
2.2 KiB
CoffeeScript

_ = require 'underscore-plus'
React = require 'react'
TemplateStore = require './template-store'
{Actions, Message, DatabaseStore} = require 'inbox-exports'
{Popover, Menu, RetinaImg} = require 'ui-components'
module.exports =
TemplatePicker = React.createClass
displayName: 'TemplatePicker'
getInitialState: ->
searchValue: ""
templates: TemplateStore.items()
componentDidMount: ->
@unsubscribe = TemplateStore.listen @_onStoreChange
componentWillUnmount: ->
@unsubscribe() if @unsubscribe
render: ->
button = <button className="btn btn-toolbar">
<RetinaImg name="toolbar-templates.png"/>
<RetinaImg name="toolbar-chevron.png"/>
</button>
headerComponents = [
<input type="text"
tabIndex="1"
className="search"
value={@state.searchValue}
onChange={@_onSearchValueChange}/>
]
footerComponents = [
<div className="item" key="new" onClick={@_onNewTemplate}>Save as Template...</div>
<div className="item" key="manage" onClick={@_onManageTemplates}>Open Templates Folder...</div>
]
<Popover ref="popover" className="template-picker pull-right" buttonComponent={button}>
<Menu ref="menu"
headerComponents={headerComponents}
footerComponents={footerComponents}
items={@state.templates}
itemKey={ (item) -> item.id }
itemContent={ (item) -> item.name }
onSelect={@_onChooseTemplate}
/>
</Popover>
_filteredTemplates: (search) ->
search ?= @state.searchValue
items = TemplateStore.items()
return items unless search.length
_.filter items, (t) ->
t.name.toLowerCase().indexOf(search.toLowerCase()) == 0
_onStoreChange: ->
@setState
templates: @_filteredTemplates()
_onSearchValueChange: ->
newSearch = event.target.value
@setState
searchValue: newSearch
templates: @_filteredTemplates(newSearch)
_onChooseTemplate: (template) ->
Actions.insertTemplateId({templateId:template.id, draftLocalId: @props.draftLocalId})
@refs.popover.close()
_onManageTemplates: ->
Actions.showTemplates()
_onNewTemplate: ->
Actions.createTemplate({draftLocalId: @props.draftLocalId})