bia-framework
Version:
Bia Framework to work with angular 2
122 lines (96 loc) • 3.19 kB
text/typescript
import {Component, ViewEncapsulation, Input, EventEmitter, Output, Optional, Host, ViewChildren, QueryList, OnInit} from "@angular/core";
import {NgFor} from "@angular/common";
import {RippleEffect} from "../ripple-effect/RippleEffect";
import {CoreControlFormBase} from "../odisseia-form/CoreControlFormBase";
import {CoreForm} from "../odisseia-form/odisseia-form";
import {ObjectOrArrayPipe} from "../../pipes/objectOrArrayPipe";
import {CoreIcon} from "../core-icon/core-icon";
/**
* Created by jd on 03/05/2016.
*/
export class CoreRadioButton extends CoreControlFormBase implements OnInit
{
_bind:any;
set bind(value:any)
{
this._bind = value;
this.bindChange.emit(value);
this.selectedIndex = this.getIndexFromBind();
}
get bind()
{
return this._bind;
}
bindChange = new EventEmitter<Object>();
source:Array<any>;
selectedValuePath:string;
showValuePath:string;
color:string;
selectedIndex:number;
rippleEffect:QueryList<RippleEffect>;
constructor( private hostForm?:CoreForm)
{
super(hostForm);
}
ngOnInit():any
{
this.selectedIndex = this.getIndexFromBind();
return undefined;
}
public onSelect = (item:any, index:number):void =>
{
var newValue = null;
if (this.selectedValuePath)
newValue = this.source[index][this.selectedValuePath];
else
newValue = this.source[index];
this.rippleEffect.toArray()[index].showEffect(null);
//FormBase
if (this.isInForm() && this.control != null)
this.control.updateValue(newValue);
};
private getIndexFromBind = ()=>
{
if (this.bind != null && this.source != null)
{
if (this.selectedValuePath != null)
{
var i = null;
this.source.forEach((item, index) =>
{
if (item[this.selectedValuePath] != null && item[this.selectedValuePath] == this.bind)
i = index;
});
return i;
}
else
{
var e = null;
this.source.forEach((item, index) =>
{
if (item == this.bind)
e = index;
});
return e;
}
}
return null;
};
}