amisa-box-elements
Version:
amisa.box.elements component of amisa!
310 lines (309 loc) • 20.2 kB
JavaScript
var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: !0, configurable: !0, writable: !0, value }) : obj[key] = value;
var __publicField = (obj, key, value) => (__defNormalProp(obj, typeof key != "symbol" ? key + "" : key, value), value), __accessCheck = (obj, member, msg) => {
if (!member.has(obj))
throw TypeError("Cannot " + msg);
};
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj)), __privateAdd = (obj, member, value) => {
if (member.has(obj))
throw TypeError("Cannot add the same private member more than once");
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
}, __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
var _element, _container, _value;
class AmisaBox {
constructor(initialValue) {
__privateAdd(this, _element, void 0);
__privateAdd(this, _container, void 0);
__privateAdd(this, _value, null);
__publicField(this, "onChange");
__publicField(this, "setValue", (value) => {
__privateSet(this, _value, value);
});
__publicField(this, "dispose", () => {
this.disposed(), this.element.remove(), this.container.remove();
});
this.initialValue = initialValue, __privateSet(this, _container, document.createElement("div")), __privateGet(this, _container).classList.add("input-container"), __privateSet(this, _element, document.createElement("input")), __privateGet(this, _element).classList.add("input-element"), this.element.value = this.initialValue || "", __privateGet(this, _container).appendChild(__privateGet(this, _element));
}
get any() {
return this;
}
get element() {
return __privateGet(this, _element);
}
get container() {
return __privateGet(this, _container);
}
get value() {
return __privateGet(this, _value);
}
}
_element = new WeakMap(), _container = new WeakMap(), _value = new WeakMap();
var _onChangeValue;
class AmountBox extends AmisaBox {
constructor(initialValue) {
super(initialValue);
__privateAdd(this, _onChangeValue, () => {
this.element.value ? (this.setValue(+persianStringToEnglishString(this.element.value)), this.element.value = formatNumberShowZero(this.value)) : (this.setValue(null), this.element.value = ""), this.onChange && this.onChange();
});
__publicField(this, "disposed", () => {
this.element.removeEventListener("input", __privateGet(this, _onChangeValue));
});
this.element.placeholder = "___.___.___.___", this.element.style.width = "100%", this.element.style.height = "100%", this.element.style.maxWidth = "120px", this.element.style.margin = "0", this.element.style.padding = "0", this.element.style.display = "block", this.element.style.backgroundColor = "white", this.element.style.color = "black", this.element.style.direction = "ltr", this.element.addEventListener("input", __privateGet(this, _onChangeValue));
}
}
_onChangeValue = new WeakMap();
const persianCharToEnglishNumber = (e) => {
switch (e) {
case "1":
case "۱":
return 1;
case "2":
case "۲":
return 2;
case "3":
case "۳":
return 3;
case "4":
case "۴":
return 4;
case "5":
case "۵":
return 5;
case "6":
case "۶":
return 6;
case "7":
case "۷":
return 7;
case "8":
case "۸":
return 8;
case "9":
case "۹":
return 9;
default:
return 0;
}
}, persianStringToEnglishString = (e, cantUseZero) => (e.match(cantUseZero ? /[/1-9/۱-۹]/g : /[/0-9/۰-۹]/g) || []).map(
(e2) => persianCharToEnglishNumber(e2).toString()
).join(""), formatNumberShowZero = (num) => typeof num == "number" ? num.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,") : "";
Number.prototype.formatNumberShowZero = function() {
return formatNumberShowZero(this);
};
var _onChangeValue2;
class DateBox extends AmisaBox {
constructor(initialValue) {
super(initialValue);
__privateAdd(this, _onChangeValue2, () => {
this.setValue(onChangeInputDateBoxShamsiHandleValue(this.element.value, this.value || "")), this.element.value = this.value || "", this.onChange && this.onChange();
});
__publicField(this, "disposed", () => {
this.element.removeEventListener("input", __privateGet(this, _onChangeValue2));
});
this.element.placeholder = "____/__/__", this.element.style.width = "100%", this.element.style.height = "100%", this.element.style.maxWidth = "80px", this.element.style.margin = "0", this.element.style.padding = "0", this.element.style.display = "block", this.element.style.backgroundColor = "white", this.element.style.color = "black", this.element.style.direction = "ltr", this.element.addEventListener("input", __privateGet(this, _onChangeValue2));
}
}
_onChangeValue2 = new WeakMap();
const onChangeInputDateBoxShamsiHandleValue = (e, value) => {
if (e = e.split("").map((i) => castChars(i)).join(""), e.length === 1)
return (e.match(/[1-4]/g) || value.split("")).join("");
if (e.length === 2)
return (e.match(/[1-4][0-4]/g) || value.split("")).join("");
if (e.length === 3)
return (e.match(/[1-4][0-4]\d/g) || value.split("")).join("");
if (e.length === 4)
return (e.match(/[1-4][0-4]\d\d/g) || value.split("")).join("");
if (e.length === 5)
return (e.match(/[1-4][0-4]\d\d(\/|-)/g) || value.split("")).join("");
if (e.length === 6)
return (e.match(/[1-4][0-4]\d\d\/[0-1]/g) || value.split("")).join("");
if (e.length === 7)
return +e.substring(5, 6) === 1 ? (e.match(/[1-4][0-4]\d\d\/[1][0-2]/g) || value.split("")).join("") : (e.match(/[1-4][0-4]\d\d\/[0][1-9]/g) || value.split("")).join("");
if (e.length === 8)
return (e.match(/[1-4][0-4]\d\d\/[0-1][0-9]\//g) || value.split("")).join("");
if (e.length === 9)
return (e.match(/[1-4][0-4]\d\d\/[0-1][0-9]\/[0-3]/g) || value.split("")).join("");
if (e.length === 10) {
const eighth = +e.substring(8, 9);
if (eighth === 0)
return (e.match(/[1-4][0-4]\d\d\/[0-1][0-9]\/[0][1-9]/g) || value.split("")).join("");
if (eighth === 1)
return (e.match(/[1-4][0-4]\d\d\/[0-1][0-9]\/[1][0-9]/g) || value.split("")).join("");
if (eighth === 2)
return (e.match(/[1-4][0-4]\d\d\/[0-1][0-9]\/[2][0-9]/g) || value.split("")).join("");
if (eighth === 3) {
const sixth = +e.substring(6, 7);
return [1, 2, 3, 4, 5, 6].includes(sixth) ? (e.match(/[1-4][0-4]\d\d\/[0-1][0-9]\/[3][0-1]/g) || value.split("")).join("") : (e.match(/[1-4][0-4]\d\d\/[0-1][0-9]\/[3][0]/g) || value.split("")).join("");
}
}
return e;
}, castChars = (e) => {
switch (e) {
case "1":
case "۱":
return "1";
case "2":
case "۲":
return "2";
case "3":
case "۳":
return "3";
case "4":
case "۴":
return "4";
case "5":
case "۵":
return "5";
case "6":
case "۶":
return "6";
case "7":
case "۷":
return "7";
case "8":
case "۸":
return "8";
case "9":
case "۹":
return "9";
case "0":
case "۰":
return "0";
default:
return "/";
}
};
var _onChangeValue3;
class StringBox extends AmisaBox {
constructor(initialValue) {
super(initialValue);
__privateAdd(this, _onChangeValue3, () => {
this.setValue(this.element.value), this.onChange && this.onChange();
});
__publicField(this, "disposed", () => {
this.element.removeEventListener("input", __privateGet(this, _onChangeValue3));
});
this.element.style.width = "100%", this.element.style.height = "100%", this.element.style.margin = "0", this.element.style.padding = "0", this.element.style.display = "block", this.element.style.backgroundColor = "white", this.element.style.color = "black", this.element.addEventListener("input", __privateGet(this, _onChangeValue3));
}
}
_onChangeValue3 = new WeakMap();
const isDownTheseKeys = (e, shiftKey, ctrlKey, altKey) => {
const isShiftKey2 = shiftKey === void 0 || shiftKey === null || shiftKey && e.shiftKey ? !0 : !shiftKey && !e.shiftKey, isCtrlKey = ctrlKey === void 0 || ctrlKey === null || ctrlKey && e.ctrlKey ? !0 : !ctrlKey && !e.ctrlKey, isAltKey2 = altKey === void 0 || altKey === null || altKey && e.altKey ? !0 : !altKey && !e.altKey;
return isShiftKey2 && isCtrlKey && isAltKey2;
}, isReletedKey = (e) => {
var keyUpper = e.key.toUpperCase(), keysControls = ["SHIFT", "ALT", "CONTROL", ""];
return keyUpper === "TAB" ? !1 : !((e.shiftKey || e.altKey || e.ctrlKey) && keysControls.some((v) => v == keyUpper));
}, isNumeralKey = (e) => "1234567890".includes(e.key), isNumeralKeyBoth = (e) => "1234567890۱۲۳۴۵۶۷۸۹۰".includes(e.key), userNameKey = (e) => (e.match(/[a-zA-Z0-9@._]/g) || []).join(""), websiteKey = (e) => (e.match(/[a-zA-Z0-9:/._]/g) || []).join(""), PhoneNumberKey = (e) => (e.match(/[0-9]/g) || []).join(""), MobileNumberKey = (e) => (e.match(/[0-9]/g) || []).join(""), englishStringNumberToPersianString = (e) => typeof e > "u" || e === null ? "" : e.toString().split("").map(
(e2) => englishCharToPershianNumber(e2).toString()
).join(""), englishCharToPershianNumber = (e) => {
switch (e) {
case "1":
return "۱";
case "2":
return "۲";
case "3":
return "۳";
case "4":
return "۴";
case "5":
return "۵";
case "6":
return "۶";
case "7":
return "۷";
case "8":
return "۸";
case "9":
return "۹";
case "0":
return "۰";
default:
return e;
}
}, isShiftKey = (e) => e.shiftKey && !e.altKey && !e.ctrlKey ? (e.preventDefault(), e.stopPropagation(), !0) : !1, isAltKey = (e) => e.altKey && !e.shiftKey && !e.ctrlKey ? (e.preventDefault(), e.stopPropagation(), !0) : !1, isAlphabetKey = (e) => e.key && e.key.length === 1 && !isNumeralKey(e) && isDownTheseKeys(e, e.shiftKey, !1, !1) ? (e.preventDefault(), !0) : !1, isAlphabetKeyBoth = (e) => e.key && e.key.length === 1 && !isNumeralKeyBoth(e) && isDownTheseKeys(e, e.shiftKey, !1, !1) ? (e.preventDefault(), !0) : !1, isEscapeKey = (e, shift, ctrl, alt, disablePreventDefult) => e.key.toUpperCase() === "Escape".toUpperCase() && isDownTheseKeys(e, shift, ctrl, alt) ? (disablePreventDefult || e.preventDefault(), !0) : !1, isEnterKey = (e, shift, ctrl, alt, disablePreventDefult) => {
var _a;
return ((_a = e.key) == null ? void 0 : _a.toUpperCase()) === "Enter".toUpperCase() && isDownTheseKeys(e, shift, ctrl, alt) ? (disablePreventDefult || e.preventDefault(), !0) : !1;
}, isSpaceKey = (e, shift, ctrl, alt) => e.key === " " && isDownTheseKeys(e, shift, ctrl, alt) ? (e.preventDefault(), !0) : !1, isBackspaceKey = (e, shift, ctrl, alt, disablePreventDefult) => e.code.toUpperCase() === "Backspace".toUpperCase() && isDownTheseKeys(e, shift, ctrl, alt) ? (disablePreventDefult || e.preventDefault(), !0) : !1, isDeleteKey = (e, shift, ctrl, alt) => e.code.toUpperCase() === "Delete".toUpperCase() && isDownTheseKeys(e, shift, ctrl, alt) ? (e.preventDefault(), !0) : !1, isArrowUpKey = (e, shift, ctrl, alt) => e.key.toUpperCase() === "arrowUp".toUpperCase() && isDownTheseKeys(e, shift, ctrl, alt) ? (e.preventDefault(), !0) : !1, isArrowDownKey = (e, shift, ctrl, alt) => e.key.toUpperCase() === "arrowDown".toUpperCase() && isDownTheseKeys(e, shift, ctrl, alt) ? (e.preventDefault(), !0) : !1, isPageUpKey = (e, shift, ctrl, alt) => e.key.toUpperCase() === "pageUp".toUpperCase() && isDownTheseKeys(e, shift, ctrl, alt) ? (e.preventDefault(), !0) : !1, isPageDownKey = (e, shift, ctrl, alt) => e.key.toUpperCase() === "pageDown".toUpperCase() && isDownTheseKeys(e, shift, ctrl, alt) ? (e.preventDefault(), !0) : !1, isArrowLeftKey = (e, shift, ctrl, alt) => e.key.toUpperCase() === "arrowLeft".toUpperCase() && isDownTheseKeys(e, shift, ctrl, alt) ? (e.preventDefault(), !0) : !1, isArrowRightKey = (e, shift, ctrl, alt) => e.key.toUpperCase() === "arrowRight".toUpperCase() && isDownTheseKeys(e, shift, ctrl, alt) ? (e.preventDefault(), !0) : !1, isF1Key = (e, shift, ctrl, alt) => e.key.toUpperCase() === "F1".toUpperCase() && isDownTheseKeys(e, shift, ctrl, alt) ? (e.preventDefault(), !0) : !1, isF2Key = (e, shift, ctrl, alt) => e.key.toUpperCase() === "F2".toUpperCase() && isDownTheseKeys(e, shift, ctrl, alt) ? (e.preventDefault(), !0) : !1, isF3Key = (e, shift, ctrl, alt) => e.key.toUpperCase() === "F3".toUpperCase() && isDownTheseKeys(e, shift, ctrl, alt) ? (e.preventDefault(), !0) : !1, isF4Key = (e, shift, ctrl, alt) => e.key.toUpperCase() === "F4".toUpperCase() && isDownTheseKeys(e, shift, ctrl, alt) ? (e.preventDefault(), !0) : !1, isF5Key = (e, shift, ctrl, alt) => e.key.toUpperCase() === "F5".toUpperCase() && isDownTheseKeys(e, shift, ctrl, alt) ? (e.preventDefault(), !0) : !1, isF6Key = (e, shift, ctrl, alt) => e.key.toUpperCase() === "F6".toUpperCase() && isDownTheseKeys(e, shift, ctrl, alt) ? (e.preventDefault(), !0) : !1, isF7Key = (e, shift, ctrl, alt) => e.key.toUpperCase() === "F7".toUpperCase() && isDownTheseKeys(e, shift, ctrl, alt) ? (e.preventDefault(), !0) : !1, isF8Key = (e, shift, ctrl, alt) => e.key.toUpperCase() === "F8".toUpperCase() && isDownTheseKeys(e, shift, ctrl, alt) ? (e.preventDefault(), !0) : !1, isF9Key = (e, shift, ctrl, alt) => e.key.toUpperCase() === "F9".toUpperCase() && isDownTheseKeys(e, shift, ctrl, alt) ? (e.preventDefault(), !0) : !1, isF11Key = (e, shift, ctrl, alt) => e.key.toUpperCase() === "F11".toUpperCase() && isDownTheseKeys(e, shift, ctrl, alt) ? (e.preventDefault(), !0) : !1, isF10Key = (e, shift, ctrl, alt) => e.key.toUpperCase() === "F10".toUpperCase() && isDownTheseKeys(e, shift, ctrl, alt) ? (e.preventDefault(), !0) : !1, isF12Key = (e, shift, ctrl, alt) => e.key.toUpperCase() === "F12".toUpperCase() && isDownTheseKeys(e, shift, ctrl, alt) ? (e.preventDefault(), !0) : !1, isTabKey = (e, shift, ctrl, alt, disablePreventDefult) => e.key && isDownTheseKeys(e, shift, ctrl, alt) && e.key.toUpperCase() === "tab".toUpperCase() ? (disablePreventDefult || e.preventDefault(), !0) : !1, isDotKey = (e, shift, ctrl, alt) => e.key && isDownTheseKeys(e, shift, ctrl, alt) && e.key === "." ? (e.preventDefault(), !0) : !1, isAsteriskKey = (e, shift, ctrl, alt) => e.key && isDownTheseKeys(e, shift, ctrl, alt) && e.key === "*" ? (e.preventDefault(), !0) : !1, isPlusKey = (e, shift, ctrl, alt) => e.key && isDownTheseKeys(e, shift, ctrl, alt) && e.key === "+" ? (e.preventDefault(), !0) : !1, isEqualKey = (e, shift, ctrl, alt) => e.key && isDownTheseKeys(e, shift, ctrl, alt) && e.code.toLowerCase() === "Equal".toLowerCase() ? (e.preventDefault(), !0) : !1, isAKey = (e, shift, ctrl, alt) => e.key && isDownTheseKeys(e, shift, ctrl, alt) && e.keyCode === 65 ? (e.preventDefault(), !0) : !1, isBKey = (e, shift, ctrl, alt) => e.key && isDownTheseKeys(e, shift, ctrl, alt) && e.keyCode === 66 ? (e.preventDefault(), !0) : !1, isCKey = (e, shift, ctrl, alt) => e.key && isDownTheseKeys(e, shift, ctrl, alt) && e.keyCode === 67 ? (e.preventDefault(), !0) : !1, isDKey = (e, shift, ctrl, alt) => e.key && isDownTheseKeys(e, shift, ctrl, alt) && e.keyCode === 68 ? (e.preventDefault(), !0) : !1, isEKey = (e, shift, ctrl, alt) => e.key && isDownTheseKeys(e, shift, ctrl, alt) && e.keyCode === 69 ? (e.preventDefault(), !0) : !1, isFKey = (e, shift, ctrl, alt) => e.key && isDownTheseKeys(e, shift, ctrl, alt) && e.keyCode === 70 ? (e.preventDefault(), !0) : !1, isGKey = (e, shift, ctrl, alt) => e.key && isDownTheseKeys(e, shift, ctrl, alt) && e.keyCode === 71 ? (e.preventDefault(), !0) : !1, isHKey = (e, shift, ctrl, alt) => e.key && isDownTheseKeys(e, shift, ctrl, alt) && e.keyCode === 72 ? (e.preventDefault(), !0) : !1, isIKey = (e, shift, ctrl, alt) => e.key && isDownTheseKeys(e, shift, ctrl, alt) && e.keyCode === 73 ? (e.preventDefault(), !0) : !1, isJKey = (e, shift, ctrl, alt) => e.key && isDownTheseKeys(e, shift, ctrl, alt) && e.keyCode === 74 ? (e.preventDefault(), !0) : !1, isKKey = (e, shift, ctrl, alt) => e.key && isDownTheseKeys(e, shift, ctrl, alt) && e.keyCode === 75 ? (e.preventDefault(), !0) : !1, isLKey = (e, shift, ctrl, alt) => e.key && isDownTheseKeys(e, shift, ctrl, alt) && e.keyCode === 76 ? (e.preventDefault(), !0) : !1, isMKey = (e, shift, ctrl, alt) => e.key && isDownTheseKeys(e, shift, ctrl, alt) && e.keyCode === 77 ? (e.preventDefault(), !0) : !1, isNKey = (e, shift, ctrl, alt) => e.key && isDownTheseKeys(e, shift, ctrl, alt) && e.keyCode === 78 ? (e.preventDefault(), !0) : !1, isOKey = (e, shift, ctrl, alt) => e.key && isDownTheseKeys(e, shift, ctrl, alt) && e.keyCode === 79 ? (e.preventDefault(), !0) : !1, isPKey = (e, shift, ctrl, alt) => e.key && isDownTheseKeys(e, shift, ctrl, alt) && e.keyCode === 80 ? (e.preventDefault(), !0) : !1, isQKey = (e, shift, ctrl, alt) => e.key && isDownTheseKeys(e, shift, ctrl, alt) && e.keyCode === 81 ? (e.preventDefault(), !0) : !1, isRKey = (e, shift, ctrl, alt) => e.key && isDownTheseKeys(e, shift, ctrl, alt) && e.keyCode === 82 ? (e.preventDefault(), !0) : !1, isSKey = (e, shift, ctrl, alt) => e.key && isDownTheseKeys(e, shift, ctrl, alt) && e.keyCode === 83 ? (e.preventDefault(), !0) : !1, isTKey = (e, shift, ctrl, alt) => e.key && isDownTheseKeys(e, shift, ctrl, alt) && e.keyCode === 84 ? (e.preventDefault(), !0) : !1, isUKey = (e, shift, ctrl, alt) => e.key && isDownTheseKeys(e, shift, ctrl, alt) && e.keyCode === 85 ? (e.preventDefault(), !0) : !1, isVKey = (e, shift, ctrl, alt) => e.key && isDownTheseKeys(e, shift, ctrl, alt) && e.keyCode === 86 ? (e.preventDefault(), !0) : !1, isWKey = (e, shift, ctrl, alt) => e.key && isDownTheseKeys(e, shift, ctrl, alt) && e.keyCode === 87 ? (e.preventDefault(), !0) : !1, isXKey = (e, shift, ctrl, alt) => e.key && isDownTheseKeys(e, shift, ctrl, alt) && e.keyCode === 88 ? (e.preventDefault(), !0) : !1, isYKey = (e, shift, ctrl, alt) => e.key && isDownTheseKeys(e, shift, ctrl, alt) && e.keyCode === 89 ? (e.preventDefault(), !0) : !1, isZKey = (e, shift, ctrl, alt) => e.key && isDownTheseKeys(e, shift, ctrl, alt) && e.keyCode === 90 ? (e.preventDefault(), !0) : !1, isAcceptingKeyF2AndEnter = (e) => isF2Key(e, !1, !1, !1) || isEnterKey(e, !1, !1, !1), isAcceptingKey = (e) => isF2Key(e, !1, !1, !1) || isSKey(e, !1, !0, !1), isNewingKey = (e) => isF3Key(e, !1, !1, !1) || isNKey(e, !1, !0, !1), isViewingKey = (e) => isF4Key(e, !1, !1, !1) || isOKey(e, !1, !0, !1), isEditingKey = (e) => isF4Key(e, !1, !1, !1) || isEKey(e, !1, !0, !1), isDeletingRowKey = (e) => isF8Key(e, !1, !1, !1) || isLKey(e, !1, !1, !0) || isDeleteKey(e, !1, !1, !0) || isBackspaceKey(e, !1, !1, !0), isDeletingCellKey = (e) => isLKey(e, !1, !0, !1) || isDeleteKey(e, !1, !0, !1) || isBackspaceKey(e, !1, !0, !1), isPrintingKey = (e) => isF9Key(e, !1, !1, !1) || isPKey(e, !1, !0, !1), isShowHistoryKey = (e) => isSpaceKey(e, !1, !0, !1) || isArrowDownKey(e, !1, !0, !1);
export {
AmisaBox,
AmountBox,
DateBox,
MobileNumberKey,
PhoneNumberKey,
StringBox,
englishCharToPershianNumber,
englishStringNumberToPersianString,
isAKey,
isAcceptingKey,
isAcceptingKeyF2AndEnter,
isAlphabetKey,
isAlphabetKeyBoth,
isAltKey,
isArrowDownKey,
isArrowLeftKey,
isArrowRightKey,
isArrowUpKey,
isAsteriskKey,
isBKey,
isBackspaceKey,
isCKey,
isDKey,
isDeleteKey,
isDeletingCellKey,
isDeletingRowKey,
isDotKey,
isDownTheseKeys,
isEKey,
isEditingKey,
isEnterKey,
isEqualKey,
isEscapeKey,
isF10Key,
isF11Key,
isF12Key,
isF1Key,
isF2Key,
isF3Key,
isF4Key,
isF5Key,
isF6Key,
isF7Key,
isF8Key,
isF9Key,
isFKey,
isGKey,
isHKey,
isIKey,
isJKey,
isKKey,
isLKey,
isMKey,
isNKey,
isNewingKey,
isNumeralKey,
isNumeralKeyBoth,
isOKey,
isPKey,
isPageDownKey,
isPageUpKey,
isPlusKey,
isPrintingKey,
isQKey,
isRKey,
isReletedKey,
isSKey,
isShiftKey,
isShowHistoryKey,
isSpaceKey,
isTKey,
isTabKey,
isUKey,
isVKey,
isViewingKey,
isWKey,
isXKey,
isYKey,
isZKey,
userNameKey,
websiteKey
};