pm-controls
Version:
ProModel Controls
102 lines (90 loc) • 2.64 kB
text/typescript
import {
Component,
Input,
OnInit,
Output,
EventEmitter,
ChangeDetectorRef
} from '@angular/core';
export class NumericBoxComponent implements OnInit {
Value: number;
IsDisabled: boolean;
ValueChange: EventEmitter<any> = new EventEmitter<any>();
Watermark: string;
MaxValue: number;
MinValue: number;
TextChange: EventEmitter<string> = new EventEmitter<string>();
constructor(
public changeDetectorRef: ChangeDetectorRef) {
this.changeDetectorRef.detach();
}
ngOnInit() {
if (this.Value) {
this.Text = this.Value.toString();
}
}
onKeyPress(e: KeyboardEvent) {
var key = e.key;
if (isNaN(+key) || key == " ")
e.preventDefault();
}
onPaste(e: ClipboardEvent) {
var pastedText = undefined;
if ((<any>window).clipboardData && (<any>window).clipboardData.getData) { // IE
pastedText = (<any>window).clipboardData.getData('Text');
} else if (e.clipboardData && e.clipboardData.getData) {
pastedText = e.clipboardData.getData('text/plain');
}
// Prevent the default handler from running.
if (isNaN(pastedText)) return false;
if (this.MinValue && pastedText < this.MinValue) return false;
if (this.MaxValue && pastedText > this.MaxValue) return false;
if (pastedText != Math.floor(pastedText)) return false;
}
incrementUp() {
if (!isNaN(+this.Text)) {
var value = +this.Text;
value++;
if (this.MaxValue && value > this.MaxValue)
return;
this.SetValue(String(value));
}
else
this.SetValue("1");
}
incrementDown() {
if (!isNaN(+this.Text)) {
var value = +this.Text;
value--;
if (this.MinValue && value < this.MinValue)
return;
this.SetValue(String(value));
}
else
this.SetValue("0");
}
onTextChange(newValue) {
this.SetValue(newValue);
}
SetValue(newValue) {
this.Text = newValue;
this.Value = this.Text && !isNaN(+this.Text) ? +this.Text : undefined;
this.ValueChange.emit(this.Value);
}
private text: string;
get Text() : string
{
return this.text;
}
set Text(value: string) {
this.text = value;
this.TextChange.emit(value);
this.changeDetectorRef.detectChanges();
}
}