refactoring RouteBuilderFactory into service
This commit is contained in:
parent
edd7314624
commit
2a59014f0b
7 changed files with 56 additions and 6 deletions
|
@ -0,0 +1,47 @@
|
|||
import { RouteBuilder } from './route-builder.service';
|
||||
|
||||
|
||||
export function routeBuilderFactory() {
|
||||
return (routeProvider, pages, profiles, currentProfile): RouteBuilder => {
|
||||
return new RouteBuilderImpl(routeProvider, pages, profiles, currentProfile);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export class RouteBuilderImpl implements RouteBuilder {
|
||||
|
||||
constructor(private routeProvider, private pages, public profiles, currentProfile) {
|
||||
for (let i = 0; i < this.profiles.length; ++i) {
|
||||
if (this.profiles[i].id == currentProfile) {
|
||||
this.profiles = this.profiles.slice(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public otherwise(options: any): void {
|
||||
this.routeProvider.otherwise(options);
|
||||
}
|
||||
|
||||
public route(path: string, pagename: string): RouteBuilder {
|
||||
// Lookup the page, matching our lists of profiles.
|
||||
var pair = this.pages.get(pagename, this.profiles);
|
||||
if (!pair) {
|
||||
throw Error('Unknown page: ' + pagename);
|
||||
}
|
||||
|
||||
// Create the route.
|
||||
var foundProfile = pair[0];
|
||||
var page = pair[1];
|
||||
var templateUrl = foundProfile.templatePath + page.templateName;
|
||||
|
||||
var options = page.flags || {};
|
||||
options['templateUrl'] = templateUrl;
|
||||
options['reloadOnSearch'] = false;
|
||||
options['controller'] = page.controller;
|
||||
|
||||
this.routeProvider.when(path, options);
|
||||
|
||||
return this;
|
||||
}
|
||||
}
|
Reference in a new issue