/*
Date Input 1.1.8
Requires jQuery version: 1.2.6
Copyright (c) 2007-2008 Jonathan Leighton & Torchbox Ltd
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
DateInput = (function($) { // Localise the $ function
function DateInput(el, opts) {
if (typeof(opts) != "object") opts = {};
$.extend(this, DateInput.DEFAULT_OPTS, opts);
this.input = $(el);
this.bindMethodsToObj("show", "hide", "hideIfClickOutside", "hideOnEsc", "selectDate", "prevMonth", "nextMonth");
this.build();
this.selectDate();
this.hide();
};
DateInput.DEFAULT_OPTS = {
month_names: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
short_month_names: ["01","02","03","04","05","06","07","08","09","10","11","12"],
short_day_names: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
start_of_week: 1
};
DateInput.prototype = {
build: function() {
this.monthNameSpan = $('<div class="month_name"></div>');
this.yeahNameSpan = $('<select class="year_name"></select>');
this.monthNameSpan.append(this.yeahNameSpan);
var d = new Date();
var curyear = d.getFullYear();
for(var li=0;li<100;li++){
var tmpyear = curyear-li;
this.yeahNameSpan.append('<option value="'+tmpyear+'">'+tmpyear+'</option>');
}
var monthNav = $('<p class="month_nav"></p>').append(
$('<a href="#" class="prev">«</a> ').click(this.prevMonth),
this.monthNameSpan,this.yeahNameSpan,
$(' <a href="#" class="next">»</a>').click(this.nextMonth)
);
var tableShell = "<table><thead><tr>";
$(this.adjustDays(this.short_day_names)).each(function() {
tableShell += "<th>" + this + "</th>";
});
tableShell += "</tr></thead><tbody></tbody></table>";
this.dateSelector = this.rootLayers = $('<div class="date_selector"></div>').append(monthNav, tableShell).insertAfter(this.input);
if ($.browser.msie && $.browser.version < 7) {
this.ieframe = $('<iframe class="date_selector_ieframe" frameborder="0" src="#"></iframe>').insertBefore(this.dateSelector);
this.rootLayers = this.rootLayers.add(this.ieframe);
};
this.tbody = $("tbody", this.dateSelector);
// The anon function ensures the event is discarded
this.input.change(this.bindToObj(function() { this.selectDate(); }));
this.yeahNameSpan.change(this.bindToObj(function(){ this.setYear(this.yeahNameSpan.val()); }));
},
setYear: function(year) {
this.selectMonth(new Date(this.currentMonth.setYear(year)));
},
selectMonth: function(date) {
this.currentMonth = new Date(date.getFullYear(), date.getMonth(), 1);
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 += "<tr>";
if (currentDay.getMonth() == date.getMonth()) {
dayCells += '<td date="' + this.dateToString(currentDay) + '"><a href="#">' + currentDay.getDate() + '</a></td>';
} else {
dayCells += '<td class="unselected_month" date="' + this.dateToString(currentDay) + '">' + currentDay.getDate() + '</td>';
};
if (this.isLastDayOfWeek(currentDay)) dayCells += "</tr>";
};
this.monthNameSpan.empty().append(this.monthName(date) + " ");
try{this.yeahNameSpan.children('option[value='+date.getFullYear()+']')[0].selected = true;}catch(e){}
this.monthNameSpan.append(this.yeahNameSpan);
this.tbody.empty().append(dayCells);
$("a", this.tbody).click(this.bindToObj(function(event) {
this.selectDate(this.stringToDate($(event.target).parent().attr("date")));
this.hide();
return false;
}));
$("td[date=" + this.dateToString(new Date()) + "]", this.tbody).addClass("today");
},
selectDate: function(date) {
if (typeof(date) == "undefined") {
date = this.stringToDate(this.input.val());
};
if (date) {
this.selectedDate = date;
this.selectMonth(date);
var stringDate = this.dateToString(date);
$('td[date=' + stringDate + ']', this.tbody).addClass("selected");
if (this.input.val() != stringDate) {
this.input.val(stringDate).change();
};
} else {
this.selectMonth(new Date());
};
},
show: function() {
this.rootLayers.css("display", "block");
this.setPosition();
this.input.unbind("focus", this.show);
$("a:last", this.dateSelector).blur(this.hide);
$([window, document.body]).click(this.hideIfClickOutside).keyup(this.hideOnEsc);
},
hide: function() {
this.rootLayers.css("display", "none");
$([window, document.body]).unbind("click", this.hideIfClickOutside).unbind("keyup", this.hideOnEsc);
$("a:last", this.dateSelector).unbind("blur", this.hide);
this.input.focus(this.show);
},
hideIfClickOutside: function(event) {
if (event.target != this.input[0] && event.target.tagName != 'OPTION' && event.target.tagName != 'SELECT' && !this.insideSelector(event)) {
this.hide();
};
},
hideOnEsc: function(event) {
if (event.keyCode == 27) {
this.hide();
};
},
stringToDate: function(string) {
var matches;
if (matches = string.match(/^(\d{4,4})-([^\s]+)-(\d{1,2})$/)) {
return new Date(matches[1], this.shortMonthNum(matches[2]), matches[3], 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()
});
};
},
moveMonthBy: function(amount) {
this.selectMonth(new Date(this.currentMonth.setMonth(this.currentMonth.getMonth() + amount)));
},
prevMonth: function() {
this.moveMonthBy(-1);
return false;
},
nextMonth: function() {
this.moveMonthBy(1);
return false;
},
monthName: function(date) {
return this.month_names[date.getMonth()];
},
insideSelector: function(event) {
var offset = this.dateSelector.offset();
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;
},
bindToObj: function(fn) {
var self = this;
return function() { return fn.apply(self,
- 1
- 2
- 3
- 4
- 5
- 6
前往页