/**
* @license
* Copyright Akveo. All Rights Reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*/
import {
Component,
Input,
Output,
EventEmitter,
ContentChildren,
QueryList,
AfterContentInit,
HostBinding,
} from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { convertToBoolProperty } from '../helpers';
/**
* Specific tab container.
*/
@Component({
selector: 'nb-tab',
template: `
`,
})
export class NbTabComponent {
@Input() tabTitle: string;
@Input() route: string;
@HostBinding('class.content-active')
activeValue: boolean = false;
@Input()
get active() {
return this.activeValue;
}
set active(val: boolean) {
this.activeValue = convertToBoolProperty(val);
if (this.activeValue) {
this.init = true;
}
}
// TODO: it makes sense to add 'lazyLoad' input to 'nb-tabset' component and make this functionality configurable
init: boolean = false;
}
// TODO: Combine tabset with route-tabset, so that we can:
// - have similar interface
// - easy to migrate from one to another
// - can mix them both (route/content tab)
/**
*
* Dynamic tabset component.
* Renders ` containers inside.
*
* @example Basic tabset example
*
* ```
*
*
* Tab content 1
*
*
* Tab content 2
*
*
*
* @styles
*
* tabs-font-family:
* tabs-font-size:
* tabs-content-font-family:
* tabs-content-font-size:
* tabs-active-bg:
* tabs-active-font-weight:
* tabs-padding:
* tabs-content-padding:
* tabs-header-bg:
* tabs-separator:
* tabs-fg:
* tabs-fg-text:
* tabs-fg-heading:
* tabs-bg:
* tabs-selected:
*
```
*/
@Component({
selector: 'nb-tabset',
styleUrls: ['./tabset.component.scss'],
template: `
`,
})
export class NbTabsetComponent implements AfterContentInit {
@ContentChildren(NbTabComponent) tabs: QueryList;
@HostBinding('class.full-width')
private fullWidthValue: boolean = false;
/**
* Take full width of a parent
* @param {boolean} val
*/
@Input()
set fullWidth(val: boolean) {
this.fullWidthValue = convertToBoolProperty(val);
}
/**
* If specified - tabset listens to this parameter and selects corresponding tab.
* @type {string}
*/
@Input() routeParam: string;
/**
* Emits when tab is selected
* @type EventEmitter
*/
@Output() changeTab = new EventEmitter();
constructor(private route: ActivatedRoute) {
}
ngAfterContentInit() {
this.route.params
.subscribe((params: any) => {
const activeTab = this.tabs.find(tab => this.routeParam ? tab.route === params[this.routeParam] : tab.active);
this.selectTab(activeTab || this.tabs.first);
});
}
// TODO: navigate to routeParam
selectTab(selectedTab: NbTabComponent) {
this.tabs.forEach(tab => tab.active = tab === selectedTab);
this.changeTab.emit(selectedTab);
}
}