UNPKG

ferngully-aurelia-tools

Version:

Ferngully Tools for Aurelia

209 lines 7.07 kB
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; import { customElement, bindable, autoinject } from "aurelia-framework"; import { LoggingService } from "../../../services/logging-service"; import { NumberService } from "../../../services/number-service"; import { BindingEngine } from "aurelia-framework"; import { bindingMode as BindingMode } from "aurelia-binding"; import "./number-input.css"; import "./spinner.css"; let NumberInput = class NumberInput { constructor(loggingService, numberService, bindingEngine) { this.loggingService = loggingService; this.numberService = numberService; this.bindingEngine = bindingEngine; this.min = Number.NEGATIVE_INFINITY; this.max = Number.POSITIVE_INFINITY; this.step = 1; this.timer = null; this.didSomething = false; this.way = whichWay.undefined; } bind() { this.target = +this.target || 0; this.originalValue = this.target; this.min = isNaN(this.min) ? Number.NEGATIVE_INFINITY : +this.min; this.max = isNaN(this.max) ? Number.POSITIVE_INFINITY : +this.max; this.step = +this.step; this.subscriptionTarget = this.bindingEngine.propertyObserver(this, 'target') .subscribe((newValue, oldValue) => { this.updateDirtyState(newValue); }); this.subscriptionDirty = this.bindingEngine.propertyObserver(this, 'dirty') .subscribe((newValue, oldValue) => { if (newValue) { $(this.inputGroup).addClass("dirty"); } else { $(this.inputGroup).removeClass("dirty"); } }); } unbind() { this.subscriptionTarget.dispose(); this.subscriptionDirty.dispose(); } incr() { let target = this.target + this.step; if (this.target === this.max) { this.done(); return; } else { target = Math.min(target, this.max); } this.target = target; } decr() { let target = this.target - this.step; if (this.target === this.min) { this.done(); return; } else { target = Math.max(target, this.min); } this.target = target; } updateDirtyState(newValue) { let isDirty = newValue !== this.originalValue; if (this.dirty !== null) { this.dirty = isDirty; } } buttonIncr(event) { this.buttonKeyPress(event, whichWay.incr); return true; } buttonDecr(event) { this.buttonKeyPress(event, whichWay.decr); return true; } buttonKeyPress(event, way) { if ((!(event.altKey)) && (event.key === " ")) { (way === whichWay.incr) ? this.incr() : this.decr(); } } startIncr() { return this.startTimer(whichWay.incr); } startDecr() { return this.startTimer(whichWay.decr); } startTimer(way) { if (this.way !== whichWay.undefined) { return false; } this.way = way; this.didSomething = false; setTimeout(() => { if ((this.way !== whichWay.undefined) && !this.didSomething && !this.timer) { this.timer = setInterval(() => { this.doIt(way); }, 160); } }, 160); return false; } mouseexit() { if (!this.didSomething) { this.doIt(this.way); } this.done(); return false; } done() { this.way = whichWay.undefined; this.endTimer(); } endTimer() { if (this.timer) { clearInterval(this.timer); this.timer = null; } return false; } doIt(way) { if (way === whichWay.incr) { this.incr(); } else if (way === whichWay.decr) { this.decr(); } this.didSomething = true; } confirmPaste(event) { if (event.defaultPrevented) { return false; } if (!this.numberService.stringIsNumber(event.clipboardData.getData("text"))) { event.preventDefault(); return false; } else { return true; } } confirmKey(event) { if (event.defaultPrevented) { return false; } if ((event.keyCode === 38) || (event.keyCode === 40)) { (event.keyCode === 38) ? this.incr() : this.decr(); return true; } if ((event.ctrlKey && (["c", "v", "x", "a"].indexOf(event.key.toLowerCase()) !== -1)) || ([8, 9, 13, 16, 17, 18, 27, 37, 39, 46].indexOf(event.keyCode) !== -1)) { return true; } if (!this.numberService.stringIsNumber(event.key)) { event.preventDefault(); return false; } else { return true; } } }; __decorate([ bindable({ defaultBindingMode: BindingMode.twoWay }), __metadata("design:type", Number) ], NumberInput.prototype, "target", void 0); __decorate([ bindable({ defaultBindingMode: BindingMode.twoWay, defaultValue: false }), __metadata("design:type", Boolean) ], NumberInput.prototype, "dirty", void 0); __decorate([ bindable, __metadata("design:type", Number) ], NumberInput.prototype, "min", void 0); __decorate([ bindable, __metadata("design:type", Number) ], NumberInput.prototype, "max", void 0); __decorate([ bindable, __metadata("design:type", Number) ], NumberInput.prototype, "step", void 0); NumberInput = __decorate([ autoinject, customElement('number-input'), __metadata("design:paramtypes", [LoggingService, NumberService, BindingEngine]) ], NumberInput); export { NumberInput }; export var whichWay; (function (whichWay) { whichWay[whichWay["undefined"] = 0] = "undefined"; whichWay[whichWay["incr"] = 1] = "incr"; whichWay[whichWay["decr"] = 2] = "decr"; })(whichWay || (whichWay = {})); //# sourceMappingURL=number-input.js.map