2016-06-08 21:30:46 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*
|
|
|
|
* @providesModule AppContainer
|
2016-11-07 04:49:10 +00:00
|
|
|
* @flow
|
2016-06-08 21:30:46 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2016-11-07 04:49:10 +00:00
|
|
|
const EmitterSubscription = require('EmitterSubscription');
|
|
|
|
const RCTDeviceEventEmitter = require('RCTDeviceEventEmitter');
|
|
|
|
const React = require('React');
|
|
|
|
const ReactNative = require('ReactNative');
|
|
|
|
const StyleSheet = require('StyleSheet');
|
|
|
|
const View = require('View');
|
2016-06-08 21:30:46 +00:00
|
|
|
|
2016-11-07 04:49:10 +00:00
|
|
|
type Context = {
|
2016-11-18 09:31:14 +00:00
|
|
|
rootTag: number,
|
2016-11-07 04:49:10 +00:00
|
|
|
};
|
|
|
|
type Props = {
|
|
|
|
children?: React.Children,
|
2016-11-18 09:31:14 +00:00
|
|
|
rootTag: number,
|
2016-11-07 04:49:10 +00:00
|
|
|
};
|
|
|
|
type State = {
|
|
|
|
inspector: ?React.Element<*>,
|
|
|
|
mainKey: number,
|
|
|
|
};
|
2016-06-08 21:30:46 +00:00
|
|
|
|
2016-11-07 04:49:10 +00:00
|
|
|
class AppContainer extends React.Component {
|
|
|
|
props: Props;
|
|
|
|
state: State = {
|
|
|
|
inspector: null,
|
|
|
|
mainKey: 1,
|
|
|
|
};
|
|
|
|
_mainRef: ?React.Element<*>;
|
|
|
|
_subscription: ?EmitterSubscription = null;
|
2016-06-08 21:30:46 +00:00
|
|
|
|
2016-11-07 04:49:10 +00:00
|
|
|
static childContextTypes = {
|
|
|
|
rootTag: React.PropTypes.number,
|
|
|
|
};
|
2016-06-08 21:30:46 +00:00
|
|
|
|
2016-11-07 04:49:10 +00:00
|
|
|
getChildContext(): Context {
|
|
|
|
return {
|
|
|
|
rootTag: this.props.rootTag,
|
|
|
|
};
|
|
|
|
}
|
2016-06-08 21:30:46 +00:00
|
|
|
|
2016-11-07 04:49:10 +00:00
|
|
|
componentDidMount(): void {
|
|
|
|
if (__DEV__) {
|
|
|
|
this._subscription = RCTDeviceEventEmitter.addListener(
|
|
|
|
'toggleElementInspector',
|
|
|
|
() => {
|
|
|
|
const Inspector = require('Inspector');
|
|
|
|
const inspector = this.state.inspector
|
|
|
|
? null
|
|
|
|
: <Inspector
|
|
|
|
inspectedViewTag={ReactNative.findNodeHandle(this._mainRef)}
|
|
|
|
onRequestRerenderApp={(updateInspectedViewTag) => {
|
|
|
|
this.setState(
|
|
|
|
(s) => ({mainKey: s.mainKey + 1}),
|
|
|
|
() => updateInspectedViewTag(
|
|
|
|
ReactNative.findNodeHandle(this._mainRef)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
/>;
|
|
|
|
this.setState({inspector});
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount(): void {
|
|
|
|
if (this._subscription) {
|
|
|
|
this._subscription.remove();
|
|
|
|
}
|
|
|
|
}
|
2016-06-08 21:30:46 +00:00
|
|
|
|
2016-11-07 04:49:10 +00:00
|
|
|
render(): React.Element<*> {
|
2016-06-08 21:30:46 +00:00
|
|
|
let yellowBox = null;
|
|
|
|
if (__DEV__) {
|
2016-11-07 04:49:10 +00:00
|
|
|
const YellowBox = require('YellowBox');
|
2016-06-08 21:30:46 +00:00
|
|
|
yellowBox = <YellowBox />;
|
|
|
|
}
|
2016-11-07 04:49:10 +00:00
|
|
|
|
2016-06-08 21:30:46 +00:00
|
|
|
return (
|
2017-01-23 19:22:39 +00:00
|
|
|
<View style={styles.appContainer} pointerEvents="box-none">
|
2016-06-08 21:30:46 +00:00
|
|
|
<View
|
2016-06-13 23:34:48 +00:00
|
|
|
collapsable={!this.state.inspector}
|
2016-06-08 21:30:46 +00:00
|
|
|
key={this.state.mainKey}
|
2017-01-23 19:22:39 +00:00
|
|
|
pointerEvents="box-none"
|
2016-11-07 04:49:10 +00:00
|
|
|
style={styles.appContainer} ref={(ref) => {this._mainRef = ref;}}>
|
2016-06-08 21:30:46 +00:00
|
|
|
{this.props.children}
|
|
|
|
</View>
|
|
|
|
{yellowBox}
|
|
|
|
{this.state.inspector}
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
2016-11-07 04:49:10 +00:00
|
|
|
}
|
2016-06-08 21:30:46 +00:00
|
|
|
|
2016-11-07 04:49:10 +00:00
|
|
|
const styles = StyleSheet.create({
|
2016-06-08 21:30:46 +00:00
|
|
|
appContainer: {
|
|
|
|
flex: 1,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = AppContainer;
|