UNPKG

2.73 kBJavaScriptView Raw
1'use strict';
2
3var React = require( 'react' );
4var ReactDOM = require( 'react-dom' );
5var _ = require( 'lodash' );
6var Provider = require( 'react-redux' ).Provider;
7
8
9module.exports = function( pageComponents, templateComponents, store, useRedux ){
10
11
12 /*
13 * Loop over each component on the page
14 */
15
16 pageComponents.components.forEach( ( component, number, componentArry ) => {
17
18 /**
19 * Check to see if the component matches one included
20 * in the template
21 */
22
23 if( templateComponents[ component.name ] ){
24 initilizeComponents( templateComponents[ component.name ], component, store, useRedux );
25 }
26
27 } );
28
29};
30
31function initilizeComponents( templateComponent, pageComponent, store, useRedux ){
32
33 pageComponent.instances.forEach( ( component, number, componentArry ) => {
34
35 /*
36 * check to see if the component is a basic component
37 */
38 if( templateComponent.hasOwnProperty( 'init' ) ){
39
40 if( useRedux ) {
41 templateComponent.init( component.config, component.data, store );
42 } else{
43 templateComponent.init( component.config, component.data );
44 }
45
46 }
47
48 /*
49 * check to see if the component is a react component
50 */
51 if( templateComponent.hasOwnProperty( 'component' ) ){
52
53 if( useRedux ){
54 /**
55 * Wrap component in a provider component that will provide
56 * the component with the hooks to access the global redux
57 * store
58 */
59
60 let elementToBindTo = document.querySelector('[data-hook="' + component.config.hook + '"]');
61
62 if( elementToBindTo ){
63 ReactDOM.render(
64 <Provider store={store}>
65 <templateComponent.component data={component.config}/>
66 </Provider>,
67 elementToBindTo
68 );
69 }
70 } else {
71
72 /**
73 * loop over each instance of the component found on
74 * the page and initilize it
75 */
76 let elementToBindTo = document.querySelector('[data-hook="' + component.config.hook + '"]');
77
78 if( elementToBindTo ){
79 ReactDOM.render(
80 <templateComponent.component data={component.config}/>,
81 document.querySelector('[data-hook="' + component.config.hook + '"]')
82 );
83 }
84
85 }
86
87
88 }
89
90 } );
91
92}