Improve timezone-select component
This commit is contained in:
		
							parent
							
								
									8766902d45
								
							
						
					
					
						commit
						75d18d7303
					
				@ -63,6 +63,15 @@ export class TimezoneSelectComponent implements ControlValueAccessor, OnInit, Af
 | 
			
		||||
    this.requiredValue = coerceBooleanProperty(value);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  private userTimezoneByDefaultValue: boolean;
 | 
			
		||||
  get userTimezoneByDefault(): boolean {
 | 
			
		||||
    return this.userTimezoneByDefaultValue;
 | 
			
		||||
  }
 | 
			
		||||
  @Input()
 | 
			
		||||
  set userTimezoneByDefault(value: boolean) {
 | 
			
		||||
    this.userTimezoneByDefaultValue = coerceBooleanProperty(value);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  @Input()
 | 
			
		||||
  disabled: boolean;
 | 
			
		||||
 | 
			
		||||
@ -129,28 +138,23 @@ export class TimezoneSelectComponent implements ControlValueAccessor, OnInit, Af
 | 
			
		||||
 | 
			
		||||
  writeValue(value: string | null): void {
 | 
			
		||||
    this.searchText = '';
 | 
			
		||||
    if (value !== null) {
 | 
			
		||||
      getTimezoneInfo(value, this.defaultTimezoneId).subscribe(
 | 
			
		||||
        (foundTimezone) => {
 | 
			
		||||
          if (foundTimezone !== null) {
 | 
			
		||||
            this.selectTimezoneFormGroup.get('timezone').patchValue(foundTimezone, {emitEvent: false});
 | 
			
		||||
            if (foundTimezone.id !== value) {
 | 
			
		||||
              setTimeout(() => {
 | 
			
		||||
                this.updateView(foundTimezone.id);
 | 
			
		||||
              }, 0);
 | 
			
		||||
            } else {
 | 
			
		||||
              this.modelValue = value;
 | 
			
		||||
            }
 | 
			
		||||
    getTimezoneInfo(value, this.defaultTimezoneId, this.userTimezoneByDefaultValue).subscribe(
 | 
			
		||||
      (foundTimezone) => {
 | 
			
		||||
        if (foundTimezone !== null) {
 | 
			
		||||
          this.selectTimezoneFormGroup.get('timezone').patchValue(foundTimezone, {emitEvent: false});
 | 
			
		||||
          if (foundTimezone.id !== value) {
 | 
			
		||||
            setTimeout(() => {
 | 
			
		||||
              this.updateView(foundTimezone.id);
 | 
			
		||||
            }, 0);
 | 
			
		||||
          } else {
 | 
			
		||||
            this.modelValue = null;
 | 
			
		||||
            this.selectTimezoneFormGroup.get('timezone').patchValue('', {emitEvent: false});
 | 
			
		||||
            this.modelValue = value;
 | 
			
		||||
          }
 | 
			
		||||
        } else {
 | 
			
		||||
          this.modelValue = null;
 | 
			
		||||
          this.selectTimezoneFormGroup.get('timezone').patchValue('', {emitEvent: false});
 | 
			
		||||
        }
 | 
			
		||||
      );
 | 
			
		||||
    } else {
 | 
			
		||||
      this.modelValue = null;
 | 
			
		||||
      this.selectTimezoneFormGroup.get('timezone').patchValue('', {emitEvent: false});
 | 
			
		||||
    }
 | 
			
		||||
      }
 | 
			
		||||
    );
 | 
			
		||||
    this.dirty = true;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
@ -165,8 +169,8 @@ export class TimezoneSelectComponent implements ControlValueAccessor, OnInit, Af
 | 
			
		||||
    if (this.ignoreClosePanel) {
 | 
			
		||||
      this.ignoreClosePanel = false;
 | 
			
		||||
    } else {
 | 
			
		||||
      if (!this.modelValue && this.defaultTimezoneId) {
 | 
			
		||||
        getTimezoneInfo(this.defaultTimezoneId).subscribe(
 | 
			
		||||
      if (!this.modelValue && (this.defaultTimezoneId || this.userTimezoneByDefaultValue)) {
 | 
			
		||||
        getTimezoneInfo(this.defaultTimezoneId, this.defaultTimezoneId, this.userTimezoneByDefaultValue).subscribe(
 | 
			
		||||
          (defaultTimezoneInfo) => {
 | 
			
		||||
            if (defaultTimezoneInfo !== null) {
 | 
			
		||||
              this.ngZone.run(() => {
 | 
			
		||||
 | 
			
		||||
@ -19,7 +19,7 @@ import { deepClone, isDefined, isUndefined } from '@app/core/utils';
 | 
			
		||||
import * as moment_ from 'moment';
 | 
			
		||||
import { Observable } from 'rxjs/internal/Observable';
 | 
			
		||||
import { from, of } from 'rxjs';
 | 
			
		||||
import { map, tap } from 'rxjs/operators';
 | 
			
		||||
import { map, mergeMap, tap } from 'rxjs/operators';
 | 
			
		||||
 | 
			
		||||
const moment = moment_;
 | 
			
		||||
 | 
			
		||||
@ -518,14 +518,22 @@ export function getTimezones(): Observable<TimezoneInfo[]> {
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export function getTimezoneInfo(timezoneId: string, defaultTimezoneId?: string): Observable<TimezoneInfo> {
 | 
			
		||||
export function getTimezoneInfo(timezoneId: string, defaultTimezoneId?: string, userTimezoneByDefault?: boolean): Observable<TimezoneInfo> {
 | 
			
		||||
  return getTimezones().pipe(
 | 
			
		||||
    map((timezoneList) => {
 | 
			
		||||
    mergeMap((timezoneList) => {
 | 
			
		||||
      let foundTimezone = timezoneList.find(timezoneInfo => timezoneInfo.id === timezoneId);
 | 
			
		||||
      if (!foundTimezone && defaultTimezoneId) {
 | 
			
		||||
        foundTimezone = timezoneList.find(timezoneInfo => timezoneInfo.id === defaultTimezoneId);
 | 
			
		||||
      if (!foundTimezone) {
 | 
			
		||||
        if (userTimezoneByDefault) {
 | 
			
		||||
          return getDefaultTimezone().pipe(
 | 
			
		||||
            map((userTimezone) => {
 | 
			
		||||
              return timezoneList.find(timezoneInfo => timezoneInfo.id === userTimezone);
 | 
			
		||||
            })
 | 
			
		||||
          );
 | 
			
		||||
        } else if (defaultTimezoneId) {
 | 
			
		||||
          foundTimezone = timezoneList.find(timezoneInfo => timezoneInfo.id === defaultTimezoneId);
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
      return foundTimezone;
 | 
			
		||||
      return of(foundTimezone);
 | 
			
		||||
    })
 | 
			
		||||
  );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user