📄 jquery_datepicker.js
字号:
/**
* Copyright (c) 2007 Kelvin Luck (http://www.kelvinluck.com/)
* Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
* and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
*
* $Id: jquery_datePicker.js,v 1.1 2008/09/09 01:54:34 liufang Exp $
**/
(function($){
$.fn.extend({
/**
* Render a calendar table into any matched elements.
*
* @param Object s (optional) Customize your calendars.
* @option Number month The month to render (NOTE that months are zero based). Default is today's month.
* @option Number year The year to render. Default is today's year.
* @option Function renderCallback A reference to a function that is called as each cell is rendered and which can add classes and event listeners to the created nodes. Default is no callback.
* @option Number showHeader Whether or not to show the header row, possible values are: $.dpConst.SHOW_HEADER_NONE (no header), $.dpConst.SHOW_HEADER_SHORT (first letter of each day) and $.dpConst.SHOW_HEADER_LONG (full name of each day). Default is $.dpConst.SHOW_HEADER_SHORT.
* @option String hoverClass The class to attach to each cell when you hover over it (to allow you to use hover effects in IE6 which doesn't support the :hover pseudo-class on elements other than links). Default is dp-hover. Pass false if you don't want a hover class.
* @type jQuery
* @name renderCalendar
* @cat plugins/datePicker
* @author Kelvin Luck (http://www.kelvinluck.com/)
*
* @example $('#calendar-me').renderCalendar({month:0, year:2007});
* @desc Renders a calendar displaying January 2007 into the element with an id of calendar-me.
*
* @example
* var testCallback = function($td, thisDate, month, year)
* {
* if ($td.is('.current-month') && thisDate.getDay() == 4) {
* var d = thisDate.getDate();
* $td.bind(
* 'click',
* function()
* {
* alert('You clicked on ' + d + '/' + (Number(month)+1) + '/' + year);
* }
* ).addClass('thursday');
* } else if (thisDate.getDay() == 5) {
* $td.html('Friday the ' + $td.html() + 'th');
* }
* }
* $('#calendar-me').renderCalendar({month:0, year:2007, renderCallback:testCallback});
*
* @desc Renders a calendar displaying January 2007 into the element with an id of calendar-me. Every Thursday in the current month has a class of "thursday" applied to it, is clickable and shows an alert when clicked. Every Friday on the calendar has the number inside replaced with text.
**/
renderCalendar : function(s)
{
var dc = function(a)
{
return document.createElement(a);
};
s = $.extend(
{
month : null,
year : null,
renderCallback : null,
showHeader : $.dpConst.SHOW_HEADER_SHORT,
dpController : null,
hoverClass : 'dp-hover'
}
, s
);
if (s.showHeader != $.dpConst.SHOW_HEADER_NONE) {
var headRow = $(dc('tr'));
for (var i=Date.firstDayOfWeek; i<Date.firstDayOfWeek+7; i++) {
var weekday = i%7;
var day = Date.dayNames[weekday];
headRow.append(
jQuery(dc('th')).attr({'scope':'col', 'abbr':day, 'title':day, 'class':(weekday == 0 || weekday == 6 ? 'weekend' : 'weekday')}).html(s.showHeader == $.dpConst.SHOW_HEADER_SHORT ? day.substr(0, 1) : day)
);
}
};
var calendarTable = $(dc('table'))
.attr(
{
'cellspacing':2,
'className':'jCalendar'
}
)
.append(
(s.showHeader != $.dpConst.SHOW_HEADER_NONE ?
$(dc('thead'))
.append(headRow)
:
dc('thead')
)
);
var tbody = $(dc('tbody'));
var today = (new Date()).zeroTime();
var month = s.month == undefined ? today.getMonth() : s.month;
var year = s.year || today.getFullYear();
var currentDate = new Date(year, month, 1);
var firstDayOffset = Date.firstDayOfWeek - currentDate.getDay() + 1;
if (firstDayOffset > 1) firstDayOffset -= 7;
var weeksToDraw = Math.ceil(( (-1*firstDayOffset+1) + currentDate.getDaysInMonth() ) /7);
currentDate.addDays(firstDayOffset-1);
var doHover = function()
{
if (s.hoverClass) {
$(this).addClass(s.hoverClass);
}
};
var unHover = function()
{
if (s.hoverClass) {
$(this).removeClass(s.hoverClass);
}
};
var w = 0;
while (w++<weeksToDraw) {
var r = jQuery(dc('tr'));
for (var i=0; i<7; i++) {
var thisMonth = currentDate.getMonth() == month;
var d = $(dc('td'))
.text(currentDate.getDate() + '')
.attr('className', (thisMonth ? 'current-month ' : 'other-month ') +
(currentDate.isWeekend() ? 'weekend ' : 'weekday ') +
(thisMonth && currentDate.getTime() == today.getTime() ? 'today ' : '')
)
.hover(doHover, unHover)
;
if (s.renderCallback) {
s.renderCallback(d, currentDate, month, year);
}
r.append(d);
currentDate.addDays(1);
}
tbody.append(r);
}
calendarTable.append(tbody);
return this.each(
function()
{
$(this).empty().append(calendarTable);
}
);
},
/**
* Create a datePicker associated with each of the matched elements.
*
* The matched element will receive a few custom events with the following signatures:
*
* dateSelected(event, date, $td, status)
* Triggered when a date is selected. event is a reference to the event, date is the Date selected, $td is a jquery object wrapped around the TD that was clicked on and status is whether the date was selected (true) or deselected (false)
*
* dpClosed(event, selected)
* Triggered when the date picker is closed. event is a reference to the event and selected is an Array containing Date objects.
*
* dpMonthChanged(event, displayedMonth, displayedYear)
* Triggered when the month of the popped up calendar is changed. event is a reference to the event, displayedMonth is the number of the month now displayed (zero based) and displayedYear is the year of the month.
*
* dpDisplayed(event, $datePickerDiv)
* Triggered when the date picker is created. $datePickerDiv is the div containing the date picker. Use this event to add custom content/ listeners to the popped up date picker.
*
* @param Object s (optional) Customize your date pickers.
* @option Number month The month to render when the date picker is opened (NOTE that months are zero based). Default is today's month.
* @option Number year The year to render when the date picker is opened. Default is today's year.
* @option String startDate The first date date can be selected.
* @option String endDate The last date that can be selected.
* @option Boolean inline Whether to create the datePicker as inline (e.g. always on the page) or as a model popup. Default is false (== modal popup)
* @option Boolean createButton Whether to create a .dp-choose-date anchor directly after the matched element which when clicked will trigger the showing of the date picker. Default is true.
* @option Boolean showYearNavigation Whether to display buttons which allow the user to navigate through the months a year at a time. Default is true.
* @option Boolean closeOnSelect Whether to close the date picker when a date is selected. Default is true.
* @option Boolean displayClose Whether to create a "Close" button within the date picker popup. Default is false.
* @option Boolean selectMultiple Whether a user should be able to select multiple dates with this date picker. Default is false.
* @option Boolean clickInput If the matched element is an input type="text" and this option is true then clicking on the input will cause the date picker to appear.
* @option Number verticalPosition The vertical alignment of the popped up date picker to the matched element. One of $.dpConst.POS_TOP and $.dpConst.POS_BOTTOM. Default is $.dpConst.POS_TOP.
* @option Number horizontalPosition The horizontal alignment of the popped up date picker to the matched element. One of $.dpConst.POS_LEFT and $.dpConst.POS_RIGHT.
* @option Number verticalOffset The number of pixels offset from the defined verticalPosition of this date picker that it should pop up in. Default in 0.
* @option Number horizontalOffset The number of pixels offset from the defined horizontalPosition of this date picker that it should pop up in. Default in 0.
* @option (Function|Array) renderCallback A reference to a function (or an array of seperate functions) that is called as each cell is rendered and which can add classes and event listeners to the created nodes. Each callback function will receive four arguments; a jquery object wrapping the created TD, a Date object containing the date this TD represents, a number giving the currently rendered month and a number giving the currently rendered year. Default is no callback.
* @option String hoverClass The class to attach to each cell when you hover over it (to allow you to use hover effects in IE6 which doesn't support the :hover pseudo-class on elements other than links). Default is dp-hover. Pass false if you don't want a hover class.
* @type jQuery
* @name datePicker
* @cat plugins/datePicker
* @author Kelvin Luck (http://www.kelvinluck.com/)
*
* @example $('input.date-picker').datePicker();
* @desc Creates a date picker button next to all matched input elements. When the button is clicked on the value of the selected date will be placed in the corresponding input (formatted according to Date.format).
*
* @example demo/index.html
* @desc See the projects homepage for many more complex examples...
**/
datePicker : function(s)
{
if (!$.event._dpCache) $.event._dpCache = [];
// initialise the date picker controller with the relevant settings...
s = $.extend(
{
month : undefined,
year : undefined,
startDate : undefined,
endDate : undefined,
inline : false,
renderCallback : [],
createButton : true,
showYearNavigation : true,
closeOnSelect : true,
displayClose : false,
selectMultiple : false,
clickInput : false,
verticalPosition : $.dpConst.POS_TOP,
horizontalPosition : $.dpConst.POS_LEFT,
verticalOffset : 0,
horizontalOffset : 0,
hoverClass : 'dp-hover'
}
, s
);
return this.each(
function()
{
var $this = $(this);
var alreadyExists = true;
if (!this._dpId) {
this._dpId = $.event.guid++;
$.event._dpCache[this._dpId] = new DatePicker(this);
alreadyExists = false;
}
if (s.inline) {
s.createButton = false;
s.displayClose = false;
s.closeOnSelect = false;
$this.empty();
}
var controller = $.event._dpCache[this._dpId];
controller.init(s);
if (!alreadyExists && s.createButton) {
// create it!
controller.button = $('<a href="#" class="dp-choose-date" title="' + $.dpText.TEXT_CHOOSE_DATE + '">' + $.dpText.TEXT_CHOOSE_DATE + '</a>')
.bind(
'click',
function()
{
$this.dpDisplay(this);
this.blur();
return false;
}
);
$this.after(controller.button);
}
if (!alreadyExists && $this.is(':text')) {
$this
.bind(
'dateSelected',
function(e, selectedDate, $td)
{
this.value = selectedDate.asString();
}
).bind(
'change',
function()
{
var d = Date.fromString(this.value);
if (d) {
controller.setSelected(d, true, true);
}
}
);
if (s.clickInput) {
$this.bind(
'click',
function()
{
$this.dpDisplay();
}
);
}
var d = Date.fromString(this.value);
if (this.value != '' && d) {
controller.setSelected(d, true, true);
}
}
$this.addClass('dp-applied');
}
)
},
/**
* Disables or enables this date picker
*
* @param Boolean s Whether to disable (true) or enable (false) this datePicker
* @type jQuery
* @name dpSetDisabled
* @cat plugins/datePicker
* @author Kelvin Luck (http://www.kelvinluck.com/)
*
* @example $('.date-picker').datePicker();
* $('.date-picker').dpSetDisabled(true);
* @desc Prevents this date picker from displaying and adds a class of dp-disabled to it (and it's associated button if it has one) for styling purposes. If the matched element is an input field then it will also set the disabled attribute to stop people directly editing the field.
**/
dpSetDisabled : function(s)
{
return _w.call(this, 'setDisabled', s);
},
/**
* Updates the first selectable date for any date pickers on any matched elements.
*
* @param String d A string representing the first selectable date (formatted according to Date.format).
* @type jQuery
* @name dpSetStartDate
* @cat plugins/datePicker
* @author Kelvin Luck (http://www.kelvinluck.com/)
*
* @example $('.date-picker').datePicker();
* $('.date-picker').dpSetStartDate('01/01/2000');
* @desc Creates a date picker associated with all elements with a class of "date-picker" then sets the first selectable date for each of these to the first day of the millenium.
**/
dpSetStartDate : function(d)
{
return _w.call(this, 'setStartDate', d);
},
/**
* Updates the last selectable date for any date pickers on any matched elements.
*
* @param String d A string representing the last selectable date (formatted according to Date.format).
* @type jQuery
* @name dpSetEndDate
* @cat plugins/datePicker
* @author Kelvin Luck (http://www.kelvinluck.com/)
*
* @example $('.date-picker').datePicker();
* $('.date-picker').dpSetEndDate('01/01/2010');
* @desc Creates a date picker associated with all elements with a class of "date-picker" then sets the last selectable date for each of these to the first Janurary 2010.
**/
dpSetEndDate : function(d)
{
return _w.call(this, 'setEndDate', d);
},
/**
* Gets a list of Dates currently selected by this datePicker. This will be an empty array if no dates are currently selected or NULL if there is no datePicker associated with the matched element.
*
* @type Array
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -