📄 calendar.js
字号:
* @property _renderStack * @private * @type Array */ _renderStack : null, /** * The private list of initially selected dates. * @property _selectedDates * @private * @type Array */ _selectedDates : null, /** * A map of DOM event handlers to attach to cells associated with specific CSS class names * @property domEventMap * @type Object */ domEventMap : null};/*** Initializes the Calendar widget.* @method init* @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.prototype.init = function(id, containerId, config) { this.initEvents(); this.today = new Date(); YAHOO.widget.DateMath.clearTime(this.today); this.id = id; this.oDomContainer = document.getElementById(containerId); /** * The Config object used to hold the configuration variables for the Calendar * @property cfg * @type YAHOO.util.Config */ this.cfg = new YAHOO.util.Config(this); /** * The local object which contains the Calendar's options * @property Options * @type Object */ this.Options = {}; /** * The local object which contains the Calendar's locale settings * @property Locale * @type Object */ this.Locale = {}; this.initStyles(); YAHOO.util.Dom.addClass(this.oDomContainer, this.Style.CSS_CONTAINER); YAHOO.util.Dom.addClass(this.oDomContainer, this.Style.CSS_SINGLE); this.cellDates = []; this.cells = []; this.renderStack = []; this._renderStack = []; this.setupConfig(); if (config) { this.cfg.applyConfig(config, true); } this.cfg.fireQueue();};/*** Default Config listener for the iframe property. If the iframe config property is set to true,* renders the built-in IFRAME shim if the container is relatively or absolutely positioned.** @method configIframe*/YAHOO.widget.Calendar.prototype.configIframe = function(type, args, obj) { var useIframe = args[0]; if (!this.parent) { if (YAHOO.util.Dom.inDocument(this.oDomContainer)) { if (useIframe) { var pos = YAHOO.util.Dom.getStyle(this.oDomContainer, "position"); if (pos == "absolute" || pos == "relative") { if (!YAHOO.util.Dom.inDocument(this.iframe)) { this.iframe = document.createElement("iframe"); this.iframe.src = "javascript:false;"; YAHOO.util.Dom.setStyle(this.iframe, "opacity", "0"); if (YAHOO.env.ua.ie && YAHOO.env.ua.ie <= 6) { YAHOO.util.Dom.addClass(this.iframe, "fixedsize"); } this.oDomContainer.insertBefore(this.iframe, this.oDomContainer.firstChild); } } } else { if (this.iframe) { if (this.iframe.parentNode) { this.iframe.parentNode.removeChild(this.iframe); } this.iframe = null; } } } }};/*** Default handler for the "title" property* @method configTitle*/YAHOO.widget.Calendar.prototype.configTitle = function(type, args, obj) { var title = args[0]; var close = this.cfg.getProperty(YAHOO.widget.Calendar._DEFAULT_CONFIG.CLOSE.key); var titleDiv; if (title && title !== "") { titleDiv = YAHOO.util.Dom.getElementsByClassName(YAHOO.widget.CalendarGroup.CSS_2UPTITLE, "div", this.oDomContainer)[0] || document.createElement("div"); titleDiv.className = YAHOO.widget.CalendarGroup.CSS_2UPTITLE; titleDiv.innerHTML = title; this.oDomContainer.insertBefore(titleDiv, this.oDomContainer.firstChild); YAHOO.util.Dom.addClass(this.oDomContainer, "withtitle"); } else { titleDiv = YAHOO.util.Dom.getElementsByClassName(YAHOO.widget.CalendarGroup.CSS_2UPTITLE, "div", this.oDomContainer)[0] || null; if (titleDiv) { YAHOO.util.Event.purgeElement(titleDiv); this.oDomContainer.removeChild(titleDiv); } if (! close) { YAHOO.util.Dom.removeClass(this.oDomContainer, "withtitle"); } }};/*** Default handler for the "close" property* @method configClose*/YAHOO.widget.Calendar.prototype.configClose = function(type, args, obj) { var close = args[0]; var title = this.cfg.getProperty(YAHOO.widget.Calendar._DEFAULT_CONFIG.TITLE.key); var DEPR_CLOSE_PATH = "us/my/bn/x_d.gif"; var linkClose; if (close === true) { linkClose = YAHOO.util.Dom.getElementsByClassName("link-close", "a", this.oDomContainer)[0] || document.createElement("a"); linkClose.href = "#"; linkClose.className = "link-close"; YAHOO.util.Event.addListener(linkClose, "click", function(e, cal) {cal.hide(); YAHOO.util.Event.preventDefault(e); }, this); if (YAHOO.widget.Calendar.IMG_ROOT !== null) { var imgClose = document.createElement("img"); imgClose.src = YAHOO.widget.Calendar.IMG_ROOT + DEPR_CLOSE_PATH; imgClose.className = YAHOO.widget.CalendarGroup.CSS_2UPCLOSE; linkClose.appendChild(imgClose); } else { linkClose.innerHTML = '<span class="' + YAHOO.widget.CalendarGroup.CSS_2UPCLOSE + ' ' + this.Style.CSS_CLOSE + '"></span>'; } this.oDomContainer.appendChild(linkClose); YAHOO.util.Dom.addClass(this.oDomContainer, "withtitle"); } else { linkClose = YAHOO.util.Dom.getElementsByClassName("link-close", "a", this.oDomContainer)[0] || null; if (linkClose) { YAHOO.util.Event.purgeElement(linkClose); this.oDomContainer.removeChild(linkClose); } if (! title || title === "") { YAHOO.util.Dom.removeClass(this.oDomContainer, "withtitle"); } }};/*** Initializes Calendar's built-in CustomEvents* @method initEvents*/YAHOO.widget.Calendar.prototype.initEvents = function() { var defEvents = YAHOO.widget.Calendar._EVENT_TYPES; /** * Fired before a selection is made * @event beforeSelectEvent */ this.beforeSelectEvent = new YAHOO.util.CustomEvent(defEvents.BEFORE_SELECT); /** * Fired when a selection is made * @event selectEvent * @param {Array} Array of Date field arrays in the format [YYYY, MM, DD]. */ this.selectEvent = new YAHOO.util.CustomEvent(defEvents.SELECT); /** * Fired before a selection is made * @event beforeDeselectEvent */ this.beforeDeselectEvent = new YAHOO.util.CustomEvent(defEvents.BEFORE_DESELECT); /** * Fired when a selection is made * @event deselectEvent * @param {Array} Array of Date field arrays in the format [YYYY, MM, DD]. */ this.deselectEvent = new YAHOO.util.CustomEvent(defEvents.DESELECT); /** * Fired when the Calendar page is changed * @event changePageEvent */ this.changePageEvent = new YAHOO.util.CustomEvent(defEvents.CHANGE_PAGE); /** * Fired before the Calendar is rendered * @event beforeRenderEvent */ this.beforeRenderEvent = new YAHOO.util.CustomEvent(defEvents.BEFORE_RENDER); /** * Fired when the Calendar is rendered * @event renderEvent */ this.renderEvent = new YAHOO.util.CustomEvent(defEvents.RENDER); /** * Fired when the Calendar is reset * @event resetEvent */ this.resetEvent = new YAHOO.util.CustomEvent(defEvents.RESET); /** * Fired when the Calendar is cleared * @event clearEvent */ this.clearEvent = new YAHOO.util.CustomEvent(defEvents.CLEAR); this.beforeSelectEvent.subscribe(this.onBeforeSelect, this, true); this.selectEvent.subscribe(this.onSelect, this, true); this.beforeDeselectEvent.subscribe(this.onBeforeDeselect, this, true); this.deselectEvent.subscribe(this.onDeselect, this, true); this.changePageEvent.subscribe(this.onChangePage, this, true); this.renderEvent.subscribe(this.onRender, this, true); this.resetEvent.subscribe(this.onReset, this, true); this.clearEvent.subscribe(this.onClear, this, true);};/*** The default event function that is attached to a date link within a calendar cell* when the calendar is rendered.* @method doSelectCell* @param {DOMEvent} e The event* @param {Calendar} cal A reference to the calendar passed by the Event utility*/YAHOO.widget.Calendar.prototype.doSelectCell = function(e, cal) { var cell,index,d,date; var target = YAHOO.util.Event.getTarget(e); var tagName = target.tagName.toLowerCase(); var defSelector = false; while (tagName != "td" && ! YAHOO.util.Dom.hasClass(target, cal.Style.CSS_CELL_SELECTABLE)) { if (!defSelector && tagName == "a" && YAHOO.util.Dom.hasClass(target, cal.Style.CSS_CELL_SELECTOR)) { defSelector = true; } target = target.parentNode; tagName = target.tagName.toLowerCase(); if (tagName == "html") { return; } } if (defSelector) { // Stop link href navigation for default renderer YAHOO.util.Event.preventDefault(e); } cell = target; if (YAHOO.util.Dom.hasClass(cell, cal.Style.CSS_CELL_SELECTABLE)) { index = cell.id.split("cell")[1]; d = cal.cellDates[index]; date = new Date(d[0],d[1]-1,d[2]); var link; if (cal.Options.MULTI_SELECT) { link = cell.getElementsByTagName("a")[0]; if (link) { link.blur(); } var cellDate = cal.cellDates[index]; var cellDateIndex = cal._indexOfSelectedFieldArray(cellDate); if (cellDateIndex > -1) { cal.deselectCell(index); } else { cal.selectCell(index); } } else { link = cell.getElementsByTagName("a")[0]; if (link) { link.blur(); } cal.selectCell(index); } }};/*** The event that is executed when the user hovers over a cell* @method doCellMouseOver* @param {DOMEvent} e The event* @param {Calendar} cal A reference to the calendar passed by the Event utility*/YAHOO.widget.Calendar.prototype.doCellMouseOver = function(e, cal) { var target; if (e) { target = YAHOO.util.Event.getTarget(e); } else { target = this; } while (target.tagName.toLowerCase() != "td") { target = target.parentNode; if (target.tagName.toLowerCase() == "html") { return; } } if (YAHOO.util.Dom.hasClass(target, cal.Style.CSS_CELL_SELECTABLE)) { YAHOO.util.Dom.addClass(target, cal.Style.CSS_CELL_HOVER); }};/*** The event that is executed when the user moves the mouse out of a cell* @method doCellMouseOut* @param {DOMEvent} e The event* @param {Calendar} cal A reference to the calendar passed by the Event utility*/YAHOO.widget.Calendar.prototype.doCellMouseOut = function(e, cal) { var target; if (e) { target = YAHOO.util.Event.getTarget(e); } else { target = this; } while (target.tagName.toLowerCase() != "td") { target = target.parentNode; if (target.tagName.toLowerCase() == "html") { return; } } if (YAHOO.util.Dom.hasClass(target, cal.Style.CSS_CELL_SELECTABLE)) { YAHOO.util.Dom.removeClass(target, cal.Style.CSS_CELL_HOVER); }};YAHOO.widget.Calendar.prototype.setupConfig = function() { var defCfg = YAHOO.widget.Calendar._DEFAULT_CONFIG; /** * The month/year representing the current visible Calendar date (mm/yyyy) * @config pagedate * @type String * @default today's date */ this.cfg.addProperty(defCfg.PAGEDATE.key, { value:new Date(), handler:this.configPageDate } ); /** * The date or range of dates representing the current Calendar selection * @config selected * @type String * @default [] */ this.cfg.addProperty(defCfg.SELECTED.key, { value:[], handler:this.configSelected } ); /** * The title to display above the Calendar's month header * @config title * @type String * @default "" */ this.cfg.addProperty(defCfg.TITLE.key, { value:defCfg.TITLE.value, handler:this.configTitle } ); /** * Whether or not a close button should be displayed for this Calendar * @config close * @type Boolean * @default false */ this.cfg.addProperty(defCfg.CLOSE.key, { value:defCfg.CLOSE.value, handler:this.configClose } ); /** * Whether or not an iframe shim should be placed under the Calendar to prevent select boxes from bleeding through in Internet Explorer 6 and below. * This property is enabled by default for IE6 and below. It is disabled by default for other browsers for performance reasons, but can be * enabled if required. * * @config iframe * @type Boolean * @default true for IE6 and below, false for all other browsers */ this.cfg.addProperty(defCfg.IFRAME.key, { value:defCfg.IFRAME.value, handler:this.configIframe, validator:this.cfg.checkBoolean } ); /** * The minimum selectable date in the current Calendar (mm/dd/yyyy) * @config mindate
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -