@stayradiated/ngreact
Version:
Use React Components in Angular
1 lines • 2.34 kB
JavaScript
(function(n,t){"undefined"!=typeof module&&module.exports?module.exports=t(require("react"),require("react-dom"),require("angular")):"function"==typeof define&&define.amd?define(["react","react-dom","angular"],function(e,o,angular){return n.ngReact=t(e,o,angular)}):n.ngReact=t(n.React,n.ReactDOM,n.angular)})(this,function(React,n,angular){"use strict";function t(n,t){if(angular.isFunction(n))return n;if(!n)throw Error("ReactComponent name attribute must be specified");var e;try{e=t.get(n)}catch(o){}if(!e)try{e=n.split(".").reduce(function(n,t){return n[t]},window)}catch(o){}if(!e)throw Error("Cannot find react component "+n);return e}function e(n,t){if(n.wrappedInApply)return n;var e=function(){var e=arguments,o=t.$root.$$phase;return"$apply"===o||"$digest"===o?n.apply(null,e):t.$apply(function(){return n.apply(null,e)})};return e.wrappedInApply=!0,e}function o(n,t){return Object.keys(n||{}).reduce(function(o,r){var c=n[r];return o[r]=angular.isFunction(c)?e(c,t):c,o},{})}function r(n,t,e,o){"collection"===n&&angular.isFunction(t.$watchCollection)?e.forEach(function(n){t.$watchCollection(n,o)}):"reference"===n?angular.isFunction(t.$watchGroup)?t.$watchGroup(e,o):e.forEach(function(n){t.$watch(n,o)}):e.forEach(function(n){t.$watch(n,o,!0)})}function c(t,e,o,r){o.$evalAsync(function(){n.render(React.createElement(t,e),r[0])})}var u=function(e){return{restrict:"E",replace:!0,link:function(u,i,a){var p=t(a.name,e),f=function(){var n=u.$eval(a.props),t=o(n,u);c(p,t,u,i)};a.props?r(a.watchDepth,u,[a.props],f):f(),u.$on("$destroy",function(){a.onScopeDestroy?u.$eval(a.onScopeDestroy,{unmountComponent:n.unmountComponentAtNode.bind(this,i[0])}):n.unmountComponentAtNode(i[0])})}}},i=function(e){return function(u,i,a,p){var f={restrict:"E",replace:!0,link:function(p,f,l){var s=t(u,e);a=a||Object.keys(s.propTypes||{});var d=function(){var n=i||{};a.forEach(function(t){l.hasOwnProperty(t)&&(n[t]=p.$eval(l[t]))}),n.$scope=p,c(s,o(n,p),p,f)},m=a.map(function(n){return l[n]});r(l.watchDepth,p,m,d),d(),p.$on("$destroy",function(){l.onScopeDestroy?p.$eval(l.onScopeDestroy,{unmountComponent:n.unmountComponentAtNode.bind(this,f[0])}):n.unmountComponentAtNode(f[0])})}};return angular.extend(f,p)}};return angular.module("react",[]).directive("reactComponent",["$injector",u]).factory("reactDirective",["$injector",i])});