📄 calendar.js
字号:
* @param {YAHOO.util.CustomEvent} evt The CustomEvent for which to check the subscriptions* @param {Function} fn The function to look for in the subscribers list* @param {Object} obj The execution scope Object for the subscription* @return {Boolean} true, if the function/Object pair is already subscribed to the CustomEvent passed in*/YAHOO.util.Config.alreadySubscribed = function(evt, fn, obj) { for (var e=0;e<evt.subscribers.length;e++) { var subsc = evt.subscribers[e]; if (subsc && subsc.obj == obj && subsc.fn == fn) { return true; } } return false;};/*** YAHOO.widget.DateMath is used for simple date manipulation. The class is a static utility* used for adding, subtracting, and comparing dates.* @namespace YAHOO.widget* @class DateMath*/YAHOO.widget.DateMath = { /** * Constant field representing Day * @property DAY * @static * @final * @type String */ DAY : "D", /** * Constant field representing Week * @property WEEK * @static * @final * @type String */ WEEK : "W", /** * Constant field representing Year * @property YEAR * @static * @final * @type String */ YEAR : "Y", /** * Constant field representing Month * @property MONTH * @static * @final * @type String */ MONTH : "M", /** * Constant field representing one day, in milliseconds * @property ONE_DAY_MS * @static * @final * @type Number */ ONE_DAY_MS : 1000*60*60*24, /** * Adds the specified amount of time to the this instance. * @method add * @param {Date} date The JavaScript Date object to perform addition on * @param {String} field The field constant to be used for performing addition. * @param {Number} amount The number of units (measured in the field constant) to add to the date. * @return {Date} The resulting Date object */ add : function(date, field, amount) { var d = new Date(date.getTime()); switch (field) { case this.MONTH: var newMonth = date.getMonth() + amount; var years = 0; if (newMonth < 0) { while (newMonth < 0) { newMonth += 12; years -= 1; } } else if (newMonth > 11) { while (newMonth > 11) { newMonth -= 12; years += 1; } } d.setMonth(newMonth); d.setFullYear(date.getFullYear() + years); break; case this.DAY: d.setDate(date.getDate() + amount); break; case this.YEAR: d.setFullYear(date.getFullYear() + amount); break; case this.WEEK: d.setDate(date.getDate() + (amount * 7)); break; } return d; }, /** * Subtracts the specified amount of time from the this instance. * @method subtract * @param {Date} date The JavaScript Date object to perform subtraction on * @param {Number} field The this field constant to be used for performing subtraction. * @param {Number} amount The number of units (measured in the field constant) to subtract from the date. * @return {Date} The resulting Date object */ subtract : function(date, field, amount) { return this.add(date, field, (amount*-1)); }, /** * Determines whether a given date is before another date on the calendar. * @method before * @param {Date} date The Date object to compare with the compare argument * @param {Date} compareTo The Date object to use for the comparison * @return {Boolean} true if the date occurs before the compared date; false if not. */ before : function(date, compareTo) { var ms = compareTo.getTime(); if (date.getTime() < ms) { return true; } else { return false; } }, /** * Determines whether a given date is after another date on the calendar. * @method after * @param {Date} date The Date object to compare with the compare argument * @param {Date} compareTo The Date object to use for the comparison * @return {Boolean} true if the date occurs after the compared date; false if not. */ after : function(date, compareTo) { var ms = compareTo.getTime(); if (date.getTime() > ms) { return true; } else { return false; } }, /** * Determines whether a given date is between two other dates on the calendar. * @method between * @param {Date} date The date to check for * @param {Date} dateBegin The start of the range * @param {Date} dateEnd The end of the range * @return {Boolean} true if the date occurs between the compared dates; false if not. */ between : function(date, dateBegin, dateEnd) { if (this.after(date, dateBegin) && this.before(date, dateEnd)) { return true; } else { return false; } }, /** * Retrieves a JavaScript Date object representing January 1 of any given year. * @method getJan1 * @param {Number} calendarYear The calendar year for which to retrieve January 1 * @return {Date} January 1 of the calendar year specified. */ getJan1 : function(calendarYear) { return new Date(calendarYear,0,1); }, /** * Calculates the number of days the specified date is from January 1 of the specified calendar year. * Passing January 1 to this function would return an offset value of zero. * @method getDayOffset * @param {Date} date The JavaScript date for which to find the offset * @param {Number} calendarYear The calendar year to use for determining the offset * @return {Number} The number of days since January 1 of the given year */ getDayOffset : function(date, calendarYear) { var beginYear = this.getJan1(calendarYear); // Find the start of the year. This will be in week 1. // Find the number of days the passed in date is away from the calendar year start var dayOffset = Math.ceil((date.getTime()-beginYear.getTime()) / this.ONE_DAY_MS); return dayOffset; }, /** * Calculates the week number for the given date. This function assumes that week 1 is the * week in which January 1 appears, regardless of whether the week consists of a full 7 days. * The calendar year can be specified to help find what a the week number would be for a given * date if the date overlaps years. For instance, a week may be considered week 1 of 2005, or * week 53 of 2004. Specifying the optional calendarYear allows one to make this distinction * easily. * @method getWeekNumber * @param {Date} date The JavaScript date for which to find the week number * @param {Number} calendarYear OPTIONAL - The calendar year to use for determining the week number. Default is * the calendar year of parameter "date". * @param {Number} weekStartsOn OPTIONAL - The integer (0-6) representing which day a week begins on. Default is 0 (for Sunday). * @return {Number} The week number of the given date. */ getWeekNumber : function(date, calendarYear) { date = this.clearTime(date); var nearestThurs = new Date(date.getTime() + (4 * this.ONE_DAY_MS) - ((date.getDay()) * this.ONE_DAY_MS)); var jan1 = new Date(nearestThurs.getFullYear(),0,1); var dayOfYear = ((nearestThurs.getTime() - jan1.getTime()) / this.ONE_DAY_MS) - 1; var weekNum = Math.ceil((dayOfYear)/ 7); return weekNum; }, /** * Determines if a given week overlaps two different years. * @method isYearOverlapWeek * @param {Date} weekBeginDate The JavaScript Date representing the first day of the week. * @return {Boolean} true if the date overlaps two different years. */ isYearOverlapWeek : function(weekBeginDate) { var overlaps = false; var nextWeek = this.add(weekBeginDate, this.DAY, 6); if (nextWeek.getFullYear() != weekBeginDate.getFullYear()) { overlaps = true; } return overlaps; }, /** * Determines if a given week overlaps two different months. * @method isMonthOverlapWeek * @param {Date} weekBeginDate The JavaScript Date representing the first day of the week. * @return {Boolean} true if the date overlaps two different months. */ isMonthOverlapWeek : function(weekBeginDate) { var overlaps = false; var nextWeek = this.add(weekBeginDate, this.DAY, 6); if (nextWeek.getMonth() != weekBeginDate.getMonth()) { overlaps = true; } return overlaps; }, /** * Gets the first day of a month containing a given date. * @method findMonthStart * @param {Date} date The JavaScript Date used to calculate the month start * @return {Date} The JavaScript Date representing the first day of the month */ findMonthStart : function(date) { var start = new Date(date.getFullYear(), date.getMonth(), 1); return start; }, /** * Gets the last day of a month containing a given date. * @method findMonthEnd * @param {Date} date The JavaScript Date used to calculate the month end * @return {Date} The JavaScript Date representing the last day of the month */ findMonthEnd : function(date) { var start = this.findMonthStart(date); var nextMonth = this.add(start, this.MONTH, 1); var end = this.subtract(nextMonth, this.DAY, 1); return end; }, /** * Clears the time fields from a given date, effectively setting the time to 12 noon. * @method clearTime * @param {Date} date The JavaScript Date for which the time fields will be cleared * @return {Date} The JavaScript Date cleared of all time fields */ clearTime : function(date) { date.setHours(12,0,0,0); return date; }};/*** The Calendar component is a UI control that enables users to choose one or more dates from a graphical calendar presented in a one-month ("one-up") or two-month ("two-up") interface. Calendars are generated entirely via script and can be navigated without any page refreshes.* @module calendar* @title Calendar* @namespace YAHOO.widget* @requires yahoo,dom,event*//*** Calendar is the base class for the Calendar widget. In its most basic* implementation, it has the ability to render a calendar widget on the page* that can be manipulated to select a single date, move back and forth between* months and years.* <p>To construct the placeholder for the calendar widget, the code is as* follows:* <xmp>* <div id="cal1Container"></div>* </xmp>* Note that the table can be replaced with any kind of element.* </p>* @namespace YAHOO.widget* @class Calendar* @constructor* @param {String} id The id of the table element that will represent the calendar widget* @param {String} containerId The id of the container div element that will wrap the calendar table* @param {Object} config The configuration object containing the Calendar's arguments*/YAHOO.widget.Calendar = function(id, containerId, config) { this.init(id, containerId, config);};/*** The path to be used for images loaded for the Calendar* @property YAHOO.widget.Calendar.IMG_ROOT* @static* @deprecated You can now customize images by overriding the calclose, calnavleft and calnavright default CSS classes for the close icon, left arrow and right arrow respectively* @type String*/YAHOO.widget.Calendar.IMG_ROOT = null;/*** Type constant used for renderers to represent an individual date (M/D/Y)* @property YAHOO.widget.Calendar.DATE* @static* @final* @type String*/YAHOO.widget.Calendar.DATE = "D";/*** Type constant used for renderers to represent an individual date across any year (M/D)* @property YAHOO.widget.Calendar.MONTH_DAY* @static* @final* @type String*/YAHOO.widget.Calendar.MONTH_DAY = "MD";/*** Type constant used for renderers to represent a weekday* @property YAHOO.widget.Calendar.WEEKDAY* @static* @final* @type String*/YAHOO.widget.Calendar.WEEKDAY = "WD";/*** Type constant used for renderers to represent a range of individual dates (M/D/Y-M/D/Y)* @property YAHOO.widget.Calendar.RANGE* @static* @final* @type String*/YAHOO.widget.Calendar.RANGE = "R";/*** Type constant used for renderers to represent a month across any year* @property YAHOO.widget.Calendar.MONTH* @static* @final* @type String*/YAHOO.widget.Calendar.MONTH = "M";/*** Constant that represents the total number of date cells that are displayed in a given month* @property YAHOO.widget.Calendar.DISPLAY_DAYS* @static* @final* @type Number*/YAHOO.widget.Calendar.DISPLAY_DAYS = 42;/*** Constant used for halting the execution of the remainder of the render stack* @property YAHOO.widget.Calendar.STOP_RENDER* @static* @final* @type String*/YAHOO.widget.Calendar.STOP_RENDER = "S";YAHOO.widget.Calendar.prototype = { /** * The configuration object used to set up the calendars various locale and style options. * @property Config * @private * @deprecated Configuration properties should be set by calling Calendar.cfg.setProperty. * @type Object */ Config : null, /** * The parent CalendarGroup, only to be set explicitly by the parent group * @property parent * @type CalendarGroup */ parent : null, /** * The index of this item in the parent group * @property index * @type Number */ index : -1, /** * The collection of calendar table cells * @property cells * @type HTMLTableCellElement[] */ cells : null, /** * The collection of calendar cell dates that is parallel to the cells collection. The array contains dates field arrays in the format of [YYYY, M, D]. * @property cellDates * @type Array[](Number[]) */ cellDates : null, /** * The id that uniquely identifies this calendar. This id should match the id of the placeholder element on the page. * @property id * @type String */ id : null, /** * The DOM element reference that points to this calendar's container element. The calendar will be inserted into this element when the shell is rendered. * @property oDomContainer * @type HTMLElement */ oDomContainer : null, /** * A Date object representing today's date. * @property today * @type Date */ today : null, /** * The list of render functions, along with required parameters, used to render cells. * @property renderStack * @type Array[] */ renderStack : null,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -