import { Component, Input, Output, EventEmitter, trigger, transition, style, animate, state, OnInit } from '@angular/core';
import { FabButtonModel } from '../../models/fab/fab-button.model';
import { SETTINGS } from '../../../assets/settings/fab/settings';
export type BtnAnimateState = ('void' | 'right-show' | 'left-show' | 'up-show' | 'down-show');
@Component({
selector: 'fab-core',
template: "
",
styles: [":host { position: relative; z-index: 100; display: inline-flex; width: 72px; height: 72px; } :host button.cf-core-fab-trigger { position: relative; z-index: 1001; margin: 8px; } :host button.cf-core-fab-trigger md-icon.fa { font-size: 24px; } :host div.cf-core-fab-actions { position: absolute; z-index: 1000; } :host div.cf-core-fab-actions div.cf-core-fab-action-item button.cf-core-fab-action-button { margin: 4px; } :host div.cf-core-fab-actions div.cf-core-fab-action-item button.cf-core-fab-action-button md-icon.fa { font-size: 24px; } :host div.cf-core-fab-actions.direction-right { top: 50%; left: 50%; padding-left: 36px; height: 0px; } :host div.cf-core-fab-actions.direction-down { top: 50%; left: 50%; padding-top: 36px; width: 0px; } :host div.cf-core-fab-actions.direction-left { top: 50%; right: 50%; padding-right: 36px; height: 0px; } :host div.cf-core-fab-actions.direction-up { bottom: 50%; left: 50%; padding-bottom: 36px; width: 0px; } [hidden] { display: none !important; } "],
animations: [ trigger( 'buttonAnimation',
[
transition( 'void => right-show', [ style({ transform: 'translateX(-100%) scale(0.5)', opacity: 0 }), animate('400ms ease-out', style({ transform: 'translateX(0) scale(1)', opacity: 1 })) ] ),
transition( 'right-show => void', [ style({ transform: 'translateX(0) scale(1)', 'opacity': 1 }), animate('400ms ease-in', style({ transform: 'translateX(-100%) scale(0.5)', opacity: 0 })) ] ),
transition( 'void => left-show', [ style({ transform: 'translateX(100%) scale(0.5)', opacity: 0 }), animate('400ms ease-out', style({ transform: 'translateX(0) scale(1)', opacity: 1 })) ] ),
transition( 'left-show => void', [ style({ transform: 'translateX(0) scale(1)', 'opacity': 1 }), animate('400ms ease-in', style({ transform: 'translateX(100%) scale(0.5)', opacity: 0 })) ] ),
transition( 'void => down-show', [ style({ transform: 'translateY(-100%) scale(0.5)', opacity: 0 }), animate('400ms ease-out', style({ transform: 'translateY(0) scale(1)', opacity: 1 })) ] ),
transition( 'down-show => void', [ style({ transform: 'translateY(0) scale(1)', 'opacity': 1 }), animate('400ms ease-in', style({ transform: 'translateY(-100%) scale(0.5)', opacity: 0 })) ] ),
transition( 'void => up-show', [ style({ transform: 'translateY(100%) scale(0.5)', opacity: 0 }), animate('400ms ease-out', style({ transform: 'translateY(0) scale(1)', opacity: 1 })) ] ),
transition( 'up-show => void', [ style({ transform: 'translateY(0) scale(1)', 'opacity': 1 }), animate('400ms ease-in', style({ transform: 'translateY(100%) scale(0.5)', opacity: 0 })) ] )
]
)
]
})
export class FabCoreComponent {
@Input() buttons: Array;
@Input() mainButton: FabButtonModel;
@Input() direction: string;
@Input() styling: any;
@Output() triggerAction: EventEmitter = new EventEmitter();
@Output() buttonAction: EventEmitter = new EventEmitter();
@Input() showButtons: boolean = false;
@Input() stayOpened: boolean = false;
public animateState: BtnAnimateState;
public constructor() {}
public setAnimateState() {
switch (this.direction) {
case 'right': this.animateState = 'right-show'; break;
case 'left': this.animateState = 'left-show'; break;
case 'down': this.animateState = 'down-show'; break;
case 'up': this.animateState = 'up-show'; break;
default: this.animateState = 'right-show';
}
}
public checkTriggerIcon(trigger) {
if(!trigger.hasOwnProperty('icon')) {
trigger['icon'] = SETTINGS.triggerButton.icon;
}
}
public checkDirection() {
if (!this.direction) { this.direction = SETTINGS.direction; }
else {
switch (this.direction) {
case 'right': break;
case 'left': break;
case 'down': break;
case 'up': break;
default: this.direction = SETTINGS.direction;
}
}
}
public tooltipPosition(button) {
if(button.hasOwnProperty('label')){
if(!button.hasOwnProperty('labelPosition')) { button['labelPosition'] = SETTINGS.labelPosition; }
else {
switch (button.labelPosition) {
case 'above': break;
case 'below': break;
case 'left': break;
case 'right': break;
default: button['labelPosition'] = SETTINGS.labelPosition;
}
}
}
}
public hasHidden(btn: Object) { return btn.hasOwnProperty('display') ? true : false; }
public iconType(icon) {
if(icon.name) {
return icon.name.indexOf('fa-') === 0 ? true : false;
}
}
ngOnInit() {
if(this.showButtons) this.setAnimateState();
this.checkDirection();
if(this.mainButton ) {
this.checkTriggerIcon(this.mainButton);
this.tooltipPosition(this.mainButton);
}else{
this.mainButton = SETTINGS.triggerButton;
this.checkTriggerIcon(this.mainButton);
this.tooltipPosition(this.mainButton);
}
if(this.buttons.length>0){
for (var i = 0; i < this.buttons.length; ++i) {
this.tooltipPosition(this.buttons[i]);
}
}
}
public triggerBtnMenu(trigger) {
this.showButtons = !this.showButtons;
if (!this.showButtons) { this.animateState = 'void'; }
else { this.setAnimateState(); }
if(trigger) this.triggerAction.emit(trigger);
}
public fireAction(index, button) {
this.buttonAction.emit({cfIndex: index, cfItem: button});
if(!this.stayOpened) this.triggerBtnMenu(null);
}
public shouldShowLabel(title: string) {
if (!title || title == '') return false;
return true;
}
}