|
| 1 | +// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:async'; |
| 6 | + |
| 7 | +import '../../model/ui/has_renderer.dart'; |
| 8 | +import '../../utils/async/async.dart'; |
| 9 | +import 'package:angular2/angular2.dart'; |
| 10 | + |
| 11 | +/// Dynamically renders another component, setting the [value] field on the |
| 12 | +/// dynamic component if it implements [RendersValue] (and not if the component |
| 13 | +/// does not implement the interface). |
| 14 | +/// |
| 15 | +/// A host component can load child components in a loop and set their values: |
| 16 | +/// |
| 17 | +/// ... *ngFor="item in items"> |
| 18 | +/// <dynamic-component |
| 19 | +/// [componentType]="aTypeThatMayNotRenderValue" |
| 20 | +/// [value]="item"> |
| 21 | +/// </dynamic-component> |
| 22 | +/// |
| 23 | +@Component( |
| 24 | + selector: 'dynamic-component', |
| 25 | +// #marker variable is used to refer to the <div> tag later when calling |
| 26 | +// DynamicComponentLoader.loadNextToLocation. The tag does not have to be |
| 27 | +// <span>. It can be anything. In this case, we simply needed something |
| 28 | +// that's not visible, and <span> does the job perfectly. |
| 29 | + template: '''<span #marker></span>''') |
| 30 | +class DynamicComponent implements OnDestroy { |
| 31 | + final DynamicComponentLoader _componentLoader; |
| 32 | + final ChangeDetectorRef _changeDetectorRef; |
| 33 | + final _onLoadController = new LazyStreamController<ComponentRef>(); |
| 34 | + |
| 35 | + ViewContainerRef _viewContainerRef; |
| 36 | + bool _loadDeferred = false; |
| 37 | + |
| 38 | + @ViewChild('marker', read: ViewContainerRef) |
| 39 | + set viewContainerRef(ViewContainerRef value) { |
| 40 | + _viewContainerRef = value; |
| 41 | + if (_loadDeferred) { |
| 42 | + _initialize(); |
| 43 | + _loadDeferred = false; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + ComponentRef _childComponent; |
| 48 | + Type _componentType; |
| 49 | + Object _value; |
| 50 | + |
| 51 | + /// Fired when component is loaded allowing clients to get a handle on the |
| 52 | + /// component loaded. |
| 53 | + @Output() |
| 54 | + Stream<ComponentRef> get onLoad => _onLoadController.stream; |
| 55 | + |
| 56 | + DynamicComponent(this._componentLoader, this._changeDetectorRef); |
| 57 | + |
| 58 | + /// Returns the loaded dynamic component reference. |
| 59 | + ComponentRef get childComponent => _childComponent; |
| 60 | + |
| 61 | + @override |
| 62 | + void ngOnDestroy() { |
| 63 | + _disposeChildComponent(); |
| 64 | + _viewContainerRef = null; |
| 65 | + } |
| 66 | + |
| 67 | + void _disposeChildComponent() { |
| 68 | + _childComponent?.destroy(); |
| 69 | + _childComponent = null; |
| 70 | + } |
| 71 | + |
| 72 | + /// The type of component to dynamically render. |
| 73 | + @Input() |
| 74 | + set componentType(Type dartType) { |
| 75 | + _disposeChildComponent(); |
| 76 | + _componentType = dartType; |
| 77 | + if (dartType == null) { |
| 78 | + return; |
| 79 | + } |
| 80 | + if (_viewContainerRef != null) { |
| 81 | + _initialize(); |
| 82 | + } else { |
| 83 | + _loadDeferred = true; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + void _initialize() { |
| 88 | + Type loadType = _componentType; |
| 89 | + _componentLoader |
| 90 | + .loadNextToLocation(loadType, _viewContainerRef) |
| 91 | + .then((ComponentRef componentRef) { |
| 92 | + if (loadType != _componentType) { |
| 93 | + // During the load time, the component type has changed, |
| 94 | + // and the type we just loaded is no longer valid. |
| 95 | + componentRef.destroy(); |
| 96 | + return; |
| 97 | + } |
| 98 | + if (_childComponent != null) { |
| 99 | + throw 'Attempting to overwrite a dynamic component'; |
| 100 | + } |
| 101 | + _childComponent = componentRef; |
| 102 | + _onLoadController.add(componentRef); |
| 103 | + _updateChildComponent(); |
| 104 | + }); |
| 105 | + } |
| 106 | + |
| 107 | + /// The value to set on the component if the component implements |
| 108 | + /// [RendersValue]. Optional. |
| 109 | + @Input() |
| 110 | + set value(dynamic s) { |
| 111 | + _value = s; |
| 112 | + _updateChildComponent(); |
| 113 | + } |
| 114 | + |
| 115 | + void _updateChildComponent() { |
| 116 | + _changeDetectorRef.markForCheck(); |
| 117 | + |
| 118 | + if (_childComponent != null) { |
| 119 | + if (_childComponent.instance is RendersValue) { |
| 120 | + _childComponent.instance.value = _value; |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments