inputmask.date.extensions.js 9.4 KB

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