ng_react.ts 10 KB

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