44
55import React , { Component , PropTypes } from 'react' ;
66import Dock from 'react-dock' ;
7- import MapProvider from './MapProvider' ;
87import { connect } from 'react-redux' ;
9- import { combineReducers } from 'redux' ;
10-
11- const TOGGLE_VISIBILITY = '@@redux-devtools/dock/TOGGLE_VISIBILITY' ;
12- function toggleVisibility ( ) {
13- return { type : TOGGLE_VISIBILITY } ;
14- }
15-
16- const CHANGE_POSITION = '@@redux-devtools/dock/CHANGE_POSITION' ;
17- function changePosition ( ) {
18- return { type : CHANGE_POSITION } ;
19- }
8+ import { combineReducers , bindActionCreators } from 'redux' ;
209
2110const POSITIONS = [ 'left' , 'top' , 'right' , 'bottom' ] ;
2211
23- function wrapReducer ( options = { } ) {
24- const {
25- isVisible : initialIsVisible = true ,
26- position : initialPosition = 'right'
27- } = options ;
28-
29- function position ( state = initialPosition , action ) {
30- return ( action . type === CHANGE_POSITION ) ?
31- POSITIONS [ ( POSITIONS . indexOf ( state ) + 1 ) % POSITIONS . length ] :
32- state ;
33- }
34-
35- function isVisible ( state = initialIsVisible , action ) {
36- return ( action . type === TOGGLE_VISIBILITY ) ?
37- ! state :
38- state ;
39- }
40-
41- return childMonitorReducer => combineReducers ( {
42- childMonitorState : childMonitorReducer ,
43- position,
44- isVisible
45- } ) ;
46- }
47-
48- function mapUpstreamStateToDownstreamState ( state ) {
49- return {
50- devToolsState : state . devToolsState ,
51- monitorState : state . monitorState . childMonitorState
52- } ;
53- }
54-
55- @connect (
56- state => state . monitorState ,
57- { toggleVisibility, changePosition }
58- )
59- export default class DockMonitor extends Component {
12+ class DockMonitor extends Component {
6013 static propTypes = {
61- position : PropTypes . oneOf ( [ 'left' , 'top' , 'right' , 'bottom' ] ) . isRequired ,
62- isVisible : PropTypes . bool . isRequired ,
63- childMonitorState : PropTypes . any ,
64- toggleVisibility : PropTypes . func . isRequired ,
65- changePosition : PropTypes . func . isRequired
14+ monitorState : PropTypes . shape ( {
15+ position : PropTypes . oneOf ( POSITIONS ) . isRequired ,
16+ isVisible : PropTypes . bool . isRequired ,
17+ childState : PropTypes . any
18+ } ) . isRequired ,
19+
20+ monitorActions : PropTypes . shape ( {
21+ toggleVisibility : PropTypes . func . isRequired ,
22+ changePosition : PropTypes . func . isRequired
23+ } ) . isRequired
6624 } ;
6725
6826 componentDidMount ( ) {
@@ -84,28 +42,86 @@ export default class DockMonitor extends Component {
8442 const char = String . fromCharCode ( key ) ;
8543 switch ( char ) {
8644 case 'H' :
87- this . props . toggleVisibility ( ) ;
45+ this . props . monitorActions . toggleVisibility ( ) ;
8846 break ;
8947 case 'D' :
90- this . props . changePosition ( ) ;
48+ this . props . monitorActions . changePosition ( ) ;
9149 break ;
9250 default :
9351 break ;
9452 }
9553 }
9654
9755 render ( ) {
98- const { position, isVisible, children } = this . props ;
56+ const { children, monitorState } = this . props ;
57+ const { position, isVisible } = monitorState ;
9958 return (
10059 < Dock position = { position }
10160 isVisible = { isVisible }
10261 dimMode = 'none' >
103- < MapProvider mapState = { mapUpstreamStateToDownstreamState } >
104- { children }
105- </ MapProvider >
62+ { children }
10663 </ Dock >
10764 ) ;
10865 }
10966}
11067
111- DockMonitor . wrapReducer = wrapReducer ;
68+ const TOGGLE_VISIBILITY = '@@redux-devtools/dock/TOGGLE_VISIBILITY' ;
69+ function toggleVisibility ( ) {
70+ return { type : TOGGLE_VISIBILITY } ;
71+ }
72+
73+ const CHANGE_POSITION = '@@redux-devtools/dock/CHANGE_POSITION' ;
74+ function changePosition ( ) {
75+ return { type : CHANGE_POSITION } ;
76+ }
77+
78+ export default function create ( ChildMonitor , {
79+ defaultIsVisible = true ,
80+ defaultPosition = 'right'
81+ } = { } ) {
82+ function position ( state = defaultPosition , action ) {
83+ return ( action . type === CHANGE_POSITION ) ?
84+ POSITIONS [ ( POSITIONS . indexOf ( state ) + 1 ) % POSITIONS . length ] :
85+ state ;
86+ }
87+
88+ function isVisible ( state = defaultIsVisible , action ) {
89+ return ( action . type === TOGGLE_VISIBILITY ) ?
90+ ! state :
91+ state ;
92+ }
93+
94+ function getChildStore ( store ) {
95+ return {
96+ ...store ,
97+ getState ( ) {
98+ const state = store . getState ( ) ;
99+ return {
100+ ...state ,
101+ monitorState : state . monitorState . childState
102+ } ;
103+ }
104+ } ;
105+ }
106+
107+ const Monitor = connect (
108+ state => state ,
109+ dispatch => ( {
110+ monitorActions : bindActionCreators ( { toggleVisibility, changePosition } , dispatch )
111+ } )
112+ ) ( DockMonitor ) ;
113+
114+ const CompositeMonitor = ( { store } ) => (
115+ < Monitor store = { store } >
116+ < ChildMonitor store = { getChildStore ( store ) } />
117+ </ Monitor >
118+ ) ;
119+
120+ CompositeMonitor . reducer = combineReducers ( {
121+ childState : ChildMonitor . reducer ,
122+ position,
123+ isVisible
124+ } ) ;
125+
126+ return CompositeMonitor ;
127+ }
0 commit comments