popupcal.js

来自「Java的框架」· JavaScript 代码 · 共 669 行 · 第 1/2 页

JS
669
字号
/*
 * This is a set of function that can be use for poping up a calendar.
 *
 * popUpCalendar							pop up the calendar.
 * setPUCalDateFormate						set the display date format.
 * addHoliday								add holiday to calendar
 *
 */


var	fixedX = -1			// x position (-1 if to appear below control)
var	fixedY = -1			// y position (-1 if to appear below control)
var startAt = 0			// 0 - sunday ; 1 - monday
var showWeekNumber = 1	// 0 - don't show; 1 - show
var showToday = 1		// 0 - don't show; 1 - show
var pUCImgDir = ""			// directory for images ... e.g. var pUCImgDir="/img/"

var gotoString = "Go To Current Month"
var todayString = "Today is"
var weekString = "Wk"
var scrollLeftMessage = "Click to scroll to previous month. Hold mouse button to scroll automatically."
var scrollRightMessage = "Click to scroll to next month. Hold mouse button to scroll automatically."
var selectMonthMessage = "Click to select a month."
var selectYearMessage = "Click to select a year."
var selectDateMessage = "Select [date] as date." // do not replace [date], it will be replaced by date.

var	crossobj, crossMonthObj, crossYearObj, monthSelected, yearSelected, dateSelected, omonthSelected, oyearSelected, odateSelected, monthConstructed, yearConstructed, intervalID1, intervalID2, timeoutID1, timeoutID2, ctlToPlaceValue, ctlNow, nStartingYear
var shimobj, shimMobj;
var dateFormat = "dd/mm/yyyy";
var formatChar = " ";

var	bPageLoaded=false
var	ie=document.all
var	dom=document.getElementById

var	ns4=document.layers
var	today =	new	Date()
var	dateNow	 = today.getDate()
var	monthNow = today.getMonth()
var	yearNow	 = today.getYear()
var	imgsrc = new Array("drop1.gif","drop2.gif","left1.gif","left2.gif","right1.gif","right2.gif")
var	img	= new Array()

function HolidayRec (d, m, y, desc)
{
	this.d = d
	this.m = m
	this.y = y
	this.desc = desc
}

var HolidaysCounter = 0
var Holidays = new Array()

function addHoliday (d, m, y, desc)
{
	Holidays[HolidaysCounter++] = new HolidayRec ( d, m, y, desc )
}

function drawPopUpCalendar () {
	if (dom)
	{
		for	(i=0;i<imgsrc.length;i++)
		{
			img[i] = new Image
			img[i].src = pUCImgDir + imgsrc[i]
		}
		document.write ("<iframe id=\"calShim\" style=\"position:absolute; z-index:10000; visibility:hidden;\" src=\"about:blank\" frameBorder=\"0\" scrolling=\"no\"></iframe>");
		document.write ("<iframe id=\"calMShim\" style=\"position:absolute; z-index:10000; visibility:hidden;\" src=\"about:blank\" frameBorder=\"0\" scrolling=\"no\"></iframe>");
		document.write ("<div id='calendar'	style='position:absolute;visibility:hidden;z-index:10001;'><table id='calTable' width="+((showWeekNumber==1)?250:220)+" style='font-family:arial;font-size:11px;border-width:1px;border-style:solid;border-color:#a0a0a0;font-family:arial; font-size:11px}' bgcolor='#ffffff'><tr bgcolor='#0000aa'><td><table width='"+((showWeekNumber==1)?248:218)+"'><tr><td style='padding:2px;font-family:arial; font-size:11px;'><font color='#ffffff'><B><span id='caption'></span></B></font></td><td align=right><a href='javascript:hideCalendar()'><IMG SRC='"+pUCImgDir+"close.gif' WIDTH='15' HEIGHT='13' BORDER='0' ALT='Close the Calendar'></a></td></tr></table></td></tr><tr><td style='padding:5px' bgcolor=#ffffff><span id='cal_content'></span></td></tr>")
		
		if (showToday==1)
		{
			document.write ("<tr bgcolor=#f0f0f0><td style='padding:5px' align=center><span id='lblToday'></span></td></tr>")
		}
			
		document.write ("</table></div><div id='selectMonth' style='position:absolute;visibility:hidden;z-index:10002;'></div><div id='selectYear'	style='position:absolute;visibility:hidden;z-index:10002;'></div>");
	}
}

var	monthName =	new	Array("January","February","March","April","May","June","July","August","September","October","November","December")
if (startAt==0)
{
	dayName = new Array	("Sun","Mon","Tue","Wed","Thu","Fri","Sat")
}
else
{
	dayName = new Array	("Mon","Tue","Wed","Thu","Fri","Sat","Sun")
}
var	styleAnchor="text-decoration:none;color:black;"
var	styleLightBorder="border-style:solid;border-width:1px;border-color:#a0a0a0;"

