inputmask.date.extensions.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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
  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) if (formatCode[match[0]]) mask += "(" + formatCode[match[0]][0] + ")"; else switch (match[0]) {
  80. case "[":
  81. mask += "(";
  82. break;
  83. case "]":
  84. mask += ")?";
  85. break;
  86. default:
  87. mask += Inputmask.escapeRegex(match[0]);
  88. } else if (formatCode[match[0]]) mask += formatCode[match[0]][3].call(dateObjValue.date); else mask += match[0];
  89. }
  90. return mask;
  91. }
  92. function pad(val, len) {
  93. for (val = String(val), len = len || 2; val.length < len; ) val = "0" + val;
  94. return val;
  95. }
  96. function analyseMask(maskString, format, opts) {
  97. var targetProp, match, dateOperation, targetValidator, dateObj = {
  98. date: new Date(1, 0, 1)
  99. }, mask = maskString;
  100. function extendProperty(value) {
  101. var correctedValue;
  102. if (opts.min && opts.min[targetProp] || opts.max && opts.max[targetProp]) {
  103. var min = opts.min && opts.min[targetProp] || opts.max[targetProp], max = opts.max && opts.max[targetProp] || opts.min[targetProp];
  104. for (correctedValue = value.replace(/[^0-9]/g, ""), correctedValue += (min.indexOf(correctedValue) < max.indexOf(correctedValue) ? max : min).toString().substr(correctedValue.length); !new RegExp(targetValidator).test(correctedValue); ) correctedValue--;
  105. } else correctedValue = value.replace(/[^0-9]/g, "0");
  106. return correctedValue;
  107. }
  108. function setValue(dateObj, value, opts) {
  109. dateObj[targetProp] = extendProperty(value), dateObj["raw" + targetProp] = value,
  110. void 0 !== dateOperation && dateOperation.call(dateObj.date, "month" == targetProp ? parseInt(dateObj[targetProp]) - 1 : dateObj[targetProp]);
  111. }
  112. if ("string" == typeof mask) {
  113. for (;match = getTokenizer(opts).exec(format); ) {
  114. var value = mask.slice(0, match[0].length);
  115. formatCode.hasOwnProperty(match[0]) && (targetValidator = formatCode[match[0]][0],
  116. targetProp = formatCode[match[0]][2], dateOperation = formatCode[match[0]][1], setValue(dateObj, value)),
  117. mask = mask.slice(value.length);
  118. }
  119. return dateObj;
  120. }
  121. }
  122. return Inputmask.extendAliases({
  123. datetime: {
  124. mask: function(opts) {
  125. return formatCode.S = opts.i18n.ordinalSuffix.join("|"), opts.inputFormat = formatAlias[opts.inputFormat] || opts.inputFormat,
  126. opts.displayFormat = formatAlias[opts.displayFormat] || opts.displayFormat || opts.inputFormat,
  127. opts.outputFormat = formatAlias[opts.outputFormat] || opts.outputFormat || opts.inputFormat,
  128. opts.placeholder = "" !== opts.placeholder ? opts.placeholder : opts.inputFormat.replace(/[\[\]]/, ""),
  129. opts.min = analyseMask(opts.min, opts.inputFormat, opts), opts.max = analyseMask(opts.max, opts.inputFormat, opts),
  130. opts.regex = parse(opts.inputFormat, void 0, opts), null;
  131. },
  132. placeholder: "",
  133. inputFormat: "isoDateTime",
  134. displayFormat: void 0,
  135. outputFormat: void 0,
  136. min: null,
  137. max: null,
  138. i18n: {
  139. dayNames: [ "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" ],
  140. 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" ],
  141. ordinalSuffix: [ "st", "nd", "rd", "th" ]
  142. },
  143. postValidation: function(buffer, currentResult, opts) {
  144. var result = currentResult, dateParts = analyseMask(buffer.join(""), opts.inputFormat, opts);
  145. return result && dateParts.date.getTime() == dateParts.date.getTime() && (result = (result = function(dateParts, currentResult) {
  146. return (!isFinite(dateParts.rawday) || "29" == dateParts.day && !isFinite(dateParts.rawyear) || new Date(dateParts.date.getFullYear(), isFinite(dateParts.rawmonth) ? dateParts.month : dateParts.date.getMonth() + 1, 0).getDate() >= dateParts.day) && currentResult;
  147. }(dateParts, result)) && function(dateParts, opts) {
  148. var result = !0;
  149. if (opts.min) {
  150. if (dateParts.rawyear) {
  151. var rawYear = dateParts.rawyear.replace(/[^0-9]/g, "");
  152. result = opts.min.year.substr(0, rawYear.length) <= rawYear;
  153. }
  154. dateParts.year === dateParts.rawyear && opts.min.date.getTime() == opts.min.date.getTime() && (result = opts.min.date.getTime() <= dateParts.date.getTime());
  155. }
  156. return result && opts.max && opts.max.date.getTime() == opts.max.date.getTime() && (result = opts.max.date.getTime() >= dateParts.date.getTime()),
  157. result;
  158. }(dateParts, opts)), result;
  159. },
  160. onKeyDown: function(e, buffer, caretPos, opts) {
  161. if (e.ctrlKey && e.keyCode === Inputmask.keyCode.RIGHT) {
  162. 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));
  163. this.inputmask._valueSet(date), $(this).trigger("setvalue");
  164. }
  165. },
  166. onUnMask: function(maskedValue, unmaskedValue, opts) {
  167. return parse(opts.outputFormat, analyseMask(maskedValue, opts.inputFormat, opts), opts);
  168. },
  169. casing: function(elem, test, pos, validPositions) {
  170. return 0 == test.nativeDef.indexOf("[ap]") ? elem.toLowerCase() : 0 == test.nativeDef.indexOf("[AP]") ? elem.toUpperCase() : elem;
  171. },
  172. insertMode: !1
  173. }
  174. }), Inputmask;
  175. });