calendar.js

来自「PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。」· JavaScript 代码 · 共 1,642 行 · 第 1/3 页

JS
1,642
字号
				if (status === true) {					cell.className += " disabled";					cell.disabled = true;				} else {					if (/disabled/i.test(status))						cell.disabled = true;					cell.className += " " + status;				}			}			if (!cell.disabled) {				ar_days[ar_days.length] = cell;				cell.caldate = iday;				cell.ttip = "_";				if (iday == mday) {					cell.className += " selected";					this.currentDateEl = cell;				}				if (hasToday && (iday == todayDate)) {					cell.className += " today";					cell.ttip += Calendar._TT["PART_TODAY"];				}				if (wday == SAT || wday == SUN) {					cell.className += " weekend";				}			}			++iday;			((++wday) ^ 7) || (wday = 0);			cell = cell.nextSibling;		}		row = row.nextSibling;	}	this.ar_days = ar_days;	this.title.firstChild.data = Calendar._MN[month] + ", " + year;	this.onSetTime();	// PROFILE	// this.tooltips.firstChild.data = "Generated in " + ((new Date()) - today) + " ms";};/** *  Calls _init function above for going to a certain date (but only if the *  date is different than the currently selected one). */Calendar.prototype.setDate = function (date) {	if (!date.equalsTo(this.date)) {		this._init(this.mondayFirst, date);	}};/** *  Refreshes the calendar.  Useful if the "disabledHandler" function is *  dynamic, meaning that the list of disabled date can change at runtime. *  Just * call this function if you think that the list of disabled dates *  should * change. */Calendar.prototype.refresh = function () {	this._init(this.mondayFirst, this.date);};/** Modifies the "mondayFirst" parameter (EU/US style). */Calendar.prototype.setMondayFirst = function (mondayFirst) {	this._init(mondayFirst, this.date);	this._displayWeekdays();};/** *  Allows customization of what dates are enabled.  The "unaryFunction" *  parameter must be a function object that receives the date (as a JS Date *  object) and returns a boolean value.  If the returned value is true then *  the passed date will be marked as disabled. */Calendar.prototype.setDateStatusHandler = Calendar.prototype.setDisabledHandler = function (unaryFunction) {	this.getDateStatus = unaryFunction;};/** Customization of allowed year range for the calendar. */Calendar.prototype.setRange = function (a, z) {	this.minYear = a;	this.maxYear = z;};/** Calls the first user handler (selectedHandler). */Calendar.prototype.callHandler = function () {	if (this.onSelected) {		this.onSelected(this, this.date.print(this.dateFormat));	}};/** Calls the second user handler (closeHandler). */Calendar.prototype.callCloseHandler = function () {	if (this.onClose) {		this.onClose(this);	}	this.hideShowCovered();};/** Removes the calendar object from the DOM tree and destroys it. */Calendar.prototype.destroy = function () {	var el = this.element.parentNode;	el.removeChild(this.element);	Calendar._C = null;	window.calendar = null;};/** *  Moves the calendar element to a different section in the DOM tree (changes *  its parent). */Calendar.prototype.reparent = function (new_parent) {	var el = this.element;	el.parentNode.removeChild(el);	new_parent.appendChild(el);};// This gets called when the user presses a mouse button anywhere in the// document, if the calendar is shown.  If the click was outside the open// calendar this function closes it.Calendar._checkCalendar = function(ev) {	if (!window.calendar) {		return false;	}	var el = Calendar.is_ie ? Calendar.getElement(ev) : Calendar.getTargetElement(ev);	for (; el != null && el != calendar.element; el = el.parentNode);	if (el == null) {		// calls closeHandler which should hide the calendar.		window.calendar.callCloseHandler();		return Calendar.stopEvent(ev);	}};/** Shows the calendar. */Calendar.prototype.show = function () {	var rows = this.table.getElementsByTagName("tr");	for (var i = rows.length; i > 0;) {		var row = rows[--i];		Calendar.removeClass(row, "rowhilite");		var cells = row.getElementsByTagName("td");		for (var j = cells.length; j > 0;) {			var cell = cells[--j];			Calendar.removeClass(cell, "hilite");			Calendar.removeClass(cell, "active");		}	}	this.element.style.display = "block";	this.hidden = false;	if (this.isPopup) {		window.calendar = this;		Calendar.addEvent(document, "keydown", Calendar._keyEvent);		Calendar.addEvent(document, "keypress", Calendar._keyEvent);		Calendar.addEvent(document, "mousedown", Calendar._checkCalendar);	}	this.hideShowCovered();};/** *  Hides the calendar.  Also removes any "hilite" from the class of any TD *  element. */Calendar.prototype.hide = function () {	if (this.isPopup) {		Calendar.removeEvent(document, "keydown", Calendar._keyEvent);		Calendar.removeEvent(document, "keypress", Calendar._keyEvent);		Calendar.removeEvent(document, "mousedown", Calendar._checkCalendar);	}	this.element.style.display = "none";	this.hidden = true;	this.hideShowCovered();};/** *  Shows the calendar at a given absolute position (beware that, depending on *  the calendar element style -- position property -- this might be relative *  to the parent's containing rectangle). */Calendar.prototype.showAt = function (x, y) {	var s = this.element.style;	s.left = x + "px";	s.top = y + "px";	this.show();};/** Shows the calendar near a given element. */Calendar.prototype.showAtElement = function (el, opts) {	var self = this;	var p = Calendar.getAbsolutePos(el);	if (!opts || typeof opts != "string") {		this.showAt(p.x, p.y + el.offsetHeight);		return true;	}	this.element.style.display = "block";	Calendar.continuation_for_the_fucking_khtml_browser = function() {		var w = self.element.offsetWidth;		var h = self.element.offsetHeight;		self.element.style.display = "none";		var valign = opts.substr(0, 1);		var halign = "l";		if (opts.length > 1) {			halign = opts.substr(1, 1);		}		// vertical alignment		switch (valign) {		    case "T": p.y -= h; break;		    case "B": p.y += el.offsetHeight; break;		    case "C": p.y += (el.offsetHeight - h) / 2; break;		    case "t": p.y += el.offsetHeight - h; break;		    case "b": break; // already there		}		// horizontal alignment		switch (halign) {		    case "L": p.x -= w; break;		    case "R": p.x += el.offsetWidth; break;		    case "C": p.x += (el.offsetWidth - w) / 2; break;		    case "r": p.x += el.offsetWidth - w; break;		    case "l": break; // already there		}		self.showAt(p.x, p.y);	};	if (Calendar.is_khtml)		setTimeout("Calendar.continuation_for_the_fucking_khtml_browser()", 10);	else		Calendar.continuation_for_the_fucking_khtml_browser();};/** Customizes the date format. */Calendar.prototype.setDateFormat = function (str) {	this.dateFormat = str;};/** Customizes the tooltip date format. */Calendar.prototype.setTtDateFormat = function (str) {	this.ttDateFormat = str;};/** *  Tries to identify the date represented in a string.  If successful it also *  calls this.setDate which moves the calendar to the given date. */Calendar.prototype.parseDate = function (str, fmt) {	var y = 0;	var m = -1;	var d = 0;	var a = str.split(/\W+/);	if (!fmt) {		fmt = this.dateFormat;	}	var b = [];	fmt.replace(/(%.)/g, function(str, par) {		return b[b.length] = par;	});	var i = 0, j = 0;	var hr = 0;	var min = 0;	for (i = 0; i < a.length; ++i) {		if (b[i] == "%a" || b[i] == "%A") {			continue;		}		if (b[i] == "%d" || b[i] == "%e") {			d = parseInt(a[i], 10);		}		if (b[i] == "%m") {			m = parseInt(a[i], 10) - 1;		}		if (b[i] == "%Y" || b[i] == "%y") {			y = parseInt(a[i], 10);			(y < 100) && (y += (y > 29) ? 1900 : 2000);		}		if (b[i] == "%b" || b[i] == "%B") {			for (j = 0; j < 12; ++j) {				if (Calendar._MN[j].substr(0, a[i].length).toLowerCase() == a[i].toLowerCase()) { m = j; break; }			}		} else if (/%[HIkl]/.test(b[i])) {			hr = parseInt(a[i], 10);		} else if (/%[pP]/.test(b[i])) {			if (/pm/i.test(a[i]) && hr < 12)				hr += 12;		} else if (b[i] == "%M") {			min = parseInt(a[i], 10);		}	}	if (y != 0 && m != -1 && d != 0) {		this.setDate(new Date(y, m, d, hr, min, 0));		return;	}	y = 0; m = -1; d = 0;	for (i = 0; i < a.length; ++i) {		if (a[i].search(/[a-zA-Z]+/) != -1) {			var t = -1;			for (j = 0; j < 12; ++j) {				if (Calendar._MN[j].substr(0, a[i].length).toLowerCase() == a[i].toLowerCase()) { t = j; break; }			}			if (t != -1) {				if (m != -1) {					d = m+1;				}				m = t;			}		} else if (parseInt(a[i], 10) <= 12 && m == -1) {			m = a[i]-1;		} else if (parseInt(a[i], 10) > 31 && y == 0) {			y = parseInt(a[i], 10);			(y < 100) && (y += (y > 29) ? 1900 : 2000);		} else if (d == 0) {			d = a[i];		}	}	if (y == 0) {		var today = new Date();		y = today.getFullYear();	}	if (m != -1 && d != 0) {		this.setDate(new Date(y, m, d, hr, min, 0));	}};Calendar.prototype.hideShowCovered = function () {	var self = this;	Calendar.continuation_for_the_fucking_khtml_browser = function() {		function getVisib(obj){			var value = obj.style.visibility;			if (!value) {				if (document.defaultView && typeof (document.defaultView.getComputedStyle) == "function") { // Gecko, W3C					if (!Calendar.is_khtml)						value = document.defaultView.							getComputedStyle(obj, "").getPropertyValue("visibility");					else						value = '';				} else if (obj.currentStyle) { // IE					value = obj.currentStyle.visibility;				} else					value = '';			}			return value;		};		var tags = new Array("applet", "iframe", "select");		var el = self.element;		var p = Calendar.getAbsolutePos(el);		var EX1 = p.x;		var EX2 = el.offsetWidth + EX1;		var EY1 = p.y;		var EY2 = el.offsetHeight + EY1;		for (var k = tags.length; k > 0; ) {			var ar = document.getElementsByTagName(tags[--k]);			var cc = null;			for (var i = ar.length; i > 0;) {				cc = ar[--i];				p = Calendar.getAbsolutePos(cc);				var CX1 = p.x;				var CX2 = cc.offsetWidth + CX1;				var CY1 = p.y;				var CY2 = cc.offsetHeight + CY1;				if (self.hidden || (CX1 > EX2) || (CX2 < EX1) || (CY1 > EY2) || (CY2 < EY1)) {					if (!cc.__msh_save_visibility) {						cc.__msh_save_visibility = getVisib(cc);					}					cc.style.visibility = cc.__msh_save_visibility;				} else {					if (!cc.__msh_save_visibility) {						cc.__msh_save_visibility = getVisib(cc);					}					cc.style.visibility = "hidden";				}			}		}	};	if (Calendar.is_khtml)		setTimeout("Calendar.continuation_for_the_fucking_khtml_browser()", 10);	else		Calendar.continuation_for_the_fucking_khtml_browser();};/** Internal function; it displays the bar with the names of the weekday. */Calendar.prototype._displayWeekdays = function () {	var MON = this.mondayFirst ? 0 : 1;	var SUN = this.mondayFirst ? 6 : 0;	var SAT = this.mondayFirst ? 5 : 6;	var cell = this.firstdayname;	for (var i = 0; i < 7; ++i) {		cell.className = "day name";		if (!i) {			cell.ttip = this.mondayFirst ? Calendar._TT["SUN_FIRST"] : Calendar._TT["MON_FIRST"];			cell.navtype = 100;			cell.calendar = this;			Calendar._add_evs(cell);		}		if (i == SUN || i == SAT) {			Calendar.addClass(cell, "weekend");		}		cell.firstChild.data = Calendar._SDN[i + 1 - MON];		cell = cell.nextSibling;	}};/** Internal function.  Hides all combo boxes that might be displayed. */Calendar.prototype._hideCombos = function () {	this.monthsCombo.style.display = "none";	this.yearsCombo.style.display = "none";};/** Internal function.  Starts dragging the element. */Calendar.prototype._dragStart = function (ev) {	if (this.dragging) {		return;	}	this.dragging = true;	var posX;	var posY;	if (Calendar.is_ie) {		posY = window.event.clientY + document.body.scrollTop;		posX = window.event.clientX + document.body.scrollLeft;	} else {		posY = ev.clientY + window.scrollY;		posX = ev.clientX + window.scrollX;	}	var st = this.element.style;	this.xOffs = posX - parseInt(st.left);	this.yOffs = posY - parseInt(st.top);	with (Calendar) {		addEvent(document, "mousemove", calDragIt);		addEvent(document, "mouseover", stopEvent);		addEvent(document, "mouseup", calDragEnd);	}};// BEGIN: DATE OBJECT PATCHES/** Adds the number of days array to the Date object. */Date._MD = new Array(31,28,31,30,31,30,31,31,30,31,30,31);/** Constants used for time computations */Date.SECOND = 1000 /* milliseconds */;Date.MINUTE = 60 * Date.SECOND;Date.HOUR   = 60 * Date.MINUTE;Date.DAY    = 24 * Date.HOUR;Date.WEEK   =  7 * Date.DAY;/** Returns the number of days in the current month */Date.prototype.getMonthDays = function(month) {	var year = this.getFullYear();	if (typeof month == "undefined") {		month = this.getMonth();	}	if (((0 == (year%4)) && ( (0 != (year%100)) || (0 == (year%400)))) && month == 1) {		return 29;	} else {		return Date._MD[month];	}};/** Returns the number of day in the year. */Date.prototype.getDayOfYear = function() {	var now = new Date(this.getFullYear(), this.getMonth(), this.getDate(), 0, 0, 0);	var then = new Date(this.getFullYear(), 0, 1, 0, 0, 0);	var time = now - then;	return Math.floor(time / Date.DAY);};/** Returns the number of the week in year, as defined in ISO 8601. */Date.prototype.getWeekNumber = function() {	var now = new Date(this.getFullYear(), this.getMonth(), this.getDate(), 0, 0, 0);	var then = new Date(this.getFullYear(), 0, 1, 0, 0, 0);	var time = now - then;	var day = then.getDay(); // 0 means Sunday	if (day == 0) day = 7;	(day > 4) && (day -= 4) || (day += 3);	return Math.round(((time / Date.DAY) + day) / 7);};/** Checks dates equality (ignores time) */Date.prototype.equalsTo = function(date) {	return ((this.getFullYear() == date.getFullYear()) &&		(this.getMonth() == date.getMonth()) &&		(this.getDate() == date.getDate()) &&		(this.getHours() == date.getHours()) &&		(this.getMinutes() == date.getMinutes()));};/** Prints the date in a string according to the given format. */Date.prototype.print = function (str) {	var m = this.getMonth();	var d = this.getDate();	var y = this.getFullYear();	var wn = this.getWeekNumber();	var w = this.getDay();	var s = {};	var hr = this.getHours();	var pm = (hr >= 12);	var ir = (pm) ? (hr - 12) : hr;	var dy = this.getDayOfYear();	if (ir == 0)		ir = 12;	var min = this.getMinutes();	var sec = this.getSeconds();	s["%a"] = Calendar._SDN[w]; // abbreviated weekday name [FIXME: I18N]	s["%A"] = Calendar._DN[w]; // full weekday name	s["%b"] = Calendar._SMN[m]; // abbreviated month name [FIXME: I18N]	s["%B"] = Calendar._MN[m]; // full month name	// FIXME: %c : preferred date and time representation for the current locale	s["%C"] = 1 + Math.floor(y / 100); // the century number	s["%d"] = (d < 10) ? ("0" + d) : d; // the day of the month (range 01 to 31)	s["%e"] = d; // the day of the month (range 1 to 31)	// FIXME: %D : american date style: %m/%d/%y	// FIXME: %E, %F, %G, %g, %h (man strftime)	s["%H"] = (hr < 10) ? ("0" + hr) : hr; // hour, range 00 to 23 (24h format)	s["%I"] = (ir < 10) ? ("0" + ir) : ir; // hour, range 01 to 12 (12h format)	s["%j"] = (dy < 100) ? ((dy < 10) ? ("00" + dy) : ("0" + dy)) : dy; // day of the year (range 001 to 366)	s["%k"] = hr;		// hour, range 0 to 23 (24h format)	s["%l"] = ir;		// hour, range 1 to 12 (12h format)	s["%m"] = (m < 9) ? ("0" + (1+m)) : (1+m); // month, range 01 to 12	s["%M"] = (min < 10) ? ("0" + min) : min; // minute, range 00 to 59	s["%n"] = "\n";		// a newline character	s["%p"] = pm ? "PM" : "AM";	s["%P"] = pm ? "pm" : "am";	// FIXME: %r : the time in am/pm notation %I:%M:%S %p	// FIXME: %R : the time in 24-hour notation %H:%M	s["%s"] = Math.floor(this.getTime() / 1000);	s["%S"] = (sec < 10) ? ("0" + sec) : sec; // seconds, range 00 to 59	s["%t"] = "\t";		// a tab character	// FIXME: %T : the time in 24-hour notation (%H:%M:%S)	s["%U"] = s["%W"] = s["%V"] = (wn < 10) ? ("0" + wn) : wn;	s["%u"] = w + 1;	// the day of the week (range 1 to 7, 1 = MON)	s["%w"] = w;		// the day of the week (range 0 to 6, 0 = SUN)	// FIXME: %x : preferred date representation for the current locale without the time	// FIXME: %X : preferred time representation for the current locale without the date	s["%y"] = ('' + y).substr(2, 2); // year without the century (range 00 to 99)	s["%Y"] = y;		// year with the century	s["%%"] = "%";		// a literal '%' character	var re = Date._msh_formatRegexp;	if (typeof re == "undefined") {		var tmp = "";		for (var i in s)			tmp += tmp ? ("|" + i) : i;		Date._msh_formatRegexp = re = new RegExp("(" + tmp + ")", 'g');	}	return str.replace(re, function(match, par) { return s[par]; });};// END: DATE OBJECT PATCHES// global object that remembers the calendarwindow.calendar = null;

⌨️ 快捷键说明

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