Final commit to master merge from
[sdc.git] / catalog-ui / src / app / ng2 / components / ui / tabs / tabs.component.ts
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 import { Component, ContentChildren, QueryList, AfterContentInit, Input, Output, EventEmitter } from '@angular/core';
22 import { Tab } from './tab/tab.component';
23 import { ViewEncapsulation } from '@angular/core';
24 import { trigger, state, style, transition, animate, keyframes } from '@angular/core';
25
26 @Component({
27     selector: 'tabs',
28     templateUrl: './tabs.component.html',
29     styleUrls: ['./tabs.component.less'],
30     encapsulation: ViewEncapsulation.None,
31     animations: [
32         trigger('indicatorAnimation', [
33             transition(':enter', [style({ transform: 'translateY(-50%)', opacity: 0 }), animate('250ms', style({ transform: 'translateY(0)', opacity: 1 })) ]),
34             transition(':leave', [style({ opacity: 1 }), animate('500ms', style({ opacity: 0 })) ])
35         ])
36     ]
37 })
38 export class Tabs implements AfterContentInit {
39
40     @Input() tabStyle: string;
41     @Input() hideIndicationOnTabChange?: boolean = false;
42     @ContentChildren(Tab) tabs: QueryList<Tab>;
43     @Output() tabChanged: EventEmitter<Tab> = new EventEmitter<Tab>();
44
45
46     ngAfterContentInit() {  
47         //after contentChildren are set, determine active tab. If no active tab set, activate the first one
48         let activeTabs = this.tabs.filter((tab) => tab.active);
49  
50         if (activeTabs.length === 0) {
51             this.selectTab(this.tabs.first);
52         }
53     }
54
55     selectTab(tab: Tab) {
56         //activate the tab the user clicked.
57         this.tabs.toArray().forEach(tab => {
58             tab.active = false;
59             if (this.hideIndicationOnTabChange && tab.indication) {
60                 tab.indication = null;
61             }
62         });
63         tab.active = true;
64         this.tabChanged.emit(tab);
65     }
66
67     triggerTabChange(tabTitle) {
68         this.tabs.toArray().forEach(tab => {
69             tab.active = (tab.title == tabTitle) ? true : false;
70         });
71     }
72
73     setTabIndication(tabTitle:string, indication?:number) {
74         let selectedTab: Tab = this.tabs.toArray().find(tab => tab.title == tabTitle);
75         selectedTab.indication = indication || null;
76     }
77
78 }