SHARE
TWEET

Untitled

a guest Dec 10th, 2019 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Component, ErrorHandler, forwardRef, OnInit, ViewChild, Input, Optional, Host, SkipSelf } from '@angular/core';
  2. import { Observable, of as ObservableOf, Subject } from 'rxjs';
  3. import { Router } from '@angular/router';
  4. import { Location } from '@angular/common';
  5. import { EquipementClass, Equipment, EquipmentType } from '../../../../common/core/model/equipment.model';
  6. import { ServerError } from '../../../../common/core/model/server-error.model';
  7. import { WizardError } from '../../../../common/core/model/wizard-error.model';
  8. import { ButtonComponent } from '../../../../common/lib/button/button.component';
  9. import { EquipmentService } from '../../../../common/core/services/equipment.service';
  10. import { catchError, debounceTime, distinctUntilChanged, filter, switchMap, tap } from 'rxjs/operators';
  11. import { AbstractControl, ControlContainer, NG_VALUE_ACCESSOR } from '@angular/forms';
  12. import { AbstractNGModelComponent } from '../../../../common/lib/abstract-ngmodel.component';
  13. import { ErrorHandlerService } from '../../../../common/core/services/error-handler.service';
  14. import { TranslateService } from '@ngx-translate/core';
  15. import { WizardShareService } from '../services/wizard-share.service';
  16. import { DemandType } from '../../../../common/core/model/demand.model';
  17. import { GenericTaskListComponent } from '../../../../common/shared/generic-task-list/generic-task-list.component';
  18.  
  19. /**
  20.  * Permet de sélectionner une equipment par auto-complétion
  21.  */
  22. @Component({
  23.   selector   : 'sip-equipment-selector',
  24.   templateUrl: './equipment-selector.component.html',
  25.   styleUrls  : ['./equipment-selector.component.scss'],
  26.   providers  : [
  27.     {provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => EquipmentSelectorComponent), multi: true}
  28.   ]
  29. })
  30. export class EquipmentSelectorComponent extends AbstractNGModelComponent<Equipment> implements OnInit {
  31.  
  32.   private $autocompleteSubject = new Subject<string>();
  33.   autocompleteData: Observable<Array<string>>;
  34.   _equipmentName: string;
  35.   error: ServerError;
  36.   isLoadingMachine = false;
  37.  
  38.   EquipementClass = EquipementClass;
  39.  
  40.   errors$: Observable<Array<WizardError>>;
  41.  
  42.   @Input()
  43.   demandType?: DemandType;
  44.  
  45.   @Input()
  46.   mandatoryEquipment: string[];
  47.  
  48.  
  49.   @Input()
  50.   wizardService: WizardShareService<any>;
  51.  
  52.   @Input()
  53.   wizardField: string;
  54.  
  55.   @Input()
  56.   equipmentClass: EquipementClass[];
  57.  
  58.   @Input()
  59.   excludeEquipmentClass: EquipementClass[];
  60.  
  61.  
  62.   @ViewChild('valideButton', {static: false}) valideButton: ButtonComponent;
  63.  
  64.   get equipmentName(): string {
  65.     return this._equipmentName;
  66.   }
  67.  
  68.   set equipmentName(equipmentName: string) {
  69.     this._equipmentName = equipmentName;
  70.     this.$autocompleteSubject.next(encodeURIComponent(equipmentName));
  71.   }
  72.  
  73.   control: AbstractControl;
  74.   @Input()
  75.   formControlName: string;
  76.  
  77.   /**
  78.    * Constructeur
  79.    * @param {EquipmentService} equipmentService
  80.    * @param {NetworkConnectionService} networkConnectionService
  81.    * @param {Router} router
  82.    * @param {Location} location
  83.    */
  84.   constructor(private equipmentService: EquipmentService,
  85.               private router: Router,
  86.               private translateService: TranslateService,
  87.               private errorHandler: ErrorHandlerService,
  88.               @Optional() @Host() @SkipSelf()
  89.               private controlContainer: ControlContainer) {
  90.     super();
  91.   }
  92.  
  93.   /**
  94.    * initialisation du composant
  95.    */
  96.   ngOnInit() {
  97.     this.autocompleteData = this.$autocompleteSubject.asObservable().pipe(
  98.       distinctUntilChanged(),
  99.       tap(() => {
  100.         if (this.wizardService) {
  101.           this.wizardService.removeErrorsByField(this.wizardField);
  102.         }
  103.       }),
  104.       debounceTime(400),
  105.       filter((filterName: string) => !!filterName),
  106.       switchMap((filterName: string) => filterName.length >= 3 ? this.equipmentService.equipmentAutocompletion(filterName, this.equipmentClass, this.excludeEquipmentClass) : ObservableOf([]))
  107.     );
  108.  
  109.     // Equipement error message
  110.     if (this.controlContainer) {
  111.       if (this.formControlName) {
  112.         this.control = this.controlContainer.control.get(this.formControlName);
  113.       } else {
  114.         console.warn('Missing FormControlName directive from host element of the component');
  115.       }
  116.     } else {
  117.       console.warn('Can\'t find parent FormGroup directive');
  118.     }
  119.  
  120.   }
  121.  
  122.   isDisplay(mandatoryEquipmentKey: string): boolean {
  123.     return this.mandatoryEquipment && this.mandatoryEquipment.includes(mandatoryEquipmentKey);
  124.   }
  125.  
  126.   /**
  127.    * Choix d'une equipment
  128.    * @param {string} equipmentName
  129.    */
  130.   public selectEquipment(equipmentName: string, equipmentClass: EquipementClass[]) {
  131.     // écrit le nom de l'equipement ds le champ en cas de selection externe de l'item
  132.     this.equipmentName = equipmentName;
  133.     this.validEquipment(equipmentName, equipmentClass);
  134.   }
  135.  
  136.   validEquipment(equipmentName: string, equipmentClass: EquipementClass[]) {
  137.     this.isLoadingMachine = true;
  138.     this.equipmentService.getEquipmentByName(encodeURIComponent(equipmentName), this.demandType).pipe(
  139.       catchError((errors: Array<ServerError>) => {
  140.         if (this.wizardService) {
  141.           this.wizardService.addErrors(errors);
  142.         }
  143.         return ObservableOf(new Equipment());
  144.       }),
  145.     ).subscribe((equipment: Equipment) => {
  146.       this.value = equipment;
  147.       this.isLoadingMachine = false;
  148.     });
  149.   }
  150.  
  151.   isCompatibleEquipment(demandType: DemandType, domain: Equipment): {label: string, resolve: string} {
  152.     return Equipment.isCompatibleEquipment(this.demandType, domain );
  153.   }
  154. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top