|
| 1 | +part of angular.core.dom_internal; |
| 2 | + |
| 3 | +abstract class ComponentFactory { |
| 4 | + FactoryFn call(dom.Node node, DirectiveRef ref); |
| 5 | +} |
| 6 | + |
| 7 | +class ShadowDomComponentFactory implements ComponentFactory { |
| 8 | + final Expando _expando; |
| 9 | + |
| 10 | + ShadowDomComponentFactory(this._expando); |
| 11 | + |
| 12 | + FactoryFn call(dom.Node node, DirectiveRef ref) { |
| 13 | + return (Injector injector) { |
| 14 | + var component = ref.annotation as Component; |
| 15 | + Compiler compiler = injector.get(Compiler); |
| 16 | + Scope scope = injector.get(Scope); |
| 17 | + ViewCache viewCache = injector.get(ViewCache); |
| 18 | + Http http = injector.get(Http); |
| 19 | + TemplateCache templateCache = injector.get(TemplateCache); |
| 20 | + DirectiveMap directives = injector.get(DirectiveMap); |
| 21 | + NgBaseCss baseCss = injector.get(NgBaseCss); |
| 22 | + // This is a bit of a hack since we are returning different type then we are. |
| 23 | + var componentFactory = new _ComponentFactory(node, ref.type, component, |
| 24 | + injector.get(dom.NodeTreeSanitizer), _expando, baseCss); |
| 25 | + var controller = componentFactory.call(injector, scope, viewCache, http, templateCache, |
| 26 | + directives); |
| 27 | + |
| 28 | + componentFactory.shadowScope.context[component.publishAs] = controller; |
| 29 | + return controller; |
| 30 | + }; |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | + |
| 35 | +/** |
| 36 | + * ComponentFactory is responsible for setting up components. This includes |
| 37 | + * the shadowDom, fetching template, importing styles, setting up attribute |
| 38 | + * mappings, publishing the controller, and compiling and caching the template. |
| 39 | + */ |
| 40 | +class _ComponentFactory implements Function { |
| 41 | + |
| 42 | + final dom.Element element; |
| 43 | + final Type type; |
| 44 | + final Component component; |
| 45 | + final dom.NodeTreeSanitizer treeSanitizer; |
| 46 | + final Expando _expando; |
| 47 | + final NgBaseCss _baseCss; |
| 48 | + |
| 49 | + dom.ShadowRoot shadowDom; |
| 50 | + Scope shadowScope; |
| 51 | + Injector shadowInjector; |
| 52 | + var controller; |
| 53 | + |
| 54 | + _ComponentFactory(this.element, this.type, this.component, this.treeSanitizer, |
| 55 | + this._expando, this._baseCss); |
| 56 | + |
| 57 | + dynamic call(Injector injector, Scope scope, |
| 58 | + ViewCache viewCache, Http http, TemplateCache templateCache, |
| 59 | + DirectiveMap directives) { |
| 60 | + shadowDom = element.createShadowRoot() |
| 61 | + ..applyAuthorStyles = component.applyAuthorStyles |
| 62 | + ..resetStyleInheritance = component.resetStyleInheritance; |
| 63 | + |
| 64 | + shadowScope = scope.createChild({}); // Isolate |
| 65 | + // TODO(pavelgj): fetching CSS with Http is mainly an attempt to |
| 66 | + // work around an unfiled Chrome bug when reloading same CSS breaks |
| 67 | + // styles all over the page. We shouldn't be doing browsers work, |
| 68 | + // so change back to using @import once Chrome bug is fixed or a |
| 69 | + // better work around is found. |
| 70 | + List<async.Future<String>> cssFutures = new List(); |
| 71 | + var cssUrls = []..addAll(_baseCss.urls)..addAll(component.cssUrls); |
| 72 | + if (cssUrls.isNotEmpty) { |
| 73 | + cssUrls.forEach((css) => cssFutures.add(http |
| 74 | + .get(css, cache: templateCache).then( |
| 75 | + (resp) => resp.responseText, |
| 76 | + onError: (e) => '/*\n$e\n*/\n') |
| 77 | + )); |
| 78 | + } else { |
| 79 | + cssFutures.add(new async.Future.value(null)); |
| 80 | + } |
| 81 | + var viewFuture; |
| 82 | + if (component.template != null) { |
| 83 | + viewFuture = new async.Future.value(viewCache.fromHtml( |
| 84 | + component.template, directives)); |
| 85 | + } else if (component.templateUrl != null) { |
| 86 | + viewFuture = viewCache.fromUrl(component.templateUrl, directives); |
| 87 | + } |
| 88 | + TemplateLoader templateLoader = new TemplateLoader( |
| 89 | + async.Future.wait(cssFutures).then((Iterable<String> cssList) { |
| 90 | + if (cssList != null) { |
| 91 | + shadowDom.setInnerHtml( |
| 92 | + cssList |
| 93 | + .where((css) => css != null) |
| 94 | + .map((css) => '<style>$css</style>') |
| 95 | + .join(''), |
| 96 | + treeSanitizer: treeSanitizer); |
| 97 | + } |
| 98 | + if (viewFuture != null) { |
| 99 | + return viewFuture.then((ViewFactory viewFactory) { |
| 100 | + return (!shadowScope.isAttached) ? |
| 101 | + shadowDom : |
| 102 | + attachViewToShadowDom(viewFactory); |
| 103 | + }); |
| 104 | + } |
| 105 | + return shadowDom; |
| 106 | + })); |
| 107 | + controller = createShadowInjector(injector, templateLoader).get(type); |
| 108 | + if (controller is ShadowRootAware) { |
| 109 | + templateLoader.template.then((_) { |
| 110 | + if (!shadowScope.isAttached) return; |
| 111 | + (controller as ShadowRootAware).onShadowRoot(shadowDom); |
| 112 | + }); |
| 113 | + } |
| 114 | + return controller; |
| 115 | + } |
| 116 | + |
| 117 | + dom.ShadowRoot attachViewToShadowDom(ViewFactory viewFactory) { |
| 118 | + var view = viewFactory(shadowInjector); |
| 119 | + shadowDom.nodes.addAll(view.nodes); |
| 120 | + return shadowDom; |
| 121 | + } |
| 122 | + |
| 123 | + Injector createShadowInjector(injector, TemplateLoader templateLoader) { |
| 124 | + var probe; |
| 125 | + var shadowModule = new Module() |
| 126 | + ..type(type) |
| 127 | + ..type(NgElement) |
| 128 | + ..type(EventHandler, implementedBy: ShadowRootEventHandler) |
| 129 | + ..value(Scope, shadowScope) |
| 130 | + ..value(TemplateLoader, templateLoader) |
| 131 | + ..value(dom.ShadowRoot, shadowDom) |
| 132 | + ..factory(ElementProbe, (_) => probe); |
| 133 | + shadowInjector = injector.createChild([shadowModule], name: SHADOW_DOM_INJECTOR_NAME); |
| 134 | + probe = _expando[shadowDom] = new ElementProbe( |
| 135 | + injector.get(ElementProbe), shadowDom, shadowInjector, shadowScope); |
| 136 | + return shadowInjector; |
| 137 | + } |
| 138 | +} |
0 commit comments