File

projects/k-components/src/lib/components/radiobutton/radiobutton.component.ts

Metadata

changeDetection ChangeDetectionStrategy.OnPush
selector wm-radio-button
styles .inner-radio-button-container { width: inherit; height: inherit; background-color: inherit; }
template
<div class="radio-button-container inner-radio-button-container">    <label style="margin: 0px">
      <input
        #theRadio
        type="radio"
        [name]="name"
        [checked]="itemCheck"
        (change)="onChange($event)"
      />
      <ng-container *ngIf="!innerContent; else elseBlock">
        <ng-content></ng-content>
      </ng-container>
      <ng-content></ng-content>
      <ng-template #elseBlock>
        <ng-container
          *ngComponentOutlet="
            innerContent.component;
            injector: innerContent.customInjector
          "
        ></ng-container>
      </ng-template>
    </label>
  </div>

Index

Properties
Methods
Inputs
Outputs
Accessors

Constructor

Public constructor(injectedModel: RadioButtonModel, selfContext: DataContextService, injector: Injector, radioService: RadioNameService)
Parameters :
Name Type Optional
injectedModel RadioButtonModel No
selfContext DataContextService No
injector Injector No
radioService RadioNameService No

Inputs

itemCheck
Type : boolean
itemCheckConverter
Type : any
itemCheckConverterParameter
Type : any
model
Type : RadioButtonModel
name
Type : string
Default value : ''

Outputs

checked
Type : EventEmitter<any>
itemCheckChange
Type : EventEmitter<any>
unchecked
Type : EventEmitter<any>

Methods

fireCheckedEvent
fireCheckedEvent()
Returns : void
fireUncheckedEvent
fireUncheckedEvent()
Returns : void
ngOnDestroy
ngOnDestroy()
Returns : void
ngOnInit
ngOnInit()
Returns : void
Private notifyOfChange
notifyOfChange(value: boolean)
Parameters :
Name Type Optional
value boolean No
Returns : void
onChange
onChange($event)
Parameters :
Name Optional
$event No
Returns : void

Properties

checked
Type : EventEmitter<any>
Default value : new EventEmitter()
Decorators :
@Output()
itemCheckChange
Type : EventEmitter<any>
Default value : new EventEmitter()
Decorators :
@Output()
itemCheckConverter
Type : any
Decorators :
@Input()
itemCheckConverterParameter
Type : any
Decorators :
@Input()
model
Type : RadioButtonModel
Decorators :
@Input()
name
Type : string
Default value : ''
Decorators :
@Input()
Private pendingItemCheckInit
Type : boolean
Default value : undefined
Private synching
Default value : false
syncSubscription
Type : Subscription
theRadio
Type : ElementRef<HTMLInputElement>
Decorators :
@ViewChild('theRadio', {static: true})
unchecked
Type : EventEmitter<any>
Default value : new EventEmitter()
Decorators :
@Output()

Accessors

itemCheck
getitemCheck()
setitemCheck(value: boolean)
Parameters :
Name Type Optional
value boolean No
Returns : void
innerContent
getinnerContent()
import {
  Component,
  ViewChild,
  ElementRef,
  Input,
  Output,
  EventEmitter,
  Optional,
  Injector,
} from '@angular/core';
import { Subscription } from 'rxjs';
import { DataContextService } from '../../services/datacontextprovider.service';
import { RadioNameService } from '../../services/radiobuttonsnames.service';
import {
  RadioButtonModel,
  AngularComponentId,
  ComponentId,
  TypeResolver,
} from '@mobilize/wms-framework';

