@rifansi/angular-datetime-picker
Version:
Angular Date Time Picker
176 lines (135 loc) • 4.47 kB
text/typescript
/**
* timer-box.component
*/
import {
ChangeDetectionStrategy,
Component,
EventEmitter,
ElementRef,
ViewChild,
Input,
OnDestroy,
OnInit,
Output
} from '@angular/core';
import { coerceNumberProperty } from '@angular/cdk/coercion';
import { Subject, Subscription } from 'rxjs';
import { debounceTime } from 'rxjs/operators';
export class OwlTimerBoxComponent implements OnInit, OnDestroy {
showDivider = false;
upBtnAriaLabel: string;
upBtnDisabled: boolean;
downBtnAriaLabel: string;
downBtnDisabled: boolean;
/**
* Value would be displayed in the box
* If it is null, the box would display [value]
* */
boxValue: number;
value: number;
min: number;
max: number;
step = 1;
inputLabel: string;
valueChange = new EventEmitter<number>();
inputChange = new EventEmitter<number>();
private inputStream = new Subject<string>();
private inputStreamSub = Subscription.EMPTY;
private hasFocus = false;
get displayValue(): string {
if (this.hasFocus) {
// Don't try to reformat the value that user is currently editing
return this.valueInput.nativeElement.value;
}
const value = this.boxValue || this.value;
if (value === null || isNaN(value)) {
return '';
}
return value < 10 ? '0' + value.toString() : value.toString();
}
get owlDTTimerBoxClass(): boolean {
return true;
}
private valueInput: ElementRef<HTMLInputElement>;
private onValueInputMouseWheelBind = this.onValueInputMouseWheel.bind(this);
constructor() {
}
public ngOnInit() {
this.inputStreamSub = this.inputStream.pipe(debounceTime(750)).subscribe(( val: string ) => {
if (val) {
const inputValue = coerceNumberProperty(val, 0);
this.updateValueViaInput(inputValue);
}
});
this.bindValueInputMouseWheel();
}
public ngOnDestroy(): void {
this.unbindValueInputMouseWheel();
this.inputStreamSub.unsubscribe();
}
public upBtnClicked(): void {
this.updateValue(this.value + this.step);
}
public downBtnClicked(): void {
this.updateValue(this.value - this.step);
}
public handleInputChange(val: string ): void {
this.inputStream.next(val);
}
public focusIn(): void {
this.hasFocus = true;
}
public focusOut(value: string): void {
this.hasFocus = false;
if (value) {
const inputValue = coerceNumberProperty(value, 0);
this.updateValueViaInput(inputValue);
}
}
private updateValue( value: number ): void {
this.valueChange.emit(value);
}
private updateValueViaInput( value: number ): void {
if (value > this.max || value < this.min) {
return;
}
this.inputChange.emit(value);
}
private onValueInputMouseWheel( event: any ): void {
event = event || window.event;
const delta = event.wheelDelta || -event.deltaY || -event.detail;
if (delta > 0) {
if (!this.upBtnDisabled) {
this.upBtnClicked();
}
} else if (delta < 0) {
if (!this.downBtnDisabled) {
this.downBtnClicked();
}
}
event.preventDefault ? event.preventDefault() : (event.returnValue = false);
}
private bindValueInputMouseWheel(): void {
this.valueInput.nativeElement.addEventListener(
'onwheel' in document ? 'wheel' : 'mousewheel',
this.onValueInputMouseWheelBind);
}
private unbindValueInputMouseWheel(): void {
this.valueInput.nativeElement.removeEventListener(
'onwheel' in document ? 'wheel' : 'mousewheel',
this.onValueInputMouseWheelBind);
}
}