import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core'; import { countries_and_timezones, Country } from 'tournee.businesslogic'; import { UtilityService } from '../../services/utility.service'; @Component({ selector: 'app-country-picker', templateUrl: './country-picker.component.html', styleUrls: ['./country-picker.component.css'] }) export class CountryPickerComponent implements OnInit { _values: Array = []; @Input() set value(input: Country) { this.multiselect = false; this._values = []; this._values.push(input); } get value() { return this._values[0]; } @Input() set values(input: Array) { this.multiselect = true; this._values = input; } @Input() multiselect: boolean = false; @Input() disabled: boolean = false; @Input() required: boolean = false; @Input() requiredValidationText: string = "Country is required."; @Input() label: string; @Input() icon: string; @Input() canremove: boolean = true; @Input() helpprompt: string; @Output() OnSelected = new EventEmitter(); @Output() OnRemoved = new EventEmitter(); Countries: Array = []; invalid_entry: boolean = true; filter_text: string; constructor(public utility: UtilityService) { } ngOnInit() { this.Countries = countries_and_timezones.get_all_countries().sort((a, b) => { return a.name.localeCompare(b.name); }); } onSelect($event: Country) { this.invalid_entry = false; if (!this.multiselect) { this._values = []; } let index = this._values.findIndex((c) => { return c.id == $event.id; }); if (index == -1) { this.OnSelected.emit($event); } } noResults($event: boolean) { if (!$event) { this.invalid_entry = true; } } remove(v: Country) { this.OnRemoved.emit(v); } clear(v: Country) { this._values = []; this.OnRemoved.emit(v); } last() : string { if (this._values.filter((v) => { return v != null; }).length > 0) { return this._values[this._values.length - 1].name; } return null; } FilteredCountries() { if (this.filter_text) { return this.Countries.filter((c) => { return c.name.toLowerCase().indexOf(this.filter_text.toLowerCase()) != -1}); } return this.Countries; } }