code improvements
This commit is contained in:
		@@ -1,18 +1,5 @@
 | 
			
		||||
import { TestBed } from '@angular/core/testing';
 | 
			
		||||
 | 
			
		||||
import { ArrayInputHelperService } from './array-input-helper.service';
 | 
			
		||||
 | 
			
		||||
// TOdo
 | 
			
		||||
 | 
			
		||||
describe('ArrayInputHelperService', () => {
 | 
			
		||||
  let service: ArrayInputHelperService;
 | 
			
		||||
 | 
			
		||||
  beforeEach(() => {
 | 
			
		||||
    TestBed.configureTestingModule({});
 | 
			
		||||
    service = TestBed.inject(ArrayInputHelperService);
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
  it('should be created', () => {
 | 
			
		||||
    expect(service).toBeTruthy();
 | 
			
		||||
  });
 | 
			
		||||
});
 | 
			
		||||
 
 | 
			
		||||
@@ -10,7 +10,7 @@ export class ArrayInputHelperService {
 | 
			
		||||
 | 
			
		||||
  constructor() { }
 | 
			
		||||
 | 
			
		||||
  values(id: string) {
 | 
			
		||||
  values(id: string) {  // observable which returns new values as they come for subscribed id
 | 
			
		||||
    return new Observable<{index: number, value: any}>(observer => {
 | 
			
		||||
      this.com.subscribe(data => {
 | 
			
		||||
        if (data.id === id) {
 | 
			
		||||
@@ -20,7 +20,7 @@ export class ArrayInputHelperService {
 | 
			
		||||
    });
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  newValue(id: string, index: number, value: any) {
 | 
			
		||||
  newValue(id: string, index: number, value: any) {  // set new value
 | 
			
		||||
    this.com.next({id, index, value});
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,26 +1,5 @@
 | 
			
		||||
// import { async, ComponentFixture, TestBed } from '@angular/core/testing';
 | 
			
		||||
//
 | 
			
		||||
// import { RbArrayInputComponent } from './rb-array-input.component';
 | 
			
		||||
//
 | 
			
		||||
// describe('RbArrayInputComponent', () => {
 | 
			
		||||
//   let component: RbArrayInputComponent;
 | 
			
		||||
//   let fixture: ComponentFixture<RbArrayInputComponent>;
 | 
			
		||||
//
 | 
			
		||||
//   beforeEach(async(() => {
 | 
			
		||||
//     TestBed.configureTestingModule({
 | 
			
		||||
//       declarations: [ RbArrayInputComponent ]
 | 
			
		||||
//     })
 | 
			
		||||
//     .compileComponents();
 | 
			
		||||
//   }));
 | 
			
		||||
//
 | 
			
		||||
//   beforeEach(() => {
 | 
			
		||||
//     fixture = TestBed.createComponent(RbArrayInputComponent);
 | 
			
		||||
//     component = fixture.componentInstance;
 | 
			
		||||
//     component.ngOnInit();
 | 
			
		||||
//     fixture.detectChanges();
 | 
			
		||||
//   });
 | 
			
		||||
//
 | 
			
		||||
//   it('should create', () => {
 | 
			
		||||
//     expect(component).toBeTruthy();
 | 
			
		||||
//   });
 | 
			
		||||
// });
 | 
			
		||||
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
 | 
			
		||||
 | 
			
		||||
describe('RbArrayInputComponent', () => {
 | 
			
		||||
 | 
			
		||||
});
 | 
			
		||||
 
 | 
			
		||||
@@ -37,7 +37,7 @@ export class RbArrayInputListenerDirective {
 | 
			
		||||
  ) { }
 | 
			
		||||
 | 
			
		||||
  @HostListener('ngModelChange', ['$event'])
 | 
			
		||||
  onChange(event) {
 | 
			
		||||
  onChange(event) {  // emit new value
 | 
			
		||||
    this.helperService.newValue(this.rbArrayInputListener, this.index, event);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
@@ -52,7 +52,7 @@ export class RbArrayInputListenerDirective {
 | 
			
		||||
})
 | 
			
		||||
export class RbArrayInputComponent implements ControlValueAccessor, OnInit, AfterViewInit {
 | 
			
		||||
 | 
			
		||||
  pushTemplate: any = '';
 | 
			
		||||
  pushTemplate: any = '';  // array element template
 | 
			
		||||
  @Input('pushTemplate') set _pushTemplate(value) {
 | 
			
		||||
    this.pushTemplate = value;
 | 
			
		||||
    if (this.values.length) {
 | 
			
		||||
@@ -98,7 +98,8 @@ export class RbArrayInputComponent implements ControlValueAccessor, OnInit, Afte
 | 
			
		||||
    if (this.pushTemplate !== null) {
 | 
			
		||||
      if (this.pushPath) {
 | 
			
		||||
        // remove last element if last two are empty
 | 
			
		||||
        if (this.values[this.values.length - 1][this.pushPath] === '' && this.values[this.values.length - 2][this.pushPath] === '') {
 | 
			
		||||
        if (this.values[this.values.length - 1][this.pushPath] === '' &&
 | 
			
		||||
          this.values[this.values.length - 2][this.pushPath] === '') {
 | 
			
		||||
          this.values.pop();
 | 
			
		||||
        }
 | 
			
		||||
        // add element if last all are filled
 | 
			
		||||
@@ -131,6 +132,7 @@ export class RbArrayInputComponent implements ControlValueAccessor, OnInit, Afte
 | 
			
		||||
  writeValue(obj: any) {  // add empty value on init
 | 
			
		||||
    if (obj) {
 | 
			
		||||
      if (this.pushTemplate !== null) {
 | 
			
		||||
        // filter out empty values
 | 
			
		||||
        if (this.pushPath) {
 | 
			
		||||
          this.values = [...obj.filter(e => e[this.pushPath] !== ''), cloneDeep(this.pushTemplate)];
 | 
			
		||||
        }
 | 
			
		||||
@@ -150,22 +152,6 @@ export class RbArrayInputComponent implements ControlValueAccessor, OnInit, Afte
 | 
			
		||||
        this.values = [''];
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
    // this.values = obj ? obj : [];
 | 
			
		||||
    // console.log('-----');
 | 
			
		||||
    // console.log(obj);
 | 
			
		||||
    // console.log(this.pushPath);
 | 
			
		||||
    // if (this.values && this.values.length) {
 | 
			
		||||
    //   this.values = obj.filter(e => this.pushPath ? e[this.pushPath] !== '' : e !== '');
 | 
			
		||||
    // }
 | 
			
		||||
    // console.log(this.values);
 | 
			
		||||
    // // console.log(obj.filter(e => this.pushPath ? e[this.pushPath] !== '' : e !== ''));
 | 
			
		||||
    // // this.values = obj ? obj.filter(e => this.pushPath ? e[this.pushPath] !== '' : e !== '') : [];
 | 
			
		||||
    // if (this.values.length === 0 || this.values[0] !== '') {
 | 
			
		||||
    //   // add empty last field if pushTemplate is specified
 | 
			
		||||
    //   if (this.pushTemplate !== null) {
 | 
			
		||||
    //     this.values.push(cloneDeep(this.pushTemplate));
 | 
			
		||||
    //   }
 | 
			
		||||
    // }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  registerOnChange(fn: any) {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user