ronaldkwandy

Untitled

Oct 24th, 2018
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Component, OnDestroy, ViewEncapsulation, forwardRef, Input, Output, EventEmitter } from '@angular/core';
  2. import { NG_VALUE_ACCESSOR, ControlValueAccessor } from '@angular/forms';
  3.  
  4. const RADIOBUTTON_CONTROL_VALUE_ACCESSOR: any = {
  5.   provide: NG_VALUE_ACCESSOR,
  6.   useExisting: forwardRef(() => RadioButtonComponent),
  7.   multi: true
  8. };
  9.  
  10. @Component({
  11.   selector: 'app-radio-button',
  12.   templateUrl: 'radio-button.component.html',
  13.   encapsulation: ViewEncapsulation.None,
  14.   providers: [RADIOBUTTON_CONTROL_VALUE_ACCESSOR]
  15. })
  16. export class RadioButtonComponent implements ControlValueAccessor {
  17.  
  18.   @Input() public id: string;
  19.   @Input() public name: string;
  20.   @Input() public classStyle: string = '';
  21.   @Input() public required: boolean = false;
  22.   @Input() public disabled: boolean = false;
  23.   @Input() public key: string = 'value';
  24.  
  25.   private _items: any[] = [];
  26.   @Input()
  27.   get items() { return this._items; }
  28.   set items(items: any[]) { this._items = items; this.setOptionList(); }
  29.  
  30.   @Output() public onValueChange: EventEmitter<any>;
  31.  
  32.   public optionList: any[] = [];
  33.   /** value emited to outside */
  34.   public value: any;
  35.   /** value binded into component */
  36.   public selectedValue: any;
  37.   public firstIndex: number = 0;
  38.  
  39.   private noKeyDefinedMessage = 'no-key-defined';
  40.  
  41.   public propagateChange = (_: any) => { /* do nothing */ };
  42.   public propagateTouch = (_: any) => { /* do nothing */ };
  43.  
  44.   constructor() {
  45.     this.onValueChange = new EventEmitter<any>();
  46.   }
  47.  
  48.   public writeValue(value: any): void {
  49.     console.log('value : ', value);
  50.     if (value === null) {
  51.       this.value = null;
  52.       this.selectedValue = null;
  53.     } else {
  54.       const itemIndex = this.optionList.findIndex(item => {
  55.         if (this.getItemByKey(item) !== this.noKeyDefinedMessage) {
  56.           return this.getItemByKey(item) === value;
  57.         } else {
  58.           return false;
  59.         }
  60.       });
  61.  
  62.       if (itemIndex !== -1) {
  63.         this.value = value;
  64.         this.selectedValue = this.optionList[itemIndex];
  65.       } else {
  66.         console.error('no item with provided key');
  67.       }
  68.     }
  69.   }
  70.  
  71.   public registerOnChange(fn: (_: any) => {}): void {
  72.     this.propagateChange = fn;
  73.   }
  74.  
  75.   public registerOnTouched(fn: (_: any) => {}): void {
  76.     this.propagateTouch = fn;
  77.   }
  78.  
  79.   public setDisabledState(isDisabled: boolean): void {
  80.     this.disabled = isDisabled;
  81.   }
  82.  
  83.   private setOptionList(): void {
  84.     this.optionList = Object.assign([], this._items);
  85.   }
  86.  
  87.   public _onValueChange(event: any): void {
  88.     console.log('getItemByKey : ', this.getItemByKey(this.selectedValue));
  89.     if (this.getItemByKey(this.selectedValue) !== this.noKeyDefinedMessage) {
  90.       if (this.getItemByKey(this.selectedValue) !== this.getItemByKey(event)) {
  91.         this.selectedValue = event;
  92.         this.value = this.getItemByKey(event);
  93.         this.propagateChange(this.value);
  94.         this.onValueChange.emit(this.value);
  95.       }
  96.     } else {
  97.       console.error('no item with provided key');
  98.     }
  99.   }
  100.  
  101.   public change(event: any) {
  102.     console.log('event : ', event);
  103.     console.log(this.selectedValue);
  104.   }
  105.  
  106.   public getItemByKey(item: any): string {
  107.     if (item) {
  108.       if (this.key) {
  109.         return item[this.key];
  110.       } else {
  111.         let localKey = null;
  112.         for (const objKey in item) {
  113.           if (objKey !== 'id' && objKey !== 'version' && objKey !== 'rootId' && objKey !== 'rootVersion') {
  114.             localKey = objKey;
  115.             break;
  116.           }
  117.         }
  118.  
  119.         if (localKey) {
  120.           return item[localKey];
  121.         }
  122.         return this.noKeyDefinedMessage;
  123.       }
  124.     } else {
  125.       return this.noKeyDefinedMessage;
  126.     }
  127.   }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment