colormask.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. Input Mask colormask extension
  3. http://github.com/RobinHerbots/inputmask
  4. Copyright (c) Robin Herbots
  5. Licensed under the MIT license
  6. */
  7. import { EventHandlers } from "../eventhandlers";
  8. import Inputmask from "../inputmask";
  9. import { keys } from "../keycode.js";
  10. import {
  11. caret,
  12. getBuffer,
  13. getLastValidPosition,
  14. translatePosition
  15. } from "../positioning";
  16. import { getPlaceholder, getTestTemplate } from "../validation-tests";
  17. const $ = Inputmask.dependencyLib;
  18. function Colormask(alias, options, internal) {
  19. // allow instanciating without new
  20. if (!(this instanceof Inputmask)) {
  21. return new Inputmask(alias, options, internal);
  22. }
  23. this.colorMask = undefined;
  24. Object.getOwnPropertyNames(Inputmask).forEach(function (key) {
  25. if (!Object.prototype.hasOwnProperty.call(this, key)) {
  26. this[key] = Inputmask[key];
  27. }
  28. }, this);
  29. }
  30. Colormask.prototype = Inputmask.prototype;
  31. Colormask.prototype.writeBufferHook = function (caretPos) {
  32. renderColorMask.call(this, this.el, caretPos, false);
  33. };
  34. Colormask.prototype.caretHook = function (caretPos) {
  35. renderColorMask.call(this, this.el, caretPos, false);
  36. };
  37. Colormask.prototype.applyMaskHook = function () {
  38. initializeColorMask.call(this, this.el);
  39. };
  40. Colormask.prototype.keyEventHook = function (e) {
  41. if (e.key === keys.ArrowRight || e.key === keys.ArrowLeft) {
  42. const inputmask = this;
  43. setTimeout(function () {
  44. const caretPos = caret.call(
  45. inputmask,
  46. inputmask.el,
  47. undefined,
  48. undefined,
  49. true
  50. );
  51. renderColorMask.call(inputmask, inputmask.el, caretPos);
  52. }, 0);
  53. }
  54. };
  55. function initializeColorMask(input) {
  56. const computedStyle = (
  57. input.ownerDocument.defaultView || window
  58. ).getComputedStyle(input, null);
  59. function findCaretPos(clientx) {
  60. // calculate text width
  61. let e = document.createElement("span"),
  62. caretPos = 0;
  63. for (const style in computedStyle) {
  64. // clone styles
  65. if (isNaN(style) && style.indexOf("font") !== -1) {
  66. e.style[style] = computedStyle[style];
  67. }
  68. }
  69. e.style.textTransform = computedStyle.textTransform;
  70. e.style.letterSpacing = computedStyle.letterSpacing;
  71. e.style.position = "absolute";
  72. e.style.height = "auto";
  73. e.style.width = "auto";
  74. e.style.visibility = "hidden";
  75. e.style.whiteSpace = "nowrap";
  76. document.body.appendChild(e);
  77. let inputText = input.inputmask.__valueGet.call(input),
  78. previousWidth = 0;
  79. while (e.offsetWidth < clientx) {
  80. const ichar = inputText.charAt(caretPos);
  81. e.innerHTML += ichar === " " || ichar === "" ? "_" : ichar;
  82. if (e.offsetWidth >= clientx) {
  83. let offset1 = clientx - previousWidth,
  84. offset2 = e.offsetWidth - clientx;
  85. e.innerHTML = inputText.charAt(caretPos);
  86. offset1 += Math.round(e.offsetWidth / 2);
  87. caretPos = (offset1 < offset2 ? caretPos - 1 : caretPos) - 1;
  88. break;
  89. }
  90. previousWidth = e.offsetWidth;
  91. caretPos++;
  92. }
  93. if (input.style.textAlign === "right") {
  94. e.innerHTML = "_";
  95. const maxChars = Math.ceil(input.offsetWidth / e.offsetWidth) - 1;
  96. caretPos = inputText.length - (maxChars - caretPos) + 1;
  97. }
  98. document.body.removeChild(e);
  99. return caretPos;
  100. }
  101. const template = document.createElement("div");
  102. template.style.width = computedStyle.width;
  103. template.style.textAlign = computedStyle.textAlign;
  104. const colorMask = document.createElement("div");
  105. input.inputmask.colorMask = colorMask;
  106. colorMask.className = "im-colormask";
  107. input.parentNode.insertBefore(colorMask, input);
  108. input.parentNode.removeChild(input);
  109. colorMask.appendChild(input);
  110. colorMask.appendChild(template);
  111. input.style.left = template.offsetLeft + "px";
  112. $(colorMask).on("mouseleave", function (e) {
  113. return EventHandlers.mouseleaveEvent.call(input, [e]);
  114. });
  115. $(colorMask).on("mouseenter", function (e) {
  116. return EventHandlers.mouseenterEvent.call(input, [e]);
  117. });
  118. $(colorMask).on("click", function (e) {
  119. caret.call(
  120. input.inputmask,
  121. input,
  122. findCaretPos(e.clientX),
  123. undefined,
  124. true
  125. );
  126. return EventHandlers.clickEvent.call(input, [e]);
  127. });
  128. }
  129. function positionColorMask(input, template) {
  130. input.style.left = template.offsetLeft + "px";
  131. }
  132. export function renderColorMask(input, caretPos, clear) {
  133. let inputmask = this,
  134. { isRTL, maskset, opts, maxLength } = inputmask,
  135. maskTemplate = [],
  136. isStatic = false,
  137. test,
  138. testPos,
  139. ndxIntlzr,
  140. pos = 0,
  141. templates = {
  142. static: {
  143. start: isRTL ? "</span>" : "<span class='im-static'>",
  144. end: isRTL ? "<span class='im-static'>" : "</span>"
  145. },
  146. caret: {
  147. start:
  148. '<mark class="im-caret" style="border-right-width: 1px;border-right-style: solid;">',
  149. start_select: '<mark class="im-caret-select">',
  150. end: "</mark>"
  151. }
  152. };
  153. function setEntry(entry) {
  154. if (entry === undefined) entry = "";
  155. if (!isStatic && (test.static === true || testPos.input === undefined)) {
  156. isStatic = true;
  157. maskTemplate.push(templates.static.start + entry);
  158. } else if (
  159. isStatic &&
  160. ((test.static !== true && testPos.input !== undefined) || test.def === "")
  161. ) {
  162. isStatic = false;
  163. maskTemplate.push(templates.static.end + entry);
  164. } else {
  165. maskTemplate.push(entry);
  166. }
  167. }
  168. function setCaret(begin, end, length) {
  169. if (document.activeElement === input) {
  170. maskTemplate.splice(
  171. begin,
  172. 0,
  173. begin === end || length > maskset.maskLength
  174. ? templates.caret.start
  175. : templates.caret.start_select
  176. );
  177. maskTemplate.splice(end + (isRTL ? 0 : 1), 0, templates.caret.end);
  178. }
  179. }
  180. if (input.inputmask.colorMask !== undefined) {
  181. const buffer = getBuffer.call(inputmask);
  182. if (caretPos === undefined) {
  183. caretPos = caret.call(inputmask, input);
  184. } else if (caretPos.begin === undefined) {
  185. caretPos = {
  186. begin: caretPos,
  187. end: caretPos
  188. };
  189. }
  190. if (isRTL) {
  191. // translate caretPos
  192. caretPos.begin = translatePosition.call(inputmask, caretPos.begin);
  193. caretPos.end = translatePosition.call(inputmask, caretPos.end);
  194. }
  195. if (clear !== true) {
  196. const lvp = getLastValidPosition.call(inputmask);
  197. do {
  198. if (maskset.validPositions[pos]) {
  199. testPos = maskset.validPositions[pos];
  200. test = testPos.match;
  201. ndxIntlzr = testPos.locator.slice();
  202. setEntry(buffer[pos]);
  203. } else {
  204. testPos = getTestTemplate.call(inputmask, pos, ndxIntlzr, pos - 1);
  205. test = testPos.match;
  206. ndxIntlzr = testPos.locator.slice();
  207. const jitMasking =
  208. opts.jitMasking !== false ? opts.jitMasking : test.jit;
  209. if (
  210. jitMasking === false ||
  211. jitMasking === undefined /* || pos < lvp */ ||
  212. (typeof jitMasking === "number" &&
  213. isFinite(jitMasking) &&
  214. jitMasking > pos)
  215. ) {
  216. setEntry(getPlaceholder.call(inputmask, pos, test));
  217. // } else {
  218. // isStatic = false;
  219. } // break infinite loop
  220. }
  221. pos++;
  222. } while (
  223. ((maxLength === undefined || pos < maxLength) &&
  224. (test.static !== true || test.def !== "")) ||
  225. lvp > pos ||
  226. isStatic
  227. );
  228. if (isStatic) setEntry();
  229. setCaret(
  230. isRTL ? caretPos.end : caretPos.begin,
  231. isRTL ? caretPos.begin : caretPos.end,
  232. caretPos.end
  233. );
  234. }
  235. const template = input.inputmask.colorMask.getElementsByTagName("div")[0];
  236. template.innerHTML = (isRTL ? maskTemplate.reverse() : maskTemplate).join(
  237. ""
  238. );
  239. positionColorMask(input, template);
  240. // console.log(template.innerHTML)
  241. // console.log(JSON.stringify(caretPos));
  242. }
  243. }
  244. // make inputmask available
  245. window.Colormask = Colormask;
  246. export default Colormask;