/*
* PHPWind util Library
* @Copyright : Copyright 2011, phpwind.com
* @Descript : datePicker 日历组件
* @Author : jquerytools (http://jquerytools.org/demos/dateinput/)
* @Modify : chaoren1641@gmail.com
* @Depend : jquery.js(1.7 or later)
* $Id: datePicker.js 22586 2012-12-25 10:54:55Z hao.lin $ :
*/
;(function($, window, document, undefined) {
var pluginName = 'datePicker';
var instances = [];
// h=72, j=74, k=75, l=76, down=40, left=37, up=38, right=39
var KEYS = [75, 76, 38, 39, 74, 72, 40, 37], LABELS = {};
var defaults = {
format : 'yyyy-mm-dd',
selectors : true,
time : false,
yearRange : [-50, 20],
lang : 'zh-CN',
offset : [0, 0],
speed : 0,
firstDay : 0, // The first day of the week, Sun = 0, Mon = 1, ...
min : undefined,
max : undefined,
trigger : false,
css : {
prefix : 'cal',
input : 'date',
// ids
root : 0,
head : 0,
title : 0,
prev : 0,
next : 0,
month : 0,
year : 0,
days : 0,
body : 0,
weeks : 0,
today : 0,
current : 0,
// classnames
week : 0,
off : 0,
sunday : 0,
focus : 0,
disabled : 0,
trigger : 0
}
};
var localize = function(language, labels) {
$.each(labels, function(key, val) {
labels[key] = val.split(",");
});
LABELS[language] = labels;
};
//多语言配置
localize("en", {
months : 'January,February,March,April,May,June,July,August,September,October,November,December',
shortMonths : 'Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec',
days : 'Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday',
shortDays : 'Sun,Mon,Tue,Wed,Thu,Fri,Sat'
});
localize("zh-CN", {
months : '一月,二月,三月,四月,五月,六月,七月,八月,九月,十月,十一月,十二月',
shortMonths : '一,二,三,四,五,六,七,八,九,十,十一,十二',
days : '周日,周一,周二,周三,周四,周五,周六',
shortDays : '日,一,二,三,四,五,六'
});
//{{{ private functions
//返回某年某月的天数
function dayAm(year, month) {
return 32 - new Date(year, month, 32).getDate();
}
function zeropad(val, len) {
val = '' + val;
len = len || 2;
while(val.length < len) {
val = "0" + val;
}
return val;
}
// thanks: http://stevenlevithan.com/assets/misc/date.format.js
var Re = /d{1,4}|m{1,4}|yy(?:yy)?|([HhMsTt])\1?|[LloSZ]|"[^"]*"|'[^']*'/g, tmpTag = $("");
//格式化时间
function format(date, fmt, lang) {
var d = date.getDate(), D = date.getDay(), m = date.getMonth(), y = date.getFullYear(),
h = date.getHours(),M = date.getMinutes(),
flags = {
d : d,
dd : zeropad(d),
ddd : LABELS[lang].shortDays[D],
dddd : LABELS[lang].days[D],
m : m + 1,
mm : zeropad(m + 1),
mmm : LABELS[lang].shortMonths[m],
mmmm : LABELS[lang].months[m],
yy : String(y).slice(2),
yyyy : y,
HH: zeropad(h),
MM : zeropad(M)
};
var ret = fmt.replace(Re, function($0) {
return $0 in flags ? flags[$0] : $0.slice(1, $0.length - 1);
});
// a small trick to handle special characters
return tmpTag.html(ret).html();
}
function integer(val) {
return parseInt(val, 10);
}
function isSameDay(d1, d2) {
return d1.getFullYear() === d2.getFullYear() && d1.getMonth() == d2.getMonth() && d1.getDate() == d2.getDate();
}
function parseDate(val) {
if(!val) {
return;
}
if(val.constructor == Date) {
return val;
}
if( typeof val == 'string') {
// rfc3339?
var els = val.split(" ");
var date,time;
var y,m,d,h,s;
date = els[0].split('-');
if(date.length !== 3) {
return;
}
if(els.length === 2) {
time = els[1].split(':');
}else {
time = '00:00'.split(':');
}
y = date[0],m = date[1]-1,d = date[2];
h = time[0],s = time[1];
return new Date(y,m,d,h,s,0);
// invalid offset
if(!/^-?\d+$/.test(val)) {
return;
}
// convert to integer
val = integer(val);
}
var date = new Date();
date.setDate(date.getDate() + val);
return date;
}
function Plugin( input, options ) {
this.options = $.extend( {}, defaults, options) ;
this.input = input;
this.init();
}
Plugin.prototype.init = function() {
var options = this.options,input = this.input;
if(options.time) {
options.format = 'yyyy-mm-dd HH:MM'
}
// CSS prefix
$.each(options.css, function(key, val) {
if(!val && key != 'prefix') {
options.css[key] = (options.css.prefix || '') + (val || key);
}
});
// variables
var self = this, now = new Date(), css = options.css, labels = LABELS[options.lang], root = $("#" + css.root), title = root.find("#" + css.title), trigger, pm, nm, currYear, currMonth, currDay, value = input.attr("data-value") || options.value || input.val(), min = input.attr("min") || options.min, max = input.attr("max") || options.max, opened;
// zero min is not undefined
if(min === 0) {
min = "0";
}
// use sane values for value, min & max
value = parseDate(value) || now;
min = parseDate(min || options.yearRange[0] * 365);
max = parseDate(max || options.yearRange[1] * 365);
// check that language exists
if(!labels) {
throw "不存在的语言: " + options.lang;
}
// Replace built-in date input: NOTE: input.attr("type", "text") throws exception by the browser
if(input.attr("type") === 'date') {
var tmp = $("");
$.each("class,disabled,id,maxlength,name,readonly,required,size,style,tabindex,title,value".split(","), function(i, attr) {
tmp.attr(attr, input.attr(attr));
});
input.replaceWith(tmp);
input = tmp;
}
input.addClass(css.input);
var fire = input.add(self);
// construct layout
if(!root.length) {
// root
root = $('
').hide().css({
position : 'absolute'
}).attr("id", css.root);
// elements
root.children().eq(0).attr("id", css.head).end().eq(1).attr("id", css.body).children().eq(0).attr("id", css.days).end().eq(1).attr("id", css.weeks).end().end().end().find("a").eq(0).attr("id", css.prev).end().eq(1).attr("id", css.next);
// title
title = root.find("#" + css.head).find("div").attr("id", css.title);
// year & month selectors
if(options.selectors) {
var monthSelector = $("").attr("id", css.month), yearSelector = $("").attr("id", css.year);
title.html(monthSelector.add(yearSelector));
}
// day titles
var days = root.find("#" + css.days);
// days of the week
for(var d = 0; d < 7; d++) {
days.append($("").text(labels.shortDays[(d + options.firstDay) % 7]));
}
var body = root.find("#" + css.body);
$('