@Component({
  selector: 'wm-radio-button',
  template: `<div class="radio-button-container inner-radio-button-container">
    <label style="margin: 0px">
      <input
        #theRadio
        type="radio"
        [name]="name"
        [checked]="itemCheck"
        (change)="onChange($event)"
      />
      <ng-container *ngIf="!innerContent; else elseBlock">
        <ng-content></ng-content>
      </ng-container>
      <ng-content></ng-content>
      <ng-template #elseBlock>
        <ng-container
          *ngComponentOutlet="
            innerContent.component;
            injector: innerContent.customInjector
          "
        ></ng-container>
      </ng-template>
    </label>
  </div>`,
  styles: [
    `
      .inner-radio-button-container {
        width: inherit;
        height: inherit;
        background-color: inherit;
      }
    `,
  ],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
@ComponentId([AngularComponentId.radioButton])
export class RadioButtonComponent {
  private pendingItemCheckInit: boolean = undefined;

  @ViewChild('theRadio', { static: true })
  theRadio: ElementRef<HTMLInputElement>;

  @Input()
  model: RadioButtonModel;

  @Input()
  name = '';

  @Input()
  get itemCheck(): boolean {
    return this.model?.IsChecked === true;
  }
  set itemCheck(value: boolean) {
    // if (this.itemChangeConverter) {
    //     value =  this.itemChangeConverter.transform(value);
    //  }
    if (this.model && this.model.IsChecked !== value) {
      this.synching = true;
      this.model.IsChecked = value;
      this.synching = false;
      this.notifyOfChange(value);
      if (value) {
        this.fireCheckedEvent();
      } else {
        this.fireUncheckedEvent();
      }
    } else {
      this.pendingItemCheckInit = value;
    }
  }

  get innerContent(): any {
    if (this.model?.Content) {
      const customInjector = Injector.create({
        providers: [
          {
            provide: this.model?.Content.constructor,
            useValue: this.model.Content,
            deps: [],
          },
        ],
        parent: this.injector,
      });
      return {
        component: TypeResolver.getType(this.model?.Content.AngularComponentId),
        customInjector,
      };
    } else {
      return undefined;
    }
  }

  @Output()
  itemCheckChange: EventEmitter<any> = new EventEmitter();

  @Output()
  checked: EventEmitter<any> = new EventEmitter();

  @Output()
  unchecked: EventEmitter<any> = new EventEmitter();

  @Input()
  itemCheckConverter: any;

  @Input()
  itemCheckConverterParameter: any;

  private synching = false;

  private notifyOfChange(value: boolean) {
    if (this.itemCheckConverter) {
      value = this.itemCheckConverter.ConvertBack(value);
    }
    this.itemCheckChange.emit(value);
    setTimeout(() => {
      this.radioService.sync.emit(null);
    });
  }

  onChange($event) {
    this.itemCheck = this.theRadio?.nativeElement?.checked;
  }

  syncSubscription: Subscription;

  public constructor(
    @Optional() protected injectedModel: RadioButtonModel = null,
    @Optional() private selfContext: DataContextService,
    private injector: Injector,
    private radioService: RadioNameService
  ) {
    this.model = injectedModel;
    this.name = radioService.getNewName();
    this.syncSubscription = radioService.sync.subscribe(() => {
      if (
        this.itemCheck === true &&
        this.theRadio?.nativeElement?.checked === false
      ) {
        this.itemCheck = false;
      }
    });
  }

  ngOnInit() {
    this.model = this.model || this.injectedModel || new RadioButtonModel();
    this.model.DataContext = this.selfContext?.dataContext;
    this.model.change.addHandler((propertyName: string) => {
      if (propertyName === 'IsChecked' && !this.synching) {
        this.notifyOfChange(this.model.IsChecked);
      }
    });
    if (this.pendingItemCheckInit === true) {
      this.model.IsChecked = this.pendingItemCheckInit;
      this.pendingItemCheckInit = undefined;
    }
  }
  fireCheckedEvent() {
    this.checked.emit(this.model);
    this.model.Checked.fire([this]);
  }
  fireUncheckedEvent() {
    this.unchecked.emit(this.model);
  }
  ngOnDestroy() {
    // this is very important to avoid leaking sync information
    this.syncSubscription.unsubscribe();
  }
}

      .inner-radio-button-container {
        width: inherit;
        height: inherit;
        background-color: inherit;
      }
    
Legend
Html element
Component
Html element with directive

result-matching ""

    No results matching ""