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

Chexkbox is creating using SelectableModel Class/Component based on Angular Material Checkbox and has abbility to customize md-checkbox's template.

. *

Example of using Checkbox:

*
 * <cf-checkbox [settings]="myFruits" (cfOnChange)="log($event)"></cf-checkbox>
 * 
*/ @Component({ selector: 'cf-checkbox', template: "
", styles: [":host { position: relative; z-index: 0; display: inline-flex; } :host.notification-clickable cf-notification:hover { cursor: pointer; } .cf-checkbox-group-container { display: inline-flex; flex-direction: column; } .cf-checkbox-group-container.before { text-align: right; } .cf-checkbox-group-container .mat-checkbox-disabled /deep/ * { color: #b0b0b0; } "], providers: [{provide: CfSelectableComponent, useExisting: forwardRef(() => CfCheckboxComponent)}] }) export class CfCheckboxComponent extends CfSelectableComponent implements OnInit { /** * Component property for rendering template for each checkbox if that template was defined in html. */ @ContentChild(TemplateRef) cfCheckboxTemplate; cfCheckbox: SelectableModel; @Input() public styling: CheckboxStylingModel; 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 = CheckboxTemplates["defaultTemplate"]; if(this.compTemplate!=null && this.compTemplate!="") { if(CheckboxTemplates[this.compTemplate]!=null) { this.activeTemplate = CheckboxTemplates[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 CheckboxStylingModel (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.selected; if(this.itemPosition!=null) this.properties.itemPosition = this.itemPosition; this.checked = this.properties.checked; this.cfCheckbox = this.properties; } ngOnChanges(changes: any){ if(this.properties!=null) { if(changes.disable!=null) this.properties.disable = changes.disable.currentValue; if(changes.display!=null) this.properties.display = changes.display.currentValue; if(changes.value!=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; } } }