inputHandling.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. import { ie } from "./environment";
  2. import { EventHandlers } from "./eventhandlers";
  3. import Inputmask from "./inputmask.js";
  4. import { keys } from "./keycode.js";
  5. import {
  6. caret,
  7. determineNewCaretPosition,
  8. getBuffer,
  9. getBufferTemplate,
  10. getLastValidPosition,
  11. isMask,
  12. resetMaskSet,
  13. seekNext
  14. } from "./positioning";
  15. import { isComplete, refreshFromBuffer } from "./validation";
  16. import { getMaskTemplate, getPlaceholder, getTest } from "./validation-tests";
  17. export {
  18. applyInputValue,
  19. clearOptionalTail,
  20. checkVal,
  21. HandleNativePlaceholder,
  22. unmaskedvalue,
  23. writeBuffer
  24. };
  25. function applyInputValue(input, value, initialEvent, strict) {
  26. const inputmask = input ? input.inputmask : this,
  27. opts = inputmask.opts;
  28. input.inputmask.refreshValue = false;
  29. if (strict !== true && typeof opts.onBeforeMask === "function")
  30. value = opts.onBeforeMask.call(inputmask, value, opts) || value;
  31. value = (value || "").toString().split("");
  32. checkVal(input, true, false, value, initialEvent);
  33. inputmask.undoValue = inputmask._valueGet(true);
  34. if (
  35. (opts.clearMaskOnLostFocus || opts.clearIncomplete) &&
  36. input.inputmask._valueGet() ===
  37. getBufferTemplate.call(inputmask).join("") &&
  38. getLastValidPosition.call(inputmask) === -1
  39. ) {
  40. input.inputmask._valueSet("");
  41. }
  42. }
  43. // todo put on prototype?
  44. function clearOptionalTail(buffer) {
  45. const inputmask = this;
  46. buffer.length = 0;
  47. let template = getMaskTemplate.call(
  48. inputmask,
  49. true,
  50. 0,
  51. true,
  52. undefined,
  53. true
  54. ),
  55. lmnt;
  56. while ((lmnt = template.shift()) !== undefined) buffer.push(lmnt);
  57. return buffer;
  58. }
  59. function checkVal(input, writeOut, strict, nptvl, initiatingEvent) {
  60. const inputmask = input ? input.inputmask : this,
  61. maskset = inputmask.maskset,
  62. opts = inputmask.opts,
  63. $ = inputmask.dependencyLib;
  64. let inputValue = nptvl.slice(),
  65. charCodes = "",
  66. initialNdx = -1,
  67. result,
  68. skipOptionalPartCharacter = opts.skipOptionalPartCharacter;
  69. opts.skipOptionalPartCharacter = ""; // see issue #2311
  70. function isTemplateMatch(ndx, charCodes) {
  71. let targetTemplate = getMaskTemplate
  72. .call(inputmask, true, 0)
  73. .slice(ndx, seekNext.call(inputmask, ndx, false, false))
  74. .join("")
  75. .replace(/'/g, ""),
  76. charCodeNdx = targetTemplate.indexOf(charCodes);
  77. // strip spaces from targetTemplate
  78. while (charCodeNdx > 0 && targetTemplate[charCodeNdx - 1] === " ")
  79. charCodeNdx--;
  80. const match =
  81. charCodeNdx === 0 &&
  82. !isMask.call(inputmask, ndx) &&
  83. (getTest.call(inputmask, ndx).match.nativeDef === charCodes.charAt(0) ||
  84. (getTest.call(inputmask, ndx).match.static === true &&
  85. getTest.call(inputmask, ndx).match.nativeDef ===
  86. "'" + charCodes.charAt(0)) ||
  87. (getTest.call(inputmask, ndx).match.nativeDef === " " &&
  88. (getTest.call(inputmask, ndx + 1).match.nativeDef ===
  89. charCodes.charAt(0) ||
  90. (getTest.call(inputmask, ndx + 1).match.static === true &&
  91. getTest.call(inputmask, ndx + 1).match.nativeDef ===
  92. "'" + charCodes.charAt(0)))));
  93. if (
  94. !match &&
  95. charCodeNdx > 0 &&
  96. !isMask.call(inputmask, ndx, false, true)
  97. ) {
  98. const nextPos = seekNext.call(inputmask, ndx);
  99. if (inputmask.caretPos.begin < nextPos) {
  100. inputmask.caretPos = { begin: nextPos };
  101. }
  102. }
  103. return match;
  104. }
  105. resetMaskSet.call(inputmask, false);
  106. inputmask.clicked = 0; // reset click counter to correctly determine the caretposition in checkval
  107. initialNdx = opts.radixPoint
  108. ? determineNewCaretPosition.call(
  109. inputmask,
  110. {
  111. begin: 0,
  112. end: 0
  113. },
  114. false,
  115. opts.__financeInput === false ? "radixFocus" : undefined
  116. ).begin
  117. : 0;
  118. maskset.p = initialNdx;
  119. inputmask.caretPos = { begin: initialNdx };
  120. let staticMatches = [],
  121. prevCaretPos = inputmask.caretPos;
  122. inputValue.forEach(function (charCode, ndx) {
  123. if (charCode !== undefined) {
  124. // inputfallback strips some elements out of the inputarray. $.each logically presents them as undefined
  125. /* if (maskset.validPositions[ndx] === undefined && inputValue[ndx] === getPlaceholder.call(inputmask, ndx) && isMask.call(inputmask, ndx, true) &&
  126. isValid.call(inputmask, ndx, inputValue[ndx], true, undefined, true, true) === false) {
  127. inputmask.caretPos.begin++;
  128. } else */
  129. // console.log("caret " + inputmask.caretPos.begin);
  130. const keypress = new $.Event("_checkval");
  131. keypress.key = charCode;
  132. charCodes += charCode;
  133. const lvp = getLastValidPosition.call(inputmask, undefined, true);
  134. if (!isTemplateMatch(initialNdx, charCodes)) {
  135. result = EventHandlers.keypressEvent.call(
  136. inputmask,
  137. keypress,
  138. true,
  139. false,
  140. strict,
  141. inputmask.caretPos.begin
  142. );
  143. if (result) {
  144. initialNdx = inputmask.caretPos.begin + 1;
  145. charCodes = "";
  146. }
  147. } else {
  148. result =
  149. getTest.call(inputmask, ndx).match.static === true
  150. ? EventHandlers.keypressEvent.call(
  151. inputmask,
  152. keypress,
  153. true,
  154. false,
  155. strict,
  156. lvp + 1
  157. )
  158. : false;
  159. }
  160. if (result) {
  161. if (
  162. result.pos !== undefined &&
  163. maskset.validPositions[result.pos] &&
  164. maskset.validPositions[result.pos].match.static === true &&
  165. maskset.validPositions[result.pos].alternation === undefined
  166. ) {
  167. staticMatches.push(result.pos);
  168. if (!inputmask.isRTL) {
  169. result.forwardPosition = result.pos + 1;
  170. }
  171. }
  172. writeBuffer.call(
  173. inputmask,
  174. undefined,
  175. getBuffer.call(inputmask),
  176. result.forwardPosition,
  177. keypress,
  178. false
  179. );
  180. inputmask.caretPos = {
  181. begin: result.forwardPosition,
  182. end: result.forwardPosition
  183. };
  184. prevCaretPos = inputmask.caretPos;
  185. } else {
  186. if (
  187. maskset.validPositions[ndx] === undefined &&
  188. inputValue[ndx] === getPlaceholder.call(inputmask, ndx) &&
  189. isMask.call(inputmask, ndx, true)
  190. ) {
  191. inputmask.caretPos.begin++;
  192. } else inputmask.caretPos = prevCaretPos; // restore the caret position from before the failed validation
  193. }
  194. }
  195. });
  196. if (staticMatches.length > 0) {
  197. let sndx,
  198. validPos,
  199. nextValid = seekNext.call(inputmask, -1, undefined, false);
  200. if (
  201. (!isComplete.call(inputmask, getBuffer.call(inputmask)) &&
  202. staticMatches.length <= nextValid) ||
  203. (isComplete.call(inputmask, getBuffer.call(inputmask)) &&
  204. staticMatches.length > 0 &&
  205. staticMatches.length !== nextValid &&
  206. staticMatches[0] === 0)
  207. ) {
  208. // should check if is sequence starting from 0
  209. let nextSndx = nextValid;
  210. while ((sndx = staticMatches.shift()) !== undefined) {
  211. if (sndx < nextSndx) {
  212. const keypress = new $.Event("_checkval");
  213. validPos = maskset.validPositions[sndx];
  214. validPos.generatedInput = true;
  215. keypress.key = validPos.input;
  216. result = EventHandlers.keypressEvent.call(
  217. inputmask,
  218. keypress,
  219. true,
  220. false,
  221. strict,
  222. nextSndx
  223. );
  224. if (
  225. result &&
  226. result.pos !== undefined &&
  227. result.pos !== sndx &&
  228. maskset.validPositions[result.pos] &&
  229. maskset.validPositions[result.pos].match.static === true
  230. ) {
  231. staticMatches.push(result.pos);
  232. } else if (!result) break;
  233. nextSndx++;
  234. }
  235. }
  236. } else {
  237. // delete all free statics
  238. while ((sndx = staticMatches.pop())) {
  239. validPos = maskset.validPositions[sndx];
  240. if (validPos && maskset.validPositions[sndx + 1] === undefined) {
  241. delete maskset.validPositions[sndx];
  242. }
  243. }
  244. }
  245. }
  246. if (writeOut) {
  247. writeBuffer.call(
  248. inputmask,
  249. input,
  250. getBuffer.call(inputmask),
  251. result ? result.forwardPosition : inputmask.caretPos.begin,
  252. initiatingEvent || new $.Event("checkval"),
  253. initiatingEvent &&
  254. ((initiatingEvent.type === "input" &&
  255. inputmask.undoValue !== getBuffer.call(inputmask).join("")) ||
  256. initiatingEvent.type === "paste")
  257. );
  258. // for (var vndx in maskset.validPositions) {
  259. // if (maskset.validPositions[vndx].match.generated !== true) { //only remove non forced generated
  260. // delete maskset.validPositions[vndx].generatedInput; //clear generated markings ~ consider initializing with a value as fully typed
  261. // }
  262. // }
  263. }
  264. opts.skipOptionalPartCharacter = skipOptionalPartCharacter;
  265. }
  266. function HandleNativePlaceholder(npt, value) {
  267. const inputmask = npt ? npt.inputmask : this;
  268. if (ie) {
  269. if (
  270. npt.inputmask._valueGet() !== value &&
  271. (npt.placeholder !== value || npt.placeholder === "")
  272. ) {
  273. let buffer = getBuffer.call(inputmask).slice(),
  274. nptValue = npt.inputmask._valueGet();
  275. if (nptValue !== value) {
  276. const lvp = getLastValidPosition.call(inputmask);
  277. if (
  278. lvp === -1 &&
  279. nptValue === getBufferTemplate.call(inputmask).join("")
  280. ) {
  281. buffer = [];
  282. } else if (lvp !== -1) {
  283. // clearout optional tail of the mask
  284. clearOptionalTail.call(inputmask, buffer);
  285. }
  286. writeBuffer(npt, buffer);
  287. }
  288. }
  289. } else if (npt.placeholder !== value) {
  290. npt.placeholder = value;
  291. if (npt.placeholder === "") npt.removeAttribute("placeholder");
  292. }
  293. }
  294. function unmaskedvalue(input) {
  295. const inputmask = input ? input.inputmask : this,
  296. opts = inputmask.opts,
  297. maskset = inputmask.maskset;
  298. if (input) {
  299. if (input.inputmask === undefined) {
  300. return input.value;
  301. }
  302. if (input.inputmask && input.inputmask.refreshValue) {
  303. // forced refresh from the value form.reset
  304. applyInputValue(input, input.inputmask._valueGet(true));
  305. }
  306. }
  307. const umValue = [],
  308. vps = maskset.validPositions;
  309. for (let pndx = 0, vpl = vps.length; pndx < vpl; pndx++) {
  310. if (
  311. vps[pndx] &&
  312. vps[pndx].match &&
  313. (vps[pndx].match.static != true ||
  314. (opts.keepStatic !== true &&
  315. Array.isArray(maskset.metadata) &&
  316. vps[pndx].generatedInput !== true))
  317. ) {
  318. // only include non generated input with multiple masks (check on metadata) and without keepStatic true
  319. umValue.push(vps[pndx].input);
  320. }
  321. }
  322. let unmaskedValue =
  323. umValue.length === 0
  324. ? ""
  325. : (inputmask.isRTL ? umValue.reverse() : umValue).join("");
  326. if (typeof opts.onUnMask === "function") {
  327. const bufferValue = (
  328. inputmask.isRTL
  329. ? getBuffer.call(inputmask).slice().reverse()
  330. : getBuffer.call(inputmask)
  331. ).join("");
  332. unmaskedValue = opts.onUnMask.call(
  333. inputmask,
  334. bufferValue,
  335. unmaskedValue,
  336. opts
  337. );
  338. }
  339. if (opts.outputMask && unmaskedValue.length > 0) {
  340. return Inputmask.format(unmaskedValue, {
  341. ...opts,
  342. mask: opts.outputMask,
  343. alias: null
  344. });
  345. }
  346. return unmaskedValue;
  347. }
  348. function writeBuffer(input, buffer, caretPos, event, triggerEvents) {
  349. const inputmask = input ? input.inputmask : this,
  350. opts = inputmask.opts,
  351. $ = inputmask.dependencyLib;
  352. if (event && typeof opts.onBeforeWrite === "function") {
  353. // buffer = buffer.slice(); //prevent uncontrolled manipulation of the internal buffer
  354. const result = opts.onBeforeWrite.call(
  355. inputmask,
  356. event,
  357. buffer,
  358. caretPos,
  359. opts
  360. );
  361. if (result) {
  362. if (result.refreshFromBuffer) {
  363. const refresh = result.refreshFromBuffer;
  364. refreshFromBuffer.call(
  365. inputmask,
  366. refresh === true ? refresh : refresh.start,
  367. refresh.end,
  368. result.buffer || buffer
  369. );
  370. buffer = getBuffer.call(inputmask, true);
  371. }
  372. if (caretPos !== undefined)
  373. caretPos = result.caret !== undefined ? result.caret : caretPos;
  374. }
  375. }
  376. if (input !== undefined) {
  377. input.inputmask._valueSet(buffer.join(""));
  378. if (
  379. caretPos !== undefined &&
  380. (event === undefined || event.type !== "blur")
  381. ) {
  382. // console.log(caretPos);
  383. caret.call(
  384. inputmask,
  385. input,
  386. caretPos,
  387. undefined,
  388. undefined,
  389. event !== undefined &&
  390. event.type === "keydown" &&
  391. (event.key === keys.Delete || event.key === keys.Backspace)
  392. );
  393. }
  394. input.inputmask.writeBufferHook === undefined ||
  395. input.inputmask.writeBufferHook(caretPos);
  396. if (triggerEvents === true) {
  397. const $input = $(input),
  398. nptVal = input.inputmask._valueGet();
  399. input.inputmask.skipInputEvent = true;
  400. $input.trigger("input");
  401. setTimeout(function () {
  402. // timeout needed for IE
  403. if (nptVal === getBufferTemplate.call(inputmask).join("")) {
  404. $input.trigger("cleared");
  405. } else if (isComplete.call(inputmask, buffer) === true) {
  406. $input.trigger("complete");
  407. }
  408. }, 0);
  409. }
  410. }
  411. }