import { Component, OnInit, Input, Output, ElementRef, EventEmitter, HostBinding } from '@angular/core'; import { CfCoreComponent } from '../core/core.component'; import { IconModel } from '../../models/icon/icon.model'; import { IconStylingModel } from '../../models/icon/icon-styling.model'; import { IconTemplates } from '../../templates/icon.template'; /** *

Icon is creating based on Icon Class.

*

Example of using component:

*
 * <cf-icon [properties]="icon"></cf-icon>
 * 
*/ @Component({ selector: 'cf-icon', template: "
{{currentIcon.type === 'mi' ? currentIcon.name : ''}}
", styles: [":host { position: relative; z-index: 0; display: inline-flex; } :host.cf-icon-is-disabled md-icon { color: rgba(0, 0, 0, 0.38) !important; background: transparent !important; background-color: transparent !important; } :host.cf-menu-trigger md-icon { cursor: pointer; transition: all 0.2s ease; } :host.cf-menu-trigger md-icon:hover { transform: scale(1.1); } :host .cf-toggling-icon { cursor: pointer; } :host.notification-clickable cf-notification:hover { cursor: pointer; } .mat-icon { padding: 0px; } .cf-default-icon { background-color: transparent; } .cf-close-icon { color: #EF5350; } "] }) export class CfIconComponent extends CfCoreComponent implements OnInit { @HostBinding('class') notifacationClickable = ''; /** * It is Icon object which will recive values provided by Icon Class and after that md-icon will be builded on page. */ cfIcon: IconModel; /** *

Whether the icon is in toggle mode or normal mode.

*/ checked: boolean = true; currentIcon: any = {}; /** * It is cf-icon html attribute [settings] with json icon object. *

Example of icon object:

*
{
	 *	name: 'home',
	 *	size: '24px'	
	 * }
*

name: string

*

It is name of icon. Default is home.

*

Next icons libraries are available:

*

Material Icons

*

Font Awesome Icons

*

If icon name consists of more then one word, then:

*

- for Material Icons: join words with underscore (like: thumb_up)

*

- for Font Awesome Icons: join words with dash (like: fa-address-book)

*

size: string

* It is icon size. Can be in pixels, em, percetages. Default is 24px */ @Input() public properties: IconModel; /** *

The styling object of the icon.

*

Refer to ../models/icon/icon-styling.model.ts.

*/ @Input() styling: IconStylingModel; /** *

The name of the icon as external property.

*

mi Icons and FontAwesome names work in this case.

*/ @Input() public name: string; /** *

The size of the icon and its container.

*

In pixels and percentage.

*/ @Input() public size: string; /** *

display or hide the icon.

*/ @Input() public display: boolean; /** * It is (cfOnToggle) attribute of cf-icon component. Means what event to call when toggle icon was clicked. */ @Output() cfOnToggle: EventEmitter = new EventEmitter(); /** * It is function for set correct toggle value/icon and emmit event with icon state value. */ cfIconToggled() { if(this.properties.toggle!=null) { this.checked = !this.checked; if(this.currentIcon==this.cfIcon) this.currentIcon = this.cfIcon.toggle; else this.currentIcon = this.cfIcon; this.cfOnToggle.emit(this.currentIcon.value); } } /** * It is function for creating Notification click events by using Core Component notificationAction event emmiter. * @param notification it is json notification object */ cfNotificationAction(notification) { this.notificationAction.emit(notification); } constructor(public elementRef: ElementRef) { super(elementRef); } /** *

On Icon component initialization next things are heppening:

* * */ ngOnInit() { if(this.notificationAction.observers.length > 0) this.notifacationClickable = 'notification-clickable'; this.activeTemplate = IconTemplates["defaultTemplate"]; if(this.compTemplate!=null && this.compTemplate!="") { if(IconTemplates[this.compTemplate]!=null) { this.activeTemplate = IconTemplates[this.compTemplate]; } else this.activeTemplate = this.compTemplate; } if(this.properties==null) this.properties = new IconModel(this.activeTemplate["property"]); else { var mainKeys = Object.keys(this.activeTemplate["property"]); if(this.properties.toggle!=null && this.activeTemplate["property"].toggle!=null) { var toggleKeys = Object.keys(this.activeTemplate["property"].toggle); for(let key of toggleKeys) { if(this.properties["toggle"][key]==null) this.properties["toggle"][key]=this.activeTemplate["property"]["toggle"][key]; } } for(let key of mainKeys) { if(this.properties[key]==null) this.properties[key]=this.activeTemplate["property"][key]; } } if(this.styling==null) this.styling = new IconStylingModel(this.activeTemplate["style"]); if(this.display!=null) this.properties.display = this.display; if(this.name!=null) { this.properties.name = this.name; if(this.name.startsWith("fa-")) this.properties.type = "fa"; else this.properties.type = "mi"; } if(this.size!=null) this.properties.size = this.size; this.cfIcon = this.properties; this.currentIcon = this.cfIcon; } /** *

Updating the properties object if any of the external inputs has been changed to keep the component dynamic.

* @param changes

Changes that occured from the user.

*/ ngOnChanges(changes: any){ if(this.properties!=null) { if(changes.display!=null) this.properties.display = changes.display.currentValue; if(changes.name!=null) { this.properties.name = changes.name.currentValue; if(changes.name.startsWith("fa-")) this.properties.type = "fa"; else this.properties.type = "mi"; } if(changes.size!=null) this.properties.size = changes.size.currentValue; } } }