calendar.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /**
  2. * Created by Ryn on 2016/8/6.
  3. * 日历脚本
  4. */
  5. ;(function(document, window, H) {
  6. // 一些私有变量
  7. var _date_array = [], // 记录每一月有多少天数
  8. current_day = new Date(),
  9. current_year = H.getFullYear(current_day),
  10. current_month = H.getMonth(current_day),
  11. calendar_elem = null,
  12. calendar_head = null,
  13. calendar_body = null,
  14. icon_left_elem = null,
  15. icon_right_elem = null,
  16. week_map = {
  17. 0 : '日',
  18. 1 : '一',
  19. 2 : '二',
  20. 3 : '三',
  21. 4 : '四',
  22. 5 : '五',
  23. 6 : '六'
  24. };
  25. // 一些常量
  26. var MONTH_NUMBER = 12,
  27. ROW_NUMBER = 6,
  28. COL_NUMBER = 7;
  29. /**
  30. * 给月份数组附上每月天数
  31. * @param year 年份
  32. * @private
  33. */
  34. function _initMonthDayNumber(year) {
  35. _date_array = [];
  36. for (var i = 0; i < MONTH_NUMBER; i++) {
  37. switch (i + 1) {
  38. case 1:
  39. case 3:
  40. case 5:
  41. case 7:
  42. case 8:
  43. case 10:
  44. case 12:
  45. _date_array.push(31);
  46. break;
  47. case 4:
  48. case 6:
  49. case 9:
  50. case 11:
  51. _date_array.push(30);
  52. break;
  53. case 2:
  54. if (H.isLeapYear(year)) {
  55. _date_array.push(29);
  56. } else {
  57. _date_array.push(28);
  58. }
  59. break;
  60. default:
  61. break;
  62. }
  63. }
  64. }
  65. /**
  66. * 渲染最外层容器
  67. * @param outerEle
  68. * @private
  69. */
  70. function _renderContainer(outerEle) {
  71. calendar_elem = document.createElement('div');
  72. calendar_elem.className = 'calendar';
  73. outerEle.appendChild(calendar_elem);
  74. }
  75. /**
  76. * 渲染头部
  77. * @param date
  78. * @private
  79. */
  80. function _renderHeader(date) {
  81. calendar_head = document.createElement('div');
  82. icon_left_elem = document.createElement('i');
  83. icon_right_elem = document.createElement('i');
  84. var spanEle = document.createElement('span');
  85. if (!date) {
  86. date = current_day;
  87. }
  88. var year = H.getFullYear(date);
  89. var month = H.getMonth(date);
  90. calendar_head.className = 'calendar-header';
  91. icon_left_elem.className = 'icon-left';
  92. icon_right_elem.className = 'icon-right';
  93. spanEle.textContent = year + ' 年 ' + (month + 1) + ' 月';
  94. calendar_head.appendChild(icon_left_elem);
  95. calendar_head.appendChild(spanEle);
  96. calendar_head.appendChild(icon_right_elem);
  97. calendar_elem.appendChild(calendar_head);
  98. }
  99. /**
  100. * 渲染内容部分星期汉字
  101. * @private
  102. */
  103. function _renderBodyWeek() {
  104. calendar_body = document.createElement('div');
  105. calendar_body.className = 'calendar-body';
  106. var cBodyHead = document.createElement('ul');
  107. cBodyHead.className = 'c-body-head';
  108. for (var i = 0; i < COL_NUMBER; i++) {
  109. var liEle = document.createElement('li');
  110. liEle.textContent = week_map[i];
  111. cBodyHead.appendChild(liEle);
  112. }
  113. calendar_body.appendChild(cBodyHead);
  114. calendar_elem.appendChild(calendar_body);
  115. }
  116. /**
  117. * 渲染具体内容
  118. * @param date
  119. * @private
  120. */
  121. function _renderBodyDay(date) {
  122. if (!date) date = current_day;
  123. var cBodyContent = document.createElement('div'),
  124. firstDay = H.weekOfMonth(date),
  125. month = H.getMonth(date),
  126. current_date = H.getDate(date),
  127. dayNumber = _date_array[month],
  128. numberIndex = 0,
  129. currentMonthIndex = 1,
  130. preMonthIndex = 1,
  131. nextMonthIndex = 1;
  132. cBodyContent.className = 'c-body-content';
  133. for (var i = 0; i < ROW_NUMBER; i++) {
  134. var ulRow = document.createElement('ul');
  135. ulRow.className = 'content-row';
  136. for (var j = 0; j < COL_NUMBER; j++) {
  137. var liCol = document.createElement('li'),
  138. linkCol = document.createElement('a');
  139. linkCol.href = 'javascript:;';
  140. liCol.appendChild(linkCol);
  141. if (numberIndex < firstDay) {
  142. if (month === 0) month = 12;
  143. liCol.className += ' item-gray';
  144. linkCol.textContent = _date_array[month - 1] - (firstDay - preMonthIndex);
  145. ulRow.appendChild(liCol);
  146. preMonthIndex++;
  147. numberIndex++;
  148. } else if(numberIndex > dayNumber + firstDay - 1) {
  149. liCol.className += ' item-gray';
  150. linkCol.textContent = nextMonthIndex;
  151. ulRow.appendChild(liCol);
  152. nextMonthIndex++;
  153. numberIndex++;
  154. } else {
  155. linkCol.className = 'item-link';
  156. linkCol.textContent = currentMonthIndex;
  157. if (currentMonthIndex === current_date) {
  158. liCol.className += ' item-current';
  159. linkCol.textContent = '今天';
  160. }
  161. ulRow.appendChild(liCol);
  162. currentMonthIndex++;
  163. numberIndex++;
  164. }
  165. }
  166. cBodyContent.appendChild(ulRow);
  167. }
  168. calendar_body.appendChild(cBodyContent);
  169. }
  170. /**
  171. * 渲染视图
  172. */
  173. function renderUI() {
  174. _renderContainer(document.getElementById('datePicker'));
  175. _renderHeader();
  176. _renderBodyWeek();
  177. _renderBodyDay();
  178. }
  179. /**
  180. * 删除UI
  181. * @private
  182. */
  183. function _removeUI() {
  184. calendar_head.remove();
  185. calendar_body.remove();
  186. }
  187. /**
  188. * 同步视图
  189. * @param date
  190. */
  191. function syncUI(date) {
  192. _removeUI();
  193. _initMonthDayNumber(current_year);
  194. _renderHeader(date);
  195. _renderBodyWeek();
  196. _renderBodyDay(date);
  197. bindEvents();
  198. }
  199. /**
  200. * 绑定事件
  201. */
  202. function bindEvents() {
  203. icon_left_elem.addEventListener('click', function() {
  204. if (current_month === 0) {
  205. current_month = 11;
  206. current_year = current_year - 1;
  207. } else {
  208. current_month = current_month - 1;
  209. }
  210. syncUI(new Date(current_year, current_month));
  211. });
  212. icon_right_elem.addEventListener('click', function() {
  213. if (current_month === 11) {
  214. current_month = 0;
  215. current_year = current_year + 1;
  216. } else {
  217. current_month = current_month + 1;
  218. }
  219. syncUI(new Date(current_year, current_month));
  220. });
  221. var itemLinks = document.getElementsByClassName('item-link'),
  222. linksArray = Array.prototype.slice.call(itemLinks);
  223. for (var i = 0; i < linksArray.length; i++) {
  224. linksArray[i].addEventListener('click', function(e) {
  225. e.preventDefault();
  226. var activeLinks = document.getElementsByClassName('item-current'),
  227. activeArray = Array.prototype.slice.call(activeLinks);
  228. for (var j = 0; j < activeArray.length; j++) {
  229. activeArray[j].className = activeArray[j].className.replace(/item-current/, '');
  230. }
  231. this.parentNode.className += ' item-current';
  232. alert("当前日期为:" + current_year + '年' + (current_month + 1) + '月' + this.textContent + '日' );
  233. });
  234. }
  235. }
  236. function init() {
  237. _initMonthDayNumber(current_year);
  238. renderUI();
  239. bindEvents();
  240. }
  241. init();
  242. })(document, window, H);