2016-11-29 18:53:31 +09:00
import Telescope from './config.js' ;
import { compose } from 'react-apollo' ; // note: at the moment, compose@react-apollo === compose@redux ; see https://github.com/apollostack/react-apollo/blob/master/src/index.ts#L4-L7
2016-11-29 11:35:20 +09:00
2016-12-06 18:06:29 +01:00
export const Components = { } ;
2016-11-29 11:35:20 +09:00
/ * *
* Register a Telescope component with a name , a raw component than can be extended
* and one or more optional higher order components .
*
* @ param { String } name The name of the component to register .
* @ param { React Component } rawComponent Interchangeable / extendable component .
* @ param { ... Function } hocs The HOCs to compose with the raw component .
2016-12-06 18:06:29 +01:00
* @ returns { Function | React Component } A component callable with Components [ name ]
2016-11-29 11:35:20 +09:00
*
* Note : when a component is registered without higher order component , ` hocs ` will be
* an empty array , and it ' s ok !
* See https : //github.com/reactjs/redux/blob/master/src/compose.js#L13-L15
* /
2016-12-06 18:06:29 +01:00
export const registerComponent = ( name , rawComponent , ... hocs ) => {
2016-11-29 11:35:20 +09:00
// console.log('// registering component');
// console.log(name);
// console.log('raw component', rawComponent);
// console.log('higher order components', hocs);
// compose the raw component with the HOCs given in option
2016-12-06 18:06:29 +01:00
Components [ name ] = compose ( ... hocs ) ( rawComponent ) ;
// keep track of the raw component & hocs, so we can extend the component if necessary
Components [ name ] . rawComponent = rawComponent ;
Components [ name ] . hocs = hocs ;
// ---- to remove later ----
2016-11-29 11:35:20 +09:00
Telescope . components [ name ] = compose ( ... hocs ) ( rawComponent ) ;
// keep track of the raw component & hocs, so we can extend the component if necessary
Telescope . components [ name ] . rawComponent = rawComponent ;
Telescope . components [ name ] . hocs = hocs ;
2016-12-06 18:06:29 +01:00
// -------------------------
2016-11-29 11:35:20 +09:00
2016-12-06 18:06:29 +01:00
return Components [ name ] ;
2016-11-29 11:35:20 +09:00
} ;
/ * *
* Get a component registered with Telescope . registerComponent ( name , component , ... hocs ) .
*
* @ param { String } name The name of the component to get .
* @ returns { Function | React Component } A ( wrapped ) React component
* /
2016-12-06 18:06:29 +01:00
export const getComponent = ( name ) => {
return Components [ name ] ;
2016-11-29 11:35:20 +09:00
} ;
/ * *
* Get the * * raw * * ( original ) component registered with Telescope . registerComponent
* without the possible HOCs wrapping it .
*
* @ param { String } name The name of the component to get .
* @ returns { Function | React Component } An interchangeable / extendable React component
* /
2016-12-06 18:06:29 +01:00
export const getRawComponent = ( name ) => {
return Components [ name ] . rawComponent ;
2016-11-29 11:35:20 +09:00
} ;
/ * *
* Replace a Telescope component with the same name with a new component or
* an extension of the raw component and one or more optional higher order components .
* This function keeps track of the previous HOCs and wrap the new HOCs around previous ones
*
* @ param { String } name The name of the component to register .
* @ param { React Component } rawComponent Interchangeable / extendable component .
* @ param { ... Function } hocs The HOCs to compose with the raw component .
2016-12-06 18:06:29 +01:00
* @ returns { Function | React Component } A component callable with Components [ name ]
2016-11-29 11:35:20 +09:00
*
* Note : when a component is registered without higher order component , ` hocs ` will be
* an empty array , and it ' s ok !
* See https : //github.com/reactjs/redux/blob/master/src/compose.js#L13-L15
* /
2016-12-06 18:06:29 +01:00
export const replaceComponent = ( name , newComponent , ... newHocs ) => {
const previousComponent = Components [ name ] ;
2016-11-29 11:35:20 +09:00
// xxx : throw an error if the previous component doesn't exist
// console.log('// replacing component');
// console.log(name);
// console.log(newComponent);
// console.log('new hocs', newHocs);
// console.log('previous hocs', previousComponent.hocs);
2016-12-06 18:06:29 +01:00
return registerComponent ( name , newComponent , ... newHocs , ... previousComponent . hocs ) ;
} ;
2016-12-10 21:58:40 +09:00
export const copyHoCs = ( sourceComponent , targetComponent ) => {
return compose ( ... sourceComponent . hocs ) ( targetComponent ) ;
}
2016-12-12 10:24:34 +09:00
// backwards compatibility
Telescope . components = { } ;
2016-12-06 18:06:29 +01:00
Telescope . registerComponent = registerComponent ;
Telescope . getComponent = getComponent ;
Telescope . getRawComponent = getRawComponent ;
2016-12-10 21:58:40 +09:00
Telescope . replaceComponent = replaceComponent ;
Telescope . copyHoCs = copyHoCs ;