picker.date.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /*!
  2. * Date picker for pickadate.js v3.5.6
  3. * http://amsul.github.io/pickadate.js/date.htm
  4. */
  5. ! function (a) {
  6. "function" == typeof define && define.amd ? define(["picker", "jquery"], a) : "object" == typeof exports ? module.exports = a(require("./picker.js"), require("jquery")) : a(Picker, jQuery)
  7. }(function (a, b) {
  8. function c(a, b) {
  9. var c = this,
  10. d = a.$node[0],
  11. e = d.value,
  12. f = a.$node.data("value"),
  13. g = f || e,
  14. h = f ? b.formatSubmit : b.format,
  15. i = function () {
  16. return d.currentStyle ? "rtl" == d.currentStyle.direction : "rtl" == getComputedStyle(a.$root[0]).direction
  17. };
  18. c.settings = b, c.$node = a.$node, c.queue = {
  19. min: "measure create",
  20. max: "measure create",
  21. now: "now create",
  22. select: "parse create validate",
  23. highlight: "parse navigate create validate",
  24. view: "parse create validate viewset",
  25. disable: "deactivate",
  26. enable: "activate"
  27. }, c.item = {}, c.item.clear = null, c.item.disable = (b.disable || []).slice(0), c.item.enable = - function (a) {
  28. return a[0] === !0 ? a.shift() : -1
  29. }(c.item.disable), c.set("min", b.min).set("max", b.max).set("now"), g ? c.set("select", g, {
  30. format: h,
  31. defaultValue: !0
  32. }) : c.set("select", null).set("highlight", c.item.now), c.key = {
  33. 40: 7,
  34. 38: -7,
  35. 39: function () {
  36. return i() ? -1 : 1
  37. },
  38. 37: function () {
  39. return i() ? 1 : -1
  40. },
  41. go: function (a) {
  42. var b = c.item.highlight,
  43. d = new Date(b.year, b.month, b.date + a);
  44. c.set("highlight", d, {
  45. interval: a
  46. }), this.render()
  47. }
  48. }, a.on("render", function () {
  49. a.$root.find("." + b.klass.selectMonth).on("change", function () {
  50. var c = this.value;
  51. c && (a.set("highlight", [a.get("view").year, c, a.get("highlight").date]), a.$root.find("." + b.klass.selectMonth).trigger("focus"))
  52. }), a.$root.find("." + b.klass.selectYear).on("change", function () {
  53. var c = this.value;
  54. c && (a.set("highlight", [c, a.get("view").month, a.get("highlight").date]), a.$root.find("." + b.klass.selectYear).trigger("focus"))
  55. })
  56. }, 1).on("open", function () {
  57. var d = "";
  58. c.disabled(c.get("now")) && (d = ":not(." + b.klass.buttonToday + ")"), a.$root.find("button" + d + ", select").attr("disabled", !1)
  59. }, 1).on("close", function () {
  60. a.$root.find("button, select").attr("disabled", !0)
  61. }, 1)
  62. }
  63. var d = 7,
  64. e = 6,
  65. f = a._;
  66. c.prototype.set = function (a, b, c) {
  67. var d = this,
  68. e = d.item;
  69. return null === b ? ("clear" == a && (a = "select"), e[a] = b, d) : (e["enable" == a ? "disable" : "flip" == a ? "enable" : a] = d.queue[a].split(" ").map(function (e) {
  70. return b = d[e](a, b, c)
  71. }).pop(), "select" == a ? d.set("highlight", e.select, c) : "highlight" == a ? d.set("view", e.highlight, c) : a.match(/^(flip|min|max|disable|enable)$/) && (e.select && d.disabled(e.select) && d.set("select", e.select, c), e.highlight && d.disabled(e.highlight) && d.set("highlight", e.highlight, c)), d)
  72. }, c.prototype.get = function (a) {
  73. return this.item[a]
  74. }, c.prototype.create = function (a, c, d) {
  75. var e, g = this;
  76. return c = void 0 === c ? a : c, c == -(1 / 0) || c == 1 / 0 ? e = c : b.isPlainObject(c) && f.isInteger(c.pick) ? c = c.obj : b.isArray(c) ? (c = new Date(c[0], c[1], c[2]), c = f.isDate(c) ? c : g.create().obj) : c = f.isInteger(c) || f.isDate(c) ? g.normalize(new Date(c), d) : g.now(a, c, d), {
  77. year: e || c.getFullYear(),
  78. month: e || c.getMonth(),
  79. date: e || c.getDate(),
  80. day: e || c.getDay(),
  81. obj: e || c,
  82. pick: e || c.getTime()
  83. }
  84. }, c.prototype.createRange = function (a, c) {
  85. var d = this,
  86. e = function (a) {
  87. return a === !0 || b.isArray(a) || f.isDate(a) ? d.create(a) : a
  88. };
  89. return f.isInteger(a) || (a = e(a)), f.isInteger(c) || (c = e(c)), f.isInteger(a) && b.isPlainObject(c) ? a = [c.year, c.month, c.date + a] : f.isInteger(c) && b.isPlainObject(a) && (c = [a.year, a.month, a.date + c]), {
  90. from: e(a),
  91. to: e(c)
  92. }
  93. }, c.prototype.withinRange = function (a, b) {
  94. return a = this.createRange(a.from, a.to), b.pick >= a.from.pick && b.pick <= a.to.pick
  95. }, c.prototype.overlapRanges = function (a, b) {
  96. var c = this;
  97. return a = c.createRange(a.from, a.to), b = c.createRange(b.from, b.to), c.withinRange(a, b.from) || c.withinRange(a, b.to) || c.withinRange(b, a.from) || c.withinRange(b, a.to)
  98. }, c.prototype.now = function (a, b, c) {
  99. return b = new Date, c && c.rel && b.setDate(b.getDate() + c.rel), this.normalize(b, c)
  100. }, c.prototype.navigate = function (a, c, d) {
  101. var e, f, g, h, i = b.isArray(c),
  102. j = b.isPlainObject(c),
  103. k = this.item.view;
  104. if (i || j) {
  105. for (j ? (f = c.year, g = c.month, h = c.date) : (f = +c[0], g = +c[1], h = +c[2]), d && d.nav && k && k.month !== g && (f = k.year, g = k.month), e = new Date(f, g + (d && d.nav ? d.nav : 0), 1), f = e.getFullYear(), g = e.getMonth(); new Date(f, g, h).getMonth() !== g;) h -= 1;
  106. c = [f, g, h]
  107. }
  108. return c
  109. }, c.prototype.normalize = function (a) {
  110. return a.setHours(0, 0, 0, 0), a
  111. }, c.prototype.measure = function (a, b) {
  112. var c = this;
  113. return b ? "string" == typeof b ? b = c.parse(a, b) : f.isInteger(b) && (b = c.now(a, b, {
  114. rel: b
  115. })) : b = "min" == a ? -(1 / 0) : 1 / 0, b
  116. }, c.prototype.viewset = function (a, b) {
  117. return this.create([b.year, b.month, 1])
  118. }, c.prototype.validate = function (a, c, d) {
  119. var e, g, h, i, j = this,
  120. k = c,
  121. l = d && d.interval ? d.interval : 1,
  122. m = -1 === j.item.enable,
  123. n = j.item.min,
  124. o = j.item.max,
  125. p = m && j.item.disable.filter(function (a) {
  126. if (b.isArray(a)) {
  127. var d = j.create(a).pick;
  128. d < c.pick ? e = !0 : d > c.pick && (g = !0)
  129. }
  130. return f.isInteger(a)
  131. }).length;
  132. if ((!d || !d.nav && !d.defaultValue) && (!m && j.disabled(c) || m && j.disabled(c) && (p || e || g) || !m && (c.pick <= n.pick || c.pick >= o.pick)))
  133. for (m && !p && (!g && l > 0 || !e && 0 > l) && (l *= -1); j.disabled(c) && (Math.abs(l) > 1 && (c.month < k.month || c.month > k.month) && (c = k, l = l > 0 ? 1 : -1), c.pick <= n.pick ? (h = !0, l = 1, c = j.create([n.year, n.month, n.date + (c.pick === n.pick ? 0 : -1)])) : c.pick >= o.pick && (i = !0, l = -1, c = j.create([o.year, o.month, o.date + (c.pick === o.pick ? 0 : 1)])), !h || !i);) c = j.create([c.year, c.month, c.date + l]);
  134. return c
  135. }, c.prototype.disabled = function (a) {
  136. var c = this,
  137. d = c.item.disable.filter(function (d) {
  138. return f.isInteger(d) ? a.day === (c.settings.firstDay ? d : d - 1) % 7 : b.isArray(d) || f.isDate(d) ? a.pick === c.create(d).pick : b.isPlainObject(d) ? c.withinRange(d, a) : void 0
  139. });
  140. return d = d.length && !d.filter(function (a) {
  141. return b.isArray(a) && "inverted" == a[3] || b.isPlainObject(a) && a.inverted
  142. }).length, -1 === c.item.enable ? !d : d || a.pick < c.item.min.pick || a.pick > c.item.max.pick
  143. }, c.prototype.parse = function (a, b, c) {
  144. var d = this,
  145. e = {};
  146. return b && "string" == typeof b ? (c && c.format || (c = c || {}, c.format = d.settings.format), d.formats.toArray(c.format).map(function (a) {
  147. var c = d.formats[a],
  148. g = c ? f.trigger(c, d, [b, e]) : a.replace(/^!/, "").length;
  149. c && (e[a] = b.substr(0, g)), b = b.substr(g)
  150. }), [e.yyyy || e.yy, +(e.mm || e.m) - 1, e.dd || e.d]) : b
  151. }, c.prototype.formats = function () {
  152. function a(a, b, c) {
  153. var d = a.match(/[^\x00-\x7F]+|\w+/)[0];
  154. return c.mm || c.m || (c.m = b.indexOf(d) + 1), d.length
  155. }
  156. function b(a) {
  157. return a.match(/\w+/)[0].length
  158. }
  159. return {
  160. d: function (a, b) {
  161. return a ? f.digits(a) : b.date
  162. },
  163. dd: function (a, b) {
  164. return a ? 2 : f.lead(b.date)
  165. },
  166. ddd: function (a, c) {
  167. return a ? b(a) : this.settings.weekdaysShort[c.day]
  168. },
  169. dddd: function (a, c) {
  170. return a ? b(a) : this.settings.weekdaysFull[c.day]
  171. },
  172. m: function (a, b) {
  173. return a ? f.digits(a) : b.month + 1
  174. },
  175. mm: function (a, b) {
  176. return a ? 2 : f.lead(b.month + 1)
  177. },
  178. mmm: function (b, c) {
  179. var d = this.settings.monthsShort;
  180. return b ? a(b, d, c) : d[c.month]
  181. },
  182. mmmm: function (b, c) {
  183. var d = this.settings.monthsFull;
  184. return b ? a(b, d, c) : d[c.month]
  185. },
  186. yy: function (a, b) {
  187. return a ? 2 : ("" + b.year).slice(2)
  188. },
  189. yyyy: function (a, b) {
  190. return a ? 4 : b.year
  191. },
  192. toArray: function (a) {
  193. return a.split(/(d{1,4}|m{1,4}|y{4}|yy|!.)/g)
  194. },
  195. toString: function (a, b) {
  196. var c = this;
  197. return c.formats.toArray(a).map(function (a) {
  198. return f.trigger(c.formats[a], c, [0, b]) || a.replace(/^!/, "")
  199. }).join("")
  200. }
  201. }
  202. }(), c.prototype.isDateExact = function (a, c) {
  203. var d = this;
  204. return f.isInteger(a) && f.isInteger(c) || "boolean" == typeof a && "boolean" == typeof c ? a === c : (f.isDate(a) || b.isArray(a)) && (f.isDate(c) || b.isArray(c)) ? d.create(a).pick === d.create(c).pick : b.isPlainObject(a) && b.isPlainObject(c) ? d.isDateExact(a.from, c.from) && d.isDateExact(a.to, c.to) : !1
  205. }, c.prototype.isDateOverlap = function (a, c) {
  206. var d = this,
  207. e = d.settings.firstDay ? 1 : 0;
  208. return f.isInteger(a) && (f.isDate(c) || b.isArray(c)) ? (a = a % 7 + e, a === d.create(c).day + 1) : f.isInteger(c) && (f.isDate(a) || b.isArray(a)) ? (c = c % 7 + e, c === d.create(a).day + 1) : b.isPlainObject(a) && b.isPlainObject(c) ? d.overlapRanges(a, c) : !1
  209. }, c.prototype.flipEnable = function (a) {
  210. var b = this.item;
  211. b.enable = a || (-1 == b.enable ? 1 : -1)
  212. }, c.prototype.deactivate = function (a, c) {
  213. var d = this,
  214. e = d.item.disable.slice(0);
  215. return "flip" == c ? d.flipEnable() : c === !1 ? (d.flipEnable(1), e = []) : c === !0 ? (d.flipEnable(-1), e = []) : c.map(function (a) {
  216. for (var c, g = 0; g < e.length; g += 1)
  217. if (d.isDateExact(a, e[g])) {
  218. c = !0;
  219. break
  220. }
  221. c || (f.isInteger(a) || f.isDate(a) || b.isArray(a) || b.isPlainObject(a) && a.from && a.to) && e.push(a)
  222. }), e
  223. }, c.prototype.activate = function (a, c) {
  224. var d = this,
  225. e = d.item.disable,
  226. g = e.length;
  227. return "flip" == c ? d.flipEnable() : c === !0 ? (d.flipEnable(1), e = []) : c === !1 ? (d.flipEnable(-1), e = []) : c.map(function (a) {
  228. var c, h, i, j;
  229. for (i = 0; g > i; i += 1) {
  230. if (h = e[i], d.isDateExact(h, a)) {
  231. c = e[i] = null, j = !0;
  232. break
  233. }
  234. if (d.isDateOverlap(h, a)) {
  235. b.isPlainObject(a) ? (a.inverted = !0, c = a) : b.isArray(a) ? (c = a, c[3] || c.push("inverted")) : f.isDate(a) && (c = [a.getFullYear(), a.getMonth(), a.getDate(), "inverted"]);
  236. break
  237. }
  238. }
  239. if (c)
  240. for (i = 0; g > i; i += 1)
  241. if (d.isDateExact(e[i], a)) {
  242. e[i] = null;
  243. break
  244. }
  245. if (j)
  246. for (i = 0; g > i; i += 1)
  247. if (d.isDateOverlap(e[i], a)) {
  248. e[i] = null;
  249. break
  250. }
  251. c && e.push(c)
  252. }), e.filter(function (a) {
  253. return null != a
  254. })
  255. }, c.prototype.nodes = function (a) {
  256. var b = this,
  257. c = b.settings,
  258. g = b.item,
  259. h = g.now,
  260. i = g.select,
  261. j = g.highlight,
  262. k = g.view,
  263. l = g.disable,
  264. m = g.min,
  265. n = g.max,
  266. o = function (a, b) {
  267. return c.firstDay && (a.push(a.shift()), b.push(b.shift())), f.node("thead", f.node("tr", f.group({
  268. min: 0,
  269. max: d - 1,
  270. i: 1,
  271. node: "th",
  272. item: function (d) {
  273. return [a[d], c.klass.weekdays, 'scope=col title="' + b[d] + '"']
  274. }
  275. })))
  276. }((c.showWeekdaysFull ? c.weekdaysFull : c.weekdaysShort).slice(0), c.weekdaysFull.slice(0)),
  277. p = function (a) {
  278. return f.node("div", " ", c.klass["nav" + (a ? "Next" : "Prev")] + (a && k.year >= n.year && k.month >= n.month || !a && k.year <= m.year && k.month <= m.month ? " " + c.klass.navDisabled : ""), "data-nav=" + (a || -1) + " " + f.ariaAttr({
  279. role: "button",
  280. controls: b.$node[0].id + "_table"
  281. }) + ' title="' + (a ? c.labelMonthNext : c.labelMonthPrev) + '"')
  282. },
  283. q = function () {
  284. var d = c.showMonthsShort ? c.monthsShort : c.monthsFull;
  285. return c.selectMonths ? f.node("select", f.group({
  286. min: 0,
  287. max: 11,
  288. i: 1,
  289. node: "option",
  290. item: function (a) {
  291. return [d[a], 0, "value=" + a + (k.month == a ? " selected" : "") + (k.year == m.year && a < m.month || k.year == n.year && a > n.month ? " disabled" : "")]
  292. }
  293. }), c.klass.selectMonth, (a ? "" : "disabled") + " " + f.ariaAttr({
  294. controls: b.$node[0].id + "_table"
  295. }) + ' title="' + c.labelMonthSelect + '"') : f.node("div", d[k.month], c.klass.month)
  296. },
  297. r = function () {
  298. var d = k.year,
  299. e = c.selectYears === !0 ? 5 : ~~(c.selectYears / 2);
  300. if (e) {
  301. var g = m.year,
  302. h = n.year,
  303. i = d - e,
  304. j = d + e;
  305. if (g > i && (j += g - i, i = g), j > h) {
  306. var l = i - g,
  307. o = j - h;
  308. i -= l > o ? o : l, j = h
  309. }
  310. return f.node("select", f.group({
  311. min: i,
  312. max: j,
  313. i: 1,
  314. node: "option",
  315. item: function (a) {
  316. return [a, 0, "value=" + a + (d == a ? " selected" : "")]
  317. }
  318. }), c.klass.selectYear, (a ? "" : "disabled") + " " + f.ariaAttr({
  319. controls: b.$node[0].id + "_table"
  320. }) + ' title="' + c.labelYearSelect + '"')
  321. }
  322. return f.node("div", d, c.klass.year)
  323. };
  324. return f.node("div", (c.selectYears ? r() + q() : q() + r()) + p() + p(1), c.klass.header) + f.node("table", o + f.node("tbody", f.group({
  325. min: 0,
  326. max: e - 1,
  327. i: 1,
  328. node: "tr",
  329. item: function (a) {
  330. var e = c.firstDay && 0 === b.create([k.year, k.month, 1]).day ? -7 : 0;
  331. return [f.group({
  332. min: d * a - k.day + e + 1,
  333. max: function () {
  334. return this.min + d - 1
  335. },
  336. i: 1,
  337. node: "td",
  338. item: function (a) {
  339. a = b.create([k.year, k.month, a + (c.firstDay ? 1 : 0)]);
  340. var d = i && i.pick == a.pick,
  341. e = j && j.pick == a.pick,
  342. g = l && b.disabled(a) || a.pick < m.pick || a.pick > n.pick,
  343. o = f.trigger(b.formats.toString, b, [c.format, a]);
  344. return [f.node("div", a.date, function (b) {
  345. return b.push(k.month == a.month ? c.klass.infocus : c.klass.outfocus), h.pick == a.pick && b.push(c.klass.now), d && b.push(c.klass.selected), e && b.push(c.klass.highlighted), g && b.push(c.klass.disabled), b.join(" ")
  346. }([c.klass.day]), "data-pick=" + a.pick + " " + f.ariaAttr({
  347. role: "gridcell",
  348. label: o,
  349. selected: d && b.$node.val() === o ? !0 : null,
  350. activedescendant: e ? !0 : null,
  351. disabled: g ? !0 : null
  352. })), "", f.ariaAttr({
  353. role: "presentation"
  354. })]
  355. }
  356. })]
  357. }
  358. })), c.klass.table, 'id="' + b.$node[0].id + '_table" ' + f.ariaAttr({
  359. role: "grid",
  360. controls: b.$node[0].id,
  361. readonly: !0
  362. })) + f.node("div", f.node("button", c.today, c.klass.buttonToday, "type=button data-pick=" + h.pick + (a && !b.disabled(h) ? "" : " disabled") + " " + f.ariaAttr({
  363. controls: b.$node[0].id
  364. })) + f.node("button", c.clear, c.klass.buttonClear, "type=button data-clear=1" + (a ? "" : " disabled") + " " + f.ariaAttr({
  365. controls: b.$node[0].id
  366. })) + f.node("button", c.close, c.klass.buttonClose, "type=button data-close=true " + (a ? "" : " disabled") + " " + f.ariaAttr({
  367. controls: b.$node[0].id
  368. })), c.klass.footer)
  369. }, c.defaults = function (a) {
  370. return {
  371. labelMonthNext: "Next month",
  372. labelMonthPrev: "Previous month",
  373. labelMonthSelect: "Select a month",
  374. labelYearSelect: "Select a year",
  375. monthsFull: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
  376. monthsShort: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
  377. weekdaysFull: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
  378. weekdaysShort: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
  379. today: "Today",
  380. clear: "Clear",
  381. close: "Close",
  382. closeOnSelect: !0,
  383. closeOnClear: !0,
  384. format: "d mmmm, yyyy",
  385. klass: {
  386. table: a + "table",
  387. header: a + "header",
  388. navPrev: a + "nav--prev",
  389. navNext: a + "nav--next",
  390. navDisabled: a + "nav--disabled",
  391. month: a + "month",
  392. year: a + "year",
  393. selectMonth: a + "select--month",
  394. selectYear: a + "select--year",
  395. weekdays: a + "weekday",
  396. day: a + "day",
  397. disabled: a + "day--disabled",
  398. selected: a + "day--selected",
  399. highlighted: a + "day--highlighted",
  400. now: a + "day--today",
  401. infocus: a + "day--infocus",
  402. outfocus: a + "day--outfocus",
  403. footer: a + "footer",
  404. buttonClear: a + "button--clear",
  405. buttonToday: a + "button--today",
  406. buttonClose: a + "button--close"
  407. }
  408. }
  409. }(a.klasses().picker + "__"), a.extend("pickadate", c)
  410. });