inputmask.date.extensions.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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.3-beta.3
  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: [ "[01][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-5][0-9]", Date.prototype.setMinutes, "minutes", function() {
  51. return pad(Date.prototype.getMinutes.call(this), 2);
  52. } ],
  53. s: [ "[1-5]?[0-9]", Date.prototype.setSeconds, "seconds", Date.prototype.getSeconds ],
  54. ss: [ "[0-5][0-9]", Date.prototype.setSeconds, "seconds", function() {
  55. return pad(Date.prototype.getSeconds.call(this), 2);
  56. } ],
  57. l: [ "[0-9]{3}", Date.prototype.setMilliseconds, "milliseconds", function() {
  58. return pad(Date.prototype.getMilliseconds.call(this), 3);
  59. } ],
  60. L: [ "[0-9]{2}", Date.prototype.setMilliseconds, "milliseconds", function() {
  61. return pad(Date.prototype.getMilliseconds.call(this), 2);
  62. } ],
  63. t: [ "[ap]" ],
  64. tt: [ "[ap]m" ],
  65. T: [ "[AP]" ],
  66. TT: [ "[AP]M" ],
  67. Z: [ "" ],
  68. o: [ "" ],
  69. S: [ "" ]
  70. }, formatAlias = {
  71. isoDate: "yyyy-mm-dd",
  72. isoTime: "HH:MM:ss",
  73. isoDateTime: "yyyy-mm-dd'T'HH:MM:ss",
  74. isoUtcDateTime: "UTC:yyyy-mm-dd'T'HH:MM:ss'Z'"
  75. };
  76. function getTokenizer(opts) {
  77. if (!opts.tokenizer) {
  78. var tokens = [];
  79. for (var ndx in formatCode) {
  80. if (tokens.indexOf(ndx[0]) === -1) tokens.push(ndx[0]);
  81. }
  82. opts.tokenizer = "(" + tokens.join("+|") + ")+?|.";
  83. opts.tokenizer = new RegExp(opts.tokenizer, "g");
  84. }
  85. return opts.tokenizer;
  86. }
  87. function isValidDate(dateParts, currentResult) {
  88. 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;
  89. }
  90. function isDateInRange(dateParts, opts) {
  91. var result = true;
  92. if (opts.min) {
  93. if (dateParts["rawyear"]) {
  94. var rawYear = dateParts["rawyear"].replace(/[^0-9]/g, ""), minYear = opts.min.year.substr(0, rawYear.length);
  95. result = minYear <= rawYear;
  96. }
  97. if (dateParts["year"] === dateParts["rawyear"]) {
  98. if (opts.min.date.getTime() === opts.min.date.getTime()) {
  99. result = opts.min.date.getTime() <= dateParts.date.getTime();
  100. }
  101. }
  102. }
  103. if (result && opts.max && opts.max.date.getTime() === opts.max.date.getTime()) {
  104. result = opts.max.date.getTime() >= dateParts.date.getTime();
  105. }
  106. return result;
  107. }
  108. function parse(format, dateObjValue, opts, raw) {
  109. var mask = "", match;
  110. while (match = getTokenizer(opts).exec(format)) {
  111. if (dateObjValue === undefined) {
  112. if (formatCode[match[0]]) {
  113. mask += "(" + formatCode[match[0]][0] + ")";
  114. } else {
  115. switch (match[0]) {
  116. case "[":
  117. mask += "(";
  118. break;
  119. case "]":
  120. mask += ")?";
  121. break;
  122. default:
  123. mask += Inputmask.escapeRegex(match[0]);
  124. }
  125. }
  126. } else {
  127. if (formatCode[match[0]]) {
  128. if (raw !== true && formatCode[match[0]][3]) {
  129. var getFn = formatCode[match[0]][3];
  130. mask += getFn.call(dateObjValue.date);
  131. } else if (formatCode[match[0]][2]) mask += dateObjValue["raw" + formatCode[match[0]][2]]; else mask += match[0];
  132. } else mask += match[0];
  133. }
  134. }
  135. return mask;
  136. }
  137. function pad(val, len) {
  138. val = String(val);
  139. len = len || 2;
  140. while (val.length < len) val = "0" + val;
  141. return val;
  142. }
  143. function analyseMask(maskString, format, opts) {
  144. var dateObj = {
  145. date: new Date(1, 0, 1)
  146. }, targetProp, mask = maskString, match, dateOperation, targetValidator;
  147. function extendProperty(value) {
  148. var correctedValue = value.replace(/[^0-9]/g, "0");
  149. if (correctedValue != value) {
  150. var enteredPart = value.replace(/[^0-9]/g, ""), min = (opts.min && opts.min[targetProp] || value).toString(), max = (opts.max && opts.max[targetProp] || value).toString();
  151. 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));
  152. }
  153. return correctedValue;
  154. }
  155. function setValue(dateObj, value, opts) {
  156. dateObj[targetProp] = extendProperty(value);
  157. dateObj["raw" + targetProp] = value;
  158. if (dateOperation !== undefined) dateOperation.call(dateObj.date, targetProp == "month" ? parseInt(dateObj[targetProp]) - 1 : dateObj[targetProp]);
  159. }
  160. if (typeof mask === "string") {
  161. while (match = getTokenizer(opts).exec(format)) {
  162. var value = mask.slice(0, match[0].length);
  163. if (formatCode.hasOwnProperty(match[0])) {
  164. targetValidator = formatCode[match[0]][0];
  165. targetProp = formatCode[match[0]][2];
  166. dateOperation = formatCode[match[0]][1];
  167. setValue(dateObj, value, opts);
  168. }
  169. mask = mask.slice(value.length);
  170. }
  171. return dateObj;
  172. } else if (mask && typeof mask === "object" && mask.hasOwnProperty("date")) {
  173. return mask;
  174. }
  175. return undefined;
  176. }
  177. Inputmask.extendAliases({
  178. datetime: {
  179. mask: function(opts) {
  180. formatCode.S = opts.i18n.ordinalSuffix.join("|");
  181. opts.inputFormat = formatAlias[opts.inputFormat] || opts.inputFormat;
  182. opts.displayFormat = formatAlias[opts.displayFormat] || opts.displayFormat || opts.inputFormat;
  183. opts.outputFormat = formatAlias[opts.outputFormat] || opts.outputFormat || opts.inputFormat;
  184. opts.placeholder = opts.placeholder !== "" ? opts.placeholder : opts.inputFormat.replace(/[\[\]]/, "");
  185. opts.regex = parse(opts.inputFormat, undefined, opts);
  186. return null;
  187. },
  188. placeholder: "",
  189. inputFormat: "isoDateTime",
  190. displayFormat: undefined,
  191. outputFormat: undefined,
  192. min: null,
  193. max: null,
  194. i18n: {
  195. dayNames: [ "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" ],
  196. 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" ],
  197. ordinalSuffix: [ "st", "nd", "rd", "th" ]
  198. },
  199. postValidation: function(buffer, pos, currentResult, opts) {
  200. opts.min = analyseMask(opts.min, opts.inputFormat, opts);
  201. opts.max = analyseMask(opts.max, opts.inputFormat, opts);
  202. var result = currentResult, dateParts = analyseMask(buffer.join(""), opts.inputFormat, opts);
  203. if (result && dateParts.date.getTime() === dateParts.date.getTime()) {
  204. result = isValidDate(dateParts, result);
  205. result = result && isDateInRange(dateParts, opts);
  206. }
  207. if (pos && result && currentResult.pos !== pos) {
  208. return {
  209. buffer: parse(opts.inputFormat, dateParts, opts),
  210. refreshFromBuffer: {
  211. start: pos,
  212. end: currentResult.pos
  213. }
  214. };
  215. }
  216. return result;
  217. },
  218. onKeyDown: function(e, buffer, caretPos, opts) {
  219. var input = this;
  220. if (e.ctrlKey && e.keyCode === Inputmask.keyCode.RIGHT) {
  221. var today = new Date(), match, date = "";
  222. while (match = getTokenizer(opts).exec(opts.inputFormat)) {
  223. if (match[0].charAt(0) === "d") {
  224. date += pad(today.getDate(), match[0].length);
  225. } else if (match[0].charAt(0) === "m") {
  226. date += pad(today.getMonth() + 1, match[0].length);
  227. } else if (match[0] === "yyyy") {
  228. date += today.getFullYear().toString();
  229. } else if (match[0].charAt(0) === "y") {
  230. date += pad(today.getYear(), match[0].length);
  231. }
  232. }
  233. input.inputmask._valueSet(date);
  234. $(input).trigger("setvalue");
  235. }
  236. },
  237. onUnMask: function(maskedValue, unmaskedValue, opts) {
  238. return parse(opts.outputFormat, analyseMask(maskedValue, opts.inputFormat, opts), opts, true);
  239. },
  240. casing: function(elem, test, pos, validPositions) {
  241. if (test.nativeDef.indexOf("[ap]") == 0) return elem.toLowerCase();
  242. if (test.nativeDef.indexOf("[AP]") == 0) return elem.toUpperCase();
  243. return elem;
  244. },
  245. insertMode: false,
  246. shiftPositions: false
  247. }
  248. });
  249. return Inputmask;
  250. });