Sync Integ to Master
[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 export {Tab};
27
28 @Component({
29     selector: 'tabs',
30     templateUrl: './tabs.component.html',
31     styleUrls: ['./tabs.component.less'],
32     encapsulation: ViewEncapsulation.None,
33     animations: [
34         trigger('indicatorAnimation', [
35             transition(':enter', [style({ transform: 'translateY(-50%)', opacity: 0 }), animate('250ms', style({ transform: 'translateY(0)', opacity: 1 })) ]),
36             transition(':leave', [style({ opacity: 1 }), animate('500ms', style({ opacity: 0 })) ])
37         ])
38     ]
39 })
40 export class Tabs implements AfterContentInit {
41
42     @Input() tabStyle: string;
43     @Input() hideIndicationOnTabChange?: boolean = false;
44     @ContentChildren(Tab) tabs: QueryList<Tab>;
45     @Output() tabChanged: EventEmitter<Tab> = new EventEmitter<Tab>();
46
47
48     ngAfterContentInit() {  
49         //after contentChildren are set, determine active tab. If no active tab set, activate the first one
50         let activeTabs = this.tabs.filter((tab) => tab.active);
51  
52         if (activeTabs.length === 0) {
53             this.selectTab(this.tabs.first);
54         }
55     }
56
57     selectTab(tab: Tab) {
58         //activate the tab the user clicked.
59         this.tabs.toArray().forEach(tab => {
60             tab.active = false;
61             if (this.hideIndicationOnTabChange && tab.indication) {
62                 tab.indication = null;
63             }
64         });
65         tab.active = true;
66         this.tabChanged.emit(tab);
67     }
68
69     triggerTabChange(tabTitle) {
70         this.tabs.toArray().forEach(tab => {
71             tab.active = (tab.title == tabTitle) ? true : false;
72         });
73     }
74
75     setTabIndication(tabTitle:string, indication?:number) {
76         let selectedTab: Tab = this.tabs.toArray().find(tab => tab.title == tabTitle);
77         selectedTab.indication = indication || null;
78     }
79
80 }