UNPKG

laya-coreui-es

Version:

laya核心库、ui库es版本,剔除了媒体、TTF等功能。基于 LayaAir-release_2.12.0 修改

389 lines (388 loc) 15.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Input = void 0; const Text_1 = require("./Text"); const Event_1 = require("../events/Event"); const Utils_1 = require("../utils/Utils"); const ILaya_1 = require("../../ILaya"); class Input extends Text_1.Text { constructor() { super(); this._multiline = false; this._editable = true; this._maxChars = 1E5; this._type = "text"; this._prompt = ''; this._promptColor = "#A9A9A9"; this._originColor = "#000000"; this._content = ''; Input.IOS_IFRAME = (ILaya_1.ILaya.Browser.onIOS && ILaya_1.ILaya.Browser.window.top != ILaya_1.ILaya.Browser.window.self); this._width = 100; this._height = 20; this.multiline = false; this.overflow = Text_1.Text.SCROLL; this.on(Event_1.Event.MOUSE_DOWN, this, this._onMouseDown); this.on(Event_1.Event.UNDISPLAY, this, this._onUnDisplay); } static __init__() { Input._createInputElement(); if (ILaya_1.ILaya.Browser.onMobile) { var isTrue = false; if (ILaya_1.ILaya.Browser.onMiniGame || ILaya_1.ILaya.Browser.onBDMiniGame || ILaya_1.ILaya.Browser.onQGMiniGame || ILaya_1.ILaya.Browser.onKGMiniGame || ILaya_1.ILaya.Browser.onVVMiniGame || ILaya_1.ILaya.Browser.onAlipayMiniGame || ILaya_1.ILaya.Browser.onQQMiniGame || ILaya_1.ILaya.Browser.onBLMiniGame || ILaya_1.ILaya.Browser.onTTMiniGame || ILaya_1.ILaya.Browser.onHWMiniGame || ILaya_1.ILaya.Browser.onTBMiniGame) { isTrue = true; } ILaya_1.ILaya.Render.canvas.addEventListener(Input.IOS_IFRAME ? (isTrue ? "touchend" : "click") : "touchend", Input._popupInputMethod); } } static _popupInputMethod(e) { if (!Input.isInputting) return; var input = Input.inputElement; input.focus(); } static _createInputElement() { Input._initInput(Input.area = ILaya_1.ILaya.Browser.createElement("textarea")); Input._initInput(Input.input = ILaya_1.ILaya.Browser.createElement("input")); Input.inputContainer = ILaya_1.ILaya.Browser.createElement("div"); Input.inputContainer.style.position = "absolute"; Input.inputContainer.style.zIndex = '1E5'; ILaya_1.ILaya.Browser.container.appendChild(Input.inputContainer); Input.inputContainer.setPos = function (x, y) { Input.inputContainer.style.left = x + 'px'; Input.inputContainer.style.top = y + 'px'; }; } static _initInput(input) { var style = input.style; style.cssText = "position:absolute;overflow:hidden;resize:none;transform-origin:0 0;-webkit-transform-origin:0 0;-moz-transform-origin:0 0;-o-transform-origin:0 0;"; style.resize = 'none'; style.backgroundColor = 'transparent'; style.border = 'none'; style.outline = 'none'; style.zIndex = '1'; input.addEventListener('input', Input._processInputting); input.addEventListener('mousemove', Input._stopEvent); input.addEventListener('mousedown', Input._stopEvent); input.addEventListener('touchmove', Input._stopEvent); input.setFontFace = function (fontFace) { input.style.fontFamily = fontFace; }; if (!ILaya_1.ILaya.Render.isConchApp) { input.setColor = function (color) { input.style.color = color; }; input.setFontSize = function (fontSize) { input.style.fontSize = fontSize + 'px'; }; } } static _processInputting(e) { var input = Input.inputElement.target; if (!input) return; var value = Input.inputElement.value; if (input._restrictPattern) { value = value.replace(/\u2006|\x27/g, ""); if (input._restrictPattern.test(value)) { value = value.replace(input._restrictPattern, ""); Input.inputElement.value = value; } } input._text = value; input.event(Event_1.Event.INPUT); } static _stopEvent(e) { if (e.type == 'touchmove') e.preventDefault(); e.stopPropagation && e.stopPropagation(); } setSelection(startIndex, endIndex) { this.focus = true; Input.inputElement.selectionStart = startIndex; Input.inputElement.selectionEnd = endIndex; } get multiline() { return this._multiline; } set multiline(value) { this._multiline = value; this.valign = value ? "top" : "middle"; } get nativeInput() { return this._multiline ? Input.area : Input.input; } _onUnDisplay(e = null) { this.focus = false; } _onMouseDown(e) { this.focus = true; } _syncInputTransform() { var inputElement = this.nativeInput; var transform = Utils_1.Utils.getTransformRelativeToWindow(this, this.padding[3], this.padding[0]); var inputWid = this._width - this.padding[1] - this.padding[3]; var inputHei = this._height - this.padding[0] - this.padding[2]; if (ILaya_1.ILaya.Render.isConchApp) { inputElement.setScale(transform.scaleX, transform.scaleY); inputElement.setSize(inputWid, inputHei); inputElement.setPos(transform.x, transform.y); } else { Input.inputContainer.style.transform = Input.inputContainer.style.webkitTransform = "scale(" + transform.scaleX + "," + transform.scaleY + ") rotate(" + (ILaya_1.ILaya.stage.canvasDegree) + "deg)"; inputElement.style.width = inputWid + 'px'; inputElement.style.height = inputHei + 'px'; Input.inputContainer.style.left = transform.x + 'px'; Input.inputContainer.style.top = transform.y + 'px'; } } select() { this.nativeInput.select(); } get focus() { return this._focus; } set focus(value) { var input = this.nativeInput; if (this._focus !== value) { if (value) { if (input.target) { input.target._focusOut(); } else { this._setInputMethod(); } input.target = this; this._focusIn(); } else { input.target = null; this._focusOut(); ILaya_1.ILaya.Browser.document.body.scrollTop = 0; input.blur(); if (ILaya_1.ILaya.Render.isConchApp) input.setPos(-10000, -10000); else if (Input.inputContainer.contains(input)) Input.inputContainer.removeChild(input); } } } _setInputMethod() { Input.input.parentElement && (Input.inputContainer.removeChild(Input.input)); Input.area.parentElement && (Input.inputContainer.removeChild(Input.area)); if (ILaya_1.ILaya.Browser.onAndroid) { Input.input = Input.inputElement = ILaya_1.ILaya.Browser.createElement('input'); Input._initInput(Input.input); } Input.inputElement = (this._multiline ? Input.area : Input.input); Input.inputContainer.appendChild(Input.inputElement); if (Text_1.Text.RightToLeft) { Input.inputElement.style.direction = "rtl"; } } _focusIn() { Input.isInputting = true; var input = this.nativeInput; Input.input && (Input.input.type = this._type); this._focus = true; var cssStyle = input.style; cssStyle.whiteSpace = (this.wordWrap ? "pre-wrap" : "nowrap"); this._setPromptColor(); input.readOnly = !this._editable; if (ILaya_1.ILaya.Render.isConchApp) { input.setType(this._type); input.setForbidEdit(!this._editable); } input.maxLength = this._maxChars; input.value = this._content; input.placeholder = this._prompt; ILaya_1.ILaya.stage.off(Event_1.Event.KEY_DOWN, this, this._onKeyDown); ILaya_1.ILaya.stage.on(Event_1.Event.KEY_DOWN, this, this._onKeyDown); ILaya_1.ILaya.stage.focus = this; this.event(Event_1.Event.FOCUS); if (ILaya_1.ILaya.Browser.onPC) input.focus(); if (!ILaya_1.ILaya.Browser.onMiniGame && !ILaya_1.ILaya.Browser.onBDMiniGame && !ILaya_1.ILaya.Browser.onQGMiniGame && !ILaya_1.ILaya.Browser.onKGMiniGame && !ILaya_1.ILaya.Browser.onVVMiniGame && !ILaya_1.ILaya.Browser.onAlipayMiniGame && !ILaya_1.ILaya.Browser.onQQMiniGame && !ILaya_1.ILaya.Browser.onBLMiniGame && !ILaya_1.ILaya.Browser.onTTMiniGame && !ILaya_1.ILaya.Browser.onHWMiniGame && !ILaya_1.ILaya.Browser.onTBMiniGame) { this._text = null; } this.typeset(); input.setColor(this._originColor); input.setFontSize(this.fontSize); input.setFontFace(ILaya_1.ILaya.Browser.onIPhone ? (Text_1.Text.fontFamilyMap[this.font] || this.font) : this.font); if (ILaya_1.ILaya.Render.isConchApp) { input.setMultiAble && input.setMultiAble(this._multiline); } cssStyle.lineHeight = (this.leading + this.fontSize) + "px"; cssStyle.fontStyle = (this.italic ? "italic" : "normal"); cssStyle.fontWeight = (this.bold ? "bold" : "normal"); cssStyle.textAlign = this.align; cssStyle.padding = "0 0"; this._syncInputTransform(); if (!ILaya_1.ILaya.Render.isConchApp && ILaya_1.ILaya.Browser.onPC) ILaya_1.ILaya.systemTimer.frameLoop(1, this, this._syncInputTransform); } _setPromptColor() { Input.promptStyleDOM = ILaya_1.ILaya.Browser.getElementById("promptStyle"); if (!Input.promptStyleDOM) { Input.promptStyleDOM = ILaya_1.ILaya.Browser.createElement("style"); Input.promptStyleDOM.setAttribute("id", "promptStyle"); ILaya_1.ILaya.Browser.document.head.appendChild(Input.promptStyleDOM); } Input.promptStyleDOM.innerText = "input::-webkit-input-placeholder, textarea::-webkit-input-placeholder {" + "color:" + this._promptColor + "}" + "input:-moz-placeholder, textarea:-moz-placeholder {" + "color:" + this._promptColor + "}" + "input::-moz-placeholder, textarea::-moz-placeholder {" + "color:" + this._promptColor + "}" + "input:-ms-input-placeholder, textarea:-ms-input-placeholder {" + "color:" + this._promptColor + "}"; } _focusOut() { if (!Input.isInputting) return; Input.isInputting = false; this._focus = false; this._text = null; this._content = this.nativeInput.value; if (!this._content) { super.set_text(this._prompt); super.set_color(this._promptColor); } else { super.set_text(this._content); super.set_color(this._originColor); } ILaya_1.ILaya.stage.off(Event_1.Event.KEY_DOWN, this, this._onKeyDown); ILaya_1.ILaya.stage.focus = null; this.event(Event_1.Event.BLUR); this.event(Event_1.Event.CHANGE); if (ILaya_1.ILaya.Render.isConchApp) this.nativeInput.blur(); ILaya_1.ILaya.Browser.onPC && ILaya_1.ILaya.systemTimer.clear(this, this._syncInputTransform); } _onKeyDown(e) { if (e.keyCode === 13) { if (ILaya_1.ILaya.Browser.onMobile && !this._multiline) this.focus = false; this.event(Event_1.Event.ENTER); } } set text(value) { super.set_color(this._originColor); value += ''; if (this._focus) { this.nativeInput.value = value || ''; this.event(Event_1.Event.CHANGE); } else { if (!this._multiline) value = value.replace(/\r?\n/g, ''); this._content = value; if (value) super.set_text(value); else { super.set_text(this._prompt); super.set_color(this.promptColor); } } } get text() { if (this._focus) return this.nativeInput.value; else return this._content || ""; } changeText(text) { this._content = text; if (this._focus) { this.nativeInput.value = text || ''; this.event(Event_1.Event.CHANGE); } else super.changeText(text); } set color(value) { if (this._focus) this.nativeInput.setColor(value); super.set_color(this._content ? value : this._promptColor); this._originColor = value; } get color() { return super.color; } set bgColor(value) { super.set_bgColor(value); if (ILaya_1.ILaya.Render.isConchApp) this.nativeInput.setBgColor(value); } get bgColor() { return super.bgColor; } get restrict() { if (this._restrictPattern) { return this._restrictPattern.source; } return ""; } set restrict(pattern) { if (pattern) { pattern = "[^" + pattern + "]"; if (pattern.indexOf("^^") > -1) pattern = pattern.replace("^^", ""); this._restrictPattern = new RegExp(pattern, "g"); } else this._restrictPattern = null; } set editable(value) { this._editable = value; if (ILaya_1.ILaya.Render.isConchApp) { Input.input.setForbidEdit(!value); } } get editable() { return this._editable; } get maxChars() { return this._maxChars; } set maxChars(value) { if (value <= 0) value = 1E5; this._maxChars = value; } get prompt() { return this._prompt; } set prompt(value) { if (!this._text && value) super.set_color(this._promptColor); this.promptColor = this._promptColor; if (this._text) super.set_text((this._text == this._prompt) ? value : this._text); else super.set_text(value); this._prompt = Text_1.Text.langPacks && Text_1.Text.langPacks[value] ? Text_1.Text.langPacks[value] : value; } get promptColor() { return this._promptColor; } set promptColor(value) { this._promptColor = value; if (!this._content) super.set_color(value); } get type() { return this._type; } set type(value) { if (value === "password") this._getTextStyle().asPassword = true; else this._getTextStyle().asPassword = false; this._type = value; } } exports.Input = Input; Input.TYPE_TEXT = "text"; Input.TYPE_PASSWORD = "password"; Input.TYPE_EMAIL = "email"; Input.TYPE_URL = "url"; Input.TYPE_NUMBER = "number"; Input.TYPE_RANGE = "range"; Input.TYPE_DATE = "date"; Input.TYPE_MONTH = "month"; Input.TYPE_WEEK = "week"; Input.TYPE_TIME = "time"; Input.TYPE_DATE_TIME = "datetime"; Input.TYPE_DATE_TIME_LOCAL = "datetime-local"; Input.TYPE_SEARCH = "search"; Input.IOS_IFRAME = false; Input.inputHeight = 45; Input.isInputting = false;