function swapImage(srcImg, destImg){
	if (ie)	{ document.getElementById(srcImg).setAttribute("src",pUCImgDir + destImg) }
}

function hideCalendar()	{
	crossobj.visibility="hidden"
	shimobj.visibility="hidden"
	if (crossMonthObj != null){crossMonthObj.visibility="hidden"}
	if (shimMobj != null){shimMobj.visibility="hidden"}
	if (crossYearObj !=	null){crossYearObj.visibility="hidden"}

	//Closing a popup window when user clicked 'x', close the calendar. Added by D.T.
	//self.closed
}

function padZero(num) {
	return (num	< 10)? '0' + num : num ;
}

function constructDate(d,m,y)
{
	sTmp = dateFormat
	sTmp = sTmp.replace	("dd","<e>")
	sTmp = sTmp.replace	("d","<d>")
	sTmp = sTmp.replace	("<e>",padZero(d))
	sTmp = sTmp.replace	("<d>",d)
	sTmp = sTmp.replace	("mmm","<o>")
	sTmp = sTmp.replace	("mm","<n>")
	sTmp = sTmp.replace	("m","<m>")
	sTmp = sTmp.replace	("<m>",m+1)
	sTmp = sTmp.replace	("<n>",padZero(m+1))
	sTmp = sTmp.replace	("<o>",monthName[m])
	return sTmp.replace ("yyyy",y)
}

function closeCalendar() {
	var	sTmp

	hideCalendar();
	if (ctlToPlaceValue.length) {
		aFormat	= dateFormat.split(formatChar)
		for	(i=0;i<3;i++)
		{
			if ((aFormat[i]=="d") || (aFormat[i]=="dd")) {
				ctlToPlaceValue[i].value = dateSelected;
			}
			else if	((aFormat[i]=="m") || (aFormat[i]=="mm"))
			{
				ctlToPlaceValue[i].value = monthSelected+1;
			}
			else if	(aFormat[i]=="yyyy")
			{
				ctlToPlaceValue[i].value = yearSelected;
			}
			else if	(aFormat[i]=="mmm")
			{
				ctlToPlaceValue[i].value = monthName[monthSelected];
			}
		}
	}
	else {
		ctlToPlaceValue.value =	constructDate(dateSelected,monthSelected,yearSelected)
	}

	//Closing popup window when date is selected. Added By D.T.
	//self.close()
}

/*** Month Pulldown	***/

function StartDecMonth()
{
	intervalID1=setInterval("decMonth()",80)
}

function StartIncMonth()
{
	intervalID1=setInterval("incMonth()",80)
}

function incMonth () {
	monthSelected++
	if (monthSelected>11) {
		monthSelected=0
		yearSelected++
	}
	constructCalendar()
}

function decMonth () {
	monthSelected--
	if (monthSelected<0) {
		monthSelected=11
		yearSelected--
	}
	constructCalendar()
}

function constructMonth() {
	popDownYear()
	if (!monthConstructed) {
		sHTML =	""
		for	(i=0; i<12;	i++) {
			sName =	monthName[i];
			if (i==monthSelected){
				sName =	"<B>" +	sName +	"</B>"
			}
			sHTML += "<tr><td id='m" + i + "' onmouseover='this.style.backgroundColor=\"#FFCC99\"' onmouseout='this.style.backgroundColor=\"\"' style='cursor:pointer' onclick='monthConstructed=false;monthSelected=" + i + ";constructCalendar();popDownMonth();event.cancelBubble=true'>&nbsp;" + sName + "&nbsp;</td></tr>"
		}

		document.getElementById("selectMonth").innerHTML = "<table width=70	style='font-family:arial; font-size:11px; border-width:1px; border-style:solid; border-color:#a0a0a0;' bgcolor='#FFFFDD' cellspacing=0 onmouseover='clearTimeout(timeoutID1)'	onmouseout='clearTimeout(timeoutID1);timeoutID1=setTimeout(\"popDownMonth()\",100);event.cancelBubble=true'>" +	sHTML +	"</table>"

		monthConstructed=true
	}
}

function popUpMonth() {
	constructMonth()
	crossMonthObj.visibility = (dom||ie)? "visible"	: "show"
	crossMonthObj.left = parseInt(crossobj.left) + 50
	crossMonthObj.top =	parseInt(crossobj.top) + 26
	shimMobj.left = crossMonthObj.left;
	shimMobj.top = crossMonthObj.top;
	shimMobj.width = 90;
	shimMobj.height = 225;
	shimMobj.visibility= (dom||ie)? "visible"	: "show"
}

