ng_react.ts 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. //
  2. // This is using ng-react with this PR applied https://github.com/ngReact/ngReact/pull/199
  3. //
  4. // # ngReact
  5. // ### Use React Components inside of your Angular applications
  6. //
  7. // Composed of
  8. // - reactComponent (generic directive for delegating off to React Components)
  9. // - reactDirective (factory for creating specific directives that correspond to reactComponent directives)
  10. import React from 'react';
  11. import ReactDOM from 'react-dom';
  12. import angular from 'angular';
  13. // get a react component from name (components can be an angular injectable e.g. value, factory or
  14. // available on window
  15. function getReactComponent(name, $injector) {
  16. // if name is a function assume it is component and return it
  17. if (angular.isFunction(name)) {
  18. return name;
  19. }
  20. // a React component name must be specified
  21. if (!name) {
  22. throw new Error('ReactComponent name attribute must be specified');
  23. }
  24. // ensure the specified React component is accessible, and fail fast if it's not
  25. let reactComponent;
  26. try {
  27. reactComponent = $injector.get(name);
  28. } catch (e) {}
  29. if (!reactComponent) {
  30. try {
  31. reactComponent = name.split('.').reduce((current, namePart) => {
  32. return current[namePart];
  33. }, window);
  34. } catch (e) {}
  35. }
  36. if (!reactComponent) {
  37. throw Error('Cannot find react component ' + name);
  38. }
  39. return reactComponent;
  40. }
  41. // wraps a function with scope.$apply, if already applied just return
  42. function applied(fn, scope) {
  43. if (fn.wrappedInApply) {
  44. return fn;
  45. }
  46. //tslint:disable-next-line:only-arrow-functions
  47. const wrapped: any = function() {
  48. const args = arguments;
  49. const phase = scope.$root.$$phase;
  50. if (phase === '$apply' || phase === '$digest') {
  51. return fn.apply(null, args);
  52. } else {
  53. return scope.$apply(() => {
  54. return fn.apply(null, args);
  55. });
  56. }
  57. };
  58. wrapped.wrappedInApply = true;
  59. return wrapped;
  60. }
  61. /**
  62. * wraps functions on obj in scope.$apply
  63. *
  64. * keeps backwards compatibility, as if propsConfig is not passed, it will
  65. * work as before, wrapping all functions and won't wrap only when specified.
  66. *
  67. * @version 0.4.1
  68. * @param obj react component props
  69. * @param scope current scope
  70. * @param propsConfig configuration object for all properties
  71. * @returns {Object} props with the functions wrapped in scope.$apply
  72. */
  73. function applyFunctions(obj, scope, propsConfig?) {
  74. return Object.keys(obj || {}).reduce((prev, key) => {
  75. const value = obj[key];
  76. const config = (propsConfig || {})[key] || {};
  77. /**
  78. * wrap functions in a function that ensures they are scope.$applied
  79. * ensures that when function is called from a React component
  80. * the Angular digest cycle is run
  81. */
  82. prev[key] = angular.isFunction(value) && config.wrapApply !== false ? applied(value, scope) : value;
  83. return prev;
  84. }, {});
  85. }
  86. /**
  87. *
  88. * @param watchDepth (value of HTML watch-depth attribute)
  89. * @param scope (angular scope)
  90. *
  91. * Uses the watchDepth attribute to determine how to watch props on scope.
  92. * If watchDepth attribute is NOT reference or collection, watchDepth defaults to deep watching by value
  93. */
  94. function watchProps(watchDepth, scope, watchExpressions, listener) {
  95. const supportsWatchCollection = angular.isFunction(scope.$watchCollection);
  96. const supportsWatchGroup = angular.isFunction(scope.$watchGroup);
  97. const watchGroupExpressions = [];
  98. watchExpressions.forEach(expr => {
  99. const actualExpr = getPropExpression(expr);
  100. const exprWatchDepth = getPropWatchDepth(watchDepth, expr);
  101. if (exprWatchDepth === 'collection' && supportsWatchCollection) {
  102. scope.$watchCollection(actualExpr, listener);
  103. } else if (exprWatchDepth === 'reference' && supportsWatchGroup) {
  104. watchGroupExpressions.push(actualExpr);
  105. } else if (exprWatchDepth === 'one-time') {
  106. //do nothing because we handle our one time bindings after this
  107. } else {
  108. scope.$watch(actualExpr, listener, exprWatchDepth !== 'reference');
  109. }
  110. });
  111. if (watchDepth === 'one-time') {
  112. listener();
  113. }
  114. if (watchGroupExpressions.length) {
  115. scope.$watchGroup(watchGroupExpressions, listener);
  116. }
  117. }
  118. // render React component, with scope[attrs.props] being passed in as the component props
  119. function renderComponent(component, props, scope, elem) {
  120. scope.$evalAsync(() => {
  121. ReactDOM.render(React.createElement(component, props), elem[0]);
  122. });
  123. }
  124. // get prop name from prop (string or array)
  125. function getPropName(prop) {
  126. return Array.isArray(prop) ? prop[0] : prop;
  127. }
  128. // get prop name from prop (string or array)
  129. function getPropConfig(prop) {
  130. return Array.isArray(prop) ? prop[1] : {};
  131. }
  132. // get prop expression from prop (string or array)
  133. function getPropExpression(prop) {
  134. return Array.isArray(prop) ? prop[0] : prop;
  135. }
  136. // find the normalized attribute knowing that React props accept any type of capitalization
  137. function findAttribute(attrs, propName) {
  138. const index = Object.keys(attrs).filter(attr => {
  139. return attr.toLowerCase() === propName.toLowerCase();
  140. })[0];
  141. return attrs[index];
  142. }
  143. // get watch depth of prop (string or array)
  144. function getPropWatchDepth(defaultWatch, prop) {
  145. const customWatchDepth = Array.isArray(prop) && angular.isObject(prop[1]) && prop[1].watchDepth;
  146. return customWatchDepth || defaultWatch;
  147. }
  148. // # reactComponent
  149. // Directive that allows React components to be used in Angular templates.
  150. //
  151. // Usage:
  152. // <react-component name="Hello" props="name"/>
  153. //
  154. // This requires that there exists an injectable or globally available 'Hello' React component.
  155. // The 'props' attribute is optional and is passed to the component.
  156. //
  157. // The following would would create and register the component:
  158. //
  159. // var module = angular.module('ace.react.components');
  160. // module.value('Hello', React.createClass({
  161. // render: function() {
  162. // return <div>Hello {this.props.name}</div>;
  163. // }
  164. // }));
  165. //
  166. const reactComponent = $injector => {
  167. return {
  168. restrict: 'E',
  169. replace: true,
  170. link: function(scope, elem, attrs) {
  171. const reactComponent = getReactComponent(attrs.name, $injector);
  172. const renderMyComponent = () => {
  173. const scopeProps = scope.$eval(attrs.props);
  174. const props = applyFunctions(scopeProps, scope);
  175. renderComponent(reactComponent, props, scope, elem);
  176. };
  177. // If there are props, re-render when they change
  178. attrs.props ? watchProps(attrs.watchDepth, scope, [attrs.props], renderMyComponent) : renderMyComponent();
  179. // cleanup when scope is destroyed
  180. scope.$on('$destroy', () => {
  181. if (!attrs.onScopeDestroy) {
  182. ReactDOM.unmountComponentAtNode(elem[0]);
  183. } else {
  184. scope.$eval(attrs.onScopeDestroy, {
  185. unmountComponent: ReactDOM.unmountComponentAtNode.bind(this, elem[0]),
  186. });
  187. }
  188. });
  189. },
  190. };
  191. };
  192. // # reactDirective
  193. // Factory function to create directives for React components.
  194. //
  195. // With a component like this:
  196. //
  197. // var module = angular.module('ace.react.components');
  198. // module.value('Hello', React.createClass({
  199. // render: function() {
  200. // return <div>Hello {this.props.name}</div>;
  201. // }
  202. // }));
  203. //
  204. // A directive can be created and registered with:
  205. //
  206. // module.directive('hello', function(reactDirective) {
  207. // return reactDirective('Hello', ['name']);
  208. // });
  209. //
  210. // Where the first argument is the injectable or globally accessible name of the React component
  211. // and the second argument is an array of property names to be watched and passed to the React component
  212. // as props.
  213. //
  214. // This directive can then be used like this:
  215. //
  216. // <hello name="name"/>
  217. //
  218. const reactDirective = $injector => {
  219. return (reactComponentName, props, conf, injectableProps) => {
  220. const directive = {
  221. restrict: 'E',
  222. replace: true,
  223. link: function(scope, elem, attrs) {
  224. const reactComponent = getReactComponent(reactComponentName, $injector);
  225. // if props is not defined, fall back to use the React component's propTypes if present
  226. props = props || Object.keys(reactComponent.propTypes || {});
  227. // for each of the properties, get their scope value and set it to scope.props
  228. const renderMyComponent = () => {
  229. let scopeProps = {};
  230. const config = {};
  231. props.forEach(prop => {
  232. const propName = getPropName(prop);
  233. scopeProps[propName] = scope.$eval(findAttribute(attrs, propName));
  234. config[propName] = getPropConfig(prop);
  235. });
  236. scopeProps = applyFunctions(scopeProps, scope, config);
  237. scopeProps = angular.extend({}, scopeProps, injectableProps);
  238. renderComponent(reactComponent, scopeProps, scope, elem);
  239. };
  240. // watch each property name and trigger an update whenever something changes,
  241. // to update scope.props with new values
  242. const propExpressions = props.map(prop => {
  243. return Array.isArray(prop) ? [attrs[getPropName(prop)], getPropConfig(prop)] : attrs[prop];
  244. });
  245. // If we don't have any props, then our watch statement won't fire.
  246. props.length ? watchProps(attrs.watchDepth, scope, propExpressions, renderMyComponent) : renderMyComponent();
  247. // cleanup when scope is destroyed
  248. scope.$on('$destroy', () => {
  249. if (!attrs.onScopeDestroy) {
  250. ReactDOM.unmountComponentAtNode(elem[0]);
  251. } else {
  252. scope.$eval(attrs.onScopeDestroy, {
  253. unmountComponent: ReactDOM.unmountComponentAtNode.bind(this, elem[0]),
  254. });
  255. }
  256. });
  257. },
  258. };
  259. return angular.extend(directive, conf);
  260. };
  261. };
  262. const ngModule = angular.module('react', []);
  263. ngModule.directive('reactComponent', ['$injector', reactComponent]);
  264. ngModule.factory('reactDirective', ['$injector', reactDirective]);