inputmask.date.extensions.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*!
  2. * inputmask.date.extensions.js
  3. * https://github.com/RobinHerbots/Inputmask
  4. * Copyright (c) 2010 - 2019 Robin Herbots
  5. * Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
  6. * Version: 4.0.9
  7. */
  8. (function(factory) {
  9. if (typeof define === "function" && define.amd) {
  10. define([ "./inputmask" ], factory);
  11. } else if (typeof exports === "object") {
  12. module.exports = factory(require("./inputmask"));
  13. } else {
  14. factory(window.Inputmask);
  15. }
  16. })(function(Inputmask) {
  17. var $ = Inputmask.dependencyLib;
  18. var formatCode = {
  19. d: [ "[1-9]|[12][0-9]|3[01]", Date.prototype.setDate, "day", Date.prototype.getDate ],
  20. dd: [ "0[1-9]|[12][0-9]|3[01]", Date.prototype.setDate, "day", function() {
  21. return pad(Date.prototype.getDate.call(this), 2);
  22. } ],
  23. ddd: [ "" ],
  24. dddd: [ "" ],
  25. m: [ "[1-9]|1[012]", Date.prototype.setMonth, "month", function() {
  26. return Date.prototype.getMonth.call(this) + 1;
  27. } ],
  28. mm: [ "0[1-9]|1[012]", Date.prototype.setMonth, "month", function() {
  29. return pad(Date.prototype.getMonth.call(this) + 1, 2);
  30. } ],
  31. mmm: [ "" ],
  32. mmmm: [ "" ],
  33. yy: [ "[0-9]{2}", Date.prototype.setFullYear, "year", function() {
  34. return pad(Date.prototype.getFullYear.call(this), 2);
  35. } ],
  36. yyyy: [ "[0-9]{4}", Date.prototype.setFullYear, "year", function() {
  37. return pad(Date.prototype.getFullYear.call(this), 4);
  38. } ],
  39. h: [ "[1-9]|1[0-2]", Date.prototype.setHours, "hours", Date.prototype.getHours ],
  40. hh: [ "0[1-9]|1[0-2]", Date.prototype.setHours, "hours", function() {
  41. return pad(Date.prototype.getHours.call(this), 2);
  42. } ],
  43. hhh: [ "[0-9]+", Date.prototype.setHours, "hours", Date.prototype.getHours ],
  44. H: [ "1?[0-9]|2[0-3]", Date.prototype.setHours, "hours", Date.prototype.getHours ],
  45. HH: [ "0[0-9]|1[0-9]|2[0-3]", Date.prototype.setHours, "hours", function() {
  46. return pad(Date.prototype.getHours.call(this), 2);
  47. } ],
  48. HHH: [ "[0-9]+", Date.prototype.setHours, "hours", Date.prototype.getHours ],
  49. M: [ "[1-5]?[0-9]", Date.prototype.setMinutes, "minutes", Date.prototype.getMinutes ],
  50. MM: [ "0[0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9]", Date.prototype.setMinutes, "minutes", function() {
  51. return pad(Date.prototype.getMinutes.call(this), 2);
  52. } ],
  53. ss: [ "[0-5][0-9]", Date.prototype.setSeconds, "seconds", function() {
  54. return pad(Date.prototype.getSeconds.call(this), 2);
  55. } ],
  56. l: [ "[0-9]{3}", Date.prototype.setMilliseconds, "milliseconds", function() {
  57. return pad(Date.prototype.getMilliseconds.call(this), 3);
  58. } ],
  59. L: [ "[0-9]{2}", Date.prototype.setMilliseconds, "milliseconds", function() {
  60. return pad(Date.prototype.getMilliseconds.call(this), 2);
  61. } ],
  62. t: [ "[ap]" ],
  63. tt: [ "[ap]m" ],
  64. T: [ "[AP]" ],
  65. TT: [ "[AP]M" ],
  66. Z: [ "" ],
  67. o: [ "" ],
  68. S: [ "" ]
  69. }, formatAlias = {
  70. isoDate: "yyyy-mm-dd",
  71. isoTime: "HH:MM:ss",
  72. isoDateTime: "yyyy-mm-dd'T'HH:MM:ss",
  73. isoUtcDateTime: "UTC:yyyy-mm-dd'T'HH:MM:ss'Z'"
  74. };
  75. function getTokenizer(opts) {
  76. if (!opts.tokenizer) {
  77. var tokens = [];
  78. for (var ndx in formatCode) {
  79. if (tokens.indexOf(ndx[0]) === -1) tokens.push(ndx[0]);
  80. }
  81. opts.tokenizer = "(" + tokens.join("+|") + ")+?|.";
  82. opts.tokenizer = new RegExp(opts.tokenizer, "g");
  83. }
  84. return opts.tokenizer;
  85. }
  86. function isValidDate(dateParts, currentResult) {
  87. return !isFinite(dateParts.rawday) || dateParts.day == "29" && !isFinite(dateParts.rawyear) || new Date(dateParts.date.getFullYear(), isFinite(dateParts.rawmonth) ? dateParts.month : dateParts.date.getMonth() + 1, 0).getDate() >= dateParts.day ? currentResult : false;
  88. }
  89. function isDateInRange(dateParts, opts) {
  90. var result = true;
  91. if (opts.min) {
  92. if (dateParts["rawyear"]) {
  93. var rawYear = dateParts["rawyear"].replace(/[^0-9]/g, ""), minYear = opts.min.year.substr(0, rawYear.length);
  94. result = minYear <= rawYear;
  95. }
  96. if (dateParts["year"] === dateParts["rawyear"]) {
  97. if (opts.min.date.getTime() === opts.min.date.getTime()) {
  98. result = opts.min.date.getTime() <= dateParts.date.getTime();
  99. }
  100. }
  101. }
  102. if (result && opts.max && opts.max.date.getTime() === opts.max.date.getTime()) {
  103. result = opts.max.date.getTime() >= dateParts.date.getTime();
  104. }
  105. return result;
  106. }
  107. function parse(format, dateObjValue, opts, raw) {
  108. var mask = "", match;
  109. while (match = getTokenizer(opts).exec(format)) {
  110. if (dateObjValue === undefined) {
  111. if (formatCode[match[0]]) {
  112. mask += "(" + formatCode[match[0]][0] + ")";
  113. } else {
  114. switch (match[0]) {
  115. case "[":
  116. mask += "(";
  117. break;
  118. case "]":
  119. mask += ")?";
  120. break;
  121. default:
  122. mask += Inputmask.escapeRegex(match[0]);
  123. }
  124. }
  125. } else {
  126. if (formatCode[match[0]]) {
  127. if (raw !== true && formatCode[match[0]][3]) {
  128. var getFn = formatCode[match[0]][3];
  129. mask += getFn.call(dateObjValue.date);
  130. } else if (formatCode[match[0]][2]) mask += dateObjValue["raw" + formatCode[match[0]][2]]; else mask += match[0];
  131. } else mask += match[0];
  132. }
  133. }
  134. return mask;
  135. }
  136. function pad(val, len) {
  137. val = String(val);
  138. len = len || 2;
  139. while (val.length < len) val = "0" + val;
  140. return val;
  141. }
  142. function analyseMask(maskString, format, opts) {
  143. var dateObj = {
  144. date: new Date(1, 0, 1)
  145. }, targetProp, mask = maskString, match, dateOperation, targetValidator;
  146. function extendProperty(value) {
  147. var correctedValue = value.replace(/[^0-9]/g, "0");
  148. if (correctedValue != value) {
  149. var enteredPart = value.replace(/[^0-9]/g, ""), min = (opts.min && opts.min[targetProp] || value).toString(), max = (opts.max && opts.max[targetProp] || value).toString();
  150. correctedValue = enteredPart + (enteredPart < min.slice(0, enteredPart.length) ? min.slice(enteredPart.length) : enteredPart > max.slice(0, enteredPart.length) ? max.slice(enteredPart.length) : correctedValue.toString().slice(enteredPart.length));
  151. }
  152. return correctedValue;
  153. }
  154. function setValue(dateObj, value, opts) {
  155. dateObj[targetProp] = extendProperty(value);
  156. dateObj["raw" + targetProp] = value;
  157. if (dateOperation !== undefined) dateOperation.call(dateObj.date, targetProp == "month" ? parseInt(dateObj[targetProp]) - 1 : dateObj[targetProp]);
  158. }
  159. if (typeof mask === "string") {
  160. while (match = getTokenizer(opts).exec(format)) {
  161. var value = mask.slice(0, match[0].length);
  162. if (formatCode.hasOwnProperty(match[0])) {
  163. targetValidator = formatCode[match[0]][0];
  164. targetProp = formatCode[match[0]][2];
  165. dateOperation = formatCode[match[0]][1];
  166. setValue(dateObj, value, opts);
  167. }
  168. mask = mask.slice(value.length);
  169. }
  170. return dateObj;
  171. } else if (mask && typeof mask === "object" && mask.hasOwnProperty("date")) {
  172. return mask;
  173. }
  174. return undefined;
  175. }
  176. Inputmask.extendAliases({
  177. datetime: {
  178. mask: function(opts) {
  179. formatCode.S = opts.i18n.ordinalSuffix.join("|");
  180. opts.inputFormat = formatAlias[opts.inputFormat] || opts.inputFormat;
  181. opts.displayFormat = formatAlias[opts.displayFormat] || opts.displayFormat || opts.inputFormat;
  182. opts.outputFormat = formatAlias[opts.outputFormat] || opts.outputFormat || opts.inputFormat;
  183. opts.placeholder = opts.placeholder !== "" ? opts.placeholder : opts.inputFormat.replace(/[\[\]]/, "");
  184. opts.regex = parse(opts.inputFormat, undefined, opts);
  185. return null;
  186. },
  187. placeholder: "",
  188. inputFormat: "isoDateTime",
  189. displayFormat: undefined,
  190. outputFormat: undefined,
  191. min: null,
  192. max: null,
  193. i18n: {
  194. dayNames: [ "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" ],
  195. monthNames: [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ],
  196. ordinalSuffix: [ "st", "nd", "rd", "th" ]
  197. },
  198. postValidation: function(buffer, pos, currentResult, opts) {
  199. opts.min = analyseMask(opts.min, opts.inputFormat, opts);
  200. opts.max = analyseMask(opts.max, opts.inputFormat, opts);
  201. var result = currentResult, dateParts = analyseMask(buffer.join(""), opts.inputFormat, opts);
  202. if (result && dateParts.date.getTime() === dateParts.date.getTime()) {
  203. result = isValidDate(dateParts, result);
  204. result = result && isDateInRange(dateParts, opts);
  205. }
  206. if (pos && result && currentResult.pos !== pos) {
  207. return {
  208. buffer: parse(opts.inputFormat, dateParts, opts),
  209. refreshFromBuffer: {
  210. start: pos,
  211. end: currentResult.pos
  212. }
  213. };
  214. }
  215. return result;
  216. },
  217. onKeyDown: function(e, buffer, caretPos, opts) {
  218. var input = this;
  219. if (e.ctrlKey && e.keyCode === Inputmask.keyCode.RIGHT) {
  220. var today = new Date(), match, date = "";
  221. while (match = getTokenizer(opts).exec(opts.inputFormat)) {
  222. if (match[0].charAt(0) === "d") {
  223. date += pad(today.getDate(), match[0].length);
  224. } else if (match[0].charAt(0) === "m") {
  225. date += pad(today.getMonth() + 1, match[0].length);
  226. } else if (match[0] === "yyyy") {
  227. date += today.getFullYear().toString();
  228. } else if (match[0].charAt(0) === "y") {
  229. date += pad(today.getYear(), match[0].length);
  230. }
  231. }
  232. input.inputmask._valueSet(date);
  233. $(input).trigger("setvalue");
  234. }
  235. },
  236. onUnMask: function(maskedValue, unmaskedValue, opts) {
  237. return parse(opts.outputFormat, analyseMask(maskedValue, opts.inputFormat, opts), opts, true);
  238. },
  239. casing: function(elem, test, pos, validPositions) {
  240. if (test.nativeDef.indexOf("[ap]") == 0) return elem.toLowerCase();
  241. if (test.nativeDef.indexOf("[AP]") == 0) return elem.toUpperCase();
  242. return elem;
  243. },
  244. insertMode: false,
  245. shiftPositions: false
  246. }
  247. });
  248. return Inputmask;
  249. });