angular-cn/public/docs/_examples/homepage-tabs/ts/app/ui_tabs.ts

52 lines
1.2 KiB
TypeScript
Raw Normal View History

2015-12-15 15:34:29 -05:00
// #docregion
import { Component, Directive, Input, QueryList,
ViewContainerRef, TemplateRef, ContentChildren } from '@angular/core';
2015-12-15 15:34:29 -05:00
@Directive({
selector: '[ui-pane]'
})
export class UiPane {
@Input() title: string;
2016-04-27 14:28:22 -04:00
private _active: boolean = false;
2015-12-15 15:34:29 -05:00
constructor(public viewContainer: ViewContainerRef,
2016-04-27 14:28:22 -04:00
public templateRef: TemplateRef<any>) { }
2015-12-15 15:34:29 -05:00
@Input() set active(active: boolean) {
2016-04-27 14:28:22 -04:00
if (active === this._active) { return; }
2015-12-15 15:34:29 -05:00
this._active = active;
if (active) {
this.viewContainer.createEmbeddedView(this.templateRef);
} else {
this.viewContainer.remove(0);
}
}
get active(): boolean {
return this._active;
}
}
@Component({
selector: 'ui-tabs',
template: `
<ul class="nav nav-tabs">
<li *ngFor="let pane of panes"
2015-12-15 15:34:29 -05:00
(click)="select(pane)"
role="presentation" [class.active]="pane.active">
<a>{{pane.title}}</a>
2015-12-15 15:34:29 -05:00
</li>
</ul>
<ng-content></ng-content>
`,
2016-04-27 14:28:22 -04:00
styles: ['a { cursor: pointer; cursor: hand; }']
2015-12-15 15:34:29 -05:00
})
export class UiTabs {
@ContentChildren(UiPane) panes: QueryList<UiPane>;
select(pane: UiPane) {
2016-04-27 14:28:22 -04:00
this.panes.toArray().forEach((p: UiPane) => p.active = p === pane);
2015-12-15 15:34:29 -05:00
}
}