jquery.dateinputpack.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. DateInput = (function($) {
  2. function DateInput(el, opts) {
  3. if (typeof(opts) != "object") opts = {};
  4. $.extend(this, DateInput.DEFAULT_OPTS, opts);
  5. this.input = $(el);
  6. this.bindMethodsToObj("show", "hide", "hideIfClickOutside", "keydownHandler", "selectDate");
  7. this.build();
  8. this.selectDate();
  9. this.hide()
  10. };
  11. DateInput.DEFAULT_OPTS = {
  12. month_names: ["一月份", "二月份", "三月份", "四月份", "五月份", "六月份", "七月份", "八月份", "九月份", "十月份", "十一月份", "十二月份"],
  13. short_month_names: ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"],
  14. short_day_names: ["日", "一", "二", "三", "四", "五", "六"],
  15. start_of_week: 1
  16. };
  17. DateInput.prototype = {
  18. build: function() {
  19. var monthNav = $('<p class="month_nav">' + '<span class="button prev" title="[Page-Up]">&#171;</span>' + ' <span class="month_name"></span> ' + '<span class="button next" title="[Page-Down]">&#187;</span>' + '</p>');
  20. this.monthNameSpan = $(".month_name", monthNav);
  21. $(".prev", monthNav).click(this.bindToObj(function() {
  22. this.moveMonthBy( - 1)
  23. }));
  24. $(".next", monthNav).click(this.bindToObj(function() {
  25. this.moveMonthBy(1)
  26. }));
  27. var yearNav = $('<p class="year_nav">' + '<span class="button prev" title="[Ctrl+Page-Up]">&#171;</span>' + ' <span class="year_name"></span> ' + '<span class="button next" title="[Ctrl+Page-Down]">&#187;</span>' + '</p>');
  28. this.yearNameSpan = $(".year_name", yearNav);
  29. $(".prev", yearNav).click(this.bindToObj(function() {
  30. this.moveMonthBy( - 12)
  31. }));
  32. $(".next", yearNav).click(this.bindToObj(function() {
  33. this.moveMonthBy(12)
  34. }));
  35. var nav = $('<div class="nav"></div>').append(monthNav, yearNav);
  36. var tableShell = "<table><thead><tr>";
  37. $(this.adjustDays(this.short_day_names)).each(function() {
  38. tableShell += "<th>" + this + "</th>"
  39. });
  40. tableShell += "</tr></thead><tbody></tbody></table>";
  41. this.dateSelector = this.rootLayers = $('<div class="date_selector"></div>').append(nav, tableShell).insertAfter(this.input);
  42. if ($.browser.msie && $.browser.version < 7) {
  43. this.ieframe = $('<iframe class="date_selector_ieframe" frameborder="0" src="#"></iframe>').insertBefore(this.dateSelector);
  44. this.rootLayers = this.rootLayers.add(this.ieframe);
  45. $(".button", nav).mouseover(function() {
  46. $(this).addClass("hover")
  47. });
  48. $(".button", nav).mouseout(function() {
  49. $(this).removeClass("hover")
  50. })
  51. };
  52. this.tbody = $("tbody", this.dateSelector);
  53. this.input.change(this.bindToObj(function() {
  54. this.selectDate()
  55. }));
  56. this.selectDate()
  57. },
  58. selectMonth: function(date) {
  59. var newMonth = new Date(date.getFullYear(), date.getMonth(), 1);
  60. if (!this.currentMonth || !(this.currentMonth.getFullYear() == newMonth.getFullYear() && this.currentMonth.getMonth() == newMonth.getMonth())) {
  61. this.currentMonth = newMonth;
  62. var rangeStart = this.rangeStart(date),
  63. rangeEnd = this.rangeEnd(date);
  64. var numDays = this.daysBetween(rangeStart, rangeEnd);
  65. var dayCells = "";
  66. for (var i = 0; i <= numDays; i++) {
  67. var currentDay = new Date(rangeStart.getFullYear(), rangeStart.getMonth(), rangeStart.getDate() + i, 12, 00);
  68. if (this.isFirstDayOfWeek(currentDay)) dayCells += "<tr>";
  69. if (currentDay.getMonth() == date.getMonth()) {
  70. dayCells += '<td class="selectable_day" date="' + this.dateToString(currentDay) + '">' + currentDay.getDate() + '</td>'
  71. } else {
  72. dayCells += '<td class="unselected_month" date="' + this.dateToString(currentDay) + '">' + currentDay.getDate() + '</td>'
  73. };
  74. if (this.isLastDayOfWeek(currentDay)) dayCells += "</tr>"
  75. };
  76. this.tbody.empty().append(dayCells);
  77. this.monthNameSpan.empty().append(this.monthName(date));
  78. this.yearNameSpan.empty().append(this.currentMonth.getFullYear());
  79. $(".selectable_day", this.tbody).click(this.bindToObj(function(event) {
  80. this.changeInput($(event.target).attr("date"))
  81. }));
  82. $("td[date=" + this.dateToString(new Date()) + "]", this.tbody).addClass("today");
  83. $("td.selectable_day", this.tbody).mouseover(function() {
  84. $(this).addClass("hover")
  85. });
  86. $("td.selectable_day", this.tbody).mouseout(function() {
  87. $(this).removeClass("hover")
  88. })
  89. };
  90. $('.selected', this.tbody).removeClass("selected");
  91. $('td[date=' + this.selectedDateString + ']', this.tbody).addClass("selected")
  92. },
  93. selectDate: function(date) {
  94. if (typeof(date) == "undefined") {
  95. date = this.stringToDate(this.input.val())
  96. };
  97. if (!date) date = new Date();
  98. this.selectedDate = date;
  99. this.selectedDateString = this.dateToString(this.selectedDate);
  100. this.selectMonth(this.selectedDate)
  101. },
  102. changeInput: function(dateString) {
  103. this.input.val(dateString).change();
  104. this.hide()
  105. },
  106. show: function() {
  107. //this.rootLayers.css("display", "block");
  108. this.rootLayers.addClass("foucs");
  109. //this.rootLayers.fadeIn();
  110. $([window, document.body]).click(this.hideIfClickOutside);
  111. this.input.unbind("focus", this.show);
  112. $(document.body).keydown(this.keydownHandler);
  113. this.setPosition()
  114. },
  115. hide: function() {
  116. //this.rootLayers.css("display", "none");
  117. this.rootLayers.removeClass("foucs");
  118. //this.rootLayers.fadeOut();
  119. $([window, document.body]).unbind("click", this.hideIfClickOutside);
  120. this.input.focus(this.show);
  121. $(document.body).unbind("keydown", this.keydownHandler)
  122. },
  123. hideIfClickOutside: function(event) {
  124. if (event.target != this.input[0] && !this.insideSelector(event)) {
  125. this.hide();
  126. }
  127. },
  128. insideSelector: function(event) {
  129. var totop=this.dateSelector.offset().top;
  130. var toleft=this.dateSelector.offset().left;
  131. var top=totop;
  132. var bottom=totop +60;
  133. var left=toleft;
  134. var right=toleft+ this.dateSelector.outerWidth();
  135. return event.pageY < bottom && event.pageY > top && event.pageX < right && event.pageX > left
  136. },
  137. keydownHandler: function(event) {
  138. switch (event.keyCode) {
  139. case 9:
  140. case 27:
  141. this.hide();
  142. return;
  143. break;
  144. case 13:
  145. this.changeInput(this.selectedDateString);
  146. break;
  147. case 33:
  148. this.moveDateMonthBy(event.ctrlKey ? -12 : -1);
  149. break;
  150. case 34:
  151. this.moveDateMonthBy(event.ctrlKey ? 12 : 1);
  152. break;
  153. case 38:
  154. this.moveDateBy( - 7);
  155. break;
  156. case 40:
  157. this.moveDateBy(7);
  158. break;
  159. case 37:
  160. this.moveDateBy( - 1);
  161. break;
  162. case 39:
  163. this.moveDateBy(1);
  164. break;
  165. default:
  166. return
  167. }
  168. event.preventDefault()
  169. },
  170. stringToDate: function(string) {
  171. var matches;
  172. if (matches = string.match(/^(\d{1,2}) ([^\s]+) (\d{4,4})$/)) {
  173. return new Date(matches[3], this.shortMonthNum(matches[2]), matches[1], 12, 00)
  174. } else {
  175. return null
  176. }
  177. },
  178. dateToString: function(date) {
  179. return date.getFullYear()+"-"+this.short_month_names[date.getMonth()]+"-" +date.getDate()
  180. },
  181. setPosition: function() {
  182. var offset = this.input.offset();
  183. this.rootLayers.css({
  184. /*top: offset.top + this.input.outerHeight(),
  185. left: offset.left*/
  186. });
  187. if (this.ieframe) {
  188. this.ieframe.css({
  189. width: this.dateSelector.outerWidth(),
  190. height: this.dateSelector.outerHeight()
  191. })
  192. }
  193. },
  194. moveDateBy: function(amount) {
  195. var newDate = new Date(this.selectedDate.getFullYear(), this.selectedDate.getMonth(), this.selectedDate.getDate() + amount);
  196. this.selectDate(newDate)
  197. },
  198. moveDateMonthBy: function(amount) {
  199. var newDate = new Date(this.selectedDate.getFullYear(), this.selectedDate.getMonth() + amount, this.selectedDate.getDate());
  200. if (newDate.getMonth() == this.selectedDate.getMonth() + amount + 1) {
  201. newDate.setDate(0)
  202. };
  203. this.selectDate(newDate)
  204. },
  205. moveMonthBy: function(amount) {
  206. var newMonth = new Date(this.currentMonth.getFullYear(), this.currentMonth.getMonth() + amount, this.currentMonth.getDate());
  207. this.selectMonth(newMonth)
  208. },
  209. monthName: function(date) {
  210. return this.month_names[date.getMonth()]
  211. },
  212. bindToObj: function(fn) {
  213. var self = this;
  214. return function() {
  215. return fn.apply(self, arguments)
  216. }
  217. },
  218. bindMethodsToObj: function() {
  219. for (var i = 0; i < arguments.length; i++) {
  220. this[arguments[i]] = this.bindToObj(this[arguments[i]])
  221. }
  222. },
  223. indexFor: function(array, value) {
  224. for (var i = 0; i < array.length; i++) {
  225. if (value == array[i]) return i
  226. }
  227. },
  228. monthNum: function(month_name) {
  229. return this.indexFor(this.month_names, month_name)
  230. },
  231. shortMonthNum: function(month_name) {
  232. return this.indexFor(this.short_month_names, month_name)
  233. },
  234. shortDayNum: function(day_name) {
  235. return this.indexFor(this.short_day_names, day_name)
  236. },
  237. daysBetween: function(start, end) {
  238. start = Date.UTC(start.getFullYear(), start.getMonth(), start.getDate());
  239. end = Date.UTC(end.getFullYear(), end.getMonth(), end.getDate());
  240. return (end - start) / 86400000
  241. },
  242. changeDayTo: function(dayOfWeek, date, direction) {
  243. var difference = direction * (Math.abs(date.getDay() - dayOfWeek - (direction * 7)) % 7);
  244. return new Date(date.getFullYear(), date.getMonth(), date.getDate() + difference)
  245. },
  246. rangeStart: function(date) {
  247. return this.changeDayTo(this.start_of_week, new Date(date.getFullYear(), date.getMonth()), -1)
  248. },
  249. rangeEnd: function(date) {
  250. return this.changeDayTo((this.start_of_week - 1) % 7, new Date(date.getFullYear(), date.getMonth() + 1, 0), 1)
  251. },
  252. isFirstDayOfWeek: function(date) {
  253. return date.getDay() == this.start_of_week
  254. },
  255. isLastDayOfWeek: function(date) {
  256. return date.getDay() == (this.start_of_week - 1) % 7
  257. },
  258. adjustDays: function(days) {
  259. var newDays = [];
  260. for (var i = 0; i < days.length; i++) {
  261. newDays[i] = days[(i + this.start_of_week) % 7]
  262. };
  263. return newDays
  264. }
  265. };
  266. $.fn.date_input = function(opts) {
  267. return this.each(function() {
  268. new DateInput(this, opts)
  269. })
  270. };
  271. $.date_input = {
  272. initialize: function(opts) {
  273. $("input.date_input").date_input(opts)
  274. }
  275. };
  276. return DateInput
  277. })(jQuery);
  278. //download by www.sucaijiayuan.com