import { Component, OnInit, Input, Output, ElementRef, EventEmitter, ContentChildren, QueryList, forwardRef } from '@angular/core';
import { CfCoreComponent } from '../core/core.component';
import { CfSelectableComponent } from '../selectable/selectable.component';
import { CfRadioComponent } from '../radio/radio.component';
import { GroupModel } from '../../models/group/group.model';
import { ControlValueAccessor, NG_VALUE_ACCESSOR, NG_VALIDATORS, FormControl } from '@angular/forms';
// export function validateCfGroup(c: FormControl) {
// let err = {
// rangeError: {
// given: c.value
// }
// };
// return (c.itemsSelected == null)? err: null;
// }
@Component({
selector: 'cf-group',
template: "
{{label}}
",
styles: [""],
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => CfGroupComponent),
multi: true
}
]
})
export class CfGroupComponent extends CfCoreComponent implements OnInit {
writeValue(obj: any): void {
if (obj) {
this.itemsSelected = obj;
}
}
registerOnChange(fn: any): void {
this.onChange = (value) => { Promise.resolve(null).then(() => fn(value)) };
}
registerOnTouched(fn: any): void {
this.onTouched = (value) => { Promise.resolve(null).then(() => fn(value)) };
}
setDisabledState(isDisabled: boolean): void {
}
onChange: any = () => { };
onTouched: any = () => { };
cfGroup: GroupModel;
itemsSelected: number[] = [];
@Input()
public properties: GroupModel;
@Input() label: string;
@Input() minSelected: number;
@Input() maxSelected: number;
@Input() allSelected: boolean;
@Output() onSelect = new EventEmitter();
randomId: number = 0;
constructor(public elementRef: ElementRef) {
super(elementRef);
}
ngOnInit() {
if(this.properties==null)
this.properties = new GroupModel();
if(this.minSelected!=null) this.properties.minSelected = this.minSelected;
if(this.maxSelected!=null) this.properties.maxSelected = this.maxSelected;
if(this.allSelected!=null) this.properties.allSelected = this.allSelected;
this.cfGroup = this.properties;
}
ngOnChanges(changes: any){
if(this.properties!=null)
{
if(changes.minSelected!=null) this.properties.minSelected = changes.minSelected.currentValue;
if(changes.maxSelected!=null) this.properties.maxSelected = changes.maxSelected.currentValue;
if(changes.allSelected!=null) this.properties.allSelected = changes.allSelected.currentValue;
}
}
@ContentChildren(CfSelectableComponent) selectables: QueryList;
@ContentChildren(CfRadioComponent) radios: QueryList;
ngAfterContentInit() {
if(this.selectables!=null && this.selectables.length>0)
{
for(let selectable of this.selectables.toArray())
{
selectable.internalId = this.randomId;
if(selectable.checked==true)
{
this.itemsSelected.push(this.randomId);
}
this.randomId++;
selectable.clicked.subscribe(data=> this.updateValues(data));
}
if(this.properties.maxSelected==null)
if(this.cfGroup.allSelected)
this.properties.maxSelected = this.selectables.length;
else
this.properties.maxSelected = this.properties.minSelected;
}
else if(this.radios!=null && this.radios.length>0)
{
for(let radio1 of this.radios.toArray())
{
radio1.internalId = this.randomId;
if(radio1.checked==true)
{
this.itemsSelected.push(this.randomId);
}
this.randomId++;
radio1.clicked.subscribe(data=> this.updateValues(data));
}
this.properties.maxSelected = 1;
this.properties.minSelected = 1;
}
}
updateValues(data: any){
if(this.selectables!=null && this.selectables.length>0)
{
if(this.itemsSelected.indexOf(data["id"])>=0)
{
if(this.cfGroup.minSelected==this.itemsSelected.length)
this.selectables.toArray()[data["id"]].updateFromGroup(false);
else
{
this.selectables.toArray()[data["id"]].updateFromGroup(true);
this.itemsSelected.splice(this.itemsSelected.indexOf(data["id"]),1);
this.onSelect.emit(this.itemsSelected);
this.onChange(this.itemsSelected);
this.onTouched();
}
}
else
{
if(this.itemsSelected.length>=this.cfGroup.maxSelected)
{
this.selectables.toArray()[this.itemsSelected[0]].updateFromGroup(false);
this.itemsSelected.splice(0,1);
this.itemsSelected.push(data["id"]);
this.itemsSelected.sort();
this.onSelect.emit(this.itemsSelected);
this.onChange(this.itemsSelected);
this.onTouched();
}
else
{
this.itemsSelected.push(data["id"]);
this.itemsSelected.sort();
this.onSelect.emit(this.itemsSelected);
this.onChange(this.itemsSelected);
this.onTouched();
}
}
}
else if(this.radios!=null && this.radios.length>0)
{
if(this.itemsSelected.length>=this.cfGroup.maxSelected) // I don't exist and max is full uncheck first and check me
{
this.radios.toArray()[this.itemsSelected[0]].updateFromGroup(false);
this.itemsSelected.splice(0,1);
this.itemsSelected.push(data["id"]);
this.itemsSelected.sort();
this.onSelect.emit(this.itemsSelected);
this.onChange(this.itemsSelected);
this.onTouched();
}
else // I don't exist and max is not full, check me
{
this.itemsSelected.push(data["id"]);
this.itemsSelected.sort();
this.onSelect.emit(this.itemsSelected);
this.onChange(this.itemsSelected);
this.onTouched();
}
}
}
}