1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', { value: true });
|
4 |
|
5 | var aureliaRouter = require('aurelia-router');
|
6 | var aureliaMetadata = require('aurelia-metadata');
|
7 | var aureliaPath = require('aurelia-path');
|
8 | var aureliaTemplating = require('aurelia-templating');
|
9 | var aureliaDependencyInjection = require('aurelia-dependency-injection');
|
10 | var aureliaBinding = require('aurelia-binding');
|
11 | var aureliaPal = require('aurelia-pal');
|
12 | var LogManager = require('aurelia-logging');
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | var extendStatics = function(d, b) {
|
31 | extendStatics = Object.setPrototypeOf ||
|
32 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
33 | function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
34 | return extendStatics(d, b);
|
35 | };
|
36 |
|
37 | function __extends(d, b) {
|
38 | extendStatics(d, b);
|
39 | function __() { this.constructor = d; }
|
40 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
41 | }
|
42 |
|
43 | var EmptyLayoutViewModel = (function () {
|
44 | function EmptyLayoutViewModel() {
|
45 | }
|
46 | return EmptyLayoutViewModel;
|
47 | }());
|
48 |
|
49 |
|
50 |
|
51 | var RouterView = (function () {
|
52 | function RouterView(element, container, viewSlot, router, viewLocator, compositionTransaction, compositionEngine) {
|
53 | this.element = element;
|
54 | this.container = container;
|
55 | this.viewSlot = viewSlot;
|
56 | this.router = router;
|
57 | this.viewLocator = viewLocator;
|
58 | this.compositionTransaction = compositionTransaction;
|
59 | this.compositionEngine = compositionEngine;
|
60 |
|
61 |
|
62 |
|
63 | this.router.registerViewPort(this, this.element.getAttribute('name'));
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 | if (!('initialComposition' in compositionTransaction)) {
|
70 | compositionTransaction.initialComposition = true;
|
71 | this.compositionTransactionNotifier = compositionTransaction.enlist();
|
72 | }
|
73 | }
|
74 |
|
75 | RouterView.inject = function () {
|
76 | return [aureliaPal.DOM.Element, aureliaDependencyInjection.Container, aureliaTemplating.ViewSlot, aureliaRouter.Router, aureliaTemplating.ViewLocator, aureliaTemplating.CompositionTransaction, aureliaTemplating.CompositionEngine];
|
77 | };
|
78 | RouterView.prototype.created = function (owningView) {
|
79 | this.owningView = owningView;
|
80 | };
|
81 | RouterView.prototype.bind = function (bindingContext, overrideContext) {
|
82 |
|
83 |
|
84 | this.container.viewModel = bindingContext;
|
85 | this.overrideContext = overrideContext;
|
86 | };
|
87 | |
88 |
|
89 |
|
90 | RouterView.prototype.process = function ($viewPortInstruction, waitToSwap) {
|
91 | var _this = this;
|
92 |
|
93 | var viewPortInstruction = $viewPortInstruction;
|
94 | var component = viewPortInstruction.component;
|
95 | var childContainer = component.childContainer;
|
96 | var viewModel = component.viewModel;
|
97 | var viewModelResource = component.viewModelResource;
|
98 | var metadata = viewModelResource.metadata;
|
99 | var config = component.router.currentInstruction.config;
|
100 | var viewPortConfig = config.viewPorts ? (config.viewPorts[viewPortInstruction.name] || {}) : {};
|
101 | childContainer.get(RouterViewLocator)._notify(this);
|
102 |
|
103 | var layoutInstruction = {
|
104 | viewModel: viewPortConfig.layoutViewModel || config.layoutViewModel || this.layoutViewModel,
|
105 | view: viewPortConfig.layoutView || config.layoutView || this.layoutView,
|
106 | model: viewPortConfig.layoutModel || config.layoutModel || this.layoutModel,
|
107 | router: viewPortInstruction.component.router,
|
108 | childContainer: childContainer,
|
109 | viewSlot: this.viewSlot
|
110 | };
|
111 |
|
112 |
|
113 |
|
114 |
|
115 | var viewStrategy = this.viewLocator.getViewStrategy(component.view || viewModel);
|
116 | if (viewStrategy && component.view) {
|
117 | viewStrategy.makeRelativeTo(aureliaMetadata.Origin.get(component.router.container.viewModel.constructor).moduleId);
|
118 | }
|
119 |
|
120 | return metadata
|
121 | .load(childContainer, viewModelResource.value, null, viewStrategy, true)
|
122 |
|
123 |
|
124 | .then(function (viewFactory) {
|
125 |
|
126 |
|
127 |
|
128 |
|
129 | if (!_this.compositionTransactionNotifier) {
|
130 | _this.compositionTransactionOwnershipToken = _this.compositionTransaction.tryCapture();
|
131 | }
|
132 | if (layoutInstruction.viewModel || layoutInstruction.view) {
|
133 | viewPortInstruction.layoutInstruction = layoutInstruction;
|
134 | }
|
135 | var viewPortComponentBehaviorInstruction = aureliaTemplating.BehaviorInstruction.dynamic(_this.element, viewModel, viewFactory);
|
136 | viewPortInstruction.controller = metadata.create(childContainer, viewPortComponentBehaviorInstruction);
|
137 | if (waitToSwap) {
|
138 | return null;
|
139 | }
|
140 | _this.swap(viewPortInstruction);
|
141 | });
|
142 | };
|
143 | RouterView.prototype.swap = function ($viewPortInstruction) {
|
144 | var _this = this;
|
145 |
|
146 | var viewPortInstruction = $viewPortInstruction;
|
147 | var viewPortController = viewPortInstruction.controller;
|
148 | var layoutInstruction = viewPortInstruction.layoutInstruction;
|
149 | var previousView = this.view;
|
150 |
|
151 | var work = function () {
|
152 | var swapStrategy = aureliaTemplating.SwapStrategies[_this.swapOrder] || aureliaTemplating.SwapStrategies.after;
|
153 | var viewSlot = _this.viewSlot;
|
154 | swapStrategy(viewSlot, previousView, function () { return Promise.resolve(viewSlot.add(_this.view)); }).then(function () {
|
155 | _this._notify();
|
156 | });
|
157 | };
|
158 |
|
159 | var ready = function (owningView_or_layoutView) {
|
160 | viewPortController.automate(_this.overrideContext, owningView_or_layoutView);
|
161 | var transactionOwnerShipToken = _this.compositionTransactionOwnershipToken;
|
162 |
|
163 |
|
164 |
|
165 |
|
166 | if (transactionOwnerShipToken) {
|
167 | return transactionOwnerShipToken
|
168 | .waitForCompositionComplete()
|
169 | .then(function () {
|
170 | _this.compositionTransactionOwnershipToken = null;
|
171 | return work();
|
172 | });
|
173 | }
|
174 |
|
175 | return work();
|
176 | };
|
177 |
|
178 |
|
179 | if (layoutInstruction) {
|
180 | if (!layoutInstruction.viewModel) {
|
181 |
|
182 |
|
183 | layoutInstruction.viewModel = new EmptyLayoutViewModel();
|
184 | }
|
185 |
|
186 | return this.compositionEngine
|
187 |
|
188 |
|
189 |
|
190 | .createController(layoutInstruction)
|
191 | .then(function (layoutController) {
|
192 | var layoutView = layoutController.view;
|
193 | aureliaTemplating.ShadowDOM.distributeView(viewPortController.view, layoutController.slots || layoutView.slots);
|
194 |
|
195 |
|
196 | layoutController.automate(aureliaBinding.createOverrideContext(layoutInstruction.viewModel), _this.owningView);
|
197 | layoutView.children.push(viewPortController.view);
|
198 | return layoutView || layoutController;
|
199 | })
|
200 | .then(function (newView) {
|
201 | _this.view = newView;
|
202 | return ready(newView);
|
203 | });
|
204 | }
|
205 |
|
206 |
|
207 |
|
208 |
|
209 | this.view = viewPortController.view;
|
210 | return ready(this.owningView);
|
211 | };
|
212 | |
213 |
|
214 |
|
215 |
|
216 |
|
217 | RouterView.prototype._notify = function () {
|
218 | var notifier = this.compositionTransactionNotifier;
|
219 | if (notifier) {
|
220 | notifier.done();
|
221 | this.compositionTransactionNotifier = null;
|
222 | }
|
223 | };
|
224 | |
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 | RouterView.$view = null;
|
231 | |
232 |
|
233 |
|
234 | RouterView.$resource = {
|
235 | name: 'router-view',
|
236 | bindables: ['swapOrder', 'layoutView', 'layoutViewModel', 'layoutModel', 'inherit-binding-context']
|
237 | };
|
238 | return RouterView;
|
239 | }());
|
240 |
|
241 |
|
242 |
|
243 | var RouterViewLocator = (function () {
|
244 | |
245 |
|
246 |
|
247 | function RouterViewLocator() {
|
248 | var _this = this;
|
249 | this.promise = new Promise(function (resolve) { return _this.resolve = resolve; });
|
250 | }
|
251 | |
252 |
|
253 |
|
254 |
|
255 | RouterViewLocator.prototype.findNearest = function () {
|
256 | return this.promise;
|
257 | };
|
258 |
|
259 | RouterViewLocator.prototype._notify = function (routerView) {
|
260 | this.resolve(routerView);
|
261 | };
|
262 | return RouterViewLocator;
|
263 | }());
|
264 |
|
265 |
|
266 | var EmptyClass = (function () {
|
267 | function EmptyClass() {
|
268 | }
|
269 | return EmptyClass;
|
270 | }());
|
271 | aureliaTemplating.inlineView('<template></template>')(EmptyClass);
|
272 |
|
273 |
|
274 |
|
275 | var TemplatingRouteLoader = (function (_super) {
|
276 | __extends(TemplatingRouteLoader, _super);
|
277 | function TemplatingRouteLoader(compositionEngine) {
|
278 | var _this = _super.call(this) || this;
|
279 | _this.compositionEngine = compositionEngine;
|
280 | return _this;
|
281 | }
|
282 | |
283 |
|
284 |
|
285 |
|
286 |
|
287 | TemplatingRouteLoader.prototype.resolveViewModel = function (router, config) {
|
288 | return new Promise(function (resolve, reject) {
|
289 | var viewModel;
|
290 | if ('moduleId' in config) {
|
291 | var moduleId = config.moduleId;
|
292 | if (moduleId === null) {
|
293 | viewModel = EmptyClass;
|
294 | }
|
295 | else {
|
296 |
|
297 |
|
298 |
|
299 | moduleId = aureliaPath.relativeToFile(moduleId, aureliaMetadata.Origin.get(router.container.viewModel.constructor).moduleId);
|
300 | if (/\.html/i.test(moduleId)) {
|
301 | viewModel = createDynamicClass(moduleId);
|
302 | }
|
303 | else {
|
304 | viewModel = moduleId;
|
305 | }
|
306 | }
|
307 | return resolve(viewModel);
|
308 | }
|
309 |
|
310 | reject(new Error('Invalid route config. No "moduleId" found.'));
|
311 | });
|
312 | };
|
313 | |
314 |
|
315 |
|
316 |
|
317 |
|
318 | TemplatingRouteLoader.prototype.createChildContainer = function (router) {
|
319 | var childContainer = router.container.createChild();
|
320 | childContainer.registerSingleton(RouterViewLocator);
|
321 | childContainer.getChildRouter = function () {
|
322 | var childRouter;
|
323 | childContainer.registerHandler(aureliaRouter.Router, function () { return childRouter || (childRouter = router.createChild(childContainer)); });
|
324 | return childContainer.get(aureliaRouter.Router);
|
325 | };
|
326 | return childContainer;
|
327 | };
|
328 | |
329 |
|
330 |
|
331 | TemplatingRouteLoader.prototype.loadRoute = function (router, config, _navInstruction) {
|
332 | var _this = this;
|
333 | return this
|
334 | .resolveViewModel(router, config)
|
335 | .then(function (viewModel) { return _this.compositionEngine.ensureViewModel({
|
336 | viewModel: viewModel,
|
337 | childContainer: _this.createChildContainer(router),
|
338 | view: config.view || config.viewStrategy,
|
339 | router: router
|
340 | }); });
|
341 | };
|
342 |
|
343 | TemplatingRouteLoader.inject = [aureliaTemplating.CompositionEngine];
|
344 | return TemplatingRouteLoader;
|
345 | }(aureliaRouter.RouteLoader));
|
346 |
|
347 | function createDynamicClass(moduleId) {
|
348 | var name = /([^\/^\?]+)\.html/i.exec(moduleId)[1];
|
349 | var DynamicClass = (function () {
|
350 | function DynamicClass() {
|
351 | }
|
352 | DynamicClass.prototype.bind = function (bindingContext) {
|
353 | this.$parent = bindingContext;
|
354 | };
|
355 | return DynamicClass;
|
356 | }());
|
357 | aureliaTemplating.customElement(name)(DynamicClass);
|
358 | aureliaTemplating.useView(moduleId)(DynamicClass);
|
359 | return DynamicClass;
|
360 | }
|
361 |
|
362 | var logger = LogManager.getLogger('route-href');
|
363 |
|
364 |
|
365 |
|
366 | var RouteHref = (function () {
|
367 | function RouteHref(router, element) {
|
368 | this.router = router;
|
369 | this.element = element;
|
370 | this.attribute = 'href';
|
371 | }
|
372 |
|
373 | RouteHref.inject = function () {
|
374 | return [aureliaRouter.Router, aureliaPal.DOM.Element];
|
375 | };
|
376 | RouteHref.prototype.bind = function () {
|
377 | this.isActive = true;
|
378 | this.processChange();
|
379 | };
|
380 | RouteHref.prototype.unbind = function () {
|
381 | this.isActive = false;
|
382 | };
|
383 | RouteHref.prototype.attributeChanged = function (value, previous) {
|
384 | if (previous) {
|
385 | this.element.removeAttribute(previous);
|
386 | }
|
387 | return this.processChange();
|
388 | };
|
389 | RouteHref.prototype.processChange = function () {
|
390 | var _this = this;
|
391 | return this.router
|
392 | .ensureConfigured()
|
393 | .then(function () {
|
394 | if (!_this.isActive) {
|
395 |
|
396 | return null;
|
397 | }
|
398 | var element = _this.element;
|
399 | var href = _this.router.generate(_this.route, _this.params);
|
400 | if (element.au.controller) {
|
401 | element.au.controller.viewModel[_this.attribute] = href;
|
402 | }
|
403 | else {
|
404 | element.setAttribute(_this.attribute, href);
|
405 | }
|
406 |
|
407 | return null;
|
408 | })
|
409 | .catch(function (reason) {
|
410 | logger.error(reason);
|
411 | });
|
412 | };
|
413 | |
414 |
|
415 |
|
416 | RouteHref.$resource = {
|
417 | type: 'attribute',
|
418 | name: 'route-href',
|
419 | bindables: [
|
420 | { name: 'route', changeHandler: 'processChange', primaryProperty: true },
|
421 | { name: 'params', changeHandler: 'processChange' },
|
422 | 'attribute'
|
423 | ]
|
424 | };
|
425 | return RouteHref;
|
426 | }());
|
427 |
|
428 | function configure(config) {
|
429 | config
|
430 | .singleton(aureliaRouter.RouteLoader, TemplatingRouteLoader)
|
431 | .singleton(aureliaRouter.Router, aureliaRouter.AppRouter)
|
432 | .globalResources(RouterView, RouteHref);
|
433 | config.container.registerAlias(aureliaRouter.Router, aureliaRouter.AppRouter);
|
434 | }
|
435 |
|
436 | exports.RouteHref = RouteHref;
|
437 | exports.RouterView = RouterView;
|
438 | exports.TemplatingRouteLoader = TemplatingRouteLoader;
|
439 | exports.configure = configure;
|
440 |
|