laydate.js 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  1. /*! laydate-v5.0.9 日期与时间组件 MIT License http://www.layui.com/laydate/ By 贤心 */
  2. ; !function () {
  3. "use strict";
  4. var e = window.layui && layui.define,
  5. t = {
  6. getPath: function () {
  7. var e = document.currentScript ? document.currentScript.src : function () {
  8. for (var e, t = document.scripts, n = t.length - 1, a = n; a > 0; a--) if ("interactive" === t[a].readyState) {
  9. e = t[a].src;
  10. break
  11. }
  12. return e || t[n].src
  13. }();
  14. return e.substring(0, e.lastIndexOf("/") + 1)
  15. }(),
  16. getStyle: function (e, t) {
  17. var n = e.currentStyle ? e.currentStyle : window.getComputedStyle(e, null);
  18. return n[n.getPropertyValue ? "getPropertyValue" : "getAttribute"](t)
  19. },
  20. link: function (e, a, i) {
  21. if (n.path) {
  22. var r = document.getElementsByTagName("head")[0],
  23. o = document.createElement("link");
  24. "string" == typeof a && (i = a);
  25. var s = (i || e).replace(/\.|\//g, ""),
  26. l = "layuicss-" + s,
  27. d = 0;
  28. o.rel = "stylesheet", o.href = n.path + e, o.id = l, document.getElementById(l) || r.appendChild(o), "function" == typeof a && !
  29. function c() {
  30. return ++d > 80 ? window.console && console.error("laydate.css: Invalid") : void (1989 === parseInt(t.getStyle(document.getElementById(l), "width")) ? a() : setTimeout(c, 100))
  31. }()
  32. }
  33. }
  34. },
  35. n = {
  36. v: "5.0.9",
  37. config: {},
  38. index: window.laydate && window.laydate.v ? 1e5 : 0,
  39. path: t.getPath,
  40. set: function (e) {
  41. var t = this;
  42. return t.config = w.extend({}, t.config, e), t
  43. },
  44. ready: function (a) {
  45. var i = "laydate",
  46. r = "",
  47. o = (e ? "modules/laydate/" : "theme/") + "default/laydate.css?v=" + n.v + r;
  48. return e ? layui.addcss(o, a, i) : t.link(o, a, i), this
  49. }
  50. },
  51. a = function () {
  52. var e = this;
  53. return {
  54. hint: function (t) {
  55. e.hint.call(e, t)
  56. },
  57. config: e.config
  58. }
  59. },
  60. i = "laydate",
  61. r = ".layui-laydate",
  62. o = "layui-this",
  63. s = "laydate-disabled",
  64. l = "开始日期超出了结束日期<br>建议重新选择",
  65. d = [100, 2e5],
  66. c = "layui-laydate-static",
  67. m = "layui-laydate-list",
  68. u = "laydate-selected",
  69. h = "layui-laydate-hint",
  70. y = "laydate-day-prev",
  71. f = "laydate-day-next",
  72. p = "layui-laydate-footer",
  73. g = ".laydate-btns-confirm",
  74. v = "laydate-time-text",
  75. D = ".laydate-btns-time",
  76. T = function (e) {
  77. var t = this;
  78. t.index = ++n.index, t.config = w.extend({}, t.config, n.config, e), n.ready(function () {
  79. t.init()
  80. })
  81. },
  82. w = function (e) {
  83. return new C(e)
  84. },
  85. C = function (e) {
  86. for (var t = 0, n = "object" == typeof e ? [e] : (this.selector = e, document.querySelectorAll(e || null)) ; t < n.length; t++) this.push(n[t])
  87. };
  88. C.prototype = [], C.prototype.constructor = C, w.extend = function () {
  89. var e = 1,
  90. t = arguments,
  91. n = function (e, t) {
  92. e = e || (t.constructor === Array ? [] : {});
  93. for (var a in t) e[a] = t[a] && t[a].constructor === Object ? n(e[a], t[a]) : t[a];
  94. return e
  95. };
  96. for (t[0] = "object" == typeof t[0] ? t[0] : {}; e < t.length; e++) "object" == typeof t[e] && n(t[0], t[e]);
  97. return t[0]
  98. }, w.ie = function () {
  99. var e = navigator.userAgent.toLowerCase();
  100. return !!(window.ActiveXObject || "ActiveXObject" in window) && ((e.match(/msie\s(\d+)/) || [])[1] || "11")
  101. }(), w.stope = function (e) {
  102. e = e || window.event, e.stopPropagation ? e.stopPropagation() : e.cancelBubble = !0
  103. }, w.each = function (e, t) {
  104. var n, a = this;
  105. if ("function" != typeof t) return a;
  106. if (e = e || [], e.constructor === Object) {
  107. for (n in e) if (t.call(e[n], n, e[n])) break
  108. } else for (n = 0; n < e.length && !t.call(e[n], n, e[n]) ; n++);
  109. return a
  110. }, w.digit = function (e, t, n) {
  111. var a = "";
  112. e = String(e), t = t || 2;
  113. for (var i = e.length; i < t; i++) a += "0";
  114. return e < Math.pow(10, t) ? a + (0 | e) : e
  115. }, w.elem = function (e, t) {
  116. var n = document.createElement(e);
  117. return w.each(t || {}, function (e, t) {
  118. n.setAttribute(e, t)
  119. }), n
  120. }, C.addStr = function (e, t) {
  121. return e = e.replace(/\s+/, " "), t = t.replace(/\s+/, " ").split(" "), w.each(t, function (t, n) {
  122. new RegExp("\\b" + n + "\\b").test(e) || (e = e + " " + n)
  123. }), e.replace(/^\s|\s$/, "")
  124. }, C.removeStr = function (e, t) {
  125. return e = e.replace(/\s+/, " "), t = t.replace(/\s+/, " ").split(" "), w.each(t, function (t, n) {
  126. var a = new RegExp("\\b" + n + "\\b");
  127. a.test(e) && (e = e.replace(a, ""))
  128. }), e.replace(/\s+/, " ").replace(/^\s|\s$/, "")
  129. }, C.prototype.find = function (e) {
  130. var t = this,
  131. n = 0,
  132. a = [],
  133. i = "object" == typeof e;
  134. return this.each(function (r, o) {
  135. for (var s = i ? [e] : o.querySelectorAll(e || null) ; n < s.length; n++) a.push(s[n]);
  136. t.shift()
  137. }), i || (t.selector = (t.selector ? t.selector + " " : "") + e), w.each(a, function (e, n) {
  138. t.push(n)
  139. }), t
  140. }, C.prototype.each = function (e) {
  141. return w.each.call(this, this, e)
  142. }, C.prototype.addClass = function (e, t) {
  143. return this.each(function (n, a) {
  144. a.className = C[t ? "removeStr" : "addStr"](a.className, e)
  145. })
  146. }, C.prototype.removeClass = function (e) {
  147. return this.addClass(e, !0)
  148. }, C.prototype.hasClass = function (e) {
  149. var t = !1;
  150. return this.each(function (n, a) {
  151. new RegExp("\\b" + e + "\\b").test(a.className) && (t = !0)
  152. }), t
  153. }, C.prototype.attr = function (e, t) {
  154. var n = this;
  155. return void 0 === t ?
  156. function () {
  157. if (n.length > 0) return n[0].getAttribute(e)
  158. }() : n.each(function (n, a) {
  159. a.setAttribute(e, t)
  160. })
  161. }, C.prototype.removeAttr = function (e) {
  162. return this.each(function (t, n) {
  163. n.removeAttribute(e)
  164. })
  165. }, C.prototype.html = function (e) {
  166. return this.each(function (t, n) {
  167. n.innerHTML = e
  168. })
  169. }, C.prototype.val = function (e) {
  170. return this.each(function (t, n) {
  171. n.value = e
  172. })
  173. }, C.prototype.append = function (e) {
  174. return this.each(function (t, n) {
  175. "object" == typeof e ? n.appendChild(e) : n.innerHTML = n.innerHTML + e
  176. })
  177. }, C.prototype.remove = function (e) {
  178. return this.each(function (t, n) {
  179. e ? n.removeChild(e) : n.parentNode.removeChild(n)
  180. })
  181. }, C.prototype.on = function (e, t) {
  182. return this.each(function (n, a) {
  183. a.attachEvent ? a.attachEvent("on" + e, function (e) {
  184. e.target = e.srcElement, t.call(a, e)
  185. }) : a.addEventListener(e, t, !1)
  186. })
  187. }, C.prototype.off = function (e, t) {
  188. return this.each(function (n, a) {
  189. a.detachEvent ? a.detachEvent("on" + e, t) : a.removeEventListener(e, t, !1)
  190. })
  191. }, T.isLeapYear = function (e) {
  192. return e % 4 === 0 && e % 100 !== 0 || e % 400 === 0
  193. }, T.prototype.config = {
  194. type: "date",
  195. range: !1,
  196. format: "yyyy-MM-dd",
  197. value: null,
  198. min: "1900-1-1",
  199. max: "2099-12-31",
  200. trigger: "focus",
  201. show: !1,
  202. showBottom: !0,
  203. btns: ["clear", "now", "confirm"],
  204. lang: "cn",
  205. theme: "default",
  206. position: null,
  207. calendar: !1,
  208. mark: {},
  209. zIndex: null,
  210. done: null,
  211. change: null
  212. }, T.prototype.lang = function () {
  213. var e = this,
  214. t = e.config,
  215. n = {
  216. cn: {
  217. weeks: ["日", "一", "二", "三", "四", "五", "六"],
  218. time: ["时", "分", "秒"],
  219. timeTips: "选择时间",
  220. startTime: "开始时间",
  221. endTime: "结束时间",
  222. dateTips: "返回日期",
  223. month: ["一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二"],
  224. tools: {
  225. confirm: "确定",
  226. clear: "清空",
  227. now: "今天"
  228. }
  229. },
  230. en: {
  231. weeks: ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"],
  232. time: ["Hours", "Minutes", "Seconds"],
  233. timeTips: "Select Time",
  234. startTime: "Start Time",
  235. endTime: "End Time",
  236. dateTips: "Select Date",
  237. month: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
  238. tools: {
  239. confirm: "Confirm",
  240. clear: "Clear",
  241. now: "Now"
  242. }
  243. }
  244. };
  245. return n[t.lang] || n.cn
  246. }, T.prototype.init = function () {
  247. var e = this,
  248. t = e.config,
  249. n = "yyyy|y|MM|M|dd|d|HH|H|mm|m|ss|s",
  250. a = "static" === t.position,
  251. i = {
  252. year: "yyyy",
  253. month: "yyyy-MM",
  254. date: "yyyy-MM-dd",
  255. time: "HH:mm:ss",
  256. datetime: "yyyy-MM-dd HH:mm:ss"
  257. };
  258. t.elem = w(t.elem), t.eventElem = w(t.eventElem), t.elem[0] && (t.range === !0 && (t.range = "-"), t.format === i.date && (t.format = i[t.type]), e.format = t.format.match(new RegExp(n + "|.", "g")) || [], e.EXP_IF = "", e.EXP_SPLIT = "", w.each(e.format, function (t, a) {
  259. var i = new RegExp(n).test(a) ? "\\d{" +
  260. function () {
  261. return new RegExp(n).test(e.format[0 === t ? t + 1 : t - 1] || "") ? /^yyyy|y$/.test(a) ? 4 : a.length : /^yyyy$/.test(a) ? "1,4" : /^y$/.test(a) ? "1,308" : "1,2"
  262. }() + "}" : "\\" + a;
  263. e.EXP_IF = e.EXP_IF + i, e.EXP_SPLIT = e.EXP_SPLIT + "(" + i + ")"
  264. }), e.EXP_IF = new RegExp("^" + (t.range ? e.EXP_IF + "\\s\\" + t.range + "\\s" + e.EXP_IF : e.EXP_IF) + "$"), e.EXP_SPLIT = new RegExp("^" + e.EXP_SPLIT + "$", ""), e.isInput(t.elem[0]) || "focus" === t.trigger && (t.trigger = "click"), t.elem.attr("lay-key") || (t.elem.attr("lay-key", e.index), t.eventElem.attr("lay-key", e.index)), t.mark = w.extend({}, t.calendar && "cn" === t.lang ? {
  265. "0-1-1": "元旦",
  266. "0-2-14": "情人",
  267. "0-3-8": "妇女",
  268. "0-3-12": "植树",
  269. "0-4-1": "愚人",
  270. "0-5-1": "劳动",
  271. "0-5-4": "青年",
  272. "0-6-1": "儿童",
  273. "0-9-10": "教师",
  274. "0-9-18": "国耻",
  275. "0-10-1": "国庆",
  276. "0-12-25": "圣诞"
  277. } : {}, t.mark), w.each(["min", "max"], function (e, n) {
  278. var a = [],
  279. i = [];
  280. if ("number" == typeof t[n]) {
  281. var r = t[n],
  282. o = (new Date).getTime(),
  283. s = 864e5,
  284. l = new Date(r ? r < s ? o + r * s : r : o);
  285. a = [l.getFullYear(), l.getMonth() + 1, l.getDate()], r < s || (i = [l.getHours(), l.getMinutes(), l.getSeconds()])
  286. } else a = (t[n].match(/\d+-\d+-\d+/) || [""])[0].split("-"), i = (t[n].match(/\d+:\d+:\d+/) || [""])[0].split(":");
  287. t[n] = {
  288. year: 0 | a[0] || (new Date).getFullYear(),
  289. month: a[1] ? (0 | a[1]) - 1 : (new Date).getMonth(),
  290. date: 0 | a[2] || (new Date).getDate(),
  291. hours: 0 | i[0],
  292. minutes: 0 | i[1],
  293. seconds: 0 | i[2]
  294. }
  295. }), e.elemID = "layui-laydate" + t.elem.attr("lay-key"), (t.show || a) && e.render(), a || e.events(), t.value && (t.value.constructor === Date ? e.setValue(e.parse(0, e.systemDate(t.value))) : e.setValue(t.value)))
  296. }, T.prototype.render = function () {
  297. var e = this,
  298. t = e.config,
  299. n = e.lang(),
  300. a = "static" === t.position,
  301. i = e.elem = w.elem("div", {
  302. id: e.elemID,
  303. "class": ["layui-laydate", t.range ? " layui-laydate-range" : "", a ? " " + c : "", t.theme && "default" !== t.theme && !/^#/.test(t.theme) ? " laydate-theme-" + t.theme : ""].join("")
  304. }),
  305. r = e.elemMain = [],
  306. o = e.elemHeader = [],
  307. s = e.elemCont = [],
  308. l = e.table = [],
  309. d = e.footer = w.elem("div", {
  310. "class": p
  311. });
  312. if (t.zIndex && (i.style.zIndex = t.zIndex), w.each(new Array(2), function (e) {
  313. if (!t.range && e > 0) return !0;
  314. var a = w.elem("div", {
  315. "class": "layui-laydate-header"
  316. }),
  317. i = [function () {
  318. var e = w.elem("i", {
  319. "class": "layui-icon laydate-icon laydate-prev-y"
  320. });
  321. return e.innerHTML = "&#xe65a;", e
  322. }(), function () {
  323. var e = w.elem("i", {
  324. "class": "layui-icon laydate-icon laydate-prev-m"
  325. });
  326. return e.innerHTML = "&#xe603;", e
  327. }(), function () {
  328. var e = w.elem("div", {
  329. "class": "laydate-set-ym"
  330. }),
  331. t = w.elem("span"),
  332. n = w.elem("span");
  333. return e.appendChild(t), e.appendChild(n), e
  334. }(), function () {
  335. var e = w.elem("i", {
  336. "class": "layui-icon laydate-icon laydate-next-m"
  337. });
  338. return e.innerHTML = "&#xe602;", e
  339. }(), function () {
  340. var e = w.elem("i", {
  341. "class": "layui-icon laydate-icon laydate-next-y"
  342. });
  343. return e.innerHTML = "&#xe65b;", e
  344. }()],
  345. d = w.elem("div", {
  346. "class": "layui-laydate-content"
  347. }),
  348. c = w.elem("table"),
  349. m = w.elem("thead"),
  350. u = w.elem("tr");
  351. w.each(i, function (e, t) {
  352. a.appendChild(t)
  353. }), m.appendChild(u), w.each(new Array(6), function (e) {
  354. var t = c.insertRow(0);
  355. w.each(new Array(7), function (a) {
  356. if (0 === e) {
  357. var i = w.elem("th");
  358. i.innerHTML = n.weeks[a], u.appendChild(i)
  359. }
  360. t.insertCell(a)
  361. })
  362. }), c.insertBefore(m, c.children[0]), d.appendChild(c), r[e] = w.elem("div", {
  363. "class": "layui-laydate-main laydate-main-list-" + e
  364. }), r[e].appendChild(a), r[e].appendChild(d), o.push(i), s.push(d), l.push(c)
  365. }), w(d).html(function () {
  366. var e = [],
  367. i = [];
  368. return "datetime" === t.type && e.push('<span lay-type="datetime" class="laydate-btns-time">' + n.timeTips + "</span>"), w.each(t.btns, function (e, r) {
  369. var o = n.tools[r] || "btn";
  370. t.range && "now" === r || (a && "clear" === r && (o = "cn" === t.lang ? "重置" : "Reset"), i.push('<span lay-type="' + r + '" class="laydate-btns-' + r + '">' + o + "</span>"))
  371. }), e.push('<div class="laydate-footer-btns">' + i.join("") + "</div>"), e.join("")
  372. }()), w.each(r, function (e, t) {
  373. i.appendChild(t)
  374. }), t.showBottom && i.appendChild(d), /^#/.test(t.theme)) {
  375. var m = w.elem("style"),
  376. u = ["#{{id}} .layui-laydate-header{background-color:{{theme}};}", "#{{id}} .layui-this{background-color:{{theme}} !important;}"].join("").replace(/{{id}}/g, e.elemID).replace(/{{theme}}/g, t.theme);
  377. "styleSheet" in m ? (m.setAttribute("type", "text/css"), m.styleSheet.cssText = u) : m.innerHTML = u, w(i).addClass("laydate-theme-molv"), i.appendChild(m)
  378. }
  379. e.remove(T.thisElemDate), a ? t.elem.append(i) : (document.body.appendChild(i), e.position()), e.checkDate().calendar(), e.changeEvent(), T.thisElemDate = e.elemID, "function" == typeof t.ready && t.ready(w.extend({}, t.dateTime, {
  380. month: t.dateTime.month + 1
  381. }))
  382. }, T.prototype.remove = function (e) {
  383. var t = this,
  384. n = (t.config, w("#" + (e || t.elemID)));
  385. return n.hasClass(c) || t.checkDate(function () {
  386. n.remove()
  387. }), t
  388. }, T.prototype.position = function () {
  389. var e = this,
  390. t = e.config,
  391. n = e.bindElem || t.elem[0],
  392. a = n.getBoundingClientRect(),
  393. i = e.elem.offsetWidth,
  394. r = e.elem.offsetHeight,
  395. o = function (e) {
  396. return e = e ? "scrollLeft" : "scrollTop", document.body[e] | document.documentElement[e]
  397. },
  398. s = function (e) {
  399. return document.documentElement[e ? "clientWidth" : "clientHeight"]
  400. },
  401. l = 5,
  402. d = a.left,
  403. c = a.bottom;
  404. d + i + l > s("width") && (d = s("width") - i - l), c + r + l > s() && (c = a.top > r ? a.top - r : s() - r, c -= 2 * l), t.position && (e.elem.style.position = t.position), e.elem.style.left = d + ("fixed" === t.position ? 0 : o(1)) + "px", e.elem.style.top = c + ("fixed" === t.position ? 0 : o()) + "px"
  405. }, T.prototype.hint = function (e) {
  406. var t = this,
  407. n = (t.config, w.elem("div", {
  408. "class": h
  409. }));
  410. n.innerHTML = e || "", w(t.elem).find("." + h).remove(), t.elem.appendChild(n), clearTimeout(t.hinTimer), t.hinTimer = setTimeout(function () {
  411. w(t.elem).find("." + h).remove()
  412. }, 3e3)
  413. }, T.prototype.getAsYM = function (e, t, n) {
  414. return n ? t-- : t++, t < 0 && (t = 11, e--), t > 11 && (t = 0, e++), [e, t]
  415. }, T.prototype.systemDate = function (e) {
  416. var t = e || new Date;
  417. return {
  418. year: t.getFullYear(),
  419. month: t.getMonth(),
  420. date: t.getDate(),
  421. hours: e ? e.getHours() : 0,
  422. minutes: e ? e.getMinutes() : 0,
  423. seconds: e ? e.getSeconds() : 0
  424. }
  425. }, T.prototype.checkDate = function (e) {
  426. var t, a, i = this,
  427. r = (new Date, i.config),
  428. o = r.dateTime = r.dateTime || i.systemDate(),
  429. s = i.bindElem || r.elem[0],
  430. l = (i.isInput(s) ? "val" : "html", i.isInput(s) ? s.value : "static" === r.position ? "" : s.innerHTML),
  431. c = function (e) {
  432. e.year > d[1] && (e.year = d[1], a = !0), e.month > 11 && (e.month = 11, a = !0), e.hours > 23 && (e.hours = 0, a = !0), e.minutes > 59 && (e.minutes = 0, e.hours++, a = !0), e.seconds > 59 && (e.seconds = 0, e.minutes++, a = !0), t = n.getEndDate(e.month + 1, e.year), e.date > t && (e.date = t, a = !0)
  433. },
  434. m = function (e, t, n) {
  435. var o = ["startTime", "endTime"];
  436. t = (t.match(i.EXP_SPLIT) || []).slice(1), n = n || 0, r.range && (i[o[n]] = i[o[n]] || {}), w.each(i.format, function (s, l) {
  437. var c = parseFloat(t[s]);
  438. t[s].length < l.length && (a = !0), /yyyy|y/.test(l) ? (c < d[0] && (c = d[0], a = !0), e.year = c) : /MM|M/.test(l) ? (c < 1 && (c = 1, a = !0), e.month = c - 1) : /dd|d/.test(l) ? (c < 1 && (c = 1, a = !0), e.date = c) : /HH|H/.test(l) ? (c < 1 && (c = 0, a = !0), e.hours = c, r.range && (i[o[n]].hours = c)) : /mm|m/.test(l) ? (c < 1 && (c = 0, a = !0), e.minutes = c, r.range && (i[o[n]].minutes = c)) : /ss|s/.test(l) && (c < 1 && (c = 0, a = !0), e.seconds = c, r.range && (i[o[n]].seconds = c))
  439. }), c(e)
  440. };
  441. return "limit" === e ? (c(o), i) : (l = l || r.value, "string" == typeof l && (l = l.replace(/\s+/g, " ").replace(/^\s|\s$/g, "")), i.startState && !i.endState && (delete i.startState, i.endState = !0), "string" == typeof l && l ? i.EXP_IF.test(l) ? r.range ? (l = l.split(" " + r.range + " "), i.startDate = i.startDate || i.systemDate(), i.endDate = i.endDate || i.systemDate(), r.dateTime = w.extend({}, i.startDate), w.each([i.startDate, i.endDate], function (e, t) {
  442. m(t, l[e], e)
  443. })) : m(o, l) : (i.hint("日期格式不合法<br>必须遵循下述格式:<br>" + (r.range ? r.format + " " + r.range + " " + r.format : r.format) + "<br>已为你重置"), a = !0) : l && l.constructor === Date ? r.dateTime = i.systemDate(l) : (r.dateTime = i.systemDate(), delete i.startState, delete i.endState, delete i.startDate, delete i.endDate, delete i.startTime, delete i.endTime), c(o), a && l && i.setValue(r.range ? i.endDate ? i.parse() : "" : i.parse()), e && e(), i)
  444. }, T.prototype.mark = function (e, t) {
  445. var n, a = this,
  446. i = a.config;
  447. return w.each(i.mark, function (e, a) {
  448. var i = e.split("-");
  449. i[0] != t[0] && 0 != i[0] || i[1] != t[1] && 0 != i[1] || i[2] != t[2] || (n = a || t[2])
  450. }), n && e.html('<span class="laydate-day-mark">' + n + "</span>"), a
  451. }, T.prototype.limit = function (e, t, n, a) {
  452. var i, r = this,
  453. o = r.config,
  454. l = {},
  455. d = o[n > 41 ? "endDate" : "dateTime"],
  456. c = w.extend({}, d, t || {});
  457. return w.each({
  458. now: c,
  459. min: o.min,
  460. max: o.max
  461. }, function (e, t) {
  462. l[e] = r.newDate(w.extend({
  463. year: t.year,
  464. month: t.month,
  465. date: t.date
  466. }, function () {
  467. var e = {};
  468. return w.each(a, function (n, a) {
  469. e[a] = t[a]
  470. }), e
  471. }())).getTime()
  472. }), i = l.now < l.min || l.now > l.max, e && e[i ? "addClass" : "removeClass"](s), i
  473. }, T.prototype.calendar = function (e) {
  474. var t, a, i, r = this,
  475. s = r.config,
  476. l = e || s.dateTime,
  477. c = new Date,
  478. m = r.lang(),
  479. u = "date" !== s.type && "datetime" !== s.type,
  480. h = e ? 1 : 0,
  481. y = w(r.table[h]).find("td"),
  482. f = w(r.elemHeader[h][2]).find("span");
  483. if (l.year < d[0] && (l.year = d[0], r.hint("最低只能支持到公元" + d[0] + "年")), l.year > d[1] && (l.year = d[1], r.hint("最高只能支持到公元" + d[1] + "年")), r.firstDate || (r.firstDate = w.extend({}, l)), c.setFullYear(l.year, l.month, 1), t = c.getDay(), a = n.getEndDate(l.month || 12, l.year), i = n.getEndDate(l.month + 1, l.year), w.each(y, function (e, n) {
  484. var d = [l.year, l.month],
  485. c = 0;
  486. n = w(n), n.removeAttr("class"), e < t ? (c = a - t + e, n.addClass("laydate-day-prev"), d = r.getAsYM(l.year, l.month, "sub")) : e >= t && e < i + t ? (c = e - t, s.range || c + 1 === l.date && n.addClass(o)) : (c = e - i - t, n.addClass("laydate-day-next"), d = r.getAsYM(l.year, l.month)), d[1]++, d[2] = c + 1, n.attr("lay-ymd", d.join("-")).html(d[2]), r.mark(n, d).limit(n, {
  487. year: d[0],
  488. month: d[1] - 1,
  489. date: d[2]
  490. }, e)
  491. }), w(f[0]).attr("lay-ym", l.year + "-" + (l.month + 1)), w(f[1]).attr("lay-ym", l.year + "-" + (l.month + 1)), "cn" === s.lang ? (w(f[0]).attr("lay-type", "year").html(l.year + "年"), w(f[1]).attr("lay-type", "month").html(l.month + 1 + "月")) : (w(f[0]).attr("lay-type", "month").html(m.month[l.month]), w(f[1]).attr("lay-type", "year").html(l.year)), u && (s.range && (e ? r.endDate = r.endDate || {
  492. year: l.year + ("year" === s.type ? 1 : 0),
  493. month: l.month + ("month" === s.type ? 0 : -1)
  494. } : r.startDate = r.startDate || {
  495. year: l.year,
  496. month: l.month
  497. }, e && (r.listYM = [
  498. [r.startDate.year, r.startDate.month + 1],
  499. [r.endDate.year, r.endDate.month + 1]
  500. ], r.list(s.type, 0).list(s.type, 1), "time" === s.type ? r.setBtnStatus("时间", w.extend({}, r.systemDate(), r.startTime), w.extend({}, r.systemDate(), r.endTime)) : r.setBtnStatus(!0))), s.range || (r.listYM = [
  501. [l.year, l.month + 1]
  502. ], r.list(s.type, 0))), s.range && !e) {
  503. var p = r.getAsYM(l.year, l.month);
  504. r.calendar(w.extend({}, l, {
  505. year: p[0],
  506. month: p[1]
  507. }))
  508. }
  509. return s.range || r.limit(w(r.footer).find(g), null, 0, ["hours", "minutes", "seconds"]), s.range && e && !u && r.stampRange(), r
  510. }, T.prototype.list = function (e, t) {
  511. var n = this,
  512. a = n.config,
  513. i = a.dateTime,
  514. r = n.lang(),
  515. l = a.range && "date" !== a.type && "datetime" !== a.type,
  516. d = w.elem("ul", {
  517. "class": m + " " + {
  518. year: "laydate-year-list",
  519. month: "laydate-month-list",
  520. time: "laydate-time-list"
  521. }[e]
  522. }),
  523. c = n.elemHeader[t],
  524. u = w(c[2]).find("span"),
  525. h = n.elemCont[t || 0],
  526. y = w(h).find("." + m)[0],
  527. f = "cn" === a.lang,
  528. p = f ? "年" : "",
  529. T = n.listYM[t] || {},
  530. C = ["hours", "minutes", "seconds"],
  531. x = ["startTime", "endTime"][t];
  532. if (T[0] < 1 && (T[0] = 1), "year" === e) {
  533. var M, b = M = T[0] - 7;
  534. b < 1 && (b = M = 1), w.each(new Array(15), function (e) {
  535. var i = w.elem("li", {
  536. "lay-ym": M
  537. }),
  538. r = {
  539. year: M
  540. };
  541. M == T[0] && w(i).addClass(o), i.innerHTML = M + p, d.appendChild(i), M < n.firstDate.year ? (r.month = a.min.month, r.date = a.min.date) : M >= n.firstDate.year && (r.month = a.max.month, r.date = a.max.date), n.limit(w(i), r, t), M++
  542. }), w(u[f ? 0 : 1]).attr("lay-ym", M - 8 + "-" + T[1]).html(b + p + " - " + (M - 1 + p))
  543. } else if ("month" === e) w.each(new Array(12), function (e) {
  544. var i = w.elem("li", {
  545. "lay-ym": e
  546. }),
  547. s = {
  548. year: T[0],
  549. month: e
  550. };
  551. e + 1 == T[1] && w(i).addClass(o), i.innerHTML = r.month[e] + (f ? "月" : ""), d.appendChild(i), T[0] < n.firstDate.year ? s.date = a.min.date : T[0] >= n.firstDate.year && (s.date = a.max.date), n.limit(w(i), s, t)
  552. }), w(u[f ? 0 : 1]).attr("lay-ym", T[0] + "-" + T[1]).html(T[0] + p);
  553. else if ("time" === e) {
  554. var E = function () {
  555. w(d).find("ol").each(function (e, a) {
  556. w(a).find("li").each(function (a, i) {
  557. n.limit(w(i), [{
  558. hours: a
  559. }, {
  560. hours: n[x].hours,
  561. minutes: a
  562. }, {
  563. hours: n[x].hours,
  564. minutes: n[x].minutes,
  565. seconds: a
  566. }][e], t, [
  567. ["hours"],
  568. ["hours", "minutes"],
  569. ["hours", "minutes", "seconds"]
  570. ][e])
  571. })
  572. }), a.range || n.limit(w(n.footer).find(g), n[x], 0, ["hours", "minutes", "seconds"])
  573. };
  574. a.range ? n[x] || (n[x] = {
  575. hours: 0,
  576. minutes: 0,
  577. seconds: 0
  578. }) : n[x] = i, w.each([24, 60, 60], function (e, t) {
  579. var a = w.elem("li"),
  580. i = ["<p>" + r.time[e] + "</p><ol>"];
  581. w.each(new Array(t), function (t) {
  582. i.push("<li" + (n[x][C[e]] === t ? ' class="' + o + '"' : "") + ">" + w.digit(t, 2) + "</li>")
  583. }), a.innerHTML = i.join("") + "</ol>", d.appendChild(a)
  584. }), E()
  585. }
  586. if (y && h.removeChild(y), h.appendChild(d), "year" === e || "month" === e) w(n.elemMain[t]).addClass("laydate-ym-show"), w(d).find("li").on("click", function () {
  587. var r = 0 | w(this).attr("lay-ym");
  588. if (!w(this).hasClass(s)) {
  589. if (0 === t) i[e] = r, l && (n.startDate[e] = r), n.limit(w(n.footer).find(g), null, 0);
  590. else if (l) n.endDate[e] = r;
  591. else {
  592. var c = "year" === e ? n.getAsYM(r, T[1] - 1, "sub") : n.getAsYM(T[0], r, "sub");
  593. w.extend(i, {
  594. year: c[0],
  595. month: c[1]
  596. })
  597. }
  598. "year" === a.type || "month" === a.type ? (w(d).find("." + o).removeClass(o), w(this).addClass(o), "month" === a.type && "year" === e && (n.listYM[t][0] = r, l && (n[["startDate", "endDate"][t]].year = r), n.list("month", t))) : (n.checkDate("limit").calendar(), n.closeList()), n.setBtnStatus(), a.range || n.done(null, "change"), w(n.footer).find(D).removeClass(s)
  599. }
  600. });
  601. else {
  602. var S = w.elem("span", {
  603. "class": v
  604. }),
  605. k = function () {
  606. w(d).find("ol").each(function (e) {
  607. var t = this,
  608. a = w(t).find("li");
  609. t.scrollTop = 30 * (n[x][C[e]] - 2), t.scrollTop <= 0 && a.each(function (e, n) {
  610. if (!w(this).hasClass(s)) return t.scrollTop = 30 * (e - 2), !0
  611. })
  612. })
  613. },
  614. H = w(c[2]).find("." + v);
  615. k(), S.innerHTML = a.range ? [r.startTime, r.endTime][t] : r.timeTips, w(n.elemMain[t]).addClass("laydate-time-show"), H[0] && H.remove(), c[2].appendChild(S), w(d).find("ol").each(function (e) {
  616. var t = this;
  617. w(t).find("li").on("click", function () {
  618. var r = 0 | this.innerHTML;
  619. w(this).hasClass(s) || (a.range ? n[x][C[e]] = r : i[C[e]] = r, w(t).find("." + o).removeClass(o), w(this).addClass(o), E(), k(), (n.endDate || "time" === a.type) && n.done(null, "change"), n.setBtnStatus())
  620. })
  621. })
  622. }
  623. return n
  624. }, T.prototype.listYM = [], T.prototype.closeList = function () {
  625. var e = this;
  626. e.config;
  627. w.each(e.elemCont, function (t, n) {
  628. w(this).find("." + m).remove(), w(e.elemMain[t]).removeClass("laydate-ym-show laydate-time-show")
  629. }), w(e.elem).find("." + v).remove()
  630. }, T.prototype.setBtnStatus = function (e, t, n) {
  631. var a, i = this,
  632. r = i.config,
  633. o = w(i.footer).find(g),
  634. d = r.range && "date" !== r.type && "time" !== r.type;
  635. d && (t = t || i.startDate, n = n || i.endDate, a = i.newDate(t).getTime() > i.newDate(n).getTime(), i.limit(null, t) || i.limit(null, n) ? o.addClass(s) : o[a ? "addClass" : "removeClass"](s), e && a && i.hint("string" == typeof e ? l.replace(/日期/g, e) : l))
  636. }, T.prototype.parse = function (e, t) {
  637. var n = this,
  638. a = n.config,
  639. i = t || (e ? w.extend({}, n.endDate, n.endTime) : a.range ? w.extend({}, n.startDate, n.startTime) : a.dateTime),
  640. r = n.format.concat();
  641. return w.each(r, function (e, t) {
  642. /yyyy|y/.test(t) ? r[e] = w.digit(i.year, t.length) : /MM|M/.test(t) ? r[e] = w.digit(i.month + 1, t.length) : /dd|d/.test(t) ? r[e] = w.digit(i.date, t.length) : /HH|H/.test(t) ? r[e] = w.digit(i.hours, t.length) : /mm|m/.test(t) ? r[e] = w.digit(i.minutes, t.length) : /ss|s/.test(t) && (r[e] = w.digit(i.seconds, t.length))
  643. }), a.range && !e ? r.join("") + " " + a.range + " " + n.parse(1) : r.join("")
  644. }, T.prototype.newDate = function (e) {
  645. return e = e || {}, new Date(e.year || 1, e.month || 0, e.date || 1, e.hours || 0, e.minutes || 0, e.seconds || 0)
  646. }, T.prototype.setValue = function (e) {
  647. var t = this,
  648. n = t.config,
  649. a = t.bindElem || n.elem[0],
  650. i = t.isInput(a) ? "val" : "html";
  651. return "static" === n.position || w(a)[i](e || ""), this
  652. }, T.prototype.stampRange = function () {
  653. var e, t, n = this,
  654. a = n.config,
  655. i = w(n.elem).find("td");
  656. if (a.range && !n.endDate && w(n.footer).find(g).addClass(s), n.endDate) return e = n.newDate({
  657. year: n.startDate.year,
  658. month: n.startDate.month,
  659. date: n.startDate.date
  660. }).getTime(), t = n.newDate({
  661. year: n.endDate.year,
  662. month: n.endDate.month,
  663. date: n.endDate.date
  664. }).getTime(), e > t ? n.hint(l) : void w.each(i, function (a, i) {
  665. var r = w(i).attr("lay-ymd").split("-"),
  666. s = n.newDate({
  667. year: r[0],
  668. month: r[1] - 1,
  669. date: r[2]
  670. }).getTime();
  671. w(i).removeClass(u + " " + o), s !== e && s !== t || w(i).addClass(w(i).hasClass(y) || w(i).hasClass(f) ? u : o), s > e && s < t && w(i).addClass(u)
  672. })
  673. }, T.prototype.done = function (e, t) {
  674. var n = this,
  675. a = n.config,
  676. i = w.extend({}, n.startDate ? w.extend(n.startDate, n.startTime) : a.dateTime),
  677. r = w.extend({}, w.extend(n.endDate, n.endTime));
  678. return w.each([i, r], function (e, t) {
  679. "month" in t && w.extend(t, {
  680. month: t.month + 1
  681. })
  682. }), e = e || [n.parse(), i, r], "function" == typeof a[t || "done"] && a[t || "done"].apply(a, e), n
  683. }, T.prototype.choose = function (e) {
  684. var t = this,
  685. n = t.config,
  686. a = n.dateTime,
  687. i = w(t.elem).find("td"),
  688. r = e.attr("lay-ymd").split("-"),
  689. l = function (e) {
  690. new Date;
  691. e && w.extend(a, r), n.range && (t.startDate ? w.extend(t.startDate, r) : t.startDate = w.extend({}, r, t.startTime), t.startYMD = r)
  692. };
  693. if (r = {
  694. year: 0 | r[0],
  695. month: (0 | r[1]) - 1,
  696. date: 0 | r[2]
  697. }, !e.hasClass(s)) if (n.range) {
  698. if (w.each(["startTime", "endTime"], function (e, n) {
  699. t[n] = t[n] || {
  700. hours: 0,
  701. minutes: 0,
  702. seconds: 0
  703. }
  704. }), t.endState) l(), delete t.endState, delete t.endDate, t.startState = !0, i.removeClass(o + " " + u), e.addClass(o);
  705. else if (t.startState) {
  706. if (e.addClass(o), t.endDate ? w.extend(t.endDate, r) : t.endDate = w.extend({}, r, t.endTime), t.newDate(r).getTime() < t.newDate(t.startYMD).getTime()) {
  707. var d = w.extend({}, t.endDate, {
  708. hours: t.startDate.hours,
  709. minutes: t.startDate.minutes,
  710. seconds: t.startDate.seconds
  711. });
  712. w.extend(t.endDate, t.startDate, {
  713. hours: t.endDate.hours,
  714. minutes: t.endDate.minutes,
  715. seconds: t.endDate.seconds
  716. }), t.startDate = d
  717. }
  718. n.showBottom || t.done(), t.stampRange(), t.endState = !0, t.done(null, "change")
  719. } else e.addClass(o), l(), t.startState = !0;
  720. w(t.footer).find(g)[t.endDate ? "removeClass" : "addClass"](s)
  721. } else "static" === n.position ? (l(!0), t.calendar().done().done(null, "change")) : "date" === n.type ? (l(!0), t.setValue(t.parse()).remove().done()) : "datetime" === n.type && (l(!0), t.calendar().done(null, "change"))
  722. }, T.prototype.tool = function (e, t) {
  723. var n = this,
  724. a = n.config,
  725. i = a.dateTime,
  726. r = "static" === a.position,
  727. o = {
  728. datetime: function () {
  729. w(e).hasClass(s) || (n.list("time", 0), a.range && n.list("time", 1), w(e).attr("lay-type", "date").html(n.lang().dateTips))
  730. },
  731. date: function () {
  732. n.closeList(), w(e).attr("lay-type", "datetime").html(n.lang().timeTips)
  733. },
  734. clear: function () {
  735. n.setValue("").remove(), r && (w.extend(i, n.firstDate), n.calendar()), a.range && (delete n.startState, delete n.endState, delete n.endDate, delete n.startTime, delete n.endTime), n.done(["",
  736. {}, {}])
  737. },
  738. now: function () {
  739. var e = new Date;
  740. w.extend(i, n.systemDate(), {
  741. hours: e.getHours(),
  742. minutes: e.getMinutes(),
  743. seconds: e.getSeconds()
  744. }), n.setValue(n.parse()).remove(), r && n.calendar(), n.done()
  745. },
  746. confirm: function () {
  747. if (a.range) {
  748. if (!n.endDate) return n.hint("请先选择日期范围");
  749. if (w(e).hasClass(s)) return n.hint("time" === a.type ? l.replace(/日期/g, "时间") : l)
  750. } else if (w(e).hasClass(s)) return n.hint("不在有效日期或时间范围内");
  751. n.done(), n.setValue(n.parse()).remove()
  752. }
  753. };
  754. o[t] && o[t]()
  755. }, T.prototype.change = function (e) {
  756. var t = this,
  757. n = t.config,
  758. a = n.dateTime,
  759. i = n.range && ("year" === n.type || "month" === n.type),
  760. r = t.elemCont[e || 0],
  761. o = t.listYM[e],
  762. s = function (s) {
  763. var l = ["startDate", "endDate"][e],
  764. d = w(r).find(".laydate-year-list")[0],
  765. c = w(r).find(".laydate-month-list")[0];
  766. return d && (o[0] = s ? o[0] - 15 : o[0] + 15, t.list("year", e)), c && (s ? o[0]-- : o[0]++, t.list("month", e)), (d || c) && (w.extend(a, {
  767. year: o[0]
  768. }), i && (t[l].year = o[0]), n.range || t.done(null, "change"), t.setBtnStatus(), n.range || t.limit(w(t.footer).find(g), {
  769. year: o[0]
  770. })), d || c
  771. };
  772. return {
  773. prevYear: function () {
  774. s("sub") || (a.year--, t.checkDate("limit").calendar(), n.range || t.done(null, "change"))
  775. },
  776. prevMonth: function () {
  777. var e = t.getAsYM(a.year, a.month, "sub");
  778. w.extend(a, {
  779. year: e[0],
  780. month: e[1]
  781. }), t.checkDate("limit").calendar(), n.range || t.done(null, "change")
  782. },
  783. nextMonth: function () {
  784. var e = t.getAsYM(a.year, a.month);
  785. w.extend(a, {
  786. year: e[0],
  787. month: e[1]
  788. }), t.checkDate("limit").calendar(), n.range || t.done(null, "change")
  789. },
  790. nextYear: function () {
  791. s() || (a.year++, t.checkDate("limit").calendar(), n.range || t.done(null, "change"))
  792. }
  793. }
  794. }, T.prototype.changeEvent = function () {
  795. var e = this;
  796. e.config;
  797. w(e.elem).on("click", function (e) {
  798. w.stope(e)
  799. }), w.each(e.elemHeader, function (t, n) {
  800. w(n[0]).on("click", function (n) {
  801. e.change(t).prevYear()
  802. }), w(n[1]).on("click", function (n) {
  803. e.change(t).prevMonth()
  804. }), w(n[2]).find("span").on("click", function (n) {
  805. var a = w(this),
  806. i = a.attr("lay-ym"),
  807. r = a.attr("lay-type");
  808. i && (i = i.split("-"), e.listYM[t] = [0 | i[0], 0 | i[1]], e.list(r, t), w(e.footer).find(D).addClass(s))
  809. }), w(n[3]).on("click", function (n) {
  810. e.change(t).nextMonth()
  811. }), w(n[4]).on("click", function (n) {
  812. e.change(t).nextYear()
  813. })
  814. }), w.each(e.table, function (t, n) {
  815. var a = w(n).find("td");
  816. a.on("click", function () {
  817. e.choose(w(this))
  818. })
  819. })
  820. /*点击选择月份——开始*/
  821. , w.each(w(e.elem).find("ul"), function (t, n) {
  822. var a = w(n).find("li");
  823. a.on("click", function () {
  824. e.done(),e.setValue(e.parse()).remove();
  825. })
  826. })
  827. /*点击选择月份——结束*/
  828. , w(e.footer).find("span").on("click", function () {
  829. var t = w(this).attr("lay-type");
  830. e.tool(this, t)
  831. })
  832. }, T.prototype.isInput = function (e) {
  833. return /input|textarea/.test(e.tagName.toLocaleLowerCase())
  834. }, T.prototype.events = function () {
  835. var e = this,
  836. t = e.config,
  837. n = function (n, a) {
  838. n.on(t.trigger, function () {
  839. a && (e.bindElem = this), e.render()
  840. })
  841. };
  842. t.elem[0] && !t.elem[0].eventHandler && (n(t.elem, "bind"), n(t.eventElem), w(document).on("click", function (n) {
  843. n.target !== t.elem[0] && n.target !== t.eventElem[0] && n.target !== w(t.closeStop)[0] && e.remove()
  844. }).on("keydown", function (t) {
  845. 13 === t.keyCode && w("#" + e.elemID)[0] && e.elemID === T.thisElem && (t.preventDefault(), w(e.footer).find(g)[0].click())
  846. }), w(window).on("resize", function () {
  847. return !(!e.elem || !w(r)[0]) && void e.position()
  848. }), t.elem[0].eventHandler = !0)
  849. }, n.render = function (e) {
  850. var t = new T(e);
  851. return a.call(t)
  852. }, n.getEndDate = function (e, t) {
  853. var n = new Date;
  854. return n.setFullYear(t || n.getFullYear(), e || n.getMonth() + 1, 1), new Date(n.getTime() - 864e5).getDate()
  855. }, window.lay = window.lay || w, e ? (n.ready(), layui.define(function (e) {
  856. n.path = layui.cache.dir, e(i, n)
  857. })) : "function" == typeof define && define.amd ? define(function () {
  858. return n
  859. }) : function () {
  860. n.ready(), window.laydate = n
  861. }()
  862. }();