Skip to content

Instantly share code, notes, and snippets.

@fxck
Last active November 8, 2023 19:22
Show Gist options
  • Select an option

  • Save fxck/108dccf4529becf9bc92 to your computer and use it in GitHub Desktop.

Select an option

Save fxck/108dccf4529becf9bc92 to your computer and use it in GitHub Desktop.
import {
Component,
View,
NgFor,
NgIf
} from 'angular2/angular2';
import {
Router,
ROUTER_DIRECTIVES
} from 'angular2/router';
import {
Observable
} from '@reactivex/rxjs/dist/cjs/Rx';
@Component({
selector: 'breadcrumbs'
})
@View({
template: `
<div *ng-for="#route of breadcrumbsCollection">
<a
href=""
*ng-if="route.terminal"
[router-link]="route.linkParams">
{{ route.displayName }}
</a>
<span *ng-if="!route.terminal">{{ route.displayName }}</span>
</div>
`,
directives: [
ROUTER_DIRECTIVES,
NgFor,
NgIf
]
})
export class BreadcrumbsComponent {
public breadcrumbsCollection: Array<any>;
constructor(private _router: Router) {
this._router.subscribe(routeUrl => {
let instructions = [];
this._router.recognize(routeUrl).then(instruction => {
instructions.push(instruction);
while (instruction.child) {
instruction = instruction.child;
instructions.push(instruction);
}
this.breadcrumbsCollection = instructions
.map((inst, index) => {
return {
displayName: inst.component.routeData.get('displayName'),
as: inst.component.routeData.get('name'),
terminal: inst.component.terminal,
linkParams: this._getLinkParams(instructions, index)
}
});
});
});
}
private _getLinkParams(instructions, until) {
let linkParams = [];
instructions.forEach((item, index) => {
let component = item.component;
if (index <= until) {
linkParams.push(component.routeData.get('name'));
if (!this._isEmpty(component.params)) {
linkParams.push(component.params);
}
}
});
return linkParams;
}
private _isEmpty(obj) {
for(var prop in obj) {
if(obj.hasOwnProperty(prop))
return false;
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment