UNPKG

8.98 kBJavaScriptView Raw
1"use strict";
2var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5 else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6 return c > 3 && r && Object.defineProperty(target, key, r), r;
7};
8var __metadata = (this && this.__metadata) || function (k, v) {
9 if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10};
11var __param = (this && this.__param) || function (paramIndex, decorator) {
12 return function (target, key) { decorator(target, key, paramIndex); }
13};
14var core_1 = require('@angular/core');
15var async_1 = require('../facade/async');
16var collection_1 = require('../facade/collection');
17var lang_1 = require('../facade/lang');
18var instruction_1 = require('../instruction');
19var hookMod = require('../lifecycle/lifecycle_annotations');
20var route_lifecycle_reflector_1 = require('../lifecycle/route_lifecycle_reflector');
21var routerMod = require('../router');
22var _resolveToTrue = async_1.PromiseWrapper.resolve(true);
23/**
24 * A router outlet is a placeholder that Angular dynamically fills based on the application's route.
25 *
26 * ## Use
27 *
28 * ```
29 * <router-outlet></router-outlet>
30 * ```
31 */
32var RouterOutlet = (function () {
33 function RouterOutlet(_viewContainerRef, _loader, _parentRouter, nameAttr) {
34 this._viewContainerRef = _viewContainerRef;
35 this._loader = _loader;
36 this._parentRouter = _parentRouter;
37 this.name = null;
38 this._componentRef = null;
39 this._currentInstruction = null;
40 this.activateEvents = new async_1.EventEmitter();
41 if (lang_1.isPresent(nameAttr)) {
42 this.name = nameAttr;
43 this._parentRouter.registerAuxOutlet(this);
44 }
45 else {
46 this._parentRouter.registerPrimaryOutlet(this);
47 }
48 }
49 /**
50 * Called by the Router to instantiate a new component during the commit phase of a navigation.
51 * This method in turn is responsible for calling the `routerOnActivate` hook of its child.
52 */
53 RouterOutlet.prototype.activate = function (nextInstruction) {
54 var _this = this;
55 var previousInstruction = this._currentInstruction;
56 this._currentInstruction = nextInstruction;
57 var componentType = nextInstruction.componentType;
58 var childRouter = this._parentRouter.childRouter(componentType);
59 var providers = core_1.ReflectiveInjector.resolve([
60 { provide: instruction_1.RouteData, useValue: nextInstruction.routeData },
61 { provide: instruction_1.RouteParams, useValue: new instruction_1.RouteParams(nextInstruction.params) },
62 { provide: routerMod.Router, useValue: childRouter }
63 ]);
64 this._componentRef =
65 this._loader.loadNextToLocation(componentType, this._viewContainerRef, providers);
66 return this._componentRef.then(function (componentRef) {
67 _this.activateEvents.emit(componentRef.instance);
68 if (route_lifecycle_reflector_1.hasLifecycleHook(hookMod.routerOnActivate, componentType)) {
69 return _this._componentRef.then(function (ref) {
70 return ref.instance.routerOnActivate(nextInstruction, previousInstruction);
71 });
72 }
73 else {
74 return componentRef;
75 }
76 });
77 };
78 /**
79 * Called by the {@link Router} during the commit phase of a navigation when an outlet
80 * reuses a component between different routes.
81 * This method in turn is responsible for calling the `routerOnReuse` hook of its child.
82 */
83 RouterOutlet.prototype.reuse = function (nextInstruction) {
84 var previousInstruction = this._currentInstruction;
85 this._currentInstruction = nextInstruction;
86 // it's possible the component is removed before it can be reactivated (if nested withing
87 // another dynamically loaded component, for instance). In that case, we simply activate
88 // a new one.
89 if (lang_1.isBlank(this._componentRef)) {
90 return this.activate(nextInstruction);
91 }
92 else {
93 return async_1.PromiseWrapper.resolve(route_lifecycle_reflector_1.hasLifecycleHook(hookMod.routerOnReuse, this._currentInstruction.componentType) ?
94 this._componentRef.then(function (ref) {
95 return ref.instance.routerOnReuse(nextInstruction, previousInstruction);
96 }) :
97 true);
98 }
99 };
100 /**
101 * Called by the {@link Router} when an outlet disposes of a component's contents.
102 * This method in turn is responsible for calling the `routerOnDeactivate` hook of its child.
103 */
104 RouterOutlet.prototype.deactivate = function (nextInstruction) {
105 var _this = this;
106 var next = _resolveToTrue;
107 if (lang_1.isPresent(this._componentRef) && lang_1.isPresent(this._currentInstruction) &&
108 route_lifecycle_reflector_1.hasLifecycleHook(hookMod.routerOnDeactivate, this._currentInstruction.componentType)) {
109 next = this._componentRef.then(function (ref) {
110 return ref.instance
111 .routerOnDeactivate(nextInstruction, _this._currentInstruction);
112 });
113 }
114 return next.then(function (_) {
115 if (lang_1.isPresent(_this._componentRef)) {
116 var onDispose = _this._componentRef.then(function (ref) { return ref.destroy(); });
117 _this._componentRef = null;
118 return onDispose;
119 }
120 });
121 };
122 /**
123 * Called by the {@link Router} during recognition phase of a navigation.
124 *
125 * If this resolves to `false`, the given navigation is cancelled.
126 *
127 * This method delegates to the child component's `routerCanDeactivate` hook if it exists,
128 * and otherwise resolves to true.
129 */
130 RouterOutlet.prototype.routerCanDeactivate = function (nextInstruction) {
131 var _this = this;
132 if (lang_1.isBlank(this._currentInstruction)) {
133 return _resolveToTrue;
134 }
135 if (route_lifecycle_reflector_1.hasLifecycleHook(hookMod.routerCanDeactivate, this._currentInstruction.componentType)) {
136 return this._componentRef.then(function (ref) {
137 return ref.instance
138 .routerCanDeactivate(nextInstruction, _this._currentInstruction);
139 });
140 }
141 else {
142 return _resolveToTrue;
143 }
144 };
145 /**
146 * Called by the {@link Router} during recognition phase of a navigation.
147 *
148 * If the new child component has a different Type than the existing child component,
149 * this will resolve to `false`. You can't reuse an old component when the new component
150 * is of a different Type.
151 *
152 * Otherwise, this method delegates to the child component's `routerCanReuse` hook if it exists,
153 * or resolves to true if the hook is not present.
154 */
155 RouterOutlet.prototype.routerCanReuse = function (nextInstruction) {
156 var _this = this;
157 var result;
158 if (lang_1.isBlank(this._currentInstruction) ||
159 this._currentInstruction.componentType != nextInstruction.componentType) {
160 result = false;
161 }
162 else if (route_lifecycle_reflector_1.hasLifecycleHook(hookMod.routerCanReuse, this._currentInstruction.componentType)) {
163 result = this._componentRef.then(function (ref) {
164 return ref.instance.routerCanReuse(nextInstruction, _this._currentInstruction);
165 });
166 }
167 else {
168 result = nextInstruction == this._currentInstruction ||
169 (lang_1.isPresent(nextInstruction.params) && lang_1.isPresent(this._currentInstruction.params) &&
170 collection_1.StringMapWrapper.equals(nextInstruction.params, this._currentInstruction.params));
171 }
172 return async_1.PromiseWrapper.resolve(result);
173 };
174 RouterOutlet.prototype.ngOnDestroy = function () { this._parentRouter.unregisterPrimaryOutlet(this); };
175 __decorate([
176 core_1.Output('activate'),
177 __metadata('design:type', Object)
178 ], RouterOutlet.prototype, "activateEvents", void 0);
179 RouterOutlet = __decorate([
180 core_1.Directive({ selector: 'router-outlet' }),
181 __param(3, core_1.Attribute('name')),
182 __metadata('design:paramtypes', [core_1.ViewContainerRef, core_1.DynamicComponentLoader, routerMod.Router, String])
183 ], RouterOutlet);
184 return RouterOutlet;
185}());
186exports.RouterOutlet = RouterOutlet;
187//# sourceMappingURL=router_outlet.js.map
\No newline at end of file