schedule.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. ;(function (undefined) {
  2. var _global;
  3. //工具函数
  4. //配置合并
  5. function extend (def,opt,override) {
  6. for(var k in opt){
  7. if(opt.hasOwnProperty(k) && (!def.hasOwnProperty(k) || override)){
  8. def[k] = opt[k]
  9. }
  10. }
  11. return def;
  12. }
  13. //日期格式化
  14. function formartDate (y,m,d,symbol) {
  15. symbol = symbol || '-';
  16. m = (m.toString())[1] ? m : '0'+m;
  17. d = (d.toString())[1] ? d : '0'+d;
  18. return y+symbol+m+symbol+d
  19. }
  20. function Schedule (opt) {
  21. var def = {},
  22. opt = extend(def,opt,true),
  23. curDate = opt.date ? new Date(opt.date) : new Date(),
  24. year = curDate.getFullYear(),
  25. month = curDate.getMonth(),
  26. day = curDate.getDate(),
  27. currentYear = curDate.getFullYear(),
  28. currentMonth = curDate.getMonth(),
  29. currentDay = curDate.getDate(),
  30. selectedDate = '',
  31. el = document.querySelector(opt.el) || document.querySelector('body'),
  32. _this = this;
  33. var bindEvent = function (){
  34. el.addEventListener('click',function(e){
  35. switch (e.target.id) {
  36. case 'nextMonth':
  37. _this.nextMonthFun();
  38. break;
  39. case 'nextYear':
  40. _this.nextYearFun();
  41. break;
  42. case 'prevMonth':
  43. _this.prevMonthFun();
  44. break;
  45. case 'prevYear':
  46. _this.prevYearFun();
  47. break;
  48. default:
  49. break;
  50. };
  51. if(e.target.className.indexOf('currentDate') > -1){
  52. opt.clickCb && opt.clickCb(year, month+1, e.target.innerHTML);
  53. selectedDate = e.target.title;
  54. day = e.target.innerHTML;
  55. render();
  56. }
  57. },false)
  58. }
  59. var init = function () {
  60. var scheduleHd = '<div class="schedule-hd">'+
  61. '<div>'+
  62. '<span class="arrow icon iconfont icon-116leftarrowheads" id="prevYear" ></span>'+
  63. '<span class="arrow icon iconfont icon-112leftarrowhead" id="prevMonth"></span>'+
  64. '</div>'+
  65. '<div class="today">'+formartDate(year,month+1,day,'-')+'</div>'+
  66. '<div>'+
  67. '<span class="arrow icon iconfont icon-111arrowheadright" id="nextMonth"></span>'+
  68. '<span class="arrow icon iconfont icon-115rightarrowheads" id="nextYear"></span>'+
  69. '</div>'+
  70. '</div>'
  71. var scheduleWeek = '<ul class="week-ul ul-box">'+
  72. '<li>日</li>'+
  73. '<li>一</li>'+
  74. '<li>二</li>'+
  75. '<li>三</li>'+
  76. '<li>四</li>'+
  77. '<li>五</li>'+
  78. '<li>六</li>'+
  79. '</ul>'
  80. var scheduleBd = '<ul class="schedule-bd ul-box" ></ul>';
  81. el.innerHTML = scheduleHd + scheduleWeek + scheduleBd;
  82. bindEvent();
  83. render();
  84. }
  85. var render = function () {
  86. var fullDay = new Date(year,month+1,0).getDate(), //当月总天数
  87. startWeek = new Date(year,month,1).getDay(), //当月第一天是周几
  88. total = (fullDay+startWeek)%7 == 0 ? (fullDay+startWeek) : fullDay+startWeek+(7-(fullDay+startWeek)%7),//元素总个数
  89. lastMonthDay = new Date(year,month,0).getDate(), //上月最后一天
  90. eleTemp = [];
  91. for(var i = 0; i < total; i++){
  92. if(i<startWeek){
  93. eleTemp.push('<li class="other-month"><span class="dayStyle">'+(lastMonthDay-startWeek+1+i)+'</span></li>')
  94. }else if(i<(startWeek+fullDay)){
  95. var nowDate = formartDate(year,month+1,(i+1-startWeek),'-');
  96. var addClass = '';
  97. selectedDate == nowDate && (addClass = 'selected-style');
  98. formartDate(currentYear,currentMonth+1,currentDay,'-') == nowDate && (addClass = 'today-flag');
  99. eleTemp.push('<li class="current-month" ><span title='+nowDate+' class="currentDate dayStyle '+addClass+'">'+(i+1-startWeek)+'</span></li>')
  100. }else{
  101. eleTemp.push('<li class="other-month"><span class="dayStyle">'+(i+1-(startWeek+fullDay))+'</span></li>')
  102. }
  103. }
  104. el.querySelector('.schedule-bd').innerHTML = eleTemp.join('');
  105. el.querySelector('.today').innerHTML = formartDate(year,month+1,day,'-');
  106. };
  107. this.nextMonthFun = function () {
  108. if(month+1 > 11){
  109. year += 1;
  110. month = 0;
  111. }else{
  112. month += 1;
  113. }
  114. render();
  115. opt.nextMonthCb && opt.nextMonthCb(year,month+1,day);
  116. },
  117. this.nextYearFun = function () {
  118. year += 1;
  119. render();
  120. opt.nextYeayCb && opt.nextYeayCb(year,month+1,day);
  121. },
  122. this.prevMonthFun = function () {
  123. if(month-1 < 0){
  124. year -= 1;
  125. month = 11;
  126. }else{
  127. month -= 1;
  128. }
  129. render();
  130. opt.prevMonthCb && opt.prevMonthCb(year,month+1,day);
  131. },
  132. this.prevYearFun = function () {
  133. year -= 1;
  134. render();
  135. opt.prevYearCb && opt.prevYearCb(year,month+1,day);
  136. }
  137. init();
  138. }
  139. //将插件暴露给全局对象
  140. _global = (function(){return this || (0,eval)('this')}());
  141. if(typeof module !== 'undefined' && module.exports){
  142. module.exports = Schedule;
  143. }else if (typeof define === "function" && define.amd){
  144. define(function () {
  145. return Schedule;
  146. })
  147. }else {
  148. !('Schedule' in _global) && (_global.Schedule = Schedule);
  149. }
  150. }());