inputmask.date.extensions.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*!
  2. * inputmask.date.extensions.js
  3. * https://github.com/RobinHerbots/Inputmask
  4. * Copyright (c) 2010 - 2018 Robin Herbots
  5. * Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
  6. * Version: 4.0.0-91
  7. */
  8. !function(factory) {
  9. "function" == typeof define && define.amd ? define([ "./dependencyLibs/inputmask.dependencyLib", "./inputmask" ], factory) : "object" == typeof exports ? module.exports = factory(require("./dependencyLibs/inputmask.dependencyLib"), require("./inputmask")) : factory(window.dependencyLib || jQuery, window.Inputmask);
  10. }(function($, Inputmask) {
  11. function getTokenizer(opts) {
  12. if (!opts.tokenizer) {
  13. var tokens = [];
  14. for (var ndx in formatCode) -1 === tokens.indexOf(ndx[0]) && tokens.push(ndx[0]);
  15. opts.tokenizer = "(" + tokens.join("+|") + ")+?|.", opts.tokenizer = new RegExp(opts.tokenizer, "g");
  16. }
  17. return opts.tokenizer;
  18. }
  19. function isValidDate(dateParts, currentResult) {
  20. return (!isFinite(dateParts.day) || "29" == dateParts.day && !isFinite(dateParts.rawyear) || new Date(dateParts.date.getFullYear(), isFinite(dateParts.month) ? dateParts.month : dateParts.date.getMonth() + 1, 0).getDate() >= dateParts.day) && currentResult;
  21. }
  22. function isDateInRange(maskDate, opts) {
  23. var result = !0;
  24. return opts.min && opts.min.date.getTime() === opts.min.date.getTime() && (result = result && opts.min.date.getTime() <= maskDate.getTime()),
  25. opts.max && opts.max.date.getTime() === opts.max.date.getTime() && (result = result && opts.max.date.getTime() >= maskDate.getTime()),
  26. result;
  27. }
  28. function parse(format, dateObjValue, opts) {
  29. for (var match, mask = ""; match = getTokenizer(opts).exec(format); ) void 0 === dateObjValue ? mask += formatCode[match[0]] ? "(" + formatCode[match[0]][0] + ")" : match[0] : formatCode[match[0]] ? mask += formatCode[match[0]][3].call(dateObjValue.date) : mask += match[0];
  30. return mask;
  31. }
  32. function pad(val, len) {
  33. for (val = String(val), len = len || 2; val.length < len; ) val = "0" + val;
  34. return val;
  35. }
  36. function analyseMask(maskString, format, opts) {
  37. function extendYear(year) {
  38. var correctedyear = 4 === year.length ? year : new Date().getFullYear().toString().substr(0, 4 - year.length) + year;
  39. return opts.min && opts.min.year && opts.max && opts.max.year ? (correctedyear = correctedyear.replace(/[^0-9]/g, ""),
  40. correctedyear = year.charAt(0) === opts.max.year.charAt(0) ? year.replace(/[^0-9]/g, "0") : correctedyear + opts.min.year.substr(correctedyear.length)) : correctedyear = correctedyear.replace(/[^0-9]/g, "0"),
  41. correctedyear;
  42. }
  43. var targetProp, match, dateOperation, dateObj = {
  44. date: new Date(1, 0, 1)
  45. }, mask = maskString;
  46. if ("string" == typeof mask) {
  47. for (;match = getTokenizer(opts).exec(format); ) {
  48. var value = mask.slice(0, match[0].length);
  49. formatCode.hasOwnProperty(match[0]) && (targetProp = formatCode[match[0]][2], dateOperation = formatCode[match[0]][1],
  50. function(dateObj, value, opts) {
  51. "year" === targetProp ? (dateObj[targetProp] = extendYear(value), dateObj["raw" + targetProp] = value) : dateObj[targetProp] = opts.min && value.match(/[^0-9]/) ? opts.min[targetProp] : value,
  52. void 0 !== dateOperation && dateOperation.call(dateObj.date, "month" == targetProp ? parseInt(dateObj[targetProp]) - 1 : dateObj[targetProp]);
  53. }(dateObj, value, opts)), mask = mask.slice(value.length);
  54. }
  55. return dateObj;
  56. }
  57. }
  58. var formatCode = {
  59. d: [ "[1-9]|[12][0-9]|3[01]", Date.prototype.setDate, "day", Date.prototype.getDate ],
  60. dd: [ "0[1-9]|[12][0-9]|3[01]", Date.prototype.setDate, "day", function() {
  61. return pad(Date.prototype.getDate.call(this), 2);
  62. } ],
  63. ddd: [ "" ],
  64. dddd: [ "" ],
  65. m: [ "[1-9]|1[012]", Date.prototype.setMonth, "month", function() {
  66. return Date.prototype.getMonth.call(this) + 1;
  67. } ],
  68. mm: [ "0[1-9]|1[012]", Date.prototype.setMonth, "month", function() {
  69. return pad(Date.prototype.getMonth.call(this) + 1, 2);
  70. } ],
  71. mmm: [ "" ],
  72. mmmm: [ "" ],
  73. yy: [ "[0-9]{2}", Date.prototype.setFullYear, "year", function() {
  74. return pad(Date.prototype.getFullYear.call(this), 2);
  75. } ],
  76. yyyy: [ "[0-9]{4}", Date.prototype.setFullYear, "year", function() {
  77. return pad(Date.prototype.getFullYear.call(this), 4);
  78. } ],
  79. h: [ "[1-9]|1[0-2]", Date.prototype.setHours, "hours", Date.prototype.getHours ],
  80. hh: [ "0[1-9]|1[0-2]", Date.prototype.setHours, "hours", function() {
  81. return pad(Date.prototype.getHours.call(this), 2);
  82. } ],
  83. hhh: [ "[0-9]+", Date.prototype.setHours, "hours", Date.prototype.getHours ],
  84. H: [ "1?[0-9]|2[0-3]", Date.prototype.setHours, "hours", Date.prototype.getHours ],
  85. HH: [ "[01][0-9]|2[0-3]", Date.prototype.setHours, "hours", function() {
  86. return pad(Date.prototype.getHours.call(this), 2);
  87. } ],
  88. HHH: [ "[0-9]+", Date.prototype.setHours, "hours", Date.prototype.getHours ],
  89. M: [ "[1-5]?[0-9]", Date.prototype.setMinutes, "minutes", Date.prototype.getMinutes ],
  90. MM: [ "[0-5][0-9]", Date.prototype.setMinutes, "minutes", function() {
  91. return pad(Date.prototype.getMinutes.call(this), 2);
  92. } ],
  93. s: [ "[1-5]?[0-9]", Date.prototype.setSeconds, "seconds", Date.prototype.getSeconds ],
  94. ss: [ "[0-5][0-9]", Date.prototype.setSeconds, "seconds", function() {
  95. return pad(Date.prototype.getSeconds.call(this), 2);
  96. } ],
  97. l: [ "[0-9]{3}", Date.prototype.setMilliseconds, "milliseconds", function() {
  98. return pad(Date.prototype.getMilliseconds.call(this), 3);
  99. } ],
  100. L: [ "[0-9]{2}", Date.prototype.setMilliseconds, "milliseconds", function() {
  101. return pad(Date.prototype.getMilliseconds.call(this), 2);
  102. } ],
  103. t: [ "" ],
  104. tt: [ "" ],
  105. T: [ "" ],
  106. TT: [ "" ],
  107. Z: [ "" ],
  108. o: [ "" ],
  109. S: [ "" ]
  110. }, formatAlias = {
  111. isoDate: "yyyy-mm-dd",
  112. isoTime: "HH:MM:ss",
  113. isoDateTime: "yyyy-mm-dd'T'HH:MM:ss",
  114. isoUtcDateTime: "UTC:yyyy-mm-dd'T'HH:MM:ss'Z'"
  115. };
  116. return Inputmask.extendAliases({
  117. datetime: {
  118. mask: function(opts) {
  119. return opts.inputFormat = formatAlias[opts.inputFormat] || opts.inputFormat, opts.displayFormat = formatAlias[opts.displayFormat] || opts.displayFormat || opts.inputFormat,
  120. opts.outputFormat = formatAlias[opts.outputFormat] || opts.outputFormat || opts.inputFormat,
  121. opts.placeholder = opts.placeholder !== Inputmask.prototype.defaults.placeholder ? opts.placeholder : opts.inputFormat,
  122. opts.min = analyseMask(opts.min, opts.inputFormat, opts), opts.max = analyseMask(opts.max, opts.inputFormat, opts),
  123. opts.regex = parse(opts.inputFormat, void 0, opts), null;
  124. },
  125. inputFormat: "isoDateTime",
  126. displayFormat: void 0,
  127. outputFormat: void 0,
  128. min: null,
  129. max: null,
  130. i18n: {
  131. dayNames: [ "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" ],
  132. 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" ]
  133. },
  134. postValidation: function(buffer, currentResult, opts) {
  135. var result = currentResult, dateParts = analyseMask(buffer.join(""), opts.inputFormat, opts);
  136. return result && dateParts.date.getTime() === dateParts.date.getTime() && (result = (result = isValidDate(dateParts, result)) && isDateInRange(dateParts.date, opts)),
  137. result;
  138. },
  139. onKeyDown: function(e, buffer, caretPos, opts) {
  140. var input = this;
  141. if (e.ctrlKey && e.keyCode === Inputmask.keyCode.RIGHT) {
  142. for (var match, today = new Date(), date = ""; match = getTokenizer(opts).exec(opts.inputFormat); ) "d" === match[0].charAt(0) ? date += pad(today.getDate(), match[0].length) : "m" === match[0].charAt(0) ? date += pad(today.getMonth() + 1, match[0].length) : "yyyy" === match[0] ? date += today.getFullYear().toString() : "y" === match[0].charAt(0) && (date += pad(today.getYear(), match[0].length));
  143. input.inputmask._valueSet(date), $(input).trigger("setvalue");
  144. }
  145. },
  146. onUnMask: function(maskedValue, unmaskedValue, opts) {
  147. return parse(opts.outputFormat, analyseMask(maskedValue, opts.inputFormat, opts), opts);
  148. },
  149. insertMode: !1
  150. }
  151. }), Inputmask;
  152. });