1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
function ZCalendar(config) { this.presentTime = new Date();
this.container = config.container;
this.currentMoment = new Date(config.y?config.y: this.presentTime.getFullYear() , config.m?config.m-1 : this.presentTime.getMonth() );
this.year = this.currentMoment.getFullYear(); this.month = this.currentMoment.getMonth() + 1;
this.date = new Date().getDate();
this.init(); }
ZCalendar.prototype = { init: function () { this.startDay = this.getStartDay(); this.endDay = this.getEnDay();
this.startDate = 1, this.endDate = this.getEndDate();
this.totalDays = this.endDate;
this.renderCalendar();
document.getElementById('zc-prev').onclick = this.prevClick.bind(this); document.getElementById('zc-next').onclick = this.nextClick.bind(this);
},
getStartDay: function () { return this.currentMoment.getDay(); },
getEndDate: function () { return new Date(this.year, this.month, 0).getDate(); },
getEnDay: function () { return new Date(this.year, this.month - 1, this.getEndDate()).getDay(); },
daysAlias: [ '周日', '周一', '周二', '周三', '周四', '周五', '周六' ],
getChineseAliasDays: function (day) { return daysAlias[day]; },
renderDateTd: function (date) {
return '<td class="currentTime' + ((date == this.date) && (this.year == this.presentTime.getFullYear() && (this.month == this.presentTime.getMonth()+1) ) ? ' active' : '') + '" >' + date + '</td>'; },
renderStartEmptyTd: function () { return '<td></td>'; },
renderEndEmptyTd: function () {
},
renderCalendarBody: function () { var calendarTds = '';
for (var j = 0; j < 6; j++) { calendarTds += '<tr>';
for (var i = 0; i < 7; i++) { var index = j * 7 + i;
if (index < this.startDay || index >= (this.endDate + this.startDay)) { calendarTds += this.renderStartEmptyTd(); }
if (index >= this.startDay && index < (this.endDate + this.startDay)) { calendarTds += this.renderDateTd(index - this.startDay + 1); }
}
calendarTds += '</tr>' }
return '<tbody>' + calendarTds + '</tbody>'; },
renderCalendarHead: function () { var head = '<thead><tr>' for (var i = 0; i < this.daysAlias.length; i++) { head += this.renderDateTd(this.daysAlias[i]); } head += '</tr></thead>' return head; },
renderTimeShowBox: function () { var timeBox = '<div class="zc-timebox">' + '<div class="zc-time">' + '<span id="zc-prev"> < </span>' + '<span class="zc-time-item">' + this.year + '</span>年' + '<span class="zc-time-item">' + this.month + '</span>月' + '<span id="zc-next"> > </span>' + '</div>' + '</div>'; return timeBox; },
renderCalendarTable: function () { var tableHead = this.renderCalendarHead(); var tableBody = this.renderCalendarBody();
var calendarTabel = '<table class="zc-table" width="100%" border=0 cellspacing=0 >' + tableHead + tableBody + '</table>'; return calendarTabel; },
renderCalendar: function () { var calender = ''; calender += this.renderTimeShowBox() + this.renderCalendarTable();
calender = '<div>' + calender + '</div>';
document.querySelector(this.container).innerHTML = calender; },
prevClick: function () { this.currentMoment = new Date(this.year, this.month - 2);
this.year = this.currentMoment.getFullYear(); this.month = this.currentMoment.getMonth() + 1;
this.date = new Date().getDate();
this.init(); },
nextClick: function () { this.currentMoment = new Date(this.year, this.month);
this.year = this.currentMoment.getFullYear(); this.month = this.currentMoment.getMonth() + 1;
this.date = new Date().getDate();
this.init(); } }
|