All files / ima/page/renderer AbstractPageRenderer.js

100% Statements 24/24
80% Branches 12/15
100% Functions 10/10
100% Lines 24/24
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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198            5x                                 39x               39x               39x               39x               39x           39x               1x                 1x                 1x                 1x 1x     2x   2x     1x               1x 1x                           2x           2x                                                           4x         4x         4x                               3x           5x  
import ns from '../../namespace';
import BlankManagedRootView from './BlankManagedRootView';
import PageRenderer from './PageRenderer';
import ViewAdapter from './ViewAdapter';
import GenericError from '../../error/GenericError';
 
ns.namespace('ima.page.renderer');
 
/**
 * Base class for implementations of the {@linkcode PageRenderer} interface.
 */
export default class AbstractPageRenderer extends PageRenderer {
  /**
	 * Initializes the abstract page renderer.
	 *
	 * @param {PageRendererFactory} factory Factory for receive $Utils to view.
	 * @param {vendor.$Helper} Helper The IMA.js helper methods.
	 * @param {vendor.ReactDOM} ReactDOM React framework instance, will be used
	 *        to render the page.
	 * @param {Object<string, *>} settings Application settings for the current
	 *        application environment.
	 */
  constructor(factory, Helper, ReactDOM, settings) {
    super();
 
    /**
		 * Factory for receive $Utils to view.
		 *
		 * @protected
		 * @type {PageRendererFactory}
		 */
    this._factory = factory;
 
    /**
		 * The IMA.js helper methods.
		 *
		 * @protected
		 * @type {Vendor.$Helper}
		 */
    this._Helper = Helper;
 
    /**
		 * Rect framework instance, used to render the page.
		 *
		 * @protected
		 * @type {Vendor.ReactDOM}
		 */
    this._ReactDOM = ReactDOM;
 
    /**
		 * Application setting for the current application environment.
		 *
		 * @protected
		 * @type {Object<string, *>}
		 */
    this._settings = settings;
 
    /**
		 * @protected
		 * @type {?React.Component}
		 */
    this._reactiveView = null;
  }
 
  /**
	 * @inheritdoc
	 * @abstract
	 */
  mount(controller, view, pageResources, routeOptions) {
    throw new GenericError(
      'The mount() method is abstract and must be overridden.'
    );
  }
 
  /**
	 * @inheritdoc
	 */
  update(controller, resourcesUpdate) {
    throw new GenericError(
      'The update() method is abstract and must be overridden.'
    );
  }
 
  /**
	 * @inheritdoc
	 */
  unmount() {
    throw new GenericError(
      'The unmount() method is abstract and must be overridden.'
    );
  }
 
  /**
	 * @inheritdoc
	 */
  clearState() {
    Eif (this._reactiveView && this._reactiveView.state) {
      let emptyState = Object.keys(
        this._reactiveView.state
      ).reduce((state, key) => {
        state[key] = undefined;
 
        return state;
      }, {});
 
      this._reactiveView.setState(emptyState);
    }
  }
 
  /**
	 * @inheritdoc
	 */
  setState(state = {}) {
    Eif (this._reactiveView) {
      this._reactiveView.setState(state);
    }
  }
 
  /**
	 * Generate properties for view from state.
	 *
	 * @protected
	 * @param {function(new:React.Component, Object<string, *>)} view The page
	 *        view React component to wrap.
	 * @param {Object<string, *>=} [state={}]
	 * @return {Object<string, *>}
	 */
  _generateViewProps(view, state = {}) {
    let props = {
      view,
      state,
      $Utils: this._factory.getUtils()
    };
 
    return props;
  }
 
  /**
	 * Returns wrapped page view component with managed root view and view adapter.
	 *
	 * @param {ControllerDecorator} controller
	 * @param {function(new: React.Component)} view
	 * @param {{
	 *          onlyUpdate: (
	 *            boolean|
	 *            function(
	 *              (string|function(new: Controller, ...*)),
	 *              (
	 *                string|
	 *                function(
	 *                  new: React.Component,
	 *                  Object<string, *>,
	 *                  ?Object<string, *>
	 *                )
	 *              )
	 *            ): boolean
	 *          ),
	 *          autoScroll: boolean,
	 *          allowSPA: boolean,
	 *          documentView: ?function(new: AbstractDocumentView),
	 *          managedRootView: ?function(new: React.Component)
	 *        }} routeOptions The current route options.
	 */
  _getWrappedPageView(controller, view, routeOptions) {
    let managedRootView = this._factory.getManagedRootView(
      routeOptions.managedRootView ||
        this._settings.$Page.$Render.managedRootView ||
        BlankManagedRootView
    );
    let props = this._generateViewProps(
      managedRootView,
      Object.assign({}, controller.getState(), { $pageView: view })
    );
 
    return this._factory.wrapView(
      this._settings.$Page.$Render.viewAdapter || ViewAdapter,
      props
    );
  }
 
  /**
	 * Returns the class constructor of the specified document view component.
	 *
	 * @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(routeOptions) {
    return this._factory.getDocumentView(
      routeOptions.documentView || this._settings.$Page.$Render.documentView
    );
  }
}
 
ns.ima.page.renderer.AbstractPageRenderer = AbstractPageRenderer;