2020-04-14 10:35:20 +02:00
|
|
|
import { Component, OnInit, ChangeDetectionStrategy, EventEmitter, Output } from '@angular/core';
|
2020-02-16 16:15:07 +01:00
|
|
|
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
|
|
|
|
import { Router } from '@angular/router';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-search-form',
|
|
|
|
templateUrl: './search-form.component.html',
|
|
|
|
styleUrls: ['./search-form.component.scss'],
|
|
|
|
changeDetection: ChangeDetectionStrategy.OnPush
|
|
|
|
})
|
|
|
|
export class SearchFormComponent implements OnInit {
|
|
|
|
searchForm: FormGroup;
|
2020-04-14 10:35:20 +02:00
|
|
|
@Output() searchTriggered = new EventEmitter();
|
2020-02-16 16:15:07 +01:00
|
|
|
|
|
|
|
regexAddress = /^([a-km-zA-HJ-NP-Z1-9]{26,35}|[a-km-zA-HJ-NP-Z1-9]{80}|[a-z]{2,5}1[ac-hj-np-z02-9]{8,87})$/;
|
2020-02-19 17:50:23 +01:00
|
|
|
regexBlockhash = /^[0]{8}[a-fA-F0-9]{56}$/;
|
|
|
|
regexTransaction = /^[a-fA-F0-9]{64}$/;
|
2020-02-16 16:15:07 +01:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
private formBuilder: FormBuilder,
|
|
|
|
private router: Router,
|
|
|
|
) { }
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
this.searchForm = this.formBuilder.group({
|
|
|
|
searchText: ['', Validators.required],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
search() {
|
|
|
|
const searchText = this.searchForm.value.searchText.trim();
|
|
|
|
if (searchText) {
|
|
|
|
if (this.regexAddress.test(searchText)) {
|
|
|
|
this.router.navigate(['/address/', searchText]);
|
2020-04-14 10:35:20 +02:00
|
|
|
this.searchTriggered.emit();
|
2020-02-19 17:50:23 +01:00
|
|
|
} else if (this.regexBlockhash.test(searchText)) {
|
|
|
|
this.router.navigate(['/block/', searchText]);
|
2020-04-14 10:35:20 +02:00
|
|
|
this.searchTriggered.emit();
|
2020-02-19 17:50:23 +01:00
|
|
|
} else if (this.regexTransaction.test(searchText)) {
|
2020-02-16 16:15:07 +01:00
|
|
|
this.router.navigate(['/tx/', searchText]);
|
2020-04-14 10:35:20 +02:00
|
|
|
this.searchTriggered.emit();
|
2020-02-19 17:50:23 +01:00
|
|
|
} else {
|
|
|
|
return;
|
2020-02-16 16:15:07 +01:00
|
|
|
}
|
|
|
|
this.searchForm.setValue({
|
|
|
|
searchText: '',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|