Light12306/Mobile12306New/js/date.js

255 lines
7.7 KiB
JavaScript
Raw Normal View History

2014-08-19 16:11:37 +08:00
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();
2014-09-04 20:32:24 +08:00
var isCheckClass = '';
var mtdate;
// var remind
2014-08-19 16:11:37 +08:00
do {
2014-09-04 20:32:24 +08:00
mtdate = new Date(max).addDays(30).getTime();
isCheckClass = ctime < min ? ' disable' : (ctime > max ? ' remind' : '');
if (ctime > mtdate) {
isCheckClass = ' disable';
};
2014-08-19 16:11:37 +08:00
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' : ''),
2014-09-04 20:32:24 +08:00
"isCheckClass": isCheckClass,
// "isCheckClass": ctime < min ? ' disable' : (ctime > max ? ' remind' : ''),
2014-08-19 16:11:37 +08:00
"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++) {
2014-09-04 20:32:24 +08:00
mtdate = new Date(max).addDays(30).getTime();
isCheckClass = ctime < min ? ' disable' : (ctime > max ? ' remind' : '');
if (ctime > mtdate) {
isCheckClass = ' disable';
};
2014-08-19 16:11:37 +08:00
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' : ''),
2014-09-04 20:32:24 +08:00
"isCheckClass": isCheckClass,
// "isCheckClass": ctime < min ? ' disable' : (ctime > max ? ' remind' : ''),
2014-08-19 16:11:37 +08:00
"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--) {
2014-09-04 20:32:24 +08:00
mtdate = new Date(max).addDays(30).getTime();
isCheckClass = ctime < min ? ' disable' : (ctime > max ? ' remind' : '');
if (ctime > mtdate) {
isCheckClass = ' disable';
};
2014-08-19 16:11:37 +08:00
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' : ''),
2014-09-04 20:32:24 +08:00
"isCheckClass": isCheckClass,
2014-08-19 16:11:37 +08:00
"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();
2014-09-04 20:32:24 +08:00
if($(this).hasClass('disable')){
2014-09-04 22:34:02 +08:00
$('#date_tip').hide();
2014-09-04 20:32:24 +08:00
return false;
}
2014-08-19 16:11:37 +08:00
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();
}
}
2014-09-01 13:50:43 +08:00
if (!!_self.checkCallback) {
2014-08-19 16:11:37 +08:00
_self.checkCallback(date);
}
});
$('.date_prev,.date_next', _self.wrap).on('click', function(e) {
2014-09-01 13:50:43 +08:00
if ($(this).hasClass('date_prev')) {
2014-08-19 16:11:37 +08:00
_self.monthPrev();
2014-09-01 13:50:43 +08:00
} else {
2014-08-19 16:11:37 +08:00
_self.monthNext();
}
2014-09-04 22:34:02 +08:00
$('#date_tip').hide();
2014-08-19 16:11:37 +08:00
});
};
2014-09-01 13:50:43 +08:00
DateComponent.prototype.setBasic = function() {
2014-08-19 16:11:37 +08:00
this.fd = this.pageCurDate.format('yyyy年 MM月');
this.generateArray();
this.setHtml();
}
2014-09-01 13:50:43 +08:00
DateComponent.prototype.setCurDate = function(date) {
if (date.date.getTime() < this._TODAY.date.getTime() || date.date.getTime() > this.maxDate.date.getTime()) {
2014-08-21 13:42:42 +08:00
return this.curDate;
}
this.curDate = date;
this.pageCurMonth = date.getMonth();
this.pageCurDate = new Date(date.getFullYear(), this.pageCurMonth, 1, 0, 0, 0);
2014-09-01 13:50:43 +08:00
2014-08-21 13:42:42 +08:00
this.setBasic();
}
2014-09-01 13:50:43 +08:00
DateComponent.prototype.monthPrev = function() {
2014-08-19 16:11:37 +08:00
var m = this.pageCurMonth - 1,
y;
2014-09-01 13:50:43 +08:00
if (m < 0) {
2014-08-19 16:11:37 +08:00
m = 11;
2014-09-01 13:50:43 +08:00
y = new Date(this.pageCurDate.getFullYear() - 1, m, 1, 0, 0, 0);
} else {
2014-08-19 16:11:37 +08:00
y = new Date(this.pageCurDate.getFullYear(), m, 1, 0, 0, 0);
}
2014-09-01 13:50:43 +08:00
if (y.getTime() < (new Date(this._TODAY.getFullYear(), this._TODAY.getMonth(), 1, 0, 0, 0)).getTime()) {
2014-08-19 16:11:37 +08:00
return false;
}
this.pageCurMonth = m;
this.pageCurDate = y;
this.setBasic();
2014-09-04 22:34:02 +08:00
return true;
2014-08-19 16:11:37 +08:00
}
2014-09-01 13:50:43 +08:00
DateComponent.prototype.monthNext = function() {
2014-08-19 16:11:37 +08:00
this.pageCurMonth++;
2014-09-01 13:50:43 +08:00
if (this.pageCurMonth > 11) {
2014-08-19 16:11:37 +08:00
this.pageCurMonth = 0;
2014-09-01 13:50:43 +08:00
this.pageCurDate = new Date(this.pageCurDate.getFullYear() + 1, this.pageCurMonth, 1, 0, 0, 0);
} else {
2014-08-19 16:11:37 +08:00
this.pageCurDate = new Date(this.pageCurDate.getFullYear(), this.pageCurMonth, 1, 0, 0, 0);
}
this.setBasic();
2014-09-04 22:34:02 +08:00
return true;
2014-08-19 16:11:37 +08:00
}
2014-09-01 13:50:43 +08:00
DateComponent.prototype.dayPrev = function() {
2014-08-19 16:11:37 +08:00
var date = new Date(this.curDate.getTime() - 86400000);
2014-09-04 22:34:02 +08:00
if (date.date.getTime() < this.minDate.date.getTime()) {
2014-08-19 16:11:37 +08:00
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;
}
2014-09-01 13:50:43 +08:00
DateComponent.prototype.dayNext = function() {
2014-08-19 16:11:37 +08:00
var date = new Date(this.curDate.getTime() + 86400000);
2014-09-01 13:50:43 +08:00
if (date.date.getTime() > this.maxDate.date.getTime()) {
2014-08-19 16:11:37 +08:00
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();