All files / ima/page/renderer PageRendererFactory.js

81.82% Statements 18/22
50% Branches 5/10
85.71% Functions 6/7
81.82% Lines 18/22
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159      5x                                       2x               2x             1x                             1x   1x 1x   1x               1x                             1x       1x 1x   1x             1x                             3x       3x                                         1x                                           5x  
import ns from '../../namespace';
import AbstractDocumentView from '../AbstractDocumentView';
 
ns.namespace('ima.page.renderer');
 
/**
 * Factory for page render.
 */
export default class PageRendererFactory {
  /**
	 * Initializes the factory used by the page renderer.
	 *
	 * @param {ObjectContainer} oc The application's dependency injector - the
	 *        object container.
	 * @param {React} React The React framework instance to use to render the
	 *        page.
	 */
  constructor(oc, React) {
    /**
		 * The application's dependency injector - the object container.
		 *
		 * @type {ObjectContainer}
		 */
    this._oc = oc;
 
    /**
		 * Rect framework instance, used to render the page.
		 *
		 * @protected
		 * @type {React}
		 */
    this._React = React;
  }
 
  /**
	 * Return object of services which are defined for alias $Utils.
	 */
  getUtils() {
    return this._oc.get('$Utils');
  }
 
  /**
	 * Returns the class constructor of the specified document view component.
	 * Document view may be specified as a namespace path or as a class
	 * constructor.
	 *
	 * @param {(function(new: React.Component)|string)} documentView The
	 *        namespace path pointing to the document view component, or the
	 *        constructor of the document view component.
	 * @return {function(new: React.Component)} The constructor of the document
	 *         view component.
	 */
  getDocumentView(documentView) {
    let documentViewComponent = this._resolveClassConstructor(documentView);
 
    Eif ($Debug) {
      let componentPrototype = documentViewComponent.prototype;
 
      Iif (!(componentPrototype instanceof AbstractDocumentView)) {
        throw new Error(
          'The document view component must extend ' +
            'ima/page/AbstractDocumentView class'
        );
      }
    }
 
    return documentViewComponent;
  }
 
  /**
	 * Returns the class constructor of the specified managed root view
	 * component. Managed root view may be specified as a namespace
	 * path or as a class constructor.
	 *
	 * @param {(function(new: React.Component)|string)} managedRootView The
	 *        namespace path pointing to the managed root view component, or
	 *        the constructor of the React component.
	 * @return {function(new: React.Component)} The constructor of the managed
	 *         root view component.
	 */
  getManagedRootView(managedRootView) {
    let managedRootViewComponent = this._resolveClassConstructor(
      managedRootView
    );
 
    Eif ($Debug) {
      let componentPrototype = managedRootViewComponent.prototype;
 
      Iif (!(componentPrototype instanceof this._React.Component)) {
        throw new Error(
          'The managed root view component must extend ' + 'React.Component'
        );
      }
    }
 
    return managedRootViewComponent;
  }
 
  /**
	 * Returns the class constructor of the specified view component.
	 * View may be specified as a namespace path or as a class
	 * constructor.
	 *
	 * @param {(function(new: React.Component)|string)} view The namespace path
	 *        pointing to the view component, or the constructor
	 *        of the {@code React.Component}.
	 * @return {function(new: React.Component)} The constructor of the view
	 *         component.
	 */
  _resolveClassConstructor(view) {
    Iif (typeof view === 'string') {
      view = ns.get(view);
    }
 
    return view;
  }
 
  /**
	 * Wraps the provided view into the view adapter so it can access the state
	 * passed from controller through the {@code props} property instead of the
	 * {@code state} property.
	 *
	 * @param {(function(new: React.Component)|string)} view The namespace path
	 *        pointing to the view component, or the constructor
	 *        of the {@code React.Component}.
	 * @param {{
	 *          view: React.Component,
	 *          state: Object<string, *>,
	 *          $Utils: Object<string, *>
	 *        }} props The initial props to pass to the view.
	 * @return {React.Element} View adapter handling passing the controller's
	 *         state to an instance of the specified page view through
	 *         properties.
	 */
  wrapView(view, props) {
    return this._React.createElement(
      this._resolveClassConstructor(view),
      props
    );
  }
 
  /**
	 * Return a function that produces ReactElements of a given type.
	 * Like React.createElement.
	 *
	 * @param {(string|function(new: React.Component))} view The react
	 *        component for which a factory function should be created.
	 * @return {function(Object<string, *>): React.Element} The created factory
	 *         function. The factory accepts an object containing the
	 *         component's properties as the argument and returns a rendered
	 *         component.
	 */
  createReactElementFactory(view) {
    return this._React.createFactory(view);
  }
}
 
ns.ima.page.renderer.PageRendererFactory = PageRendererFactory;