Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Component, ErrorHandler, forwardRef, OnInit, ViewChild, Input, Optional, Host, SkipSelf } from '@angular/core';
- import { Observable, of as ObservableOf, Subject } from 'rxjs';
- import { Router } from '@angular/router';
- import { Location } from '@angular/common';
- import { EquipementClass, Equipment, EquipmentType } from '../../../../common/core/model/equipment.model';
- import { ServerError } from '../../../../common/core/model/server-error.model';
- import { WizardError } from '../../../../common/core/model/wizard-error.model';
- import { ButtonComponent } from '../../../../common/lib/button/button.component';
- import { EquipmentService } from '../../../../common/core/services/equipment.service';
- import { catchError, debounceTime, distinctUntilChanged, filter, switchMap, tap } from 'rxjs/operators';
- import { AbstractControl, ControlContainer, NG_VALUE_ACCESSOR } from '@angular/forms';
- import { AbstractNGModelComponent } from '../../../../common/lib/abstract-ngmodel.component';
- import { ErrorHandlerService } from '../../../../common/core/services/error-handler.service';
- import { TranslateService } from '@ngx-translate/core';
- import { WizardShareService } from '../services/wizard-share.service';
- import { DemandType } from '../../../../common/core/model/demand.model';
- import { GenericTaskListComponent } from '../../../../common/shared/generic-task-list/generic-task-list.component';
- /**
- * Permet de sélectionner une equipment par auto-complétion
- */
- @Component({
- selector : 'sip-equipment-selector',
- templateUrl: './equipment-selector.component.html',
- styleUrls : ['./equipment-selector.component.scss'],
- providers : [
- {provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => EquipmentSelectorComponent), multi: true}
- ]
- })
- export class EquipmentSelectorComponent extends AbstractNGModelComponent<Equipment> implements OnInit {
- private $autocompleteSubject = new Subject<string>();
- autocompleteData: Observable<Array<string>>;
- _equipmentName: string;
- error: ServerError;
- isLoadingMachine = false;
- EquipementClass = EquipementClass;
- errors$: Observable<Array<WizardError>>;
- @Input()
- demandType?: DemandType;
- @Input()
- mandatoryEquipment: string[];
- @Input()
- wizardService: WizardShareService<any>;
- @Input()
- wizardField: string;
- @Input()
- equipmentClass: EquipementClass[];
- @Input()
- excludeEquipmentClass: EquipementClass[];
- @ViewChild('valideButton', {static: false}) valideButton: ButtonComponent;
- get equipmentName(): string {
- return this._equipmentName;
- }
- set equipmentName(equipmentName: string) {
- this._equipmentName = equipmentName;
- this.$autocompleteSubject.next(encodeURIComponent(equipmentName));
- }
- control: AbstractControl;
- @Input()
- formControlName: string;
- /**
- * Constructeur
- * @param {EquipmentService} equipmentService
- * @param {NetworkConnectionService} networkConnectionService
- * @param {Router} router
- * @param {Location} location
- */
- constructor(private equipmentService: EquipmentService,
- private router: Router,
- private translateService: TranslateService,
- private errorHandler: ErrorHandlerService,
- @Optional() @Host() @SkipSelf()
- private controlContainer: ControlContainer) {
- super();
- }
- /**
- * initialisation du composant
- */
- ngOnInit() {
- this.autocompleteData = this.$autocompleteSubject.asObservable().pipe(
- distinctUntilChanged(),
- tap(() => {
- if (this.wizardService) {
- this.wizardService.removeErrorsByField(this.wizardField);
- }
- }),
- debounceTime(400),
- filter((filterName: string) => !!filterName),
- switchMap((filterName: string) => filterName.length >= 3 ? this.equipmentService.equipmentAutocompletion(filterName, this.equipmentClass, this.excludeEquipmentClass) : ObservableOf([]))
- );
- // Equipement error message
- if (this.controlContainer) {
- if (this.formControlName) {
- this.control = this.controlContainer.control.get(this.formControlName);
- } else {
- console.warn('Missing FormControlName directive from host element of the component');
- }
- } else {
- console.warn('Can\'t find parent FormGroup directive');
- }
- }
- isDisplay(mandatoryEquipmentKey: string): boolean {
- return this.mandatoryEquipment && this.mandatoryEquipment.includes(mandatoryEquipmentKey);
- }
- /**
- * Choix d'une equipment
- * @param {string} equipmentName
- */
- public selectEquipment(equipmentName: string, equipmentClass: EquipementClass[]) {
- // écrit le nom de l'equipement ds le champ en cas de selection externe de l'item
- this.equipmentName = equipmentName;
- this.validEquipment(equipmentName, equipmentClass);
- }
- validEquipment(equipmentName: string, equipmentClass: EquipementClass[]) {
- this.isLoadingMachine = true;
- this.equipmentService.getEquipmentByName(encodeURIComponent(equipmentName), this.demandType).pipe(
- catchError((errors: Array<ServerError>) => {
- if (this.wizardService) {
- this.wizardService.addErrors(errors);
- }
- return ObservableOf(new Equipment());
- }),
- ).subscribe((equipment: Equipment) => {
- this.value = equipment;
- this.isLoadingMachine = false;
- });
- }
- isCompatibleEquipment(demandType: DemandType, domain: Equipment): {label: string, resolve: string} {
- return Equipment.isCompatibleEquipment(this.demandType, domain );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement