2015-04-01 01:02:57 +00:00
var React = require ( 'React' ) ;
2015-04-19 22:53:30 +00:00
var DeviceEventEmitter = require ( 'RCTDeviceEventEmitter' ) ;
2015-04-01 01:02:57 +00:00
var NativeModules = require ( 'NativeModules' ) ;
var ReactIOSViewAttributes = require ( 'ReactIOSViewAttributes' ) ;
var StyleSheet = require ( 'StyleSheet' ) ;
var createReactIOSNativeComponentClass = require ( 'createReactIOSNativeComponentClass' ) ;
var PropTypes = require ( 'ReactPropTypes' ) ;
var StyleSheetPropType = require ( 'StyleSheetPropType' ) ;
var NativeMethodsMixin = require ( 'NativeMethodsMixin' ) ;
var flattenStyle = require ( 'flattenStyle' ) ;
var merge = require ( 'merge' ) ;
2015-05-01 08:16:32 +00:00
var constants = {
Aspect : NativeModules . CameraManager . Aspect ,
2015-05-01 21:33:23 +00:00
Type : NativeModules . CameraManager . Type ,
2015-05-01 08:16:32 +00:00
CaptureMode : NativeModules . CameraManager . CaptureMode ,
CaptureTarget : NativeModules . CameraManager . CaptureTarget ,
Orientation : NativeModules . CameraManager . Orientation
} ;
2015-04-01 01:02:57 +00:00
var Camera = React . createClass ( {
propTypes : {
2015-05-01 21:33:23 +00:00
aspect : PropTypes . oneOfType ( [
PropTypes . string ,
PropTypes . number
] ) ,
captureMode : PropTypes . oneOfType ( [
PropTypes . string ,
PropTypes . number
] ) ,
captureTarget : PropTypes . oneOfType ( [
PropTypes . string ,
PropTypes . number
] ) ,
type : PropTypes . oneOfType ( [
PropTypes . string ,
PropTypes . number
] ) ,
orientation : PropTypes . oneOfType ( [
PropTypes . string ,
PropTypes . number
] )
2015-04-01 01:02:57 +00:00
} ,
mixins : [ NativeMethodsMixin ] ,
viewConfig : {
uiViewClassName : 'UIView' ,
validAttributes : ReactIOSViewAttributes . UIView
} ,
2015-05-01 08:16:32 +00:00
getDefaultProps ( ) {
return {
aspect : constants . Aspect . fill ,
2015-05-01 21:33:23 +00:00
type : constants . Type . back ,
orientation : constants . Orientation . auto ,
2015-05-01 08:16:32 +00:00
captureMode : constants . CaptureMode . still ,
captureTarget : constants . CaptureTarget . memory
} ;
2015-04-30 07:10:21 +00:00
} ,
2015-05-01 08:16:32 +00:00
getInitialState ( ) {
2015-04-01 01:02:57 +00:00
return {
2015-05-01 08:16:32 +00:00
isAuthorized : false
2015-04-01 01:02:57 +00:00
} ;
} ,
2015-05-01 08:16:32 +00:00
componentWillMount ( ) {
2015-04-01 01:02:57 +00:00
NativeModules . CameraManager . checkDeviceAuthorizationStatus ( ( function ( err , isAuthorized ) {
this . state . isAuthorized = isAuthorized ;
this . setState ( this . state ) ;
} ) . bind ( this ) ) ;
2015-04-19 22:53:30 +00:00
this . cameraBarCodeReadListener = DeviceEventEmitter . addListener ( 'CameraBarCodeRead' , this . _onBarCodeRead ) ;
} ,
2015-05-01 08:16:32 +00:00
componentWillUnmount ( ) {
2015-04-19 22:53:30 +00:00
this . cameraBarCodeReadListener . remove ( ) ;
2015-04-01 01:02:57 +00:00
} ,
2015-05-01 08:16:32 +00:00
render ( ) {
2015-04-01 01:02:57 +00:00
var style = flattenStyle ( [ styles . base , this . props . style ] ) ;
2015-04-01 02:27:18 +00:00
2015-05-01 08:16:32 +00:00
var aspect = this . props . aspect ,
type = this . props . type ,
orientation = this . props . orientation ;
2015-05-01 21:33:23 +00:00
var legacyProps = {
aspect : {
2015-05-02 08:13:40 +00:00
Fill : 'fill' ,
Fit : 'fit' ,
Stretch : 'stretch'
2015-05-01 21:33:23 +00:00
} ,
orientation : {
2015-05-02 08:13:40 +00:00
LandscapeLeft : 'landscapeLeft' ,
LandscapeRight : 'landscapeRight' ,
Portrait : 'portrait' ,
PortraitUpsideDown : 'portraitUpsideDown'
2015-05-01 21:33:23 +00:00
} ,
type : {
2015-05-02 08:13:40 +00:00
Front : 'front' ,
Back : 'back'
2015-05-01 21:33:23 +00:00
}
} ;
var foundLegacyAspect = legacyProps . aspect [ aspect ] ;
var foundLegacyOrientation = legacyProps . orientation [ orientation ] ;
var foundLegacyType = legacyProps . type [ type ] ;
2015-05-01 08:16:32 +00:00
2015-05-01 21:33:23 +00:00
if ( _ _DEV _ _ ) {
2015-05-02 08:13:40 +00:00
if ( foundLegacyAspect ) {
2015-05-01 21:33:23 +00:00
console . log ( ` RCTCamera: aspect=" ${ aspect } " is deprecated, use aspect={Camera.constants.Aspect. ${ foundLegacyAspect } } instead. ` ) ;
2015-05-02 08:13:40 +00:00
aspect = constants . Aspect [ foundLegacyAspect ] ;
2015-05-01 08:16:32 +00:00
}
2015-05-02 08:13:40 +00:00
if ( foundLegacyOrientation ) {
2015-05-01 21:33:23 +00:00
console . log ( ` RCTCamera: orientation=" ${ orientation } " is deprecated, use orientation={Camera.constants.Orientation. ${ foundLegacyOrientation } } instead. ` ) ;
2015-05-02 08:13:40 +00:00
orientation = constants . Orientation [ foundLegacyOrientation ] ;
2015-05-01 21:33:23 +00:00
}
2015-05-02 08:13:40 +00:00
if ( foundLegacyType ) {
2015-05-01 21:33:23 +00:00
console . log ( ` RCTCamera: type=" ${ type } " is deprecated, use type={Camera.constants.Type. ${ foundLegacyType } } instead. ` ) ;
2015-05-02 08:13:40 +00:00
type = constants . Type [ foundLegacyType ] ;
2015-05-01 21:33:23 +00:00
}
}
if ( typeof aspect === 'string' ) {
aspect = constants . Aspect [ aspect ] ;
}
if ( typeof orientation === 'string' ) {
orientation = constants . Orientation [ orientation ] ;
2015-05-01 08:16:32 +00:00
}
2015-05-01 21:33:23 +00:00
if ( typeof type === 'string' ) {
type = constants . Type [ type ] ;
2015-05-01 08:16:32 +00:00
}
2015-04-01 01:02:57 +00:00
var nativeProps = merge ( this . props , {
style ,
2015-04-01 02:27:18 +00:00
aspect : aspect ,
2015-04-08 20:51:31 +00:00
type : type ,
2015-04-19 22:53:30 +00:00
orientation : orientation
2015-04-01 01:02:57 +00:00
} ) ;
return < RCTCamera { ... nativeProps } / >
} ,
2015-04-02 09:40:03 +00:00
2015-04-19 22:53:30 +00:00
_onBarCodeRead ( e ) {
this . props . onBarCodeRead && this . props . onBarCodeRead ( e ) ;
} ,
2015-05-01 08:16:32 +00:00
capture ( options , cb ) {
2015-04-02 09:40:03 +00:00
2015-05-01 08:16:32 +00:00
if ( arguments . length == 1 ) {
cb = options ;
options = { } ;
}
options = Object . assign ( { } , {
mode : this . props . captureMode ,
target : this . props . captureTarget
} , options ) ;
2015-05-01 04:04:16 +00:00
2015-05-01 21:33:23 +00:00
if ( typeof options . mode === 'string' ) {
options . mode = constants . CaptureMode [ options . mode ] ;
}
if ( typeof options . target === 'string' ) {
2015-05-04 19:27:36 +00:00
options . target = constants . CaptureTarget [ options . target ] ;
2015-05-01 21:33:23 +00:00
}
2015-05-01 08:16:32 +00:00
NativeModules . CameraManager . capture ( options , cb ) ;
2015-04-02 09:40:03 +00:00
}
2015-05-01 04:04:16 +00:00
2015-04-01 01:02:57 +00:00
} ) ;
var RCTCamera = createReactIOSNativeComponentClass ( {
2015-04-01 17:32:53 +00:00
validAttributes : merge ( ReactIOSViewAttributes . UIView , {
aspect : true ,
2015-04-08 20:51:31 +00:00
type : true ,
2015-04-01 17:32:53 +00:00
orientation : true
} ) ,
2015-04-01 01:02:57 +00:00
uiViewClassName : 'RCTCamera' ,
} ) ;
var styles = StyleSheet . create ( {
2015-04-08 20:51:31 +00:00
base : { } ,
2015-04-01 01:02:57 +00:00
} ) ;
2015-05-01 08:16:32 +00:00
Camera . constants = constants ;
2015-04-01 01:02:57 +00:00
module . exports = Camera ;