ferngully-aurelia-tools
Version:
Ferngully Tools for Aurelia
212 lines • 7.89 kB
JavaScript
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";
var NumberInput = (function () {
function NumberInput(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;
}
NumberInput.prototype.bind = function () {
var _this = this;
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(function (newValue, oldValue) {
_this.updateDirtyState(newValue);
});
this.subscriptionDirty = this.bindingEngine.propertyObserver(this, 'dirty')
.subscribe(function (newValue, oldValue) {
if (newValue) {
$(_this.inputGroup).addClass("dirty");
}
else {
$(_this.inputGroup).removeClass("dirty");
}
});
};
NumberInput.prototype.unbind = function () {
this.subscriptionTarget.dispose();
this.subscriptionDirty.dispose();
};
NumberInput.prototype.incr = function () {
var target = this.target + this.step;
if (this.target === this.max) {
this.done();
return;
}
else {
target = Math.min(target, this.max);
}
this.target = target;
};
NumberInput.prototype.decr = function () {
var target = this.target - this.step;
if (this.target === this.min) {
this.done();
return;
}
else {
target = Math.max(target, this.min);
}
this.target = target;
};
NumberInput.prototype.updateDirtyState = function (newValue) {
var isDirty = newValue !== this.originalValue;
if (this.dirty !== null) {
this.dirty = isDirty;
}
};
NumberInput.prototype.buttonIncr = function (event) {
this.buttonKeyPress(event, whichWay.incr);
return true;
};
NumberInput.prototype.buttonDecr = function (event) {
this.buttonKeyPress(event, whichWay.decr);
return true;
};
NumberInput.prototype.buttonKeyPress = function (event, way) {
if ((!(event.altKey)) && (event.key === " ")) {
(way === whichWay.incr) ? this.incr() : this.decr();
}
};
NumberInput.prototype.startIncr = function () {
return this.startTimer(whichWay.incr);
};
NumberInput.prototype.startDecr = function () {
return this.startTimer(whichWay.decr);
};
NumberInput.prototype.startTimer = function (way) {
var _this = this;
if (this.way !== whichWay.undefined) {
return false;
}
this.way = way;
this.didSomething = false;
setTimeout(function () {
if ((_this.way !== whichWay.undefined) && !_this.didSomething && !_this.timer) {
_this.timer = setInterval(function () {
_this.doIt(way);
}, 160);
}
}, 160);
return false;
};
NumberInput.prototype.mouseexit = function () {
if (!this.didSomething) {
this.doIt(this.way);
}
this.done();
return false;
};
NumberInput.prototype.done = function () {
this.way = whichWay.undefined;
this.endTimer();
};
NumberInput.prototype.endTimer = function () {
if (this.timer) {
clearInterval(this.timer);
this.timer = null;
}
return false;
};
NumberInput.prototype.doIt = function (way) {
if (way === whichWay.incr) {
this.incr();
}
else if (way === whichWay.decr) {
this.decr();
}
this.didSomething = true;
};
NumberInput.prototype.confirmPaste = function (event) {
if (event.defaultPrevented) {
return false;
}
if (!this.numberService.stringIsNumber(event.clipboardData.getData("text"))) {
event.preventDefault();
return false;
}
else {
return true;
}
};
NumberInput.prototype.confirmKey = function (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);
return 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