PageSourceSearch

https://durabledns.com/calendar.js

js durabledns.com collected 2026-09-28 10:20:30 UTC 8,300 bytes, 232 lines download raw bytes

1/**
2 * Calendar Script
3 * Creates a calendar widget which can be used to select the date more easily than using just a text box
4 * http://www.openjs.com/scripts/ui/calendar/
5 *
6 * Example:
7 * <input type="text" name="date" id="date" />
8 * <script type="text/javascript">
9 * 		calendar.set("date");
10 * </script>
11 */
12calendar = {
13	month_names: ["January","February","March","April","May","June","July","Augest","September","October","November","December"],
14	weekdays: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
15	month_days: [31,28,31,30,31,30,31,31,30,31,30,31],
16	//Get today's date - year, month, day and date
17	today : new Date(),
18	opt : {},
19	data: [],
20
21	//Functions
22	/// Used to create HTML in a optimized way.
23	wrt:function(txt) {
24		this.data.push(txt);
25	},
26	
27	/* Inspired by http://www.quirksmode.org/dom/getstyles.html */
28	getStyle: function(ele, property){
29		if (ele.currentStyle) {
30			var alt_property_name = property.replace(/\-(\w)/g,function(m,c){return c.toUpperCase();});//background-color becomes backgroundColor
31			var value = ele.currentStyle[property]||ele.currentStyle[alt_property_name];
32		
33		} else if (window.getComputedStyle) {
34			property = property.replace(/([A-Z])/g,"-$1").toLowerCase();//backgroundColor becomes background-color
35
36			var value = document.defaultView.getComputedStyle(ele,null).getPropertyValue(property);
37		}
38		
39		//Some properties are special cases
40		if(property == "opacity" && ele.filter) value = (parseFloat( ele.filter.match(/opacity\=([^)]*)/)[1] ) / 100);
41		else if(property == "width" && isNaN(value)) value = ele.clientWidth || ele.offsetWidth;
42		else if(property == "height" && isNaN(value)) value = ele.clientHeight || ele.offsetHeight;
43		return value;
44	},
45	getPosition:function(ele) {
46		var x = 0;
47		var y = 0;
48		while (ele) {
49			x += ele.offsetLeft;
50			y += ele.offsetTop;
51			ele = ele.offsetParent;
52		}
53		if (navigator.userAgent.indexOf("Mac") != -1 && typeof document.body.leftMargin != "undefined") {
54			x += document.body.leftMargin;
55			offsetTop += document.body.topMargin;
56		}
57	
58		var xy = new Array(x,y);
59		return xy;
60	},
61	/// Called when the user clicks on a date in the calendar.
62	selectDate:function(year,month,day) {
63		var ths = _calendar_active_instance;
64		document.getElementById(ths.opt["input"]).value = year + "-" + month + "-" + day; // Date format is :HARDCODE:
65		ths.hideCalendar();
66	},
67	/// Creates a calendar with the date given in the argument as the selected date.
68	makeCalendar:function(year, month, day) {
69		year = parseInt(year);
70		month= parseInt(month);
71		day	 = parseInt(day);
72		
73		//Display the table
74		var next_month = month+1;
75		var next_month_year = year;
76		if(next_month>=12) {
77			next_month = 0;
78			next_month_year++;
79		}
80		
81		var previous_month = month-1;
82		var previous_month_year = year;
83		if(previous_month< 0) {
84			previous_month = 11;
85			previous_month_year--;
86		}
87		
88		this.wrt("<table>");
89		this.wrt("<tr><th><a href='javascript:calendar.makeCalendar("+(previous_month_year)+","+(previous_month)+");' title='"+this.month_names[previous_month]+" "+(previous_month_year)+"'>&lt;</a></th>");
90		this.wrt("<th colspan='5' class='calendar-title'><select name='calendar-month' class='calendar-month' onChange='calendar.makeCalendar("+year+",this.value);'>");
91		for(var i in this.month_names) {
92			this.wrt("<option value='"+i+"'");
93			if(i == month) this.wrt(" selected='selected'");
94			this.wrt(">"+this.month_names[i]+"</option>");
95		}
96		this.wrt("</select>");
97		this.wrt("<select name='calendar-year' class='calendar-year' onChange='calendar.makeCalendar(this.value, "+month+");'>");
98		var current_year = this.today.getYear();
99		if(current_year < 1900) current_year += 1900;
100		
101		for(var i=current_year-70; i<current_year+10; i++) {
102			this.wrt("<option value='"+i+"'")
103			if(i == year) this.wrt(" selected='selected'");
104			this.wrt(">"+i+"</option>");
105		}
106		this.wrt("</select></th>");
107		this.wrt("<th><a href='javascript:calendar.makeCalendar("+(next_month_year)+","+(next_month)+");' title='"+this.month_names[next_month]+" "+(next_month_year)+"'>&gt;</a></th></tr>");
108		this.wrt("<tr class='header'>");
109		for(var weekday=0; weekday<7; weekday++) this.wrt("<td>"+this.weekdays[weekday]+"</td>");
110		this.wrt("</tr>");
111		
112		//Get the first day of this month
113		var first_day = new Date(year,month,1);
114		var start_day = first_day.getDay();
115		
116		var d = 1;
117		var flag = 0;
118		
119		//Leap year support
120		if(year % 4 == 0) this.month_days[1] = 29;
121		else this.month_days[1] = 28;
122		
123		var days_in_this_month = this.month_days[month];
124
125		//Create the calender
126		for(var i=0;i<=5;i++) {
127			if(w >= days_in_this_month) break;
128			this.wrt("<tr>");
129			for(var j=0;j<7;j++) {
130				if(d > days_in_this_month) flag=0; //If the days has overshooted the number of days in this month, stop writing
131				else if(j >= start_day && !flag) flag=1;//If the first day of this month has come, start the date writing
132
133				if(flag) {
134					var w = d, mon = month+1;
135					if(w < 10)	w	= "0" + w;
136					if(mon < 10)mon = "0" + mon;
137
138					//Is it today?
139					var class_name = '';
140					var yea = this.today.getYear();
141					if(yea < 1900) yea += 1900;
142
143					if(yea == year && this.today.getMonth() == month && this.today.getDate() == d) class_name = " today";
144					if(day == d) class_name += " selected";
145					
146					class_name += " " + this.weekdays[j].toLowerCase();
147
148					this.wrt("<td class='days"+class_name+"'><a href='javascript:calendar.selectDate(\""+year+"\",\""+mon+"\",\""+w+"\")'>"+w+"</a></td>");
149					d++;
150				} else {
151					this.wrt("<td class='days'>&nbsp;</td>");
152				}
153			}
154			this.wrt("</tr>");
155		}
156		this.wrt("</table>");
157		this.wrt("<input type='button' value='Cancel' class='calendar-cancel' onclick='calendar.hideCalendar();' />");
158
159		document.getElementById(this.opt['calendar']).innerHTML = this.data.join("");
160		this.data = [];
161	},
162	
163	/// Display the calendar - if a date exists in the input box, that will be selected in the calendar.
164	showCalendar: function() {
165		var input = document.getElementById(this.opt['input']);
166		
167		//Position the div in the correct location...
168		var div = document.getElementById(this.opt['calendar']);
169		var xy = this.getPosition(input);
170		var width = parseInt(this.getStyle(input,'width'));
171		div.style.left=(xy[0]+width+10)+"px";
172		div.style.top=xy[1]+"px";
173
174		// Show the calendar with the date in the input as the selected date
175		var existing_date = new Date();
176		var date_in_input = input.value;
177		if(date_in_input) {
178			var selected_date = false;
179			var date_parts = date_in_input.split("-");
180			if(date_parts.length == 3) {
181				date_parts[1]--; //Month starts with 0
182				selected_date = new Date(date_parts[0], date_parts[1], date_parts[2]);
183			}
184			if(selected_date && !isNaN(selected_date.getYear())) { //Valid date.
185				existing_date = selected_date;
186			}
187		}
188		
189		var the_year = existing_date.getYear();
190		if(the_year < 1900) the_year += 1900;
191		this.makeCalendar(the_year, existing_date.getMonth(), existing_date.getDate());
192		document.getElementById(this.opt['calendar']).style.display = "block";
193		_calendar_active_instance = this;
194	},
195	
196	/// Hides the currently show calendar.
197	hideCalendar: function(instance) {
198		var active_calendar_id = "";
199		if(instance) active_calendar_id = instance.opt['calendar'];
200		else active_calendar_id = _calendar_active_instance.opt['calendar'];
201		
202		if(active_calendar_id) document.getElementById(active_calendar_id).style.display = "none";
203		_calendar_active_instance = {};
204	},
205	
206	/// Setup a text input box to be a calendar box.
207	set: function(input_id) {
208		var input = document.getElementById(input_id);
209		if(!input) return; //If the input field is not there, exit.
210		
211		if(!this.opt['calendar']) this.init();
212		
213		var ths = this;
214		input.onclick=function(){
215			ths.opt['input'] = this.id;
216			ths.showCalendar();
217		};
218	},
219	
220	/// Will be called once when the first input is set.
221	init: function() {
222		if(!this.opt['calendar'] || !document.getElementById(this.opt['calendar'])) {
223			var div = document.createElement('div');
224			if(!this.opt['calendar']) this.opt['calendar'] = 'calender_div_'+ Math.round(Math.random() * 100);
225
226			div.setAttribute('id',this.opt['calendar']);
227			div.className="calendar-box";
228
229			document.getElementsByTagName("body")[0].insertBefore(div,document.getElementsByTagName("body")[0].firstChild);
230		}
231	}
232}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.