2015-02-21 05:17:11 +08:00
|
|
|
React = require 'react'
|
|
|
|
_ = require 'underscore-plus'
|
|
|
|
{Actions,ComponentRegistry} = require "inbox-exports"
|
2015-02-28 07:34:37 +08:00
|
|
|
Flexbox = require './components/flexbox.cjsx'
|
|
|
|
ResizableRegion = require './components/resizable-region.cjsx'
|
2015-02-21 05:17:11 +08:00
|
|
|
|
|
|
|
module.exports =
|
|
|
|
Sheet = React.createClass
|
|
|
|
displayName: 'Sheet'
|
|
|
|
|
|
|
|
propTypes:
|
|
|
|
type: React.PropTypes.string.isRequired
|
|
|
|
depth: React.PropTypes.number.isRequired
|
|
|
|
columns: React.PropTypes.arrayOf(React.PropTypes.string)
|
2015-03-13 06:07:38 +08:00
|
|
|
onColumnSizeChanged: React.PropTypes.func
|
2015-02-21 05:17:11 +08:00
|
|
|
|
|
|
|
getDefaultProps: ->
|
|
|
|
columns: ['Left', 'Center', 'Right']
|
|
|
|
|
|
|
|
getInitialState: ->
|
|
|
|
@_getComponentRegistryState()
|
|
|
|
|
|
|
|
componentDidMount: ->
|
|
|
|
@unlistener = ComponentRegistry.listen (event) =>
|
|
|
|
@setState(@_getComponentRegistryState())
|
|
|
|
|
|
|
|
componentWillUnmount: ->
|
|
|
|
@unlistener() if @unlistener
|
|
|
|
|
|
|
|
render: ->
|
|
|
|
style =
|
|
|
|
position:'absolute'
|
|
|
|
backgroundColor:'white'
|
|
|
|
width:'100%'
|
|
|
|
height:'100%'
|
|
|
|
|
2015-03-13 06:07:38 +08:00
|
|
|
<div name={"Sheet"}
|
|
|
|
style={style}
|
|
|
|
data-type={@props.type}>
|
2015-02-21 05:17:11 +08:00
|
|
|
<Flexbox direction="row">
|
|
|
|
{@_backButtonComponent()}
|
|
|
|
{@_columnFlexboxComponents()}
|
|
|
|
</Flexbox>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
_backButtonComponent: ->
|
|
|
|
return [] if @props.depth is 0
|
2015-03-13 08:55:26 +08:00
|
|
|
<div onClick={@_pop} key="back">
|
2015-02-21 05:17:11 +08:00
|
|
|
Back
|
|
|
|
</div>
|
|
|
|
|
|
|
|
_columnFlexboxComponents: ->
|
|
|
|
@props.columns.map (column) =>
|
|
|
|
classes = @state[column] || []
|
|
|
|
return if classes.length is 0
|
|
|
|
|
2015-03-13 08:55:26 +08:00
|
|
|
components = classes.map ({name, view}) => <view key={name} {...@props} />
|
2015-03-07 03:05:25 +08:00
|
|
|
|
2015-03-13 08:55:26 +08:00
|
|
|
maxWidth = _.reduce classes, ((m,{view}) -> Math.min(view.maxWidth ? 10000, m)), 10000
|
|
|
|
minWidth = _.reduce classes, ((m,{view}) -> Math.max(view.minWidth ? 0, m)), 0
|
2015-02-21 05:17:11 +08:00
|
|
|
resizable = minWidth != maxWidth && column != 'Center'
|
|
|
|
|
|
|
|
if resizable
|
2015-02-28 07:34:37 +08:00
|
|
|
if column is 'Left' then handle = ResizableRegion.Handle.Right
|
|
|
|
if column is 'Right' then handle = ResizableRegion.Handle.Left
|
2015-03-13 08:55:26 +08:00
|
|
|
<ResizableRegion key={"#{@props.type}:#{column}"}
|
|
|
|
name={"#{@props.type}:#{column}"}
|
2015-03-13 06:07:38 +08:00
|
|
|
data-column={column}
|
|
|
|
onResize={@props.onColumnSizeChanged}
|
|
|
|
minWidth={minWidth}
|
|
|
|
maxWidth={maxWidth}
|
|
|
|
handle={handle}>
|
|
|
|
<Flexbox direction="column">
|
2015-02-21 05:17:11 +08:00
|
|
|
{components}
|
|
|
|
</Flexbox>
|
2015-02-28 07:34:37 +08:00
|
|
|
</ResizableRegion>
|
2015-02-21 05:17:11 +08:00
|
|
|
else
|
2015-03-13 06:07:38 +08:00
|
|
|
<Flexbox direction="column"
|
2015-03-13 08:55:26 +08:00
|
|
|
key={"#{@props.type}:#{column}"}
|
2015-03-13 06:07:38 +08:00
|
|
|
name={"#{@props.type}:#{column}"}
|
|
|
|
data-column={column}
|
|
|
|
style={flex: 1}>
|
2015-02-21 05:17:11 +08:00
|
|
|
{components}
|
|
|
|
</Flexbox>
|
|
|
|
|
|
|
|
_getComponentRegistryState: ->
|
|
|
|
state = {}
|
|
|
|
for column in @props.columns
|
2015-03-13 08:55:26 +08:00
|
|
|
state["#{column}"] = ComponentRegistry.findAllByRole("#{@props.type}:#{column}")
|
2015-02-21 05:17:11 +08:00
|
|
|
state
|
|
|
|
|
|
|
|
_pop: ->
|
|
|
|
Actions.popSheet()
|