| OLD | NEW |
| 1 part of angular.routing; | 1 part of angular.routing; |
| 2 | 2 |
| 3 /** | 3 /** |
| 4 * A factory of route to template bindings. | 4 * A factory of route to template bindings. |
| 5 */ | 5 */ |
| 6 class ViewFactory { | 6 class ViewFactory { |
| 7 NgRoutingHelper locationService; | 7 NgRoutingHelper locationService; |
| 8 | 8 |
| 9 ViewFactory(this.locationService); | 9 ViewFactory(this.locationService); |
| 10 | 10 |
| 11 call(String templateUrl) => | 11 call(String templateUrl) => |
| 12 (RouteEvent event) => | 12 (RouteEnterEvent event) => _enterHandler(event, templateUrl); |
| 13 locationService._route(event.route, templateUrl, fromEvent: true); | 13 |
| 14 _enterHandler(RouteEnterEvent event, String templateUrl, [List<Module> modules
]) => |
| 15 locationService._route(event.route, templateUrl, fromEvent: true, modules:
modules); |
| 16 |
| 17 configure(Map<String, NgRouteCfg> config) => |
| 18 _configure(locationService.router.root, config); |
| 19 |
| 20 _configure(Route route, Map<String, NgRouteCfg> config) { |
| 21 config.forEach((name, cfg) { |
| 22 var moduledCalled = false; |
| 23 List<Module> newModules; |
| 24 route.addRoute( |
| 25 name: name, |
| 26 path: cfg.path, |
| 27 defaultRoute: cfg.defaultRoute, |
| 28 enter: (RouteEnterEvent e) { |
| 29 if (cfg.view != null) { |
| 30 _enterHandler(e, cfg.view, newModules); |
| 31 } |
| 32 if (cfg.enter != null) { |
| 33 cfg.enter(e); |
| 34 } |
| 35 }, |
| 36 preEnter: (RoutePreEnterEvent e) { |
| 37 if (cfg.modules != null && !moduledCalled) { |
| 38 moduledCalled = true; |
| 39 var modules = cfg.modules(); |
| 40 if (modules is Future) { |
| 41 e.allowEnter(modules.then((List<Module> m) { |
| 42 newModules = m; |
| 43 return true; |
| 44 })); |
| 45 } else { |
| 46 newModules = modules; |
| 47 } |
| 48 } |
| 49 if (cfg.preEnter != null) { |
| 50 cfg.preEnter(e); |
| 51 } |
| 52 }, |
| 53 leave: cfg.leave, |
| 54 mount: (Route mountRoute) { |
| 55 if (cfg.mount != null) { |
| 56 _configure(mountRoute, cfg.mount); |
| 57 } |
| 58 }); |
| 59 }); |
| 60 } |
| 61 } |
| 62 |
| 63 NgRouteCfg ngRoute({String path, String view, Map<String, NgRouteCfg> mount, |
| 64 modules(), bool defaultRoute: false, RoutePreEnterEventHandler preEnter, |
| 65 RouteEnterEventHandler enter, RouteLeaveEventHandler leave}) => |
| 66 new NgRouteCfg(path: path, view: view, mount: mount, modules: modules, |
| 67 defaultRoute: defaultRoute, preEnter: preEnter, enter: enter, |
| 68 leave: leave); |
| 69 |
| 70 class NgRouteCfg { |
| 71 final String path; |
| 72 final String view; |
| 73 final Map<String, NgRouteCfg> mount; |
| 74 final Function modules; |
| 75 final bool defaultRoute; |
| 76 final RouteEnterEventHandler enter; |
| 77 final RoutePreEnterEventHandler preEnter; |
| 78 final RouteLeaveEventHandler leave; |
| 79 |
| 80 NgRouteCfg({this.view, this.path, this.mount, this.modules, this.defaultRoute, |
| 81 this.enter, this.preEnter, this.leave}); |
| 14 } | 82 } |
| 15 | 83 |
| 16 /** | 84 /** |
| 17 * An interface that must be implemented by the user of routing library and | 85 * An interface that must be implemented by the user of routing library and |
| 18 * should include the route initialization. | 86 * should include the route initialization. |
| 19 * | 87 * |
| 20 * The [init] method will be called by the framework once the router is | 88 * The [init] method will be called by the framework once the router is |
| 21 * instantiated but before [NgBindRouteDirective] and [NgViewDirective]. | 89 * instantiated but before [NgBindRouteDirective] and [NgViewDirective]. |
| 90 * |
| 91 * Deprecated: use RouteInitializerFn instead. |
| 22 */ | 92 */ |
| 93 @deprecated |
| 23 abstract class RouteInitializer { | 94 abstract class RouteInitializer { |
| 24 void init(Router router, ViewFactory viewFactory); | 95 void init(Router router, ViewFactory viewFactory); |
| 25 } | 96 } |
| 26 | 97 |
| 27 /** | 98 /** |
| 99 * An typedef that must be implemented by the user of routing library and |
| 100 * should include the route initialization. |
| 101 * |
| 102 * The function will be called by the framework once the router is |
| 103 * instantiated but before [NgBindRouteDirective] and [NgViewDirective]. |
| 104 */ |
| 105 typedef void RouteInitializerFn(Router router, ViewFactory viewFactory); |
| 106 |
| 107 /** |
| 28 * A singleton helper service that handles routing initialization, global | 108 * A singleton helper service that handles routing initialization, global |
| 29 * events and view registries. | 109 * events and view registries. |
| 30 */ | 110 */ |
| 31 @NgInjectableService() | 111 @NgInjectableService() |
| 32 class NgRoutingHelper { | 112 class NgRoutingHelper { |
| 33 final Router router; | 113 final Router router; |
| 114 final NgApp _ngApp; |
| 34 List<NgViewDirective> portals = <NgViewDirective>[]; | 115 List<NgViewDirective> portals = <NgViewDirective>[]; |
| 35 Map<String, String> _templates = new Map<String, String>(); | 116 Map<String, _View> _templates = new Map<String, _View>(); |
| 36 | 117 |
| 37 NgRoutingHelper(RouteInitializer initializer, this.router) { | 118 NgRoutingHelper(RouteInitializer initializer, Injector injector, this.router,
this._ngApp) { |
| 38 if (initializer == null) { | 119 // TODO: move this to constructor parameters when di issue is fixed: |
| 120 // https://github.com/angular/di.dart/issues/40 |
| 121 RouteInitializerFn initializerFn = injector.get(RouteInitializerFn); |
| 122 if (initializer == null && initializerFn == null) { |
| 39 window.console.error('No RouteInitializer implementation provided.'); | 123 window.console.error('No RouteInitializer implementation provided.'); |
| 40 return; | 124 return; |
| 41 }; | 125 }; |
| 42 | 126 |
| 43 initializer.init(router, new ViewFactory(this)); | 127 if (initializerFn != null) { |
| 128 initializerFn(router, new ViewFactory(this)); |
| 129 } else { |
| 130 initializer.init(router, new ViewFactory(this)); |
| 131 } |
| 44 router.onRouteStart.listen((RouteStartEvent routeEvent) { | 132 router.onRouteStart.listen((RouteStartEvent routeEvent) { |
| 45 routeEvent.completed.then((success) { | 133 routeEvent.completed.then((success) { |
| 46 if (success) { | 134 if (success) { |
| 47 portals.forEach((NgViewDirective p) => p._maybeReloadViews()); | 135 portals.forEach((NgViewDirective p) => p._maybeReloadViews()); |
| 48 } | 136 } |
| 49 }); | 137 }); |
| 50 }); | 138 }); |
| 51 | 139 |
| 52 router.listen(); | 140 router.listen(appRoot: _ngApp.root); |
| 53 } | 141 } |
| 54 | 142 |
| 55 _reloadViews({Route startingFrom}) { | 143 _reloadViews({Route startingFrom}) { |
| 56 var alreadyActiveViews = []; | 144 var alreadyActiveViews = []; |
| 57 var activePath = router.activePath; | 145 var activePath = router.activePath; |
| 58 if (startingFrom != null) { | 146 if (startingFrom != null) { |
| 59 activePath = activePath.skip(_routeDepth(startingFrom)); | 147 activePath = activePath.skip(_routeDepth(startingFrom)); |
| 60 } | 148 } |
| 61 for (Route route in activePath) { | 149 for (Route route in activePath) { |
| 62 var templateUrl = _templates[_routePath(route)]; | 150 var viewDef = _templates[_routePath(route)]; |
| 63 if (templateUrl == null) continue; | 151 if (viewDef == null) continue; |
| 152 var templateUrl = viewDef.template; |
| 64 | 153 |
| 65 NgViewDirective view = portals.lastWhere((NgViewDirective v) { | 154 NgViewDirective view = portals.lastWhere((NgViewDirective v) { |
| 66 return _routePath(route) != _routePath(v._route) && | 155 return _routePath(route) != _routePath(v._route) && |
| 67 _routePath(route).startsWith(_routePath(v._route)); | 156 _routePath(route).startsWith(_routePath(v._route)); |
| 68 }, orElse: () => null); | 157 }, orElse: () => null); |
| 69 if (view != null && !alreadyActiveViews.contains(view)) { | 158 if (view != null && !alreadyActiveViews.contains(view)) { |
| 70 view._show(templateUrl, route); | 159 view._show(templateUrl, route, viewDef.modules); |
| 71 alreadyActiveViews.add(view); | 160 alreadyActiveViews.add(view); |
| 72 break; | 161 break; |
| 73 } | 162 } |
| 74 } | 163 } |
| 75 } | 164 } |
| 76 | 165 |
| 77 _route(Route route, String template, {bool fromEvent}) { | 166 _route(Route route, String template, {bool fromEvent, List<Module> modules}) { |
| 78 _templates[_routePath(route)] = template; | 167 _templates[_routePath(route)] = new _View(template, modules); |
| 79 } | 168 } |
| 80 | 169 |
| 81 _registerPortal(NgViewDirective ngView) { | 170 _registerPortal(NgViewDirective ngView) { |
| 82 portals.add(ngView); | 171 portals.add(ngView); |
| 83 } | 172 } |
| 84 | 173 |
| 85 _unregisterPortal(NgViewDirective ngView) { | 174 _unregisterPortal(NgViewDirective ngView) { |
| 86 portals.remove(ngView); | 175 portals.remove(ngView); |
| 87 } | 176 } |
| 88 } | 177 } |
| 89 | 178 |
| 179 class _View { |
| 180 final String template; |
| 181 final List<Module> modules; |
| 182 |
| 183 _View(this.template, this.modules); |
| 184 } |
| 185 |
| 90 String _routePath(Route route) { | 186 String _routePath(Route route) { |
| 91 var path = []; | 187 var path = []; |
| 92 var p = route; | 188 var p = route; |
| 93 while (p.parent != null) { | 189 while (p.parent != null) { |
| 94 path.insert(0, p.name); | 190 path.insert(0, p.name); |
| 95 p = p.parent; | 191 p = p.parent; |
| 96 } | 192 } |
| 97 return path.join('.'); | 193 return path.join('.'); |
| 98 } | 194 } |
| 99 | 195 |
| 100 int _routeDepth(Route route) { | 196 int _routeDepth(Route route) { |
| 101 var depth = 0; | 197 var depth = 0; |
| 102 var p = route; | 198 var p = route; |
| 103 while (p.parent != null) { | 199 while (p.parent != null) { |
| 104 depth++; | 200 depth++; |
| 105 p = p.parent; | 201 p = p.parent; |
| 106 } | 202 } |
| 107 return depth; | 203 return depth; |
| 108 } | 204 } |
| OLD | NEW |