import { Component, OnInit, Input, Output, EventEmitter, ElementRef, ContentChild, TemplateRef, HostBinding,forwardRef } from '@angular/core'; import { CfSelectableComponent } from '../selectable/selectable.component'; import { SelectableModel } from '../../models/selectable/selectable.model'; import { SwitchStylingModel } from '../../models/selectable/switch-styling.model'; import { SwitchTemplates } from '../../templates/switch.template'; /** *

Switch is creating using SelectableModel Class/Component based on Angular Material Slide toggle (Switch) and has abbility to customize md-slide-toggle's template.

. *

Example of using Switch:

*
 * <cf-switch [settings]="myFruits" (cfOnChange)="log($event)"></cf-switch>
 * 
*/ @Component({ selector: 'cf-switch', template: "
", styles: [":host { position: relative; z-index: 0; display: inline-flex; } :host.notification-clickable cf-notification:hover { cursor: pointer; } .cf-switch-group-container { display: inline-flex; flex-direction: column; } :host /deep/ md-slide-toggle { height: auto; margin: 2px 0; } :host /deep/ md-slide-toggle .mat-slide-toggle-label { align-items: center; } :host /deep/ md-slide-toggle .mat-slide-toggle-label .mat-disabled * { color: #b0b0b0; } :host /deep/ md-slide-toggle.mat-slide-toggle-label-before .mat-slide-toggle-label { justify-content: flex-end; } "], providers: [{provide: CfSelectableComponent, useExisting: forwardRef(() => CfSwitchComponent)}] }) export class CfSwitchComponent extends CfSelectableComponent implements OnInit { @HostBinding('class') notifacationClickable = ''; /** * Component property for rendering template for each switch if that template was defined in html. */ @ContentChild(TemplateRef) cfSwitchTemplate; cfSwitch: SelectableModel; /** *

The styling property of the checkbox.

*/ @Input() public styling: SwitchStylingModel; constructor(public elementRef: ElementRef) { super(elementRef); } /** * It will be generated cfCheckbox object and rendered inside component template. Also cfCheckStates function will initialize checkboxes states. */ ngOnInit() { this.activeTemplate = SwitchTemplates["defaultTemplate"]; if(this.compTemplate!=null && this.compTemplate!="") { if(SwitchTemplates[this.compTemplate]!=null) { this.activeTemplate = SwitchTemplates[this.compTemplate]; } else this.activeTemplate = this.compTemplate; } if(this.properties==null) { this.properties = new SelectableModel(this.activeTemplate["property"]); } else { var mainKeys = Object.keys(this.activeTemplate["property"]); for(let key of mainKeys) { if(this.properties[key]==null) this.properties[key]=this.activeTemplate["property"][key]; } } if(this.styling==null) this.styling = new SwitchStylingModel (this.activeTemplate["style"]); if(this.disable!=null) this.properties.disable = this.disable; if(this.display!=null) this.properties.display = this.display; if(this.value!=null) this.properties.value = this.value; if(this.item!=null) this.properties.item = this.item; if(this.checked!=null) this.properties.checked = this.checked; if(this.itemPosition!=null) this.properties.itemPosition = this.itemPosition; this.checked = this.properties.checked; this.cfSwitch = this.properties; } ngOnChanges(changes: any){ if(changes.disable!=null) this.properties.disable = changes.disable.currentValue; if(changes.display!=null) this.properties.display = changes.display.currentValue; if(changes.checkedValue!=null) this.properties.value = changes.value.currentValue; if(changes.item!=null) this.properties.item = changes.item.currentValue; if(changes.itemPosition!=null) this.properties.itemPosition = changes.itemPosition.currentValue; } }