function CoolInput(setting) { //setting.data; //setting.slt; var _this = this; _this.cursorPosition = -1; _this.cursorX = -1; _this.cursorT=-1;// _this.focused = false; _this.inputEl = $(setting.slt); var modelVar = _this.inputEl.attr('v-model'); _this.inputEl.attr('readonly', 'readonly'); if (_this.inputEl.length === 0) throw (slt + ' not exist'); var input = _this.inputEl[0]; if (!CoolInput.arrInputEl) CoolInput.arrInputEl = []; CoolInput.arrInputEl.push(input); function IsPC() { var userAgentInfo = navigator.userAgent; var Agents = ["Android", "iPhone", "Windows Phone", "iPad", "iPod"]; var flag = true; for (var v = 0; v < Agents.length; v++) { if (userAgentInfo.indexOf(Agents[v]) > 0) { flag = false; break; } } return flag; } var isOnPC = IsPC(); function val(newV) { if (newV != undefined) { if (input.tagName === 'DIV') { input.innerText = newV; } else if (input.value != undefined) { input.value = newV; } } else { if (input.tagName === 'DIV') { return input.innerText; } else if (input.value != undefined) { return input.value; } } } function getTextWidth(txt,size) { var tmpDiv = window.coolInputTmpDiv; if (!tmpDiv) { tmpDiv = window.coolInputTmpDiv = $('
'); $(document.body).append(tmpDiv); // tmpDiv.css('top', _this.inputEl.offset().top - 30 + 'px'); //tmpDiv.css('left', _this.inputEl.offset().left + 'px'); } var fontSize = _this.inputEl.css('font-size'); tmpDiv.css('font-size', fontSize); tmpDiv.css('font-weight', _this.inputEl.css('font-weight')); var sp = _this.inputEl.css('letter-spacing'); tmpDiv.css('letter-spacing', sp); var fnt = _this.inputEl.css('font-family'); tmpDiv.css('font-family', fnt); tmpDiv.html(txt); if (size) { size.height = tmpDiv[0].offsetHeight; size.width = tmpDiv[0].offsetWidth; } return tmpDiv[0].offsetWidth;// width(); } function showPanel (mouseDownEvent) { var Panel = _this.Panel = $('#coolInputPanel'); if (Panel.length === 0) { var style = ``; $(document.body).append($(style)); var divHtml = `
1
2
3
4
5
6
7
8
9
.
0
`; Panel = _this.Panel = $(divHtml); $(document.body).append(Panel); } if (isOnPC) { Panel[0].onmousedown = onTouchPanel; } else { Panel[0].ontouchstart = onTouchPanel; } if (mouseDownEvent) { // mouseDownEvent.preventDefault(); // mouseDownEvent.stopPropagation(); } function onTouchPanel (evt) { var digit = evt.target.innerText; var txt = val(); var targetDiv = evt.target; if (evt.target.tagName !== 'DIV') targetDiv = $(evt.target).parents('div')[0]; if (targetDiv.id === 'del' ) { if (_this.cursorPosition === val().length) { val(txt.substr(0, txt.length - 1)); _this.cursorX = getTextWidth(val()); _this.cursorPosition--; } else { val(txt.substring(0, _this.cursorPosition - 1) + txt.substring(_this.cursorPosition, txt.length)); _this.cursorPosition--; _this.cursorX = getTextWidth(val().substring(0, _this.cursorPosition)); } if (val().length === 0) { _this.cursorPosition = val().length; _this.cursorX = getTextWidth(val()); } } else if (digit && digit.length === 1 && '0,1,2,3,4,5,6,7,8,9,.'.indexOf(digit) >= 0) { if(val().length>5)return; if (_this.cursorPosition === val().length) { val(txt + digit); _this.cursorX = getTextWidth(val()); _this.cursorPosition++; } else { val(txt.substring(0, _this.cursorPosition) + digit + txt.substring(_this.cursorPosition, txt.length)); _this.cursorPosition++; _this.cursorX = getTextWidth(val().substring(0, _this.cursorPosition)); } } else { var ffff = 8; } showCursor(true); if (setting.data && modelVar) { setting.data[modelVar] = val(); } }; if (!_this.focused) { if (_this.Panel.css('display') === 'none') { _this.Panel.css('margin-bottom', - _this.Panel.height()); _this.Panel.show(); _this.Panel.animate({ 'margin-bottom': '0px' }, { duration: 400 }); } } var top = _this.inputEl.offset().top; var left = _this.inputEl.offset().left; if (!mouseDownEvent || val().length === 0) { _this.cursorPosition = val().length; _this.cursorX = getTextWidth(val()); } else { var insertIndex = -1; var preDigitX = -1; var curDigitX = -1; for (var i = 0; i < val().length; i++) { var bc = val().substring(0, i + 1); var wd = getTextWidth(bc); var curDigit = val().substr(i, 1); var digitWd = getTextWidth(curDigit); curDigitX = wd - digitWd / 2; if (mouseDownEvent.offsetX > preDigitX && mouseDownEvent.offsetX <= curDigitX) { _this.cursorPosition = i; _this.cursorX = wd - digitWd; break; } else if (i === val().length - 1) { _this.cursorPosition = val().length; _this.cursorX = wd; } preDigitX = curDigitX; } } if (!_this.tmCoolInput) { var bShow = true; _this.tmCoolInput = setInterval(function () { showCursor(bShow); bShow = !bShow; }, 600); } showCursor(true); _this.focused = true; }; function showCursor(bShow) { _this.divCursor = $('#coolInputCursor'); if (_this.divCursor.length === 0) { _this.divCursor = $(''); $(document.body).append(_this.divCursor); // var sz = {}; //getTextWidth('3', sz); //var ht=sz.height+2; } var cx = _this.inputEl.offset().left + _this.cursorX+5; var cy =_this.cursorT+3; var ht = _this.inputEl.height()-4; // var cy = _this.inputEl.offset().top+3; if (navigator.userAgent.indexOf("iPhone") > 0) { cx = _this.inputEl.offset().left + _this.cursorX+10; cy =_this.cursorT+5; } _this.divCursor.height(ht); _this.divCursor.css('left', cx + 'px'); _this.divCursor.css('top', cy + 'px'); if (bShow) { _this.divCursor.show(); } else { _this.divCursor.hide(); } } input.onclick = function () { showPanel(); }; document.addEventListener('touchend', function (evt) { if (!_this.focused) return; //ignore the click on the panel if (evt.target.id === "coolInputPanel") return; if (evt.target === input) return; var panelMum = $(evt.target).parents('#coolInputPanel'); if (panelMum.length > 0) { return; } if (_this.divCursor && _this.divCursor.length > 0 && evt.target === _this.divCursor[0]) return; var destIsInput = false; CoolInput.arrInputEl.every(function (el) { if (el === evt.target) { destIsInput = true; return false; } return true; }); if (!destIsInput) { if (_this.divCursor) { _this.divCursor.hide(); } if (_this.Panel) { _this.Panel.animate({ 'margin-bottom': '-200px' }, { duration: 400, complete: function () { _this.Panel.hide(); } }); } } if (_this.tmCoolInput) { clearInterval(_this.tmCoolInput); _this.tmCoolInput = 0; } _this.focused = false; }); }