22// TODO: extract to a separate project.
33//
44
5- import React , { Component , PropTypes } from 'react' ;
5+ import React , { cloneElement , Children , Component , PropTypes } from 'react' ;
66import Dock from 'react-dock' ;
77import { combineReducers } from 'redux' ;
88
99const POSITIONS = [ 'left' , 'top' , 'right' , 'bottom' ] ;
1010
11- class DockMonitor extends Component {
11+ export default class DockMonitor extends Component {
1212 static propTypes = {
13+ defaultPosition : PropTypes . oneOf ( POSITIONS ) . isRequired ,
14+ defaultIsVisible : PropTypes . bool . isRequired ,
15+ toggleVisibilityShortcut : PropTypes . string . isRequired ,
16+ changePositionShortcut : PropTypes . string . isRequired ,
17+
1318 monitorState : PropTypes . shape ( {
1419 position : PropTypes . oneOf ( POSITIONS ) . isRequired ,
1520 isVisible : PropTypes . bool . isRequired ,
1621 child : PropTypes . any
17- } ) . isRequired ,
22+ } ) ,
1823
1924 monitorActions : PropTypes . shape ( {
2025 toggleVisibility : PropTypes . func . isRequired ,
2126 changePosition : PropTypes . func . isRequired
22- } ) . isRequired
27+ } )
28+ } ;
29+
30+ static defaultProps = {
31+ defaultIsVisible : true ,
32+ defaultPosition : 'right' ,
33+ toggleVisibilityShortcut : 'H' ,
34+ changePositionShortcut : 'Q'
2335 } ;
2436
2537 componentDidMount ( ) {
@@ -39,11 +51,11 @@ class DockMonitor extends Component {
3951
4052 const key = event . keyCode || event . which ;
4153 const char = String . fromCharCode ( key ) ;
42- switch ( char ) {
43- case 'H' :
54+ switch ( char . toUpperCase ( ) ) {
55+ case this . props . toggleVisibilityShortcut . toUpperCase ( ) :
4456 this . props . monitorActions . toggleVisibility ( ) ;
4557 break ;
46- case 'D' :
58+ case this . props . changePositionShortcut . toUpperCase ( ) :
4759 this . props . monitorActions . changePosition ( ) ;
4860 break ;
4961 default :
@@ -52,13 +64,29 @@ class DockMonitor extends Component {
5264 }
5365
5466 render ( ) {
55- const { children, monitorState } = this . props ;
56- const { position, isVisible } = monitorState ;
67+ const {
68+ monitorState,
69+ monitorActions,
70+ historyState,
71+ historyActions,
72+ children
73+ } = this . props ;
74+
75+ const {
76+ position,
77+ isVisible
78+ } = monitorState ;
79+
5780 return (
5881 < Dock position = { position }
5982 isVisible = { isVisible }
6083 dimMode = 'none' >
61- { children }
84+ { cloneElement ( Children . only ( children ) , {
85+ monitorState : monitorState . child ,
86+ monitorActions : monitorActions . child ,
87+ historyState,
88+ historyActions
89+ } ) }
6290 </ Dock >
6391 ) ;
6492 }
@@ -74,43 +102,32 @@ function changePosition() {
74102 return { type : CHANGE_POSITION } ;
75103}
76104
77- export default function create ( child , {
78- defaultIsVisible = true ,
79- defaultPosition = 'right'
80- } = { } ) {
81- function position ( state = defaultPosition , action ) {
105+ DockMonitor . setup = function setup ( props ) {
106+ function position ( state = props . defaultPosition , action ) {
82107 return ( action . type === CHANGE_POSITION ) ?
83108 POSITIONS [ ( POSITIONS . indexOf ( state ) + 1 ) % POSITIONS . length ] :
84109 state ;
85110 }
86111
87- function isVisible ( state = defaultIsVisible , action ) {
112+ function isVisible ( state = props . defaultIsVisible , action ) {
88113 return ( action . type === TOGGLE_VISIBILITY ) ?
89114 ! state :
90115 state ;
91116 }
92117
93- const ChildMonitor = child . component ;
94- const CompositeMonitor = ( { monitorState, monitorActions, ...rest } ) => (
95- < DockMonitor monitorState = { monitorState }
96- monitorActions = { monitorActions } >
97- < ChildMonitor { ...rest }
98- monitorState = { monitorState . child }
99- monitorActions = { monitorActions . child } />
100- </ DockMonitor >
101- ) ;
118+ const child = Children . only ( props . children ) ;
119+ const childSetupResult = child . type . setup ( child . props ) ;
102120
103121 return {
104- component : CompositeMonitor ,
105122 reducer : combineReducers ( {
106123 position,
107124 isVisible,
108- child : child . reducer
125+ child : childSetupResult . reducer
109126 } ) ,
110127 actionCreators : {
111128 toggleVisibility,
112129 changePosition,
113- child : child . actionCreators
130+ child : childSetupResult . actionCreators
114131 }
115132 } ;
116133}
0 commit comments