import { Component, OnInit, ViewChild, ElementRef, Input } from '@angular/core'; import { FormGroup } from '@angular/forms'; import { Observable } from 'rxjs'; import { Tournament, ElsewhereType, TournamentElsewhere } from 'tournee.businesslogic'; import { UtilityService } from 'src/app/shared/services/utility.service'; import { LookupsService } from 'src/app/shared/services/lookups.service'; @Component({ selector: 'app-selectable-listing-elsewhere', templateUrl: './selectable-listing-elsewhere.component.html', styleUrls: ['./selectable-listing-elsewhere.component.css'] }) export class SelectableListingElsewhereComponent implements OnInit { @ViewChild('url') url:ElementRef; @ViewChild('f') urlForm: FormGroup; public input : Observable; @Input() EditModel: Tournament; Elsewheres: Array = []; SelectedElsewhere: TournamentElsewhere = TournamentElsewhere.create(); validatingUrl: boolean = false; url_valid: boolean = false; url_rejected: boolean = false; constructor(public utility: UtilityService, private _lookups: LookupsService) { } ngOnInit() { this._lookups.get("elsewheretype") .then((result) => { this.Elsewheres = result.result.map((e) => { return new ElsewhereType(e); }); }) .catch((err) => { console.log(err); }); } add() { this.SelectedElsewhere.url = this.SelectedElsewhere.url.toLowerCase().replace('https://', '').replace('http://', ''); this.EditModel.addElsewhere(JSON.parse(JSON.stringify(this.SelectedElsewhere))); this.reset(); } edit(elsewhere: TournamentElsewhere) { this.SelectedElsewhere = JSON.parse(JSON.stringify(elsewhere)); } delete(elsewhere: TournamentElsewhere) { this.EditModel.removeElsewhere(elsewhere); } reset() { this.SelectedElsewhere = TournamentElsewhere.create(); } /*ngAfterViewInit() { this.input = fromEvent(this.url.nativeElement, 'input') .pipe( debounceTime(300), map((e: KeyboardEvent) => e.target['value']) ); this.input.subscribe((val: string) => { this.urlChanged(val); }); } validate() { this.urlChanged(this.EditModel.url); } urlChanged(url: string) { this.validatingUrl = true; this.url_valid = false; this.url_rejected = false; this.SelectedElsewhere.url = url; if (url == '') { this.validatingUrl = false; return; } this._utility.validate_url('https://' + url) .then(() => { this.validatingUrl = false; this.url_valid = true; }) .catch((err) => { this.url_valid = false; this.urlForm.controls['url'].setErrors({ rejected: true }); console.log(err); }); }*/ }