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