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