| OLD | NEW |
| (Empty) | |
| 1 part of angular.routing; |
| 2 |
| 3 /** |
| 4 * A factory of route to template bindings. |
| 5 */ |
| 6 class ViewFactory { |
| 7 NgRoutingHelper locationService; |
| 8 |
| 9 ViewFactory(this.locationService); |
| 10 |
| 11 call(String templateUrl) => |
| 12 (RouteEvent event) => |
| 13 locationService._route(event.route, templateUrl, fromEvent: true); |
| 14 } |
| 15 |
| 16 /** |
| 17 * An interface that must be implemented by the user of routing library and |
| 18 * should include the route initialization. |
| 19 * |
| 20 * The [init] method will be called by the framework once the router is |
| 21 * instantiated but before [NgBindRouteDirective] and [NgViewDirective]. |
| 22 */ |
| 23 abstract class RouteInitializer { |
| 24 void init(Router router, ViewFactory viewFactory); |
| 25 } |
| 26 |
| 27 /** |
| 28 * A singleton helper service that handles routing initialization, global |
| 29 * events and view registries. |
| 30 */ |
| 31 @NgInjectableService() |
| 32 class NgRoutingHelper { |
| 33 final Router router; |
| 34 List<NgViewDirective> portals = <NgViewDirective>[]; |
| 35 Map<String, String> _templates = new Map<String, String>(); |
| 36 |
| 37 NgRoutingHelper(RouteInitializer initializer, this.router) { |
| 38 if (initializer == null) { |
| 39 window.console.error('No RouteInitializer implementation provided.'); |
| 40 return; |
| 41 }; |
| 42 |
| 43 initializer.init(router, new ViewFactory(this)); |
| 44 router.onRouteStart.listen((RouteStartEvent routeEvent) { |
| 45 routeEvent.completed.then((success) { |
| 46 if (success) { |
| 47 portals.forEach((NgViewDirective p) => p._maybeReloadViews()); |
| 48 } |
| 49 }); |
| 50 }); |
| 51 |
| 52 router.listen(); |
| 53 } |
| 54 |
| 55 _reloadViews({Route startingFrom}) { |
| 56 var alreadyActiveViews = []; |
| 57 var activePath = router.activePath; |
| 58 if (startingFrom != null) { |
| 59 activePath = activePath.skip(_routeDepth(startingFrom)); |
| 60 } |
| 61 for (Route route in activePath) { |
| 62 var templateUrl = _templates[_routePath(route)]; |
| 63 if (templateUrl == null) continue; |
| 64 |
| 65 NgViewDirective view = portals.lastWhere((NgViewDirective v) { |
| 66 return _routePath(route) != _routePath(v._route) && |
| 67 _routePath(route).startsWith(_routePath(v._route)); |
| 68 }, orElse: () => null); |
| 69 if (view != null && !alreadyActiveViews.contains(view)) { |
| 70 view._show(templateUrl, route); |
| 71 alreadyActiveViews.add(view); |
| 72 break; |
| 73 } |
| 74 } |
| 75 } |
| 76 |
| 77 _route(Route route, String template, {bool fromEvent}) { |
| 78 _templates[_routePath(route)] = template; |
| 79 } |
| 80 |
| 81 _registerPortal(NgViewDirective ngView) { |
| 82 portals.add(ngView); |
| 83 } |
| 84 |
| 85 _unregisterPortal(NgViewDirective ngView) { |
| 86 portals.remove(ngView); |
| 87 } |
| 88 } |
| 89 |
| 90 String _routePath(Route route) { |
| 91 var path = []; |
| 92 var p = route; |
| 93 while (p.parent != null) { |
| 94 path.insert(0, p.name); |
| 95 p = p.parent; |
| 96 } |
| 97 return path.join('.'); |
| 98 } |
| 99 |
| 100 int _routeDepth(Route route) { |
| 101 var depth = 0; |
| 102 var p = route; |
| 103 while (p.parent != null) { |
| 104 depth++; |
| 105 p = p.parent; |
| 106 } |
| 107 return depth; |
| 108 } |
| OLD | NEW |