2018-01-12 12:27:57 +01:00
|
|
|
// (C) Copyright 2015 Martin Dougiamas
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-01-19 15:41:14 +01:00
|
|
|
import { Component, Input, Output, OnInit, OnDestroy, ElementRef, EventEmitter, ContentChild, TemplateRef } from '@angular/core';
|
2018-01-12 12:27:57 +01:00
|
|
|
import { CoreTabsComponent } from './tabs';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A tab to use inside core-tabs. The content of this tab will be displayed when the tab is selected.
|
|
|
|
*
|
|
|
|
* You must provide either a title or an icon for the tab.
|
|
|
|
*
|
2018-01-19 15:41:14 +01:00
|
|
|
* The tab content MUST be surrounded by ng-template. This component uses ngTemplateOutlet instead of ng-content because the
|
|
|
|
* latter executes all the code immediately. This means that all the tabs would be initialized as soon as your view is
|
|
|
|
* loaded, leading to performance issues.
|
|
|
|
*
|
2018-01-12 12:27:57 +01:00
|
|
|
* Example usage:
|
|
|
|
*
|
|
|
|
* <core-tabs selectedIndex="1">
|
|
|
|
* <core-tab [title]="'core.courses.timeline' | translate" (ionSelect)="switchTab('timeline')">
|
2018-01-19 15:41:14 +01:00
|
|
|
* <ng-template> <!-- This ng-template is required. -->
|
|
|
|
* <!-- Tab contents. -->
|
|
|
|
* </ng-template>
|
2018-01-12 12:27:57 +01:00
|
|
|
* </core-tab>
|
|
|
|
* </core-tabs>
|
|
|
|
*/
|
|
|
|
@Component({
|
|
|
|
selector: 'core-tab',
|
2018-01-19 15:41:14 +01:00
|
|
|
template: '<ng-container *ngIf="loaded" [ngTemplateOutlet]="template"></ng-container>'
|
2018-01-12 12:27:57 +01:00
|
|
|
})
|
|
|
|
export class CoreTabComponent implements OnInit, OnDestroy {
|
|
|
|
@Input() title?: string; // The tab title.
|
|
|
|
@Input() icon?: string; // The tab icon.
|
|
|
|
@Input() badge?: string; // A badge to add in the tab.
|
|
|
|
@Input() badgeStyle?: string; // The badge color.
|
|
|
|
@Input() enabled?: boolean = true; // Whether the tab is enabled.
|
|
|
|
@Input() show?: boolean = true; // Whether the tab should be shown.
|
|
|
|
@Input() id?: string; // An ID to identify the tab.
|
|
|
|
@Output() ionSelect: EventEmitter<CoreTabComponent> = new EventEmitter<CoreTabComponent>();
|
|
|
|
|
2018-01-19 15:41:14 +01:00
|
|
|
@ContentChild(TemplateRef) template: TemplateRef<any> // Template defined by the content.
|
|
|
|
|
2018-01-12 12:27:57 +01:00
|
|
|
element: HTMLElement; // The core-tab element.
|
2018-01-19 15:41:14 +01:00
|
|
|
loaded = false;
|
2018-01-12 12:27:57 +01:00
|
|
|
|
|
|
|
constructor(private tabs: CoreTabsComponent, element: ElementRef) {
|
|
|
|
this.element = element.nativeElement;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Component being initialized.
|
|
|
|
*/
|
|
|
|
ngOnInit() {
|
|
|
|
this.tabs.addTab(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Component destroyed.
|
|
|
|
*/
|
|
|
|
ngOnDestroy() {
|
|
|
|
this.tabs.removeTab(this);
|
|
|
|
}
|
2018-01-19 15:41:14 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Select tab.
|
|
|
|
*/
|
|
|
|
selectTab() {
|
|
|
|
this.element.classList.add('selected');
|
|
|
|
this.loaded = true;
|
|
|
|
this.ionSelect.emit(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unselect tab.
|
|
|
|
*/
|
|
|
|
unselectTab() {
|
|
|
|
this.element.classList.remove('selected');
|
|
|
|
}
|
2018-01-12 12:27:57 +01:00
|
|
|
}
|