import { Component, Input, EventEmitter } from '@angular/core';
import { CrudComponentObj, setObject } from '../index';
import { BaseComponent } from './base.component';
import * as $ from 'jquery';
@Component({
selector: 'inputTextCrudRestful',
template: `
`
})
export class InputTextComponent extends BaseComponent {
@Input() broadcast: EventEmitter = new EventEmitter();
inputType : string;
value : string;
ngOnInit() {
this.readCommonsParameters(this.index);
let crudComponentObj = CrudComponentObj.getComponents(this.clazzName)[this.index];
this.inputType = crudComponentObj.inputType;
let name = null;
if (this.translateKey) {
name = this.translateKey;
} else {
name = this.name;
}
setTimeout(() => {
if (this.translateKey) {
name=this.translate.instant(name);
}
let width = this.width;
$("input[name='"+name+"']").each(function() {
$(this).width(width);
});
let style = this.style;
if (style != undefined) {
$("input[name='"+name+"']").each(function() {
style.split(';').forEach(value => {
let kv : any[] = value.split(':');
$(this).css(kv[0], kv[1]);
});
});
}
}, 50);
setTimeout(() => {
if (this.value) {
$('#'+this.id).prop('checked', 'true');
}
}, 50);
let id = this.id;
if (this.broadcast != undefined) {
let clazzName = this.clazzName;
let property = this.property;
this.broadcast.subscribe((value : any) => {
if (value != undefined && value[property] != undefined) {
setObject(clazzName, value);
$("#" + id).val(value[property]);
if (this.inputType == 'checkbox') {
this.value = value[property];
$('#'+this.id).prop('checked', value[property]);
}
}
});
}
}
setValue(value:string){
if (this.inputType == 'checkbox') {
CrudComponentObj.getComponents(this.clazzName)[this.index].value = $('#'+this.id).is(':checked');
} else {
if (this.mask) {
CrudComponentObj.getComponents(this.clazzName)[this.index].value = this.value;
} else {
CrudComponentObj.getComponents(this.clazzName)[this.index].value = value;
}
}
}
setFocus(clazzName,name) {
setTimeout(() => {
$('#'+clazzName+'_'+name).focus();
$('#'+clazzName+'_'+name).select();
}, 50);
}
}