214 lines
6.6 KiB
JavaScript
214 lines
6.6 KiB
JavaScript
function DateComponent(o) {
|
|
var _self = this;
|
|
var obj = {
|
|
_TODAY: (new Date()).date,
|
|
wrap: $('#date'),
|
|
tdHtml: '<td><a href="javascript:;" class="$class$$isCheckClass$" data-time="$time$">$day$<span class="lunar">$lunar$</span></a></td>',
|
|
curDate: (new Date()).date,
|
|
startDate: (new Date()).date,
|
|
minDate: (new Date()).date,
|
|
maxDate: (new Date()).date.addDays(19),
|
|
arr: []
|
|
}
|
|
|
|
$.extend(this, obj);
|
|
typeof o == 'object' && $.extend(this, o);
|
|
this.fd = this.curDate.format('yyyy年 MM月');
|
|
this.pageCurDate = new Date(this.curDate.getFullYear(), this.curDate.getMonth(), 1, 0, 0, 0);
|
|
this.pageCurMonth = this.curDate.getMonth();
|
|
this.generateArray();
|
|
this.setHtml();
|
|
}
|
|
|
|
DateComponent.prototype.generateArray = function() {
|
|
var _self = this;
|
|
var curMonth = this.pageCurMonth;
|
|
var date = this.pageCurDate;
|
|
var dateDay = date.getDay();
|
|
var ctime = date.date.getTime();
|
|
var arr = [];
|
|
var min = this.minDate.date.getTime();
|
|
var max = this.maxDate.date.getTime();
|
|
|
|
do {
|
|
arr.push({
|
|
"year": date.getFullYear(),
|
|
"month": date.getMonth() + 1,
|
|
"day": date.getDate(),
|
|
"format": date.format('yyyy/MM/dd'),
|
|
"time": date.date.getTime(),
|
|
"class": date.isToday ? ' today' + (this.curDate.date.getTime() == date.date.getTime() ? ' cur' : '') : (this.curDate.date.getTime() == date.date.getTime() ? ' cur' : ''),
|
|
"isCheckClass": ctime < min ? ' disable' : (ctime > max? ' remind':''),
|
|
"lunar": (new LunarCalendar(date)).getMOD()
|
|
});
|
|
date = new Date(date.getTime() + 86400000);
|
|
ctime = date.date.getTime();
|
|
|
|
} while (curMonth == date.getMonth());
|
|
|
|
var i = date.getDay();
|
|
for (; i < 7 && i != 0; i++) {
|
|
arr.push({
|
|
"year": date.getFullYear(),
|
|
"month": date.getMonth() + 1,
|
|
"day": date.getDate(),
|
|
"format": date.format('yyyy/MM/dd'),
|
|
"time": date.date.getTime(), //this._TODAY.date.getTime() > ctime
|
|
"class": date.isToday ? ' today' + (this.curDate.date.getTime() == date.date.getTime() ? ' cur' : '') : (this.curDate.date.getTime() == date.date.getTime() ? ' cur' : ''),
|
|
"isCheckClass": ctime < min ? ' disable' : (ctime > max? ' remind':''),
|
|
"lunar": (new LunarCalendar(date)).getMOD()
|
|
});
|
|
date = new Date(date.getTime() + 86400000);
|
|
ctime = date.date.getTime();
|
|
};
|
|
|
|
i = dateDay;
|
|
date = new Date(new Date(this.pageCurDate.getFullYear(), this.pageCurDate.getMonth(), 1, 0, 0, 0).getTime() - 86400000);
|
|
ctime = date.date.getTime();
|
|
for (; i > 0; i--) {
|
|
arr.unshift({
|
|
"year": date.getFullYear(),
|
|
"month": date.getMonth() + 1,
|
|
"day": date.getDate(),
|
|
"format": date.format('yyyy/MM/dd'),
|
|
"time": date.date.getTime(),
|
|
"class": date.isToday ? ' today' + (this.curDate.date.getTime() == date.date.getTime() ? ' cur' : '') : (this.curDate.date.getTime() == date.date.getTime() ? ' cur' : ''),
|
|
"isCheckClass": ctime < min ? ' disable' : (ctime > max? ' remind':''),
|
|
"lunar": (new LunarCalendar(date)).getMOD()
|
|
});
|
|
date = new Date(date.getTime() - 86400000);
|
|
ctime = date.date.getTime();
|
|
};
|
|
|
|
|
|
this.arr = arr;
|
|
}
|
|
|
|
DateComponent.prototype.setHtml = function() {
|
|
var str = '<div class="date_title"><span class="date_prev"><i class="icon icon_left"></i></span>' + this.fd + '<span class="date_next"><i class="icon icon_right"></i></span></div><table class="date_component"><tr><th>日</th><th>一</th><th>二</th><th>三</th><th>四</th><th>五</th><th>六</th></tr><tr>';
|
|
for (var i = 0, len = this.arr.length; i < len; i++) {
|
|
if (i != 0 && i % 7 == 0) {
|
|
str += '</tr><tr>';
|
|
}
|
|
str += this.tdHtml.temp(this.arr[i]);
|
|
};
|
|
str += '</tr></table>';
|
|
|
|
this.wrap.html(str);
|
|
this.bindDomEvent();
|
|
};
|
|
DateComponent.prototype.bindDomEvent = function() {
|
|
var _self = this;
|
|
$('table a', _self.wrap).on('click', function(e) {
|
|
var date = new Date(parseInt($(this).attr('data-time')));
|
|
var min = _self.minDate.date.getTime();
|
|
var max = _self.maxDate.date.getTime();
|
|
|
|
if (!$(this).hasClass('cur')) {
|
|
if (date.getTime() < _self._TODAY.date.getTime()) {
|
|
return false;
|
|
}
|
|
if (_self.pageCurMonth == date.getMonth()) {
|
|
$('table a', _self.wrap).removeClass('cur');
|
|
$(this).addClass('cur');
|
|
_self.curDate = date;
|
|
} else {
|
|
_self.curDate = date;
|
|
_self.startDate = date;
|
|
|
|
_self.pageCurDate = new Date(_self.curDate.getFullYear(), _self.curDate.getMonth(), 1, 0, 0, 0);
|
|
_self.pageCurMonth = _self.curDate.getMonth();
|
|
|
|
_self.fd = _self.curDate.format('yyyy年 MM月');
|
|
_self.generateArray();
|
|
_self.setHtml();
|
|
}
|
|
}
|
|
if(!!_self.checkCallback){
|
|
_self.checkCallback(date);
|
|
}
|
|
|
|
});
|
|
$('.date_prev,.date_next', _self.wrap).on('click', function(e) {
|
|
if($(this).hasClass('date_prev')){
|
|
_self.monthPrev();
|
|
}else{
|
|
_self.monthNext();
|
|
}
|
|
});
|
|
};
|
|
|
|
|
|
DateComponent.prototype.setBasic = function(){
|
|
this.fd = this.pageCurDate.format('yyyy年 MM月');
|
|
this.generateArray();
|
|
this.setHtml();
|
|
}
|
|
|
|
DateComponent.prototype.monthPrev = function(){
|
|
var m = this.pageCurMonth - 1,
|
|
y;
|
|
|
|
if(m < 0){
|
|
m = 11;
|
|
y = new Date(this.pageCurDate.getFullYear()-1, m, 1, 0, 0, 0);
|
|
}else{
|
|
y = new Date(this.pageCurDate.getFullYear(), m, 1, 0, 0, 0);
|
|
}
|
|
if(y.getTime() < (new Date(this._TODAY.getFullYear(),this._TODAY.getMonth(),1,0,0,0)).getTime()){
|
|
return false;
|
|
}
|
|
|
|
this.pageCurMonth = m;
|
|
this.pageCurDate = y;
|
|
|
|
this.setBasic();
|
|
}
|
|
|
|
DateComponent.prototype.monthNext = function(){
|
|
this.pageCurMonth++;
|
|
if(this.pageCurMonth > 11){
|
|
this.pageCurMonth = 0;
|
|
this.pageCurDate = new Date(this.pageCurDate.getFullYear()+1, this.pageCurMonth, 1, 0, 0, 0);
|
|
}else{
|
|
this.pageCurDate = new Date(this.pageCurDate.getFullYear(), this.pageCurMonth, 1, 0, 0, 0);
|
|
}
|
|
this.setBasic();
|
|
}
|
|
|
|
DateComponent.prototype.dayPrev = function(){
|
|
var date = new Date(this.curDate.getTime() - 86400000);
|
|
if(date.date.getTime() < this._TODAY.date.getTime()){
|
|
return this.curDate;
|
|
}
|
|
this.curDate = date;
|
|
this.pageCurDate = new Date(this.curDate.getFullYear(), this.curDate.getMonth(), 1, 0, 0, 0);
|
|
this.pageCurMonth = this.pageCurDate.getMonth();
|
|
this.setBasic();
|
|
return this.curDate;
|
|
}
|
|
DateComponent.prototype.dayNext = function(){
|
|
var date = new Date(this.curDate.getTime() + 86400000);
|
|
if(date.date.getTime() > this.maxDate.date.getTime()){
|
|
return this.curDate;
|
|
}
|
|
this.curDate = date;
|
|
this.pageCurDate = new Date(this.curDate.getFullYear(), this.curDate.getMonth(), 1, 0, 0, 0);
|
|
this.pageCurMonth = this.pageCurDate.getMonth();
|
|
this.setBasic();
|
|
return this.curDate;
|
|
}
|
|
DateComponent.prototype.setSection = function(minDate, num) {
|
|
this.minDate = minDate;
|
|
this.maxDate = this.minDate.addDays(num);
|
|
}
|
|
|
|
DateComponent.prototype.setMinDay = function(date) {
|
|
this.minDate = date;
|
|
}
|
|
|
|
DateComponent.prototype.setMaxDay = function(date) {
|
|
this.maxDate = date;
|
|
}
|
|
|
|
// var abc = new DateComponent();
|