import { MicroEditorBase } from "../../../Views/editor/MicroEditorBase.js";
export class IntegerEditor extends MicroEditorBase {
    get inputNode() {
        return this.node.find("input");
    }
    get inputValue() {
        return Jade.String.isNullOrWhiteSpace(this.inputRawValue) ?
            null : parseInt(this.inputRawValue);
    }
    get inputRawValue() {
        return this.inputNode.val();
    }
    template() {
        return `<div class="-fb-integer-editor">
                    <input type="number" />
                </div>`;
    }
    focus() {
        this.inputNode.focus().select();
    }
    onActivate(e) {
        super.onActivate(e);
        if (!this.source.enabled) {
            this.inputNode.attr("disabled", "disabled");
        }
        this.inputNode.val(this.source.getFeaturePrimaryValue(""));
        this.inputNode.blur(Jade.BrowserEvent.BLUR, (e) => {
            this.source.setFeaturePrimaryValue(this.inputValue);
        });
        this.inputNode.on(Jade.BrowserEvent.FOCUS, () => { this.focus(); });
        this.inputNode.on(Jade.BrowserEvent.KEYPRESS, (e) => {
            let currentValue = `${this.inputRawValue}`;
            let caretPosition = this.inputNode.get(0).selectionStart;
            let stringStart = currentValue.slice(0, caretPosition);
            let newCharacter = String.fromCharCode(e.which);
            let stringEnd = currentValue.slice(caretPosition);
            currentValue = `${stringStart}${newCharacter}${stringEnd}`;
            if (!(/(^\-{1}$)|(^\-?[1-9]+[0-9]*$)/).test(currentValue)) {
                e.preventDefault();
                e.cancelBubble = true;
            }
        });
    }
}
