1- //
2- // TODO: extract to a separate project.
3- //
4-
5- import React , { cloneElement , Children , Component , PropTypes } from 'react' ;
1+ import React , { cloneElement , Component , PropTypes } from 'react' ;
62import Dock from 'react-dock' ;
7- import { combineReducers } from 'redux ' ;
8-
9- const POSITIONS = [ 'left' , 'top' , 'right' , 'bottom' ] ;
3+ import { POSITIONS } from './constants ' ;
4+ import { toggleVisibility , changePosition , changeSize } from './actions' ;
5+ import reducer from './reducers' ;
106
117export default class DockMonitor extends Component {
8+ static reducer = reducer ;
9+
1210 static propTypes = {
1311 defaultPosition : PropTypes . oneOf ( POSITIONS ) . isRequired ,
1412 defaultIsVisible : PropTypes . bool . isRequired ,
13+ defaultSize : PropTypes . number . isRequired ,
1514 toggleVisibilityShortcut : PropTypes . string . isRequired ,
1615 changePositionShortcut : PropTypes . string . isRequired ,
16+ children : PropTypes . element ,
1717
18+ dispatch : PropTypes . func ,
1819 monitorState : PropTypes . shape ( {
1920 position : PropTypes . oneOf ( POSITIONS ) . isRequired ,
21+ size : PropTypes . number . isRequired ,
2022 isVisible : PropTypes . bool . isRequired ,
21- child : PropTypes . any
22- } ) ,
23-
24- monitorActions : PropTypes . shape ( {
25- toggleVisibility : PropTypes . func . isRequired ,
26- changePosition : PropTypes . func . isRequired
23+ childMonitorState : PropTypes . any
2724 } )
2825 } ;
2926
3027 static defaultProps = {
3128 defaultIsVisible : true ,
3229 defaultPosition : 'right' ,
30+ defaultSize : 0.3 ,
3331 toggleVisibilityShortcut : 'H' ,
3432 changePositionShortcut : 'Q'
3533 } ;
3634
37- componentDidMount ( ) {
35+ constructor ( props ) {
36+ super ( props ) ;
3837 this . handleKeyDown = this . handleKeyDown . bind ( this ) ;
38+ this . handleSizeChange = this . handleSizeChange . bind ( this ) ;
39+ }
40+
41+ componentDidMount ( ) {
3942 window . addEventListener ( 'keydown' , this . handleKeyDown ) ;
4043 }
4144
@@ -53,81 +56,36 @@ export default class DockMonitor extends Component {
5356 const char = String . fromCharCode ( key ) ;
5457 switch ( char . toUpperCase ( ) ) {
5558 case this . props . toggleVisibilityShortcut . toUpperCase ( ) :
56- this . props . monitorActions . toggleVisibility ( ) ;
59+ this . props . dispatch ( toggleVisibility ( ) ) ;
5760 break ;
5861 case this . props . changePositionShortcut . toUpperCase ( ) :
59- this . props . monitorActions . changePosition ( ) ;
62+ this . props . dispatch ( changePosition ( ) ) ;
6063 break ;
6164 default :
6265 break ;
6366 }
6467 }
6568
66- render ( ) {
67- const {
68- monitorState,
69- monitorActions,
70- historyState,
71- historyActions,
72- children
73- } = this . props ;
69+ handleSizeChange ( requestedSize ) {
70+ this . props . dispatch ( changeSize ( requestedSize ) ) ;
71+ }
7472
75- const {
76- position,
77- isVisible
78- } = monitorState ;
73+ render ( ) {
74+ const { monitorState, children, ...rest } = this . props ;
75+ const { position, isVisible, size } = monitorState ;
76+ const childProps = {
77+ ...rest ,
78+ monitorState : monitorState . childMonitorState
79+ } ;
7980
8081 return (
8182 < Dock position = { position }
8283 isVisible = { isVisible }
84+ size = { size }
85+ onSizeChange = { this . handleSizeChange }
8386 dimMode = 'none' >
84- { cloneElement ( Children . only ( children ) , {
85- monitorState : monitorState . child ,
86- monitorActions : monitorActions . child ,
87- historyState,
88- historyActions
89- } ) }
87+ { cloneElement ( children , childProps ) }
9088 </ Dock >
9189 ) ;
9290 }
9391}
94-
95- const TOGGLE_VISIBILITY = '@@redux-devtools/dock/TOGGLE_VISIBILITY' ;
96- function toggleVisibility ( ) {
97- return { type : TOGGLE_VISIBILITY } ;
98- }
99-
100- const CHANGE_POSITION = '@@redux-devtools/dock/CHANGE_POSITION' ;
101- function changePosition ( ) {
102- return { type : CHANGE_POSITION } ;
103- }
104-
105- DockMonitor . setup = function setup ( props ) {
106- function position ( state = props . defaultPosition , action ) {
107- return ( action . type === CHANGE_POSITION ) ?
108- POSITIONS [ ( POSITIONS . indexOf ( state ) + 1 ) % POSITIONS . length ] :
109- state ;
110- }
111-
112- function isVisible ( state = props . defaultIsVisible , action ) {
113- return ( action . type === TOGGLE_VISIBILITY ) ?
114- ! state :
115- state ;
116- }
117-
118- const child = Children . only ( props . children ) ;
119- const childSetupResult = child . type . setup ( child . props ) ;
120-
121- return {
122- reducer : combineReducers ( {
123- position,
124- isVisible,
125- child : childSetupResult . reducer
126- } ) ,
127- actionCreators : {
128- toggleVisibility,
129- changePosition,
130- child : childSetupResult . actionCreators
131- }
132- } ;
133- }
0 commit comments