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