📄 menu.js
字号:
/*
Copyright (c) 2006, Yahoo! Inc. All rights reserved.
Code licensed under the BSD License:
http://developer.yahoo.net/yui/license.txt
Version 0.11.0
*/
/**
* @class The superclass of all menu containers.
* @constructor
* @extends YAHOO.widget.Overlay
* @base YAHOO.widget.Overlay
* @param {String or HTMLElement} p_oElement String id or HTMLElement
* (either HTMLSelectElement or HTMLDivElement) of the source HTMLElement node.
* @param {Object} p_oConfig Optional. The configuration object literal
* containing the configuration for a MenuModule instance. See
* configuration class documentation for more details.
*/
YAHOO.widget.MenuModule = function(p_oElement, p_oConfig) {
YAHOO.widget.MenuModule.superclass.constructor.call(
this,
p_oElement,
p_oConfig
);
};
YAHOO.extend(YAHOO.widget.MenuModule, YAHOO.widget.Overlay);
// Constants
/**
* Constant representing the CSS class(es) to be applied to the root
* HTMLDivElement of the MenuModule instance.
* @final
* @type String
*/
YAHOO.widget.MenuModule.prototype.CSS_CLASS_NAME = "yuimenu";
/**
* Constant representing the type of item to instantiate and add when parsing
* the child nodes (either HTMLLIElement, HTMLOptGroupElement or
* HTMLOptionElement) of a menu's DOM. The default
* is YAHOO.widget.MenuModuleItem.
* @final
* @type YAHOO.widget.MenuModuleItem
*/
YAHOO.widget.MenuModule.prototype.ITEM_TYPE = null;
/**
* Constant representing the tagname of the HTMLElement used to title
* a group of items.
* @final
* @type String
*/
YAHOO.widget.MenuModule.prototype.GROUP_TITLE_TAG_NAME = "H6";
// Private properties
/**
* Array of HTMLElements used to title groups of items.
* @private
* @type {Array}
*/
YAHOO.widget.MenuModule.prototype._aGroupTitleElements = null;
/**
* Multi-dimensional array of items.
* @private
* @type {Array}
*/
YAHOO.widget.MenuModule.prototype._aItemGroups = null;
/**
* An array of HTMLUListElements, each of which is the parent node of each
* items's HTMLLIElement node.
* @private
* @type {Array}
*/
YAHOO.widget.MenuModule.prototype._aListElements = null;
/**
* Reference to the Event utility singleton.
* @private
* @type {YAHOO.util.Event}
*/
YAHOO.widget.MenuModule.prototype._oEventUtil = YAHOO.util.Event;
/**
* Reference to the Dom utility singleton.
* @private
* @type {YAHOO.util.Dom}
*/
YAHOO.widget.MenuModule.prototype._oDom = YAHOO.util.Dom;
/**
* Reference to the item the mouse is currently over.
* @private
* @type {YAHOO.widget.MenuModuleItem}
*/
YAHOO.widget.MenuModule.prototype._oCurrentItem = null;
/**
* The current state of a MenuModule instance's "mouseover" event
* @private
* @type {Boolean}
*/
YAHOO.widget.MenuModule.prototype._bFiredMouseOverEvent = false;
/**
* The current state of a MenuModule instance's "mouseout" event
* @private
* @type {Boolean}
*/
YAHOO.widget.MenuModule.prototype._bFiredMouseOutEvent = false;
// Public properties
/**
* Reference to the item that has focus.
* @private
* @type {YAHOO.widget.MenuModuleItem}
*/
YAHOO.widget.MenuModule.prototype.activeItem = null;
/**
* Returns a MenuModule instance's parent object.
* @type {YAHOO.widget.MenuModuleItem}
*/
YAHOO.widget.MenuModule.prototype.parent = null;
/**
* Returns the HTMLElement (either HTMLSelectElement or HTMLDivElement)
* used create the MenuModule instance.
* @type {HTMLSelectElement/HTMLDivElement}
*/
YAHOO.widget.MenuModule.prototype.srcElement = null;
// Events
/**
* Fires when the mouse has entered a MenuModule instance. Passes back the
* DOM Event object as an argument.
* @type {YAHOO.util.CustomEvent}
* @see YAHOO.util.CustomEvent
*/
YAHOO.widget.MenuModule.prototype.mouseOverEvent = null;
/**
* Fires when the mouse has left a MenuModule instance. Passes back the DOM
* Event object as an argument.
* @type {YAHOO.util.CustomEvent}
* @see YAHOO.util.CustomEvent
*/
YAHOO.widget.MenuModule.prototype.mouseOutEvent = null;
/**
* Fires when the user mouses down on a MenuModule instance. Passes back the
* DOM Event object as an argument.
* @type {YAHOO.util.CustomEvent}
* @see YAHOO.util.CustomEvent
*/
YAHOO.widget.MenuModule.prototype.mouseDownEvent = null;
/**
* Fires when the user releases a mouse button while the mouse is over
* a MenuModule instance. Passes back the DOM Event object as an argument.
* @type {YAHOO.util.CustomEvent}
* @see YAHOO.util.CustomEvent
*/
YAHOO.widget.MenuModule.prototype.mouseUpEvent = null;
/**
* Fires when the user clicks the on a MenuModule instance. Passes back the
* DOM Event object as an argument.
* @type {YAHOO.util.CustomEvent}
* @see YAHOO.util.CustomEvent
*/
YAHOO.widget.MenuModule.prototype.clickEvent = null;
/**
* Fires when the user presses an alphanumeric key. Passes back the
* DOM Event object as an argument.
* @type {YAHOO.util.CustomEvent}
* @see YAHOO.util.CustomEvent
*/
YAHOO.widget.MenuModule.prototype.keyPressEvent = null;
/**
* Fires when the user presses a key. Passes back the DOM Event
* object as an argument.
* @type {YAHOO.util.CustomEvent}
* @see YAHOO.util.CustomEvent
*/
YAHOO.widget.MenuModule.prototype.keyDownEvent = null;
/**
* Fires when the user releases a key. Passes back the DOM Event
* object as an argument.
* @type {YAHOO.util.CustomEvent}
* @see YAHOO.util.CustomEvent
*/
YAHOO.widget.MenuModule.prototype.keyUpEvent = null;
/**
* The MenuModule class's initialization method. This method is automatically
* called by the constructor, and sets up all DOM references for
* pre-existing markup, and creates required markup if it is not already present.
* @param {String or HTMLElement} p_oElement String id or HTMLElement
* (either HTMLSelectElement or HTMLDivElement) of the source HTMLElement node.
* @param {Object} p_oConfig Optional. The configuration object literal
* containing the configuration for a MenuModule instance. See
* configuration class documentation for more details.
*/
YAHOO.widget.MenuModule.prototype.init = function(p_oElement, p_oConfig) {
var Dom = this._oDom;
var Event = this._oEventUtil;
if(!this.ITEM_TYPE) {
this.ITEM_TYPE = YAHOO.widget.MenuModuleItem;
}
this._aItemGroups = [];
this._aListElements = [];
this._aGroupTitleElements = [];
var oElement;
if(typeof p_oElement == "string") {
oElement = document.getElementById(p_oElement);
}
else if(p_oElement.tagName) {
oElement = p_oElement;
}
if(oElement) {
switch(oElement.tagName) {
case "DIV":
this.srcElement = oElement;
/*
Note: we don't pass the user config in here yet
because we only want it executed once, at the lowest
subclass level.
*/
YAHOO.widget.MenuModule.superclass.init.call(this, oElement);
this.beforeInitEvent.fire(YAHOO.widget.MenuModule);
/*
Populate the collection of item groups and item
group titles
*/
var oNode = this.body.firstChild;
var i = 0;
do {
switch(oNode.tagName) {
case this.GROUP_TITLE_TAG_NAME:
this._aGroupTitleElements[i] = oNode;
break;
case "UL":
this._aListElements[i] = oNode;
this._aItemGroups[i] = [];
i++;
break;
}
}
while((oNode = oNode.nextSibling));
/*
Apply the "first-of-type" class to the first UL to mimic
the "first-of-type" CSS3 psuedo class.
*/
if(this._aListElements[0]) {
Dom.addClass(this._aListElements[0], "first-of-type");
}
break;
case "SELECT":
this.srcElement = oElement;
/*
The source element is not something that we can use
outright, so we need to create a new Overlay
*/
var sId = Dom.generateId();
/*
Note: we don't pass the user config in here yet
because we only want it executed once, at the lowest
subclass level.
*/
YAHOO.widget.MenuModule.superclass.init.call(this, sId);
this.beforeInitEvent.fire(YAHOO.widget.MenuModule);
break;
}
}
else {
/*
Note: we don't pass the user config in here yet
because we only want it executed once, at the lowest
subclass level.
*/
YAHOO.widget.MenuModule.superclass.init.call(this, p_oElement);
this.beforeInitEvent.fire(YAHOO.widget.MenuModule);
}
if(this.element) {
var oEl = this.element;
var CustomEvent = YAHOO.util.CustomEvent;
Dom.addClass(oEl, this.CSS_CLASS_NAME);
// Assign DOM event handlers
Event.addListener(
oEl,
"mouseover",
this._onElementMouseOver,
this,
true
);
Event.addListener(oEl, "mouseout", this._onElementMouseOut, this, true);
Event.addListener(oEl, "mousedown", this._onDOMEvent, this, true);
Event.addListener(oEl, "mouseup", this._onDOMEvent, this, true);
Event.addListener(oEl, "click", this._onElementClick, this, true);
Event.addListener(oEl, "keydown", this._onDOMEvent, this, true);
Event.addListener(oEl, "keyup", this._onDOMEvent, this, true);
Event.addListener(oEl, "keypress", this._onDOMEvent, this, true);
// Create custom events
this.mouseOverEvent = new CustomEvent("mouseOverEvent", this);
this.mouseOutEvent = new CustomEvent("mouseOutEvent", this);
this.mouseDownEvent = new CustomEvent("mouseDownEvent", this);
this.mouseUpEvent = new CustomEvent("mouseUpEvent", this);
this.clickEvent = new CustomEvent("clickEvent", this);
this.keyPressEvent = new CustomEvent("keyPressEvent", this);
this.keyDownEvent = new CustomEvent("keyDownEvent", this);
this.keyUpEvent = new CustomEvent("keyUpEvent", this);
// Subscribe to Custom Events
this.beforeRenderEvent.subscribe(this._onBeforeRender, this, true);
this.renderEvent.subscribe(this._onRender, this, true);
this.showEvent.subscribe(this._onShow, this, true);
this.beforeHideEvent.subscribe(this._onBeforeHide, this, true);
if(p_oConfig) {
this.cfg.applyConfig(p_oConfig, true);
}
this.cfg.queueProperty("visible", false);
if(this.srcElement) {
this._initSubTree();
}
}
this.initEvent.fire(YAHOO.widget.MenuModule);
};
// Private methods
/**
* Iterates the source element's childNodes collection and uses the child
* nodes to instantiate MenuModule and MenuModuleItem instances.
* @private
*/
YAHOO.widget.MenuModule.prototype._initSubTree = function() {
var oNode;
switch(this.srcElement.tagName) {
case "DIV":
if(this._aListElements.length > 0) {
var i = this._aListElements.length - 1;
do {
oNode = this._aListElements[i].firstChild;
do {
switch(oNode.tagName) {
case "LI":
this.addItem(new this.ITEM_TYPE(oNode), i);
break;
}
}
while((oNode = oNode.nextSibling));
}
while(i--);
}
break;
case "SELECT":
oNode = this.srcElement.firstChild;
do {
switch(oNode.tagName) {
case "OPTGROUP":
case "OPTION":
this.addItem(new this.ITEM_TYPE(oNode));
break;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -