Нет описания

date.js 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. $.use(["jQuery", "form", "doc", "util", "dropdown"], function($, form, doc, util, dd) {
  2. var m_rd = "必选的",
  3. readOnly = "readOnly",
  4. dd_ctn = "dd-ctn",
  5. dd_clean = "dd-clean",
  6. showOnly = "showOnly",
  7. modelName = 'date',
  8. def = "defVal",
  9. placeholder = "placeholder",
  10. required = "required",
  11. date_year = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
  12. date_leapYear = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
  13. date_isleap = function(y) {
  14. return(y % 4 === 0 && y % 100 !== 0) || y % 400 === 0;
  15. },
  16. date_validDate = function(v) {
  17. if(v.length != 8) return false;
  18. for(var i = 0; i < 8; ++i) {
  19. var c = v.charAt(i);
  20. if(c > '9' || c < '0') return false;
  21. }
  22. var m = parseInt(v.substring(4, 6)) - 1;
  23. if(m < 0 || m > 11) return false;
  24. var d = parseInt(v.substring(6, 8));
  25. var ym = date_isleap(parseInt(v.substring(0, 4))) ? date_leapYear : date_year;
  26. if(d < 1 || d > ym[m]) return false;
  27. return true;
  28. },
  29. date_fmt = function(v) {
  30. return(v > 9 ? "" : "0") + v;
  31. },
  32. date_MS_IN_DAY = 24 * 60 * 60 * 1000,
  33. date_incDay = function(d) {
  34. return new Date(d.getTime() + date_MS_IN_DAY);
  35. },
  36. date_decDay = function(d) {
  37. return new Date(d.getTime() - date_MS_IN_DAY);
  38. },
  39. date_dayFmt = function(d) {
  40. return "" + d.getFullYear() + date_fmt(d.getMonth() + 1) + date_fmt(d.getDate());
  41. },
  42. date_now = function() {
  43. var d = new Date();
  44. return "" + d.getFullYear() + date_fmt(d.getMonth() + 1) + date_fmt(d.getDate()) + date_fmt(d.getHours()) + date_fmt(d.getMinutes()) + date_fmt(d.getSeconds());
  45. },
  46. table_year = { an: "year", av: "" },
  47. table_month = { an: "month", av: "" },
  48. daySwitch = [""],
  49. thead = {
  50. tn: "thead",
  51. chs: [{
  52. tn: "tr",
  53. chs: [
  54. { tn: "th", attrs: [{ an: "class", av: "prev-year" }], chs: [{ tn: "i", attrs: [{ an: "class", av: "icon-arrow-left" }] }] },
  55. { tn: "th", attrs: [{ an: "class", av: "prev-month" }], chs: [{ tn: "i", attrs: [{ an: "class", av: "icon-chevron-left" }] }] },
  56. { tn: "th", attrs: [{ an: "class", av: "switch" }, { an: "colspan", av: "3" }], chs: daySwitch },
  57. { tn: "th", attrs: [{ an: "class", av: "next-month" }], chs: [{ tn: "i", attrs: [{ an: "class", av: "icon-chevron-right" }] }] },
  58. { tn: "th", attrs: [{ an: "class", av: "next-year" }], chs: [{ tn: "i", attrs: [{ an: "class", av: "icon-arrow-right" }] }] }
  59. ]
  60. }, {
  61. tn: "tr",
  62. chs: [
  63. { tn: "th", attrs: [{ an: "class", av: "dow" }], chs: ["日"] },
  64. { tn: "th", attrs: [{ an: "class", av: "dow" }], chs: ["一"] },
  65. { tn: "th", attrs: [{ an: "class", av: "dow" }], chs: ["二"] },
  66. { tn: "th", attrs: [{ an: "class", av: "dow" }], chs: ["三"] },
  67. { tn: "th", attrs: [{ an: "class", av: "dow" }], chs: ["四"] },
  68. { tn: "th", attrs: [{ an: "class", av: "dow" }], chs: ["五"] },
  69. { tn: "th", attrs: [{ an: "class", av: "dow" }], chs: ["六"] },
  70. ]
  71. }]
  72. },
  73. tbody = { tn: "tbody" },
  74. tfoot = { tn: "tfoot" },
  75. table = { tn: "table", attrs: [{ an: "class", av: "day" }, table_year, table_month], chs: [thead, tbody, tfoot] },
  76. date_drop = { tn: "div", attrs: [{ an: "class", av: "dd-drop date" }], chs: [table] },
  77. show_day = function($e, y, m) {
  78. table_year.av = "" + y;
  79. table_month.av = "" + (m + 1);
  80. daySwitch[0] = date_fmt(m + 1) + "月 " + y + "年";
  81. var dd = new Date();
  82. dd.setFullYear(y);
  83. dd.setMonth(m);
  84. dd.setDate(1);
  85. var dayInWeek = dd.getDay();
  86. tbody.chs = [];
  87. var weekDays = { tn: "tr" };
  88. var days = weekDays.chs = [];
  89. for(var i = 0; i < dayInWeek; ++i) {
  90. var cd = date_dayFmt(new Date(dd.getTime() - ((dayInWeek - i) * date_MS_IN_DAY)));
  91. days.push({ tn: "td", attrs: [{ an: "class", av: "no-current-month" }, { an: "day", av: "" + cd }], chs: [cd.substring(6, 8)] });
  92. }
  93. var ld = date_isleap(y) ? date_leapYear[m] : date_year[m];
  94. for(var i = 0; i < ld; ++i) {
  95. var cd = date_dayFmt(dd);
  96. days.push({ tn: "td", attrs: [{ an: "class", av: "day-item" }, { an: "day", av: "" + cd }], chs: [cd.substring(6, 8)] });
  97. if(dd.getDay() == 6) {
  98. tbody.chs.push(weekDays);
  99. weekDays = { tn: "tr" };
  100. days = weekDays.chs = [];
  101. }
  102. dd = date_incDay(dd);
  103. }
  104. dayInWeek = dd.getDay();
  105. if(dayInWeek) {
  106. for(var i = dayInWeek; i < 7; ++i) {
  107. var cd = date_dayFmt(dd);
  108. days.push({ tn: "td", attrs: [{ an: "class", av: "no-current-month" }, { an: "day", av: "" + cd }], chs: [cd.substring(6, 8)] });
  109. dd = date_incDay(dd);
  110. }
  111. tbody.chs.push(weekDays);
  112. }
  113. $e.find(".dd-drop").remove();
  114. util.appendChild($e[0], date_drop);
  115. },
  116. handCode = { an: "code", av: "" },
  117. pa = [""],
  118. jhref = { an: "href", av: "javascript:;" },
  119. dd_hand = { an: "class", av: "dd-hand" },
  120. icon_drop = { tn: "i", attrs: [{ an: "class", av: "icon icon-drop" }] },
  121. icon_close = { tn: "i", attrs: [{ an: "class", av: "icon icon-close" }] },
  122. a_placeHolder = { tn: "a", attrs: [jhref, { an: "class", av: placeholder }], chs: pa },
  123. hand = {
  124. tn: "a",
  125. attrs: [
  126. dd_hand,
  127. jhref,
  128. handCode,
  129. ],
  130. chs: [
  131. icon_drop,
  132. { tn: "span" },
  133. icon_close,
  134. a_placeHolder,
  135. ]
  136. };
  137. form.register(function($e) {
  138. var cls = util.classCheck($e[0], [readOnly, showOnly, modelName, required]),
  139. rv;
  140. if(cls[modelName]) {
  141. var n = $e.attr("name") || $e.attr("id"),
  142. rules = [];
  143. if(!n) {
  144. throw "Attribute[name] is invalid";
  145. }
  146. var dv = $e.attr(def) || "";
  147. $e.empty().addClass(dd_ctn).addClass(dd_clean);
  148. pa[0] = $e.attr(placeholder) || "请选择日期......";
  149. util.appendChild($e[0], hand);
  150. var $h = $e.children("a");
  151. var $span = $h.children("span");
  152. var date_change = function(val) {
  153. if(val) {
  154. if(val == "current") {
  155. val = date_now().substr(0, 8);
  156. }
  157. var dt = val.substring(0, 4) + "-" + val.substring(4, 6) + "-" + val.substring(6, 8);
  158. if(rv !== val) {
  159. $h.attr("code", val);
  160. $span.text(dt);
  161. rv = val;
  162. }
  163. } else {
  164. rv = "";
  165. $h.attr("code", "");
  166. $span.text("");
  167. }
  168. };
  169. date_change(dv);
  170. if(!(cls[readOnly] || cls[showOnly])) {
  171. $e.on("shown.dropdown", function(evt) {
  172. var dp_chs = [];
  173. var n = date_now();
  174. var val = rv ? (date_validDate(rv) ? rv : n) : n;
  175. show_day($e, parseInt(val.substring(0, 4)), parseInt(val.substring(4, 6)) - 1);
  176. });
  177. $e.find(".icon-close").on("click", function(evt) {
  178. date_change("");
  179. $e.addClass("dd-hold-once");
  180. });
  181. $e.on("click", ".day .day-item , .day .no-current-month", function() {
  182. date_change(this.getAttribute("day"));
  183. });
  184. $e.on("click", ".day .prev-year", function(e) {
  185. var date_tb = $e.find("table"),
  186. yv = parseInt(date_tb.attr("year")) - 1,
  187. mv = parseInt(date_tb.attr("month")) - 1;
  188. $e.addClass("dd-hold-once");
  189. show_day($e, yv, mv);
  190. });
  191. $e.on("click", ".day .prev-month", function(e) {
  192. var date_tb = $e.find("table"),
  193. yv = parseInt(date_tb.attr("year")),
  194. mv = parseInt(date_tb.attr("month")) - 2;
  195. if(0 > mv) {
  196. yv -= 1;
  197. mv = 11
  198. }
  199. $e.addClass("dd-hold-once");
  200. show_day($e, yv, mv);
  201. });
  202. $e.on("click", ".day .next-year", function(e) {
  203. var date_tb = $e.find("table"),
  204. yv = parseInt(date_tb.attr("year")) + 1,
  205. mv = parseInt(date_tb.attr("month")) - 1;
  206. $e.addClass("dd-hold-once");
  207. show_day($e, yv, mv);
  208. });
  209. $e.on("click", ".day .next-month", function(e) {
  210. var date_tb = $e.find("table"),
  211. yv = parseInt(date_tb.attr("year")),
  212. mv = parseInt(date_tb.attr("month"));
  213. if(mv === 12) {
  214. yv += 1;
  215. mv = 0
  216. }
  217. $e.addClass("dd-hold-once");
  218. show_day($e, yv, mv);
  219. });
  220. }
  221. return {
  222. name: n,
  223. get: function() {
  224. return rv ? rv : undefined;
  225. },
  226. set: function(data) {
  227. date_change(data ? data : "");
  228. },
  229. validate: function() {
  230. if(cls[required]) {
  231. if(!rv) {
  232. this.invalid(m_rd);
  233. return m_rd;
  234. }
  235. }
  236. return util.validate(rules, this);
  237. },
  238. addRules: function(rule) {
  239. util.addRules(rules, rule);
  240. },
  241. reset: function() {
  242. this.set(dv);
  243. },
  244. valid: function() { util.valid($e); },
  245. invalid: function(reson) {
  246. util.invalid($e);
  247. util.error(reson);
  248. }
  249. };
  250. }
  251. });
  252. });