2015-11-24 11:41:53 +08:00
|
|
|
React = require 'react'
|
|
|
|
_ = require 'underscore'
|
|
|
|
_str = require 'underscore.string'
|
|
|
|
AccountSidebarItem = require './account-sidebar-item'
|
|
|
|
|
2015-11-24 12:08:10 +08:00
|
|
|
{RetinaImg,
|
|
|
|
DisclosureTriangle} = require 'nylas-component-kit'
|
2015-11-24 11:41:53 +08:00
|
|
|
|
|
|
|
class AccountSidebarSection extends React.Component
|
|
|
|
@displayName: "AccountSidebarSection"
|
|
|
|
|
|
|
|
@propTypes: {
|
|
|
|
section: React.PropTypes.object.isRequired
|
|
|
|
collapsed: React.PropTypes.object.isRequired
|
|
|
|
selected: React.PropTypes.object.isRequired
|
|
|
|
onToggleCollapsed: React.PropTypes.func.isRequired
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor: (@props) ->
|
|
|
|
@state = {showCreateInput: false}
|
|
|
|
|
|
|
|
render: ->
|
|
|
|
section = @props.section
|
|
|
|
showInput = @state.showCreateInput
|
|
|
|
allowCreate = section.createItem?
|
|
|
|
|
|
|
|
<section>
|
|
|
|
<div className="heading">{section.label}</div>
|
|
|
|
{@_createItemButton(section) if allowCreate}
|
|
|
|
{@_createItemInput(section) if allowCreate and showInput}
|
|
|
|
{@_itemComponents(section.items)}
|
|
|
|
</section>
|
|
|
|
|
|
|
|
_createItemButton: ({label}) ->
|
|
|
|
<div
|
|
|
|
className="add-item-button"
|
2015-11-25 03:06:09 +08:00
|
|
|
onMouseDown={@_onCreateButtonMouseDown}
|
|
|
|
onMouseUp={@_onCreateButtonClicked.bind(@, label)}>
|
2015-11-24 11:41:53 +08:00
|
|
|
<RetinaImg
|
|
|
|
url="nylas://account-sidebar/assets/icon-sidebar-addcategory@2x.png"
|
|
|
|
style={height: 14, width: 14}
|
2015-11-24 12:08:10 +08:00
|
|
|
mode={RetinaImg.Mode.ContentIsMask} />
|
2015-11-24 11:41:53 +08:00
|
|
|
</div>
|
|
|
|
|
|
|
|
_createItemInput: (section) ->
|
|
|
|
label = _str.decapitalize section.label[...-1]
|
|
|
|
placeholder = "Create new #{label}"
|
|
|
|
<span className="item-container">
|
|
|
|
<div className="item add-item-container">
|
2015-11-24 12:08:10 +08:00
|
|
|
<DisclosureTriangle collapsed={false} visible={false} />
|
2015-11-24 11:41:53 +08:00
|
|
|
<div className="icon">
|
|
|
|
<RetinaImg
|
|
|
|
name="#{section.iconName}"
|
|
|
|
fallback="folder.png"
|
|
|
|
mode={RetinaImg.Mode.ContentIsMask} />
|
|
|
|
</div>
|
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
tabIndex="1"
|
|
|
|
className="input-bordered add-item-input"
|
|
|
|
autoFocus={true}
|
|
|
|
onKeyDown={_.partial @_onInputKeyDown, _, section}
|
2015-11-25 03:06:09 +08:00
|
|
|
onBlur={@_onInputBlur}
|
2015-11-24 11:41:53 +08:00
|
|
|
placeholder={placeholder}/>
|
|
|
|
</div>
|
|
|
|
</span>
|
|
|
|
|
|
|
|
_itemComponents: (items) =>
|
|
|
|
components = []
|
|
|
|
|
|
|
|
items.forEach (item) =>
|
|
|
|
components.push(
|
|
|
|
<AccountSidebarItem
|
|
|
|
key={item.id}
|
|
|
|
item={item}
|
|
|
|
collapsed={@props.collapsed[item.id]}
|
|
|
|
selected={@props.selected}
|
|
|
|
onDestroyItem={@props.section.destroyItem}
|
|
|
|
onToggleCollapsed={@props.onToggleCollapsed} />
|
|
|
|
)
|
|
|
|
|
|
|
|
if item.children.length and not @props.collapsed[item.id]
|
|
|
|
components.push(
|
|
|
|
<section key={"#{item.id}-children"}>
|
|
|
|
{@_itemComponents(item.children)}
|
|
|
|
</section>
|
|
|
|
)
|
|
|
|
|
|
|
|
components
|
|
|
|
|
2015-11-25 03:06:09 +08:00
|
|
|
_onCreateButtonMouseDown: =>
|
|
|
|
@_clickingCreateButton = true
|
|
|
|
|
2015-11-24 11:41:53 +08:00
|
|
|
_onCreateButtonClicked: (sectionLabel) =>
|
2015-11-25 03:06:09 +08:00
|
|
|
@_clickingCreateButton = false
|
2015-11-24 11:41:53 +08:00
|
|
|
@setState(showCreateInput: not @state.showCreateInput)
|
|
|
|
|
2015-11-25 03:06:09 +08:00
|
|
|
_onInputBlur: =>
|
|
|
|
@setState(showCreateInput: false) if not @_clickingCreateButton
|
|
|
|
|
2015-11-24 11:41:53 +08:00
|
|
|
_onInputKeyDown: (event, section) =>
|
|
|
|
if event.key is 'Escape'
|
|
|
|
@setState(showCreateInput: false)
|
|
|
|
if event.key in ['Enter', 'Return']
|
|
|
|
@props.section.createItem?(event.target.value)
|
|
|
|
@setState(showCreateInput: false)
|
|
|
|
|
|
|
|
module.exports = AccountSidebarSection
|