| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
yet another calendar, allowing the user to select date.
unlike most calendars though, this one use pure javascript, requires minimum code and is not using pop up window. it also can be customized in most, e.g. fonts, colors and text size. I will attach the full source code and usage sample, and post it here as well: Code:
--Calendar.js
var _panelID="CalendarPanel";
var _currentYear=0;
var _currentMonth=-1;
function OpenCalendar(defYear, defMonth, defDay) {
var now=new Date();
if ((typeof defYear == "undefined")||(isNaN(defYear)))
defYear = now.getFullYear();
if ((typeof defMonth == "undefined")||(isNaN(defMonth)))
defMonth = now.getMonth();
else
defMonth--;
if ((typeof defDay == "undefined")||(isNaN(defDay)))
defDay = now.getDate();
FillCalendar(defYear, defMonth+1, defDay);
var objPanel = document.getElementById(_panelID);
objPanel.style.display = "block";
var panelWidth = GetElementWidth(objPanel);
var panelHeight = GetElementHeight(objPanel);
var bodyWidth = document.body.clientWidth;
var bodyHeight = document.body.clientHeight;
var panelLeft = parseInt((bodyWidth/2)-(panelWidth/2));
var panelTop = parseInt((bodyHeight/2)-(panelHeight/2));
objPanel.style.left = panelLeft+"px";
objPanel.style.top = panelTop+"px";
}
function FillCalendar(year, month, day) {
//var date=new Date();
//date.setFullYear(year+1, month, day);
//alert(date);
_currentYear = year;
_currentMonth = month-1;
var daysCount=DaysInMonth(year, month);
var objPanel = document.getElementById(_panelID);
if (!objPanel) {
objPanel = document.createElement("div");
objPanel.id = _panelID;
objPanel.style.position = "absolute";
objPanel.style.display = "none";
objPanel.style.border = _calenderBorderSize+"px solid "+_calenderBorderColor;
objPanel.style.backgroundColor = _calendarBackground;
objPanel.onclick = new Function("HideCalendar()");
document.body.appendChild(objPanel);
}
while (objPanel.childNodes.length > 0)
objPanel.removeChild(objPanel.childNodes[0]);
BuildCalendarDetails(objPanel, _currentMonth, _currentYear);
var cellWidth=50;
var cellHeight=50;
var currentDay=1;
var objTable=document.createElement("table");
objTable.setAttribute("border", "0");
objTable.setAttribute("cellpadding", "0");
objTable.setAttribute("cellspacing", "0");
var objRow=0;
while (currentDay <= daysCount) {
if (((currentDay-1)%7) == 0) {
objRow=objTable.insertRow(objTable.rows.length);
}
for (var i=1; i<=7; i++) {
if (currentDay > daysCount)
break;
var objCell=objRow.insertCell(objRow.cells.length);
objCell.setAttribute("width", cellWidth+"");
objCell.setAttribute("height", cellHeight+"");
objCell.style.border = "1px solid black";
objCell.style.textAlign = "center";
objCell.style.backgroundColor = _calenderCellColor;
objCell.style.fontFamily = _calenderCellFontName;
objCell.style.fontSize = _calenderCellFontSize;
objCell.style.cursor = "pointer";
objCell.onmouseover = new Function("PutMoreLight(this)");
objCell.onmouseout = new Function("RestoreColor(this)");
objCell.onclick = new Function("CalenderCellClick(this);");
objCell.innerHTML = currentDay+"";
currentDay++;
}
}
objPanel.appendChild(objTable);
//window.resizeTo(objTable.clientWidth, objPanel.offsetHeight);
}
function BuildCalendarDetails(objContainer, month, year) {
var btnPreviousMonth=BuildCalendarButton((_rightToLeft )?">>":"<<");
btnPreviousMonth.onclick = PreviousMonthClick;
var btnNextMonth=BuildCalendarButton((_rightToLeft)?"<<":">>");
btnNextMonth.onclick = NextMonthClick;
var objMonthSpan=BuildCalendarSpan(_monthNames[month]);
var objYearSpan=BuildCalendarSpan(year+"");
var objSpan=document.createElement("div");
objSpan.id = _panelID+"_details";
objSpan.style.textAlign = "center";
objSpan.appendChild(BuildCalendarSpan(" "));
if (_rightToLeft) {
objSpan.appendChild(btnNextMonth);
objSpan.appendChild(BuildCalendarSpan(" "));
objSpan.appendChild(objMonthSpan);
objSpan.appendChild(BuildCalendarSpan(" "));
objSpan.appendChild(objYearSpan);
objSpan.appendChild(BuildCalendarSpan(" "));
objSpan.appendChild(btnPreviousMonth);
}
else {
objSpan.appendChild(btnPreviousMonth);
objSpan.appendChild(BuildCalendarSpan(" "));
objSpan.appendChild(objYearSpan);
objSpan.appendChild(BuildCalendarSpan(" "));
objSpan.appendChild(objMonthSpan);
objSpan.appendChild(BuildCalendarSpan(" "));
objSpan.appendChild(btnNextMonth);
}
objSpan.appendChild(BuildCalendarSpan(" "));
//objSpan.appendChild(BuildCalendarSpan("<br />"));
objContainer.appendChild(objSpan);
}
function BuildCalendarButton(strText) {
var result=document.createElement("button");
result.setAttribute("type", "button");
result.style.fontSize = "18px";
result.innerHTML = strText;
return result;
}
function BuildCalendarSpan(strText) {
var result=document.createElement("span");
result.style.fontFamily = _calenderTextFontName;
result.style.fontSize = _calenderTextFontSize;
result.style.color = _calenderTextColor;
result.innerHTML = strText;
return result;
}
function CalenderCellClick(objCell) {
//hide:
HideCalendar();
//set date:
var date=new Date();
date.setFullYear(_currentYear, _currentMonth, parseInt(objCell.innerHTML));
//activate callback function:
eval(_calendarCallbackFunction+"('"+date+"')");
}
function HideCalendar() {
var objPanel = document.getElementById(_panelID);
objPanel.style.display = "none";
}
function PreviousMonthClick() {
_currentMonth--;
if (_currentMonth < 0) {
_currentMonth = 11;
_currentYear--;
}
FillCalendar(_currentYear, _currentMonth+1, 1);
}
function NextMonthClick() {
_currentMonth++;
if (_currentMonth > 11) {
_currentMonth = 0;
_currentYear++;
}
FillCalendar(_currentYear, _currentMonth+1, 1);
}
function DaysInMonth(year, month) {
var date=new Date();
var result=0;
date.setFullYear(year, month-1, 1);
while ((date.getFullYear() <= year)&&(date.getMonth() <= (month-1))) {
result++;
if (result > 31) {
alert("error getting days in month!\nyear: "+year+", month: "+month);
return 0;
}
date.setFullYear(year, month-1, date.getDate()+1);
}
return result;
}
function GetElementWidth(element) {
return element.clientWidth;
}
function GetElementHeight(element) {
return element.clientHeight;
}
var arrColoredControls=new Array();
function PutMoreLight(objControl, color, lightAmount) {
var cancelAddLight=objControl.getAttribute("cancel_add_light");
if (cancelAddLight == "1")
return true;
if (typeof color == "undefined")
color = _calenderCellColor;
if (typeof lightAmount == "undefined")
lightAmount = _calendarAddLight;
arrColoredControls[objControl] = color;
var R=HexToInt(color.substring(1, 3));
var G=HexToInt(color.substring(3, 5));
var B=HexToInt(color.substring(5, 7));
R = SafeAdd(R, lightAmount, 0, 255);
G = SafeAdd(G, lightAmount, 0, 255);
B = SafeAdd(B, lightAmount, 0, 255);
var lightedColor=BuildColor(R, G, B);
objControl.style.backgroundColor = lightedColor;
}
function RestoreColor(objControl) {
var cancelAddLight=objControl.getAttribute("cancel_add_light");
if (cancelAddLight == "1")
return true;
objControl.style.backgroundColor = arrColoredControls[objControl];
}
function IntToHex(num) {
if (num < 10)
return (num+"");
switch (num) {
case 10: return "a";
case 11: return "b";
case 12: return "c";
case 13: return "d";
case 14: return "e";
case 15: return "f";
}
return IntToHex(parseInt(num/16))+IntToHex(parseInt(num%16));
}
function HexToInt(strHex) {
return parseInt(strHex, 16);
}
function SafeAdd(num, addition, min, max) {
num += addition;
if (num > max)
num = max;
if (num < min)
num = min;
return num;
}
function BuildColor(r, g, b) {
var R=IntToHex(r);
var G=IntToHex(g);
var B=IntToHex(b);
R=(R.length == 1)?("0"+R):R;
G=(G.length == 1)?("0"+G):G;
B=(B.length == 1)?("0"+B):B;
return "#"+R+G+B;
}
Code:
--CalendarTest.html
<html>
<head>
<meta http-equiv="Content-Type" context="text/html; charset=windows-1255" />
<title>Calendar Test</title>
<script type="text/javascript">
//var _monthNames=new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var _monthNames=new Array("ינואר", "פברואר", "מרץ", "אפריל", "מאי", "יוני", "יולי", "אוגוסט", "ספטמבר", "אוקטובר", "נובמבר", "דצמבר");
var _rightToLeft=true;
var _calendarBackground="#B5C3C3";
var _calenderBorderSize=3;
var _calenderBorderColor="blue";
var _calenderCellColor="#F6B853";
var _calenderCellFontName="Tahoma";
var _calenderCellFontSize="21px";
var _calenderTextFontName="Tahoma";
var _calenderTextFontSize="22px";
var _calenderTextColor="white";
var _calendarAddLight=30;
var _calendarCallbackFunction="CalendarCallback";
</script>
<script type="text/javascript" src="Calendar.js"></script>
<script type="text/javascript">
function BrowseDate() {
var strDate = document.forms[0].elements["Date"].value;
var arrParts=strDate.split("/");
OpenCalendar(parseInt(arrParts[2]), parseInt(arrParts[1]), parseInt(arrParts[0]));
}
function CalendarCallback(strSelectedDate) {
var date=new Date(strSelectedDate);
document.forms[0].elements["Date"].value = date.getDate()+"/"+(date.getMonth()+1)+"/"+date.getFullYear();
}
</script>
</head>
<body>
Hello World!
<form>
Date: <input type="text" name="Date" /><button type="button" onclick="BrowseDate();">...</button>
</form>
</body>
</html>
the usage sample let you choose date and put it inside textbox. any comments or questions are welcome! ![]() |
|
#2
|
||||
|
||||
|
important note: you should not change the code inside Calendar.js!
you have to include that file as-is, any changes will cause unknown problems. |
|
#3
|
||||
|
||||
|
English Version????
Shadow..
Any English version available for the calendar. Thanks Rajesh Sivaraman.
__________________
e-CVs.net--DubaiDonkey.com--NetaSpace.com--e-jobSearch--TelePortMyJob Job Beaver--Dot Net Disciple |
|
#4
|
||||
|
||||
|
Modify these lines in CalendarTest.html:
Code:
//var _monthNames=new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var _monthNames=new Array("ינואר", "פברואר", "מרץ", "אפריל", "מאי", "יוני", "יולי", "אוגוסט", "ספטמבר", "אוקטובר", "נובמבר", "דצמבר");
To: Code:
var _monthNames=new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
//var _monthNames=new Array("ינואר", "פברואר", "מרץ", "אפריל", "מאי", "יוני", "יולי", "אוגוסט", "ספטמבר", "אוקטובר", "נובמבר", "דצמבר");
__________________
www.simplelyrics.net - www.xoise.com - www.g1games.com - www.quotemeanings.net - www.ourfreegames.com |
|
#5
|
||||
|
||||
|
yep BBD is 100% correct - changing language is as easy as that.
![]() |
|
#6
|
|||
|
|||
|
Updated/changed version
I've used this calendar as a foundation and rewritten much of it for some of the thing we needed here for our site. Unfortunately, I've taken some of the flexiblity away, but occationally added other flexibility.
Differences: I got rid of most global variables that were not just configuration. That way two calendars can run at the same time and be interacted with without messing eachother up. I put the days into S,M,T,W,T,F,S columns, and shaded sunday and monday a little darker. I took out the right to left code since we'd never use it, and it made the code easier for me to read . (left to right is only option now)The default format is 'mm-dd-yyyy' now, but should still be easily changed. Now you simply give your input field and ID= setting, and launch BrowseDate wtih "BrowseDate(ID)". When a change is made, the "onchange" of your input box is executed. I generally shrunk the whole thing by changing the default font sizes and stuff. I made the calendar pop-up relative to where your input box is, so if you know the size of your box, you can make it up in a predictable stop right next to the mouse if you like.. I don't think I made it too much longer or slower.. I think it's still pretty snappy which is what attracted me to it in the first place. I think IE renders it slowly, but firefox is very quick.. I'm sure IE7 will probably be quick. I'm not really done yet, but I thought someone might be interested because it's a good little calendar that doesn't get in your way.. It integrates very easily, and it's pretty self-contained which is what attracted me to it in the first place... |
|
#7
|
||||
|
||||
|
sounds great - welcome to the forum and thanks!
![]() |
|
#8
|
|||
|
|||
|
Any chance of some code on how you call the - updated - calender control?
Thanks |
|
#9
|
|||
|
|||
|
Usage example
I'm working on another updated version. It currently has highlighting of the currently selected day, and highlighting of "today" as well, but I'm also going to add a "today" button, s,m,t,w,t,f,s column headers, and a better close mechanism. I'd also like to fix a rendering problem with the previous/next buttons. I want them justified out into the corners all the way, and right now it's just approximated with some spacing of the text between them. I couldn't find a happy css way to do it though, so I'll probably be putting them into a small table :/...
This is the code that my other code generates to use it.. (I've chopped out some css style stuff, but mechanically, this should work). The "780534342" is just generated by srand() so that I have unique ID's and can have many calendar's on a page. Any ID should be fine as long as it's unique to the input box that you'd like the values to be taken from and written to. <form> <input type=text id='780534342' title='mm-dd-YYYY' name='submitted_to_eng' value='' size=10 onchange=""> <a href=javascript:void(0); onclick="BrowseDate('780534342');" title='Choose Date'> <img src='/images/show-calendar2.gif' border=0> </a> </form> |
|
#10
|
|||
|
|||
|
New version
OK, this is my latest calendar version. It's fairly complete. It could be more configurable, but I've been entertaining myself trying to make it as short as possible.
Usage is simple (note: ID must be numeric, I don't know why): <input id='2112' type=text size=10 name=form_field_name> <a href=javascript:BrowseDate('2112');>Edit Date</a> <input id='2113' type=text size=10 name=form_field_name> <a href=javascript:BrowseDate('2113');>Edit Date2</a> A better example can be found at: http://pipeline.umci.com/javascript/cal_example.html I'm having troubles figuring out why it's so slow in IE, but so blazingly fast in firefox. Most of our userbase is firefox so it doesn't matter that much, but it'd sure be great to fix anyway. I've tried to fix most of the obviously extraneous string catenations, but this had little/no affect. If anyone knows why this is slow in IE, I'd love to fix it. I'm afraid I'm not much of a javascript programer though. It looks like it runs quickly, and renders quickly, but then there's a delay before the onclick on the table cell becomes available. It's strange because it should be done with the code by then. Wishlist: 1) Figure out why the "id" fields have to be numeric.. I'm pretty sure I've used arbitrary text with getElementById before and it's worked fine, so it must be something I'm doing. 2) I would like to make it so you can click on the month/year, and you get a month/year list going backward and forward about a year (with arrows to jump the list by a year back and forward). 3) I would like to create a view that shows an entire year at a time. |
|
#11
|
|||
|
|||
|
Calendar
Of course, it's always better to attach the file when your message says something along the lines of "here it is".. Ooops..
|
|
#12
|
|||
|
|||
|
Hey thanks
Will review all this !! |
![]() |
| Viewing: ASP Free Forums > Programming > Code Bank > Pure Javascript Calendar |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|