|
| 1 | +part of angular.core.dom_internal; |
| 2 | + |
| 3 | +@Decorator( |
| 4 | + selector: 'content') |
| 5 | +class Content implements AttachAware, DetachAware { |
| 6 | + final ContentPort _port; |
| 7 | + final dom.Element _element; |
| 8 | + dom.Comment _beginComment; |
| 9 | + Content(this._port, this._element); |
| 10 | + |
| 11 | + void attach() { |
| 12 | + if (_port == null) return; |
| 13 | + _beginComment = _port.content(_element); |
| 14 | + } |
| 15 | + |
| 16 | + void detach() { |
| 17 | + if (_port == null) return; |
| 18 | + _port.detachContent(_beginComment); |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +class ContentPort { |
| 23 | + dom.Element _element; |
| 24 | + var _childNodes = []; |
| 25 | + |
| 26 | + ContentPort(this._element); |
| 27 | + |
| 28 | + void pullNodes() { |
| 29 | + _childNodes.addAll(_element.nodes); |
| 30 | + _element.nodes = []; |
| 31 | + } |
| 32 | + |
| 33 | + content(dom.Element elt) { |
| 34 | + var hash = elt.hashCode; |
| 35 | + var beginComment = new dom.Comment("content $hash"); |
| 36 | + |
| 37 | + if (_childNodes.isNotEmpty) { |
| 38 | + elt.parent.insertBefore(beginComment, elt); |
| 39 | + elt.parent.insertAllBefore(_childNodes, elt); |
| 40 | + elt.parent.insertBefore(new dom.Comment("end-content $hash"), elt); |
| 41 | + _childNodes = []; |
| 42 | + } |
| 43 | + elt.remove(); |
| 44 | + return beginComment; |
| 45 | + } |
| 46 | + |
| 47 | + void detachContent(dom.Comment _beginComment) { |
| 48 | + // Search for endComment and extract everything in between. |
| 49 | + // TODO optimize -- there may be a better way of pulling out nodes. |
| 50 | + |
| 51 | + var endCommentText = "end-${_beginComment.text}"; |
| 52 | + |
| 53 | + var next; |
| 54 | + for (next = _beginComment.nextNode; |
| 55 | + next.nodeType != dom.Node.COMMENT_NODE && next.text != endCommentText; |
| 56 | + next = _beginComment.nextNode) { |
| 57 | + _childNodes.add(next); |
| 58 | + next.remove(); |
| 59 | + } |
| 60 | + assert(next.nodeType == dom.Node.COMMENT_NODE && next.text == endCommentText); |
| 61 | + next.remove(); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +class TranscludingComponentFactory implements ComponentFactory { |
| 66 | + final Expando _expando; |
| 67 | + |
| 68 | + TranscludingComponentFactory(this._expando); |
| 69 | + |
| 70 | + FactoryFn call(dom.Node node, DirectiveRef ref) { |
| 71 | + // CSS is not supported. |
| 72 | + assert((ref.annotation as Component).cssUrls == null || |
| 73 | + (ref.annotation as Component).cssUrls.isEmpty); |
| 74 | + |
| 75 | + var element = node as dom.Element; |
| 76 | + return (Injector injector) { |
| 77 | + var childInjector; |
| 78 | + var component = ref.annotation as Component; |
| 79 | + Scope scope = injector.get(Scope); |
| 80 | + ViewCache viewCache = injector.get(ViewCache); |
| 81 | + Http http = injector.get(Http); |
| 82 | + TemplateCache templateCache = injector.get(TemplateCache); |
| 83 | + DirectiveMap directives = injector.get(DirectiveMap); |
| 84 | + NgBaseCss baseCss = injector.get(NgBaseCss); |
| 85 | + |
| 86 | + var contentPort = new ContentPort(element); |
| 87 | + |
| 88 | + // Append the component's template as children |
| 89 | + var viewFuture = ComponentFactory._viewFuture(component, viewCache, directives); |
| 90 | + |
| 91 | + if (viewFuture != null) { |
| 92 | + viewFuture = viewFuture.then((ViewFactory viewFactory) { |
| 93 | + contentPort.pullNodes(); |
| 94 | + element.nodes.addAll(viewFactory(childInjector).nodes); |
| 95 | + return element; |
| 96 | + }); |
| 97 | + } else { |
| 98 | + viewFuture = new async.Future.microtask(() => contentPort.pullNodes()); |
| 99 | + } |
| 100 | + TemplateLoader templateLoader = new TemplateLoader(viewFuture); |
| 101 | + |
| 102 | + Scope shadowScope = scope.createChild({}); |
| 103 | + |
| 104 | + var probe; |
| 105 | + var childModule = new Module() |
| 106 | + ..type(ref.type) |
| 107 | + ..type(NgElement) |
| 108 | + ..value(ContentPort, contentPort) |
| 109 | + ..value(Scope, shadowScope) |
| 110 | + ..value(TemplateLoader, templateLoader) |
| 111 | + ..value(dom.ShadowRoot, new ShadowlessShadowRoot(element)) |
| 112 | + ..factory(ElementProbe, (_) => probe); |
| 113 | + childInjector = injector.createChild([childModule], name: SHADOW_DOM_INJECTOR_NAME); |
| 114 | + |
| 115 | + var controller = childInjector.get(ref.type); |
| 116 | + shadowScope.context[component.publishAs] = controller; |
| 117 | + ComponentFactory._setupOnShadowDomAttach(controller, templateLoader, shadowScope); |
| 118 | + return controller; |
| 119 | + }; |
| 120 | + } |
| 121 | +} |
0 commit comments