dateinput = (function($) {
function dateinput(el, opts) {
if (typeof(opts) != "object") opts = {};
$.extend(this, dateinput.default_opts, opts);
this.input = $(el);
this.bindmethodstoobj("show", "hide", "hideifclickoutside", "keydownhandler", "selectdate");
this.build();
this.selectdate();
this.hide()
};
dateinput.default_opts = {
month_names: ["一月份", "二月份", "三月份", "四月份", "五月份", "六月份", "七月份", "八月份", "九月份", "十月份", "十一月份", "十二月份"],
short_month_names: ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"],
short_day_names: ["日", "一", "二", "三", "四", "五", "六"],
start_of_week: 1
};
dateinput.prototype = {
build: function() {
var monthnav = $('
' + '«' + ' ' + '»' + '
');
this.monthnamespan = $(".month_name", monthnav);
$(".prev", monthnav).click(this.bindtoobj(function() {
this.movemonthby( - 1)
}));
$(".next", monthnav).click(this.bindtoobj(function() {
this.movemonthby(1)
}));
var yearnav = $('' + '«' + ' ' + '»' + '
');
this.yearnamespan = $(".year_name", yearnav);
$(".prev", yearnav).click(this.bindtoobj(function() {
this.movemonthby( - 12)
}));
$(".next", yearnav).click(this.bindtoobj(function() {
this.movemonthby(12)
}));
var nav = $('').append(monthnav, yearnav);
var tableshell = "";
$(this.adjustdays(this.short_day_names)).each(function() {
tableshell += "" + this + " | "
});
tableshell += "
";
this.dateselector = this.rootlayers = $('').append(nav, tableshell).insertafter(this.input);
if ($.browser.msie && $.browser.version < 7) {
this.ieframe = $('').insertbefore(this.dateselector);
this.rootlayers = this.rootlayers.add(this.ieframe);
$(".button", nav).mouseover(function() {
$(this).addclass("hover")
});
$(".button", nav).mouseout(function() {
$(this).removeclass("hover")
})
};
this.tbody = $("tbody", this.dateselector);
this.input.change(this.bindtoobj(function() {
this.selectdate()
}));
this.selectdate()
},
selectmonth: function(date) {
var newmonth = new date(date.getfullyear(), date.getmonth(), 1);
if (!this.currentmonth || !(this.currentmonth.getfullyear() == newmonth.getfullyear() && this.currentmonth.getmonth() == newmonth.getmonth())) {
this.currentmonth = newmonth;
var rangestart = this.rangestart(date),
rangeend = this.rangeend(date);
var numdays = this.daysbetween(rangestart, rangeend);
var daycells = "";
for (var i = 0; i <= numdays; i++) {
var currentday = new date(rangestart.getfullyear(), rangestart.getmonth(), rangestart.getdate() + i, 12, 00);
if (this.isfirstdayofweek(currentday)) daycells += "";
if (currentday.getmonth() == date.getmonth()) {
daycells += '' + currentday.getdate() + ' | '
} else {
daycells += '' + currentday.getdate() + ' | '
};
if (this.islastdayofweek(currentday)) daycells += "
"
};
this.tbody.empty().append(daycells);
this.monthnamespan.empty().append(this.monthname(date));
this.yearnamespan.empty().append(this.currentmonth.getfullyear());
$(".selectable_day", this.tbody).click(this.bindtoobj(function(event) {
this.changeinput($(event.target).attr("date"))
}));
$("td[date=" + this.datetostring(new date()) + "]", this.tbody).addclass("today");
$("td.selectable_day", this.tbody).mouseover(function() {
$(this).addclass("hover")
});
$("td.selectable_day", this.tbody).mouseout(function() {
$(this).removeclass("hover")
})
};
$('.selected', this.tbody).removeclass("selected");
$('td[date=' + this.selecteddatestring + ']', this.tbody).addclass("selected")
},
selectdate: function(date) {
if (typeof(date) == "undefined") {
date = this.stringtodate(this.input.val())
};
if (!date) date = new date();
this.selecteddate = date;
this.selecteddatestring = this.datetostring(this.selecteddate);
this.selectmonth(this.selecteddate)
},
changeinput: function(datestring) {
this.input.val(datestring).change();
this.hide()
},
show: function() {
this.rootlayers.css("display", "block");
$([window, document.body]).click(this.hideifclickoutside);
this.input.unbind("focus", this.show);
$(document.body).keydown(this.keydownhandler);
this.setposition()
},
hide: function() {
this.rootlayers.css("display", "none");
$([window, document.body]).unbind("click", this.hideifclickoutside);
this.input.focus(this.show);
$(document.body).unbind("keydown", this.keydownhandler)
},
hideifclickoutside: function(event) {
if (event.target != this.input[0] && !this.insideselector(event)) {
this.hide()
}
},
insideselector: function(event) {
var offset = this.dateselector.position();
offset.right = offset.left + this.dateselector.outerwidth();
offset.bottom = offset.top + this.dateselector.outerheight();
return event.pagey < offset.bottom && event.pagey > offset.top && event.pagex < offset.right && event.pagex > offset.left
},
keydownhandler: function(event) {
switch (event.keycode) {
case 9:
case 27:
this.hide();
return;
break;
case 13:
this.changeinput(this.selecteddatestring);
break;
case 33:
this.movedatemonthby(event.ctrlkey ? -12 : -1);
break;
case 34:
this.movedatemonthby(event.ctrlkey ? 12 : 1);
break;
case 38:
this.movedateby( - 7);
break;
case 40:
this.movedateby(7);
break;
case 37:
this.movedateby( - 1);
break;
case 39:
this.movedateby(1);
break;
default:
return
}
event.preventdefault()
},
stringtodate: function(string) {
var matches;
if (matches = string.match(/^(\d{1,2}) ([^\s]+) (\d{4,4})$/)) {
return new date(matches[3], this.shortmonthnum(matches[2]), matches[1], 12, 00)
} else {
return null
}
},
datetostring: function(date) {
return date.getfullyear()+"-"+this.short_month_names[date.getmonth()]+"-" +date.getdate()
},
setposition: function() {
var offset = this.input.offset();
this.rootlayers.css({
top: offset.top + this.input.outerheight(),
left: offset.left
});
if (this.ieframe) {
this.ieframe.css({
width: this.dateselector.outerwidth(),
height: this.dateselector.outerheight()
})
}
},
movedateby: function(amount) {
var newdate = new date(this.selecteddate.getfullyear(), this.selecteddate.getmonth(), this.selecteddate.getdate() + amount);
this.selectdate(newdate)
},
movedatemonthby: function(amount) {
var newdate = new date(this.selecteddate.getfullyear(), this.selecteddate.getmonth() + amount, this.selecteddate.getdate());
if (newdate.getmonth() == this.selecteddate.getmonth() + amount + 1) {
newdate.setdate(0)
};
this.selectdate(newdate)
},
movemonthby: function(amount) {
var newmonth = new date(this.currentmonth.getfullyear(), this.currentmonth.getmonth() + amount, this.currentmonth.getdate());
this.selectmonth(newmonth)
},
monthname: function(date) {
return this.month_names[date.getmonth()]
},
bindtoobj: function(fn) {
var self = this;
return function() {
return fn.apply(self, arguments)
}
},
bindmethodstoobj: function() {
for (var i = 0; i < arguments.length; i++) {
this[arguments[i]] = this.bindtoobj(this[arguments[i]])
}
},
indexfor: function(array, value) {
for (var i = 0; i < array.length; i++) {
if (value == array[i]) return i
}
},
monthnum: function(month_name) {
return this.indexfor(this.month_names, month_name)
},
shortmonthnum: function(month_name) {
return this.indexfor(this.short_month_names, month_name)
},
shortdaynum: function(day_name) {
return this.indexfor(this.short_day_names, day_name)
},
daysbetween: function(start, end) {
start = date.utc(start.getfullyear(), start.getmonth(), start.getdate());
end = date.utc(end.getfullyear(), end.getmonth(), end.getdate());
return (end - start) / 86400000
},
changedayto: function(dayofweek, date, direction) {
var difference = direction * (math.abs(date.getday() - dayofweek - (direction * 7)) % 7);
return new date(date.getfullyear(), date.getmonth(), date.getdate() + difference)
},
rangestart: function(date) {
return this.changedayto(this.start_of_week, new date(date.getfullyear(), date.getmonth()), -1)
},
rangeend: function(date) {
return this.changedayto((this.start_of_week - 1) % 7, new date(date.getfullyear(), date.getmonth() + 1, 0), 1)
},
isfirstdayofweek: function(date) {
return date.getday() == this.start_of_week
},
islastdayofweek: function(date) {
return date.getday() == (this.start_of_week - 1) % 7
},
adjustdays: function(days) {
var newdays = [];
for (var i = 0; i < days.length; i++) {
newdays[i] = days[(i + this.start_of_week) % 7]
};
return newdays
}
};
$.fn.date_input = function(opts) {
return this.each(function() {
new dateinput(this, opts)
})
};
$.date_input = {
initialize: function(opts) {
$("input.date_input").date_input(opts)
}
};
return dateinput
})(jquery);