CoolInput.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. function CoolInput(setting) {
  2. //setting.data;
  3. //setting.slt;
  4. var _this = this;
  5. _this.cursorPosition = -1;
  6. _this.cursorX = -1;
  7. _this.cursorT=-1;//
  8. _this.focused = false;
  9. _this.inputEl = $(setting.slt);
  10. var modelVar = _this.inputEl.attr('v-model');
  11. _this.inputEl.attr('readonly', 'readonly');
  12. if (_this.inputEl.length === 0) throw (slt + ' not exist');
  13. var input = _this.inputEl[0];
  14. if (!CoolInput.arrInputEl) CoolInput.arrInputEl = [];
  15. CoolInput.arrInputEl.push(input);
  16. function IsPC() {
  17. var userAgentInfo = navigator.userAgent;
  18. var Agents = ["Android", "iPhone", "Windows Phone", "iPad", "iPod"];
  19. var flag = true;
  20. for (var v = 0; v < Agents.length; v++) {
  21. if (userAgentInfo.indexOf(Agents[v]) > 0) {
  22. flag = false;
  23. break;
  24. }
  25. }
  26. return flag;
  27. }
  28. var isOnPC = IsPC();
  29. function val(newV) {
  30. if (newV != undefined) {
  31. if (input.tagName === 'DIV') {
  32. input.innerText = newV;
  33. }
  34. else if (input.value != undefined) {
  35. input.value = newV;
  36. }
  37. }
  38. else {
  39. if (input.tagName === 'DIV') {
  40. return input.innerText;
  41. }
  42. else if (input.value != undefined) {
  43. return input.value;
  44. }
  45. }
  46. }
  47. function getTextWidth(txt,size) {
  48. var tmpDiv = window.coolInputTmpDiv;
  49. if (!tmpDiv) {
  50. tmpDiv = window.coolInputTmpDiv = $('<div style="position:fixed;display:block;z-index:99999;top:300px;left:100px;visibility:hidden;"><div>');
  51. $(document.body).append(tmpDiv);
  52. // tmpDiv.css('top', _this.inputEl.offset().top - 30 + 'px');
  53. //tmpDiv.css('left', _this.inputEl.offset().left + 'px');
  54. }
  55. var fontSize = _this.inputEl.css('font-size');
  56. tmpDiv.css('font-size', fontSize);
  57. tmpDiv.css('font-weight', _this.inputEl.css('font-weight'));
  58. var sp = _this.inputEl.css('letter-spacing');
  59. tmpDiv.css('letter-spacing', sp);
  60. var fnt = _this.inputEl.css('font-family');
  61. tmpDiv.css('font-family', fnt);
  62. tmpDiv.html(txt);
  63. if (size) {
  64. size.height = tmpDiv[0].offsetHeight; size.width = tmpDiv[0].offsetWidth;
  65. }
  66. return tmpDiv[0].offsetWidth;// width();
  67. }
  68. function showPanel (mouseDownEvent) {
  69. var Panel = _this.Panel = $('#coolInputPanel');
  70. if (Panel.length === 0) {
  71. var style = `<style>
  72. #coolInputPanel>div{
  73. width: 31%;height:40px;text-align:center;line-height:40px;
  74. border-width:1px;border-style:solid;border-color:#cacaca;border-top-style:none;border-left-style:none;
  75. }
  76. #coolInputPanel>div:nth-child(n+10){border-bottom-style:none;}
  77. #coolInputPanel>div:nth-child(3n){ border-right-style:none;}
  78. #coolInputPanel>div:active{background-color:#7af;}
  79. </style>`;
  80. $(document.body).append($(style));
  81. var divHtml = `
  82. <div id="coolInputPanel" style="background-color:#dedede;font-size:18px;font-weight:bold; position:fixed;left:0px;bottom:0px;width:100%;height:200px;display:flex;justify-content:center;align-content:center; flex-wrap:wrap;z-index:99999;">
  83. <div>1</div><div>2</div><div>3</div>
  84. <div>4</div><div>5</div><div>6</div>
  85. <div>7</div><div>8</div><div>9</div>
  86. <div>.</div><div>0</div>
  87. <div id="del">
  88. <svg style="display:inline;height:23px;width:23px;margin-top:9px;" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 1000 1000" enable-background="new 0 0 1000 1000" xml:space="preserve">
  89. <g><g transform="translate(0.000000,511.000000) scale(0.100000,-0.100000)"><path d="M3781.8,3617c-116.7-17.2-350.2-82.3-482.2-137.8c-344.4-141.6-317.6-118.6-1722.2-1519.4C186.2,572.5,191.9,578.2,126.9,329.5C73.3,119,100.1-104.9,205.3-317.3c40.2-78.5,311.9-363.6,1333.8-1387.4c1379.7-1381.6,1408.4-1408.4,1743.3-1548.1c82.3-34.4,216.2-82.3,296.6-107.1l145.4-42.1l2669.4-5.7c1883-3.8,2698.1,1.9,2769,15.3c334.9,70.8,616.2,342.5,704.2,683.2c44,164.6,44,5472.9,0,5637.4c-86.1,331.1-340.6,587.5-669.7,673.6c-99.5,24.9-436.3,28.7-2736.4,26.8C5019.9,3626.6,3814.3,3620.8,3781.8,3617z M9141.7,2968.3c28.7-21.1,70.8-63.1,91.9-91.9c40.2-51.7,40.2-55.5,40.2-2767c0-2711.6,0-2715.4-40.2-2767c-21.1-28.7-63.2-70.8-91.9-91.9c-51.7-40.2-61.2-40.2-2648.4-45.9c-2543.2-3.8-2598.7-3.8-2747.9,34.4c-84.2,21-225.8,72.7-315.7,116.7l-164.6,78.5L1998.3-1299C622.5,75,687.5,0.3,718.2,182.1c11.5,63.2,158.8,214.3,1280.2,1335.7c1264.9,1264.9,1266.8,1266.8,1429.4,1345.2c340.6,164.6,111,153.1,3056,149.3C9082.4,3008.5,9090.1,3008.5,9141.7,2968.3z"/><path d="M4566.4,1919.7c-78.5-34.4-158.8-132-174.1-212.4c-30.6-168.4-40.2-156.9,706.1-903.2l692.7-694.6l-692.7-692.7c-748.2-750.1-736.7-736.7-704.2-909c17.2-93.8,132-208.6,225.8-225.8c172.2-32.5,158.8-44,909,704.2l692.7,692.7l694.6-692.7c748.2-748.2,734.8-736.7,907-704.2c93.8,17.2,208.6,132,225.8,225.8c32.5,172.2,44,158.8-704.2,909l-692.7,692.7L7344.9,804c748.2,748.2,736.7,734.8,704.2,907.1c-17.2,93.8-132,208.6-225.8,225.8c-172.2,32.5-158.8,44-909-704.2L6221.6,540l-685.1,683.1c-375,375.1-708,692.7-736.7,704.2C4727.1,1954.1,4637.2,1950.3,4566.4,1919.7z"/></g></g>
  90. </svg>
  91. </div>
  92. </div>`;
  93. Panel = _this.Panel = $(divHtml);
  94. $(document.body).append(Panel);
  95. }
  96. if (isOnPC) {
  97. Panel[0].onmousedown = onTouchPanel;
  98. }
  99. else
  100. {
  101. Panel[0].ontouchstart = onTouchPanel;
  102. }
  103. if (mouseDownEvent) {
  104. // mouseDownEvent.preventDefault();
  105. // mouseDownEvent.stopPropagation();
  106. }
  107. function onTouchPanel (evt) {
  108. var digit = evt.target.innerText;
  109. var txt = val();
  110. var targetDiv = evt.target;
  111. if (evt.target.tagName !== 'DIV')
  112. targetDiv = $(evt.target).parents('div')[0];
  113. if (targetDiv.id === 'del' ) {
  114. if (_this.cursorPosition === val().length) {
  115. val(txt.substr(0, txt.length - 1));
  116. _this.cursorX = getTextWidth(val());
  117. _this.cursorPosition--;
  118. }
  119. else {
  120. val(txt.substring(0, _this.cursorPosition - 1) + txt.substring(_this.cursorPosition, txt.length));
  121. _this.cursorPosition--;
  122. _this.cursorX = getTextWidth(val().substring(0, _this.cursorPosition));
  123. }
  124. if (val().length === 0) {
  125. _this.cursorPosition = val().length;
  126. _this.cursorX = getTextWidth(val());
  127. }
  128. }
  129. else if (digit && digit.length === 1 && '0,1,2,3,4,5,6,7,8,9,.'.indexOf(digit) >= 0) {
  130. if(val().length>5)return;
  131. if (_this.cursorPosition === val().length) {
  132. val(txt + digit);
  133. _this.cursorX = getTextWidth(val());
  134. _this.cursorPosition++;
  135. }
  136. else {
  137. val(txt.substring(0, _this.cursorPosition) + digit + txt.substring(_this.cursorPosition, txt.length));
  138. _this.cursorPosition++;
  139. _this.cursorX = getTextWidth(val().substring(0, _this.cursorPosition));
  140. }
  141. }
  142. else {
  143. var ffff = 8;
  144. }
  145. showCursor(true);
  146. if (setting.data && modelVar) {
  147. setting.data[modelVar] = val();
  148. }
  149. };
  150. if (!_this.focused) {
  151. if (_this.Panel.css('display') === 'none') {
  152. _this.Panel.css('margin-bottom', - _this.Panel.height());
  153. _this.Panel.show();
  154. _this.Panel.animate({ 'margin-bottom': '0px' }, { duration: 400 });
  155. }
  156. }
  157. var top = _this.inputEl.offset().top;
  158. var left = _this.inputEl.offset().left;
  159. if (!mouseDownEvent || val().length === 0) {
  160. _this.cursorPosition = val().length;
  161. _this.cursorX = getTextWidth(val());
  162. }
  163. else {
  164. var insertIndex = -1;
  165. var preDigitX = -1; var curDigitX = -1;
  166. for (var i = 0; i < val().length; i++) {
  167. var bc = val().substring(0, i + 1);
  168. var wd = getTextWidth(bc);
  169. var curDigit = val().substr(i, 1);
  170. var digitWd = getTextWidth(curDigit);
  171. curDigitX = wd - digitWd / 2;
  172. if (mouseDownEvent.offsetX > preDigitX && mouseDownEvent.offsetX <= curDigitX) {
  173. _this.cursorPosition = i;
  174. _this.cursorX = wd - digitWd;
  175. break;
  176. }
  177. else if (i === val().length - 1) {
  178. _this.cursorPosition = val().length;
  179. _this.cursorX = wd;
  180. }
  181. preDigitX = curDigitX;
  182. }
  183. }
  184. if (!_this.tmCoolInput) {
  185. var bShow = true;
  186. _this.tmCoolInput = setInterval(function () {
  187. showCursor(bShow);
  188. bShow = !bShow;
  189. }, 600);
  190. }
  191. showCursor(true);
  192. _this.focused = true;
  193. };
  194. function showCursor(bShow) {
  195. _this.divCursor = $('#coolInputCursor');
  196. if (_this.divCursor.length === 0) {
  197. _this.divCursor = $('<div id="coolInputCursor" style="width:2px;z-index:99999;background-color:#000;position:fixed;display:none;"></div>');
  198. $(document.body).append(_this.divCursor);
  199. // var sz = {};
  200. //getTextWidth('3', sz);
  201. //var ht=sz.height+2;
  202. }
  203. var cx = _this.inputEl.offset().left + _this.cursorX+5;
  204. var cy =_this.cursorT+3;
  205. var ht = _this.inputEl.height()-4;
  206. // var cy = _this.inputEl.offset().top+3;
  207. if (navigator.userAgent.indexOf("iPhone") > 0) {
  208. cx = _this.inputEl.offset().left + _this.cursorX+10;
  209. cy =_this.cursorT+5;
  210. }
  211. _this.divCursor.height(ht);
  212. _this.divCursor.css('left', cx + 'px');
  213. _this.divCursor.css('top', cy + 'px');
  214. if (bShow) {
  215. _this.divCursor.show();
  216. }
  217. else {
  218. _this.divCursor.hide();
  219. }
  220. }
  221. input.onclick = function () {
  222. showPanel();
  223. };
  224. document.addEventListener('touchend', function (evt) {
  225. if (!_this.focused) return;
  226. //ignore the click on the panel
  227. if (evt.target.id === "coolInputPanel")
  228. return;
  229. if (evt.target === input)
  230. return;
  231. var panelMum = $(evt.target).parents('#coolInputPanel');
  232. if (panelMum.length > 0) {
  233. return;
  234. }
  235. if (_this.divCursor && _this.divCursor.length > 0 && evt.target === _this.divCursor[0])
  236. return;
  237. var destIsInput = false;
  238. CoolInput.arrInputEl.every(function (el) {
  239. if (el === evt.target) {
  240. destIsInput = true;
  241. return false;
  242. }
  243. return true;
  244. });
  245. if (!destIsInput) {
  246. if (_this.divCursor) {
  247. _this.divCursor.hide();
  248. }
  249. if (_this.Panel) {
  250. _this.Panel.animate({ 'margin-bottom': '-200px' }, {
  251. duration: 400, complete: function () {
  252. _this.Panel.hide();
  253. }
  254. });
  255. }
  256. }
  257. if (_this.tmCoolInput) {
  258. clearInterval(_this.tmCoolInput); _this.tmCoolInput = 0;
  259. }
  260. _this.focused = false;
  261. });
  262. }