simulator.js 7.7 KB

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