function popDownMonth()	{
	crossMonthObj.visibility= "hidden"
	shimMobj.visibility= "hidden"
}

/*** Year Pulldown ***/

function incYear() {
	for	(i=0; i<7; i++){
		newYear	= (i+nStartingYear)+1
		if (newYear==yearSelected)
		{ txtYear =	"&nbsp;<B>"	+ newYear +	"</B>&nbsp;" }
		else
		{ txtYear =	"&nbsp;" + newYear + "&nbsp;" }
		document.getElementById("y"+i).innerHTML = txtYear
	}
	nStartingYear ++;
}

function decYear() {
	for	(i=0; i<7; i++){
		newYear	= (i+nStartingYear)-1
		if (newYear==yearSelected)
		{ txtYear =	"&nbsp;<B>"	+ newYear +	"</B>&nbsp;" }
		else
		{ txtYear =	"&nbsp;" + newYear + "&nbsp;" }
		document.getElementById("y"+i).innerHTML = txtYear
	}
	nStartingYear --;
}

function selectYear(nYear) {
	yearSelected=parseInt(nYear+nStartingYear);
	yearConstructed=false;
	constructCalendar();
	popDownYear();
}

function constructYear() {
	popDownMonth()
	sHTML =	""
	if (!yearConstructed) {

		sHTML =	"<tr><td align='center'	onmouseover='this.style.backgroundColor=\"#FFCC99\"' onmouseout='clearInterval(intervalID1);this.style.backgroundColor=\"\"' style='cursor:pointer'	onmousedown='clearInterval(intervalID1);intervalID1=setInterval(\"decYear()\",30)' onmouseup='clearInterval(intervalID1)'>-</td></tr>"

		j =	0
		nStartingYear =	yearSelected-3
		for	(i=(yearSelected-3); i<=(yearSelected+3); i++) {
			sName =	i;
			if (i==yearSelected){
				sName =	"<B>" +	sName +	"</B>"
			}

			sHTML += "<tr><td id='y" + j + "' onmouseover='this.style.backgroundColor=\"#FFCC99\"' onmouseout='this.style.backgroundColor=\"\"' style='cursor:pointer' onclick='selectYear("+j+");event.cancelBubble=true'>&nbsp;" + sName + "&nbsp;</td></tr>"
			j ++;
		}

		sHTML += "<tr><td align='center' onmouseover='this.style.backgroundColor=\"#FFCC99\"' onmouseout='clearInterval(intervalID2);this.style.backgroundColor=\"\"' style='cursor:pointer' onmousedown='clearInterval(intervalID2);intervalID2=setInterval(\"incYear()\",30)'	onmouseup='clearInterval(intervalID2)'>+</td></tr>"

		document.getElementById("selectYear").innerHTML	= "<table width=44 style='font-family:arial; font-size:11px; border-width:1px; border-style:solid; border-color:#a0a0a0;'	bgcolor='#FFFFDD' onmouseover='clearTimeout(timeoutID2)' onmouseout='clearTimeout(timeoutID2);timeoutID2=setTimeout(\"popDownYear()\",100)' cellspacing=0>"	+ sHTML	+ "</table>"

		yearConstructed	= true
	}
}

function popDownYear() {
	clearInterval(intervalID1)
	clearTimeout(timeoutID1)
	clearInterval(intervalID2)
	clearTimeout(timeoutID2)
	crossYearObj.visibility= "hidden"
}

function popUpYear() {
	var	leftOffset

	constructYear()
	crossYearObj.visibility	= (dom||ie)? "visible" : "show"
	leftOffset = parseInt(crossobj.left) + document.getElementById("spanYear").offsetLeft
	if (ie)
	{
		leftOffset += 6
	}
	crossYearObj.left =	leftOffset
	crossYearObj.top = parseInt(crossobj.top) +	26
}

/*** calendar ***/

function WeekNbr(today)
{
	Year = takeYear(today);
	Month = today.getMonth();
	Day = today.getDate();
	now = Date.UTC(Year,Month,Day+1,0,0,0);
	var Firstday = new Date();
	Firstday.setYear(Year);
	Firstday.setMonth(0);
	Firstday.setDate(1);
	then = Date.UTC(Year,0,1,0,0,0);
	var Compensation = Firstday.getDay();
	if (Compensation > 3) Compensation -= 4;
	else Compensation += 3;
	NumberOfWeek =  Math.round((((now-then)/86400000)+Compensation)/7);
	return NumberOfWeek;
}

function takeYear(theDate)
{
	x = theDate.getYear();
	var y = x % 100;
	y += (y < 38) ? 2000 : 1900;
	return y;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?