calendar.js

来自「国外很不错的一个开源OA系统Group-Office」· JavaScript 代码 · 共 2,305 行 · 第 1/5 页

JS
2,305
字号
/*** Builds the date digit that will be displayed in calendar cells* @method buildDayLabel* @param {Date}	workingDate	The current working date* @return	{String}	The formatted day label*/YAHOO.widget.Calendar.prototype.buildDayLabel = function(workingDate) {	var day = workingDate.getDate();	return day;};/*** Renders the calendar header.* @method renderHeader* @param {Array}	html	The current working HTML array* @return {Array} The current working HTML array*/YAHOO.widget.Calendar.prototype.renderHeader = function(html) {	var colSpan = 7;	if (this.cfg.getProperty("SHOW_WEEK_HEADER")) {		colSpan += 1;	}	if (this.cfg.getProperty("SHOW_WEEK_FOOTER")) {		colSpan += 1;	}	html[html.length] = "<thead>";	html[html.length] =		"<tr>";	html[html.length] =			'<th colspan="' + colSpan + '" class="' + this.Style.CSS_HEADER_TEXT + '">';	html[html.length] =				'<div class="' + this.Style.CSS_HEADER + '">';		var renderLeft, renderRight = false;		if (this.parent) {			if (this.index === 0) {				renderLeft = true;			}			if (this.index == (this.parent.cfg.getProperty("pages") -1)) {				renderRight = true;			}		} else {			renderLeft = true;			renderRight = true;		}		var cal = this.parent || this;		if (renderLeft) {			html[html.length] = '<a class="' + this.Style.CSS_NAV_LEFT + '" style="background-image:url(' + this.cfg.getProperty("NAV_ARROW_LEFT") + ')">&#160;</a>';		}		html[html.length] = this.buildMonthLabel();		if (renderRight) {			html[html.length] = '<a class="' + this.Style.CSS_NAV_RIGHT + '" style="background-image:url(' + this.cfg.getProperty("NAV_ARROW_RIGHT") + ')">&#160;</a>';		}	html[html.length] =				'</div>';	html[html.length] =			'</th>';	html[html.length] =		'</tr>';	if (this.cfg.getProperty("SHOW_WEEKDAYS")) {		html = this.buildWeekdays(html);	}	html[html.length] = '</thead>';	return html;};/*** Renders the Calendar's weekday headers.* @method buildWeekdays* @param {Array}	html	The current working HTML array* @return {Array} The current working HTML array*/YAHOO.widget.Calendar.prototype.buildWeekdays = function(html) {	html[html.length] = '<tr class="' + this.Style.CSS_WEEKDAY_ROW + '">';	if (this.cfg.getProperty("SHOW_WEEK_HEADER")) {		html[html.length] = '<th>&#160;</th>';	}	for(var i=0;i<this.Locale.LOCALE_WEEKDAYS.length;++i) {		html[html.length] = '<th class="calweekdaycell">' + this.Locale.LOCALE_WEEKDAYS[i] + '</th>';	}	if (this.cfg.getProperty("SHOW_WEEK_FOOTER")) {		html[html.length] = '<th>&#160;</th>';	}	html[html.length] = '</tr>';	return html;};/*** Renders the calendar body.* @method renderBody* @param {Date}	workingDate	The current working Date being used for the render process* @param {Array}	html	The current working HTML array* @return {Array} The current working HTML array*/YAHOO.widget.Calendar.prototype.renderBody = function(workingDate, html) {	var startDay = this.cfg.getProperty("START_WEEKDAY");	this.preMonthDays = workingDate.getDay();	if (startDay > 0) {		this.preMonthDays -= startDay;	}	if (this.preMonthDays < 0) {		this.preMonthDays += 7;	}	this.monthDays = YAHOO.widget.DateMath.findMonthEnd(workingDate).getDate();	this.postMonthDays = YAHOO.widget.Calendar.DISPLAY_DAYS-this.preMonthDays-this.monthDays;	workingDate = YAHOO.widget.DateMath.subtract(workingDate, YAHOO.widget.DateMath.DAY, this.preMonthDays);	var useDate,weekNum,weekClass;	useDate = this.cfg.getProperty("pagedate");	html[html.length] = '<tbody class="m' + (useDate.getMonth()+1) + '">';	var i = 0;	var tempDiv = document.createElement("div");	var cell = document.createElement("td");	tempDiv.appendChild(cell);	var jan1 = new Date(useDate.getFullYear(),0,1);	var cal = this.parent || this;	for (var r=0;r<6;r++) {		weekNum = YAHOO.widget.DateMath.getWeekNumber(workingDate, useDate.getFullYear(), startDay);		weekClass = "w" + weekNum;		if (r !== 0 && this.isDateOOM(workingDate) && this.cfg.getProperty("HIDE_BLANK_WEEKS") === true) {			break;		} else {			html[html.length] = '<tr class="' + weekClass + '">';			if (this.cfg.getProperty("SHOW_WEEK_HEADER")) { html = this.renderRowHeader(weekNum, html); }			for (var d=0;d<7;d++){ // Render actual days				var cellRenderers = [];				this.clearElement(cell);				YAHOO.util.Dom.addClass(cell, "calcell");				cell.id = this.id + "_cell" + i;				cell.innerHTML = i;				var renderer = null;				if (workingDate.getFullYear()	== this.today.getFullYear() &&					workingDate.getMonth()		== this.today.getMonth() &&					workingDate.getDate()		== this.today.getDate()) {					cellRenderers[cellRenderers.length]=cal.renderCellStyleToday;				}				this.cellDates[this.cellDates.length]=[workingDate.getFullYear(),workingDate.getMonth()+1,workingDate.getDate()]; // Add this date to cellDates				if (this.isDateOOM(workingDate)) {					cellRenderers[cellRenderers.length]=cal.renderCellNotThisMonth;				} else {					YAHOO.util.Dom.addClass(cell, "wd" + workingDate.getDay());					YAHOO.util.Dom.addClass(cell, "d" + workingDate.getDate());					for (var s=0;s<this.renderStack.length;++s) {						var rArray = this.renderStack[s];						var type = rArray[0];						var month;						var day;						var year;						switch (type) {							case YAHOO.widget.Calendar.DATE:								month = rArray[1][1];								day = rArray[1][2];								year = rArray[1][0];								if (workingDate.getMonth()+1 == month && workingDate.getDate() == day && workingDate.getFullYear() == year) {									renderer = rArray[2];									this.renderStack.splice(s,1);								}								break;							case YAHOO.widget.Calendar.MONTH_DAY:								month = rArray[1][0];								day = rArray[1][1];								if (workingDate.getMonth()+1 == month && workingDate.getDate() == day) {									renderer = rArray[2];									this.renderStack.splice(s,1);								}								break;							case YAHOO.widget.Calendar.RANGE:								var date1 = rArray[1][0];								var date2 = rArray[1][1];								var d1month = date1[1];								var d1day = date1[2];								var d1year = date1[0];								var d1 = new Date(d1year, d1month-1, d1day);								var d2month = date2[1];								var d2day = date2[2];								var d2year = date2[0];								var d2 = new Date(d2year, d2month-1, d2day);								if (workingDate.getTime() >= d1.getTime() && workingDate.getTime() <= d2.getTime()) {									renderer = rArray[2];									if (workingDate.getTime()==d2.getTime()) {										this.renderStack.splice(s,1);									}								}								break;							case YAHOO.widget.Calendar.WEEKDAY:								var weekday = rArray[1][0];								if (workingDate.getDay()+1 == weekday) {									renderer = rArray[2];								}								break;							case YAHOO.widget.Calendar.MONTH:								month = rArray[1][0];								if (workingDate.getMonth()+1 == month) {									renderer = rArray[2];								}								break;						}						if (renderer) {							cellRenderers[cellRenderers.length]=renderer;						}					}				}				if (this._indexOfSelectedFieldArray([workingDate.getFullYear(),workingDate.getMonth()+1,workingDate.getDate()]) > -1) {					cellRenderers[cellRenderers.length]=cal.renderCellStyleSelected;				}				var mindate = this.cfg.getProperty("mindate");				var maxdate = this.cfg.getProperty("maxdate");				if (mindate) {					mindate = YAHOO.widget.DateMath.clearTime(mindate);				}				if (maxdate) {					maxdate = YAHOO.widget.DateMath.clearTime(maxdate);				}				if (					(mindate && (workingDate.getTime() < mindate.getTime())) ||					(maxdate && (workingDate.getTime() > maxdate.getTime()))				) {					cellRenderers[cellRenderers.length]=cal.renderOutOfBoundsDate;				} else {					cellRenderers[cellRenderers.length]=cal.styleCellDefault;					cellRenderers[cellRenderers.length]=cal.renderCellDefault;				}				for (var x=0;x<cellRenderers.length;++x) {					var ren = cellRenderers[x];					if (ren.call((this.parent || this),workingDate,cell) == YAHOO.widget.Calendar.STOP_RENDER) {						break;					}				}				workingDate.setTime(workingDate.getTime() + YAHOO.widget.DateMath.ONE_DAY_MS);				if (i >= 0 && i <= 6) {					YAHOO.util.Dom.addClass(cell, this.Style.CSS_CELL_TOP);				}				if ((i % 7) === 0) {					YAHOO.util.Dom.addClass(cell, this.Style.CSS_CELL_LEFT);				}				if (((i+1) % 7) === 0) {					YAHOO.util.Dom.addClass(cell, this.Style.CSS_CELL_RIGHT);				}				var postDays = this.postMonthDays;				if (postDays >= 7 && this.cfg.getProperty("HIDE_BLANK_WEEKS")) {					var blankWeeks = Math.floor(postDays/7);					for (var p=0;p<blankWeeks;++p) {						postDays -= 7;					}				}				if (i >= ((this.preMonthDays+postDays+this.monthDays)-7)) {					YAHOO.util.Dom.addClass(cell, this.Style.CSS_CELL_BOTTOM);				}				html[html.length] = tempDiv.innerHTML;				i++;			}			if (this.cfg.getProperty("SHOW_WEEK_FOOTER")) { html = this.renderRowFooter(weekNum, html); }			html[html.length] = '</tr>';		}	}	html[html.length] = '</tbody>';	return html;};/*** Renders the calendar footer. In the default implementation, there is* no footer.* @method renderFooter* @param {Array}	html	The current working HTML array* @return {Array} The current working HTML array*/YAHOO.widget.Calendar.prototype.renderFooter = function(html) { return html; };/*** Renders the calendar after it has been configured. The render() method has a specific call chain that will execute* when the method is called: renderHeader, renderBody, renderFooter.* Refer to the documentation for those methods for information on* individual render tasks.* @method render*/YAHOO.widget.Calendar.prototype.render = function() {	this.beforeRenderEvent.fire();	// Find starting day of the current month	var workingDate = YAHOO.widget.DateMath.findMonthStart(this.cfg.getProperty("pagedate"));	this.resetRenderers();	this.cellDates.length = 0;	YAHOO.util.Event.purgeElement(this.oDomContainer, true);	var html = [];	html[html.length] = '<table cellSpacing="0" class="' + this.Style.CSS_CALENDAR + ' y' + workingDate.getFullYear() + '" id="' + this.id + '">';	html = this.renderHeader(html);	html = this.renderBody(workingDate, html);	html = this.renderFooter(html);	html[html.length] = '</table>';	this.oDomContainer.innerHTML = html.join("\n");	this.applyListeners();	this.cells = this.oDomContainer.getElementsByTagName("td");	this.cfg.refireEvent("title");	this.cfg.refireEvent("close");	this.cfg.refireEvent("iframe");	this.renderEvent.fire();};/*** Applies the Calendar's DOM listeners to applicable elements.* @method applyListeners*/YAHOO.widget.Calendar.prototype.applyListeners = function() {	var root = this.oDomContainer;	var cal = this.parent || this;	var linkLeft, linkRight;	linkLeft = YAHOO.util.Dom.getElementsByClassName(this.Style.CSS_NAV_LEFT, "a", root);	linkRight = YAHOO.util.Dom.getElementsByClassName(this.Style.CSS_NAV_RIGHT, "a", root);	if (linkLeft) {		this.linkLeft = linkLeft[0];		YAHOO.util.Event.addListener(this.linkLeft, "mousedown", cal.previousMonth, cal, true);	}	if (linkRight) {		this.linkRight = linkRight[0];		YAHOO.util.Event.addListener(this.linkRight, "mousedown", cal.nextMonth, cal, true);	}	if (this.domEventMap) {		var el,elements;		for (var cls in this.domEventMap) {			if (this.domEventMap.hasOwnProperty(cls)) {				var items = this.domEventMap[cls];				if (! (items instanceof Array)) {					items = [items];				}				for (var i=0;i<items.length;i++)	{					var item = items[i];					elements = YAHOO.util.Dom.getElementsByClassName(cls, item.tag, this.oDomContainer);					for (var c=0;c<elements.length;c++) {						el = elements[c];						 YAHOO.util.Event.addListener(el, item.event, item.handler, item.scope, item.correct );					}				}			}		}	}	YAHOO.util.Event.addListener(this.oDomContainer, "click", this.doSelectCell, this);	YAHOO.util.Event.addListener(this.oDomContainer, "mouseover", this.doCellMouseOver, this);	YAHOO.util.Event.addListener(this.oDomContainer, "mouseout", this.doCellMouseOut, this);};/*** Retrieves the Date object for the specified Calendar cell* @method getDateByCellId* @param {String}	id	The id of the cell* @return {Date} The Date object for the specified Calendar cell*/YAHOO.widget.Calendar.prototype.getDateByCellId = function(id) {	var date = this.getDateFieldsByCellId(id);	return new Date(date[0],date[1]-1,date[2]);};/*** Retrieves the Date object for the specified Calendar cell* @method getDateFieldsByCellId* @param {String}	id	The id of the cell* @return {Array}	The array of Date fields for the specified Calendar cell*/YAHOO.widget.Calendar.prototype.getDateFieldsByCellId = function(id) {	id = id.toLowerCase().split("_cell")[1];	id = parseInt(id, 10);	return this.cellDates[id];};// BEGIN BUILT-IN TABLE CELL RENDERERS/*** Renders a cell that falls before the minimum date or after the maximum date.* widget class.* @method renderOutOfBoundsDate* @param {Date}					workingDate		The current working Date object being used to generate t

⌨️ 快捷键说明

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