simulator.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. export default function ($, Inputmask) {
  2. $.caret = function (input, begin, end) {
  3. input = input.nodeName ? input : input[0];
  4. var inputType = input.type;
  5. input.type = "text";
  6. input.focus();
  7. var range;
  8. if (typeof begin === "number") {
  9. end = (typeof end == "number") ? end : begin;
  10. // if (!$(input).is(":visible")) {
  11. // return;
  12. // }
  13. if (input.setSelectionRange) {
  14. input.selectionStart = begin;
  15. input.selectionEnd = end;
  16. } else if (window.getSelection) {
  17. range = document.createRange();
  18. if (input.firstChild === undefined) {
  19. var textNode = document.createTextNode("");
  20. input.appendChild(textNode);
  21. }
  22. range.setStart(input.firstChild, begin < input.value.length ? begin : input.value.length);
  23. range.setEnd(input.firstChild, end < input.value.length ? end : input.value.length);
  24. range.collapse(true);
  25. var sel = window.getSelection();
  26. sel.removeAllRanges();
  27. sel.addRange(range);
  28. //input.focus();
  29. } else if (input.createTextRange) {
  30. range = input.createTextRange();
  31. range.collapse(true);
  32. range.moveEnd("character", end);
  33. range.moveStart("character", begin);
  34. range.select();
  35. }
  36. input.type = inputType;
  37. } else {
  38. if (input.setSelectionRange) {
  39. begin = input.selectionStart;
  40. end = input.selectionEnd;
  41. } else if (window.getSelection) {
  42. range = window.getSelection().getRangeAt(0);
  43. if (range.commonAncestorContainer.parentNode === input || range.commonAncestorContainer === input) {
  44. begin = range.startOffset;
  45. end = range.endOffset;
  46. }
  47. } else if (document.selection && document.selection.createRange) {
  48. range = document.selection.createRange();
  49. begin = 0 - range.duplicate().moveStart("character", -100000);
  50. end = begin + range.text.length;
  51. }
  52. /*eslint-disable consistent-return */
  53. input.type = inputType;
  54. return {
  55. "begin": begin,
  56. "end": end
  57. };
  58. /*eslint-enable consistent-return */
  59. }
  60. };
  61. $.fn = $.fn || $.prototype;
  62. $.fn.SendKey = function (keyCode, modifier) {
  63. function trigger(elem, evnt) {
  64. elem.focus();
  65. if ($ === window.jQuery) {
  66. $(elem).trigger(evnt);
  67. } else {
  68. if (document.createEvent) {
  69. elem.dispatchEvent(evnt);
  70. } else {
  71. elem.fireEvent("on" + evnt.eventType, evnt);
  72. }
  73. }
  74. }
  75. var sendDummyKeydown = false;
  76. if (Object.prototype.toString.call(keyCode) == '[object String]') {
  77. keyCode = keyCode.charCodeAt(0);
  78. sendDummyKeydown = true;
  79. }
  80. switch (keyCode) {
  81. case Inputmask.keyCode.LEFT:
  82. if (modifier == undefined) {
  83. var pos = $.caret(this);
  84. $.caret(this, pos.begin - 1);
  85. break;
  86. }
  87. case Inputmask.keyCode.RIGHT:
  88. if (modifier == undefined) {
  89. var pos = $.caret(this);
  90. $.caret(this, pos.begin + 1);
  91. break;
  92. }
  93. default:
  94. if (window.Inputmask && window.Inputmask.prototype.defaults.inputEventOnly === true) {
  95. var input = new $.Event("input"),
  96. elem = this.nodeName ? this : this[0],
  97. currentValue = elem.inputmask.__valueGet ? elem.inputmask.__valueGet.call(elem) : elem.value,
  98. caretPos = $.caret(elem);
  99. console.log("initial " + currentValue);
  100. console.log(caretPos);
  101. var front = currentValue.substring(0, caretPos.begin),
  102. back = currentValue.substring(caretPos.end),
  103. newValue = currentValue;
  104. switch (keyCode) {
  105. case Inputmask.keyCode.BACKSPACE:
  106. newValue = front.substr(0, front.length - (caretPos.end - caretPos.begin ) - 1) + back;
  107. break;
  108. case Inputmask.keyCode.DELETE :
  109. back = back.substr(1);
  110. newValue = front + back;
  111. break;
  112. default:
  113. newValue = front + String.fromCharCode(keyCode) + back;
  114. }
  115. if (elem.inputmask.__valueSet)
  116. elem.inputmask.__valueSet.call(elem, newValue);
  117. else elem.value = newValue;
  118. $.caret(elem, newValue.length - back.length);
  119. console.log("new " + newValue);
  120. trigger(this.nodeName ? this : this[0], input);
  121. } else {
  122. var keydown = new $.Event("keydown"),
  123. keypress = new $.Event("keypress"),
  124. keyup = new $.Event("keyup");
  125. if (!sendDummyKeydown) {
  126. keydown.keyCode = keyCode;
  127. if (modifier == Inputmask.keyCode.CONTROL)
  128. keydown.ctrlKey = true;
  129. }
  130. trigger(this.nodeName ? this : this[0], keydown);
  131. if (!keydown.defaultPrevented) {
  132. keypress.keyCode = keyCode;
  133. if (modifier == Inputmask.keyCode.CONTROL)
  134. keypress.ctrlKey = true;
  135. trigger(this.nodeName ? this : this[0], keypress);
  136. //if (!keypress.isDefaultPrevented()) {
  137. keyup.keyCode = keyCode;
  138. if (modifier == Inputmask.keyCode.CONTROL)
  139. keyup.ctrlKey = true;
  140. trigger(this.nodeName ? this : this[0], keyup);
  141. //}
  142. }
  143. }
  144. }
  145. }
  146. if (!('append' in $.fn)) {
  147. $.fn.append = function (child) {
  148. var input = this.nodeName ? this : this[0];
  149. input.insertAdjacentHTML('beforeend', child);
  150. };
  151. }
  152. if (!('remove' in $.fn)) {
  153. $.fn.remove = function () {
  154. var input = this.nodeName ? this : this[0];
  155. if (input !== undefined && input !== null) {
  156. input.parentElement.removeChild(input);
  157. input = undefined;
  158. }
  159. };
  160. }
  161. if (!('val' in $.fn)) {
  162. $.fn.val = function (value) {
  163. var input = this.nodeName ? this : this[0];
  164. if (value !== undefined) {
  165. if (input.inputmask) {
  166. input.inputmask._valueSet(value, true);
  167. $(input).trigger("setvalue");
  168. } else input.value = value;
  169. }
  170. return input.value;
  171. };
  172. }
  173. $.fn.Type = function (inputStr) {
  174. var input = this.nodeName ? this : this[0],
  175. $input = $(input);
  176. $.each(inputStr.split(''), function (ndx, lmnt) {
  177. $input.SendKey(lmnt);
  178. });
  179. }
  180. $.fn.paste = function (inputStr) {
  181. var input = this.nodeName ? this : this[0],
  182. $input = $(input);
  183. if (window.clipboardData) {
  184. window.clipboardData.setData('Text', inputStr);
  185. } else {
  186. $.data($input, "clipboard", inputStr);
  187. window.clipboardData = {
  188. getData: function () {
  189. window.clipboardData = undefined;
  190. return $.data($input, "clipboard");
  191. }
  192. }
  193. }
  194. $input.trigger('paste');
  195. }
  196. $.fn.input = function (inputStr, caretBegin, caretEnd) {
  197. var input = this.nodeName ? this : this[0];
  198. input.inputmask.__valueSet.call(input, inputStr);
  199. if (caretBegin !== undefined)
  200. $.caret(input, caretBegin, caretEnd);
  201. $(input).trigger("input");
  202. }
  203. };