menu.js
来自「原名JSPackager」· JavaScript 代码 · 共 2,425 行 · 第 1/5 页
JS
2,425 行
this.cfg.configChangedEvent.subscribe(
this._onParentMenuModuleConfigChange,
oSubmenu,
true
);
this.renderEvent.subscribe(
this._onParentMenuModuleRender,
oSubmenu,
true
);
oSubmenu.beforeShowEvent.subscribe(
this._onSubmenuBeforeShow,
oSubmenu,
true
);
oSubmenu.showEvent.subscribe(this._onSubmenuShow, oSubmenu, true);
oSubmenu.hideEvent.subscribe(this._onSubmenuHide, oSubmenu, true);
}
};
/**
* Subscribes a MenuModule instance to the specified item's Custom Events.
* @private
* @param {YAHOO.widget.MenuModuleItem} p_oItem The item to listen for events on.
*/
YAHOO.widget.MenuModule.prototype._subscribeToItemEvents = function(p_oItem) {
var aArguments = [this, p_oItem];
p_oItem.focusEvent.subscribe(this._onItemFocus, aArguments);
p_oItem.blurEvent.subscribe(this._onItemBlur, aArguments);
p_oItem.cfg.configChangedEvent.subscribe(
this._onItemConfigChange,
aArguments
);
};
/**
* Returns the offset width of a MenuModule instance.
* @private
*/
YAHOO.widget.MenuModule.prototype._getOffsetWidth = function() {
var oClone = this.element.cloneNode(true);
this._oDom.setStyle(oClone, "width", "");
document.body.appendChild(oClone);
var sWidth = oClone.offsetWidth;
document.body.removeChild(oClone);
return sWidth;
};
/**
* Determines if a DOM event was fired on an item and (if so) fires the item's
* associated Custom Event
* @private
* @param {HTMLElement} p_oElement The original target of the event.
* @param {String} p_sEventType The type/name of the Custom Event to fire.
* @param {Event} p_oDOMEvent The DOM event to pass back when firing the
* Custom Event.
* @return An item.
* @type YAHOO.widget.MenuModuleItem
*/
YAHOO.widget.MenuModule.prototype._fireItemEvent =
function(p_oElement, p_sEventType, p_oDOMEvent) {
var me = this;
/**
* Returns the specified element's parent HTMLLIElement (<LI<)
* @param {p_oElement} An HTMLElement node
* @ignore
* @return Returns an HTMLElement node
* @type HTMLElement
*/
function getItemElement(p_oElement) {
if(p_oElement == me.element) {
return;
}
else if(p_oElement.tagName == "LI") {
return p_oElement;
}
else if(p_oElement.parentNode) {
return getItemElement(p_oElement.parentNode);
}
}
var oElement = getItemElement(p_oElement);
if(oElement) {
/*
Retrieve the item that corresponds to the
HTMLLIElement (<LI<) and fire the Custom Event
*/
var nGroupIndex = parseInt(oElement.getAttribute("groupindex"), 10);
var nIndex = parseInt(oElement.getAttribute("index"), 10);
var oItem = this._aItemGroups[nGroupIndex][nIndex];
if(!oItem.cfg.getProperty("disabled")) {
oItem[p_sEventType].fire(p_oDOMEvent);
return oItem;
}
}
};
// Private DOM event handlers
/**
* Generic event handler for the MenuModule's root HTMLDivElement node. Used
* to handle "mousedown," "mouseup," "keydown," "keyup," and "keypress" events.
* @private
* @param {Event} p_oEvent Event object passed back by the event
* utility (YAHOO.util.Event).
* @param {YAHOO.widget.MenuModule} p_oMenuModule The MenuModule instance
* corresponding to the HTMLDivElement that fired the event.
*/
YAHOO.widget.MenuModule.prototype._onDOMEvent =
function(p_oEvent, p_oMenuModule) {
var Event = this._oEventUtil;
// Map of DOM event types to Custom Event types
var oEventTypes = {
"mousedown": "mouseDownEvent",
"mouseup": "mouseUpEvent",
"keydown": "keyDownEvent",
"keyup": "keyUpEvent",
"keypress": "keyPressEvent"
};
var sCustomEventType = oEventTypes[p_oEvent.type];
var oTarget = Event.getTarget(p_oEvent);
/*
Check if the target was an element that is a part of a
an item and (if so), fire the associated custom event.
*/
this._fireItemEvent(oTarget, sCustomEventType, p_oEvent);
// Fire the associated custom event for the MenuModule
this[sCustomEventType].fire(p_oEvent);
/*
Stop the propagation of the event at each MenuModule instance
since menus can be embedded in eachother.
*/
Event.stopPropagation(p_oEvent);
};
/**
* "mouseover" event handler for the MenuModule's root HTMLDivElement node.
* @private
* @param {Event} p_oEvent Event object passed back by the event
* utility (YAHOO.util.Event).
* @param {YAHOO.widget.MenuModule} p_oMenuModule The MenuModule instance
* corresponding to the HTMLDivElement that fired the event.
*/
YAHOO.widget.MenuModule.prototype._onElementMouseOver =
function(p_oEvent, p_oMenuModule) {
var Event = this._oEventUtil;
var oTarget = Event.getTarget(p_oEvent);
if(
(
oTarget == this.element ||
this._oDom.isAncestor(this.element, oTarget)
) &&
!this._bFiredMouseOverEvent
) {
// Fire the "mouseover" Custom Event for the MenuModule instance
this.mouseOverEvent.fire(p_oEvent);
this._bFiredMouseOverEvent = true;
this._bFiredMouseOutEvent = false;
}
/*
Check if the target was an element that is a part of an item
and (if so), fire the "mouseover" Custom Event.
*/
if(!this._oCurrentItem) {
this._oCurrentItem =
this._fireItemEvent(oTarget, "mouseOverEvent", p_oEvent);
}
/*
Stop the propagation of the event at each MenuModule instance
since menus can be embedded in eachother.
*/
Event.stopPropagation(p_oEvent);
};
/**
* "mouseout" event handler for the MenuModule's root HTMLDivElement node.
* @private
* @param {Event} p_oEvent Event object passed back by the event
* utility (YAHOO.util.Event).
* @param {YAHOO.widget.MenuModule} p_oMenuModule The MenuModule instance
* corresponding to the HTMLDivElement that fired the event.
*/
YAHOO.widget.MenuModule.prototype._onElementMouseOut =
function(p_oEvent, p_oMenuModule) {
var Dom = this._oDom;
var Event = this._oEventUtil;
var oRelatedTarget = Event.getRelatedTarget(p_oEvent);
var bLIMouseOut = true;
var bMovingToSubmenu = false;
// Determine where the mouse is going
if(this._oCurrentItem && oRelatedTarget) {
if(
oRelatedTarget == this._oCurrentItem.element ||
Dom.isAncestor(this._oCurrentItem.element, oRelatedTarget)
) {
bLIMouseOut = false;
}
var oSubmenu = this._oCurrentItem.cfg.getProperty("submenu");
if(
oSubmenu &&
(
oRelatedTarget == oSubmenu.element ||
Dom.isAncestor(oSubmenu.element, oRelatedTarget)
)
) {
bMovingToSubmenu = true;
}
}
if(this._oCurrentItem && (bLIMouseOut || bMovingToSubmenu)) {
// Fire the "mouseout" Custom Event for the item
this._oCurrentItem.mouseOutEvent.fire(p_oEvent);
this._oCurrentItem = null;
}
if(
!this._bFiredMouseOutEvent &&
(
!Dom.isAncestor(this.element, oRelatedTarget) ||
bMovingToSubmenu
)
) {
// Fire the "mouseout" Custom Event for the MenuModule instance
this.mouseOutEvent.fire(p_oEvent);
this._bFiredMouseOutEvent = true;
this._bFiredMouseOverEvent = false;
}
/*
Stop the propagation of the event at each MenuModule instance
since menus can be embedded in eachother.
*/
Event.stopPropagation(p_oEvent);
};
/**
* "click" event handler for the MenuModule's root HTMLDivElement node.
* @private
* @param {Event} p_oEvent Event object passed back by the
* event utility (YAHOO.util.Event).
* @param {YAHOO.widget.MenuModule} p_oMenuModule The MenuModule instance
* corresponding to the HTMLDivElement that fired the event.
*/
YAHOO.widget.MenuModule.prototype._onElementClick =
function(p_oEvent, p_oMenuModule) {
var Event = this._oEventUtil;
var oTarget = Event.getTarget(p_oEvent);
/*
Check if the target was a DOM element that is a part of an
item and (if so), fire the associated "click"
Custom Event.
*/
var oItem = this._fireItemEvent(oTarget, "clickEvent", p_oEvent);
var bCurrentPageURL; // Indicates if the URL points to the current page
if(oItem) {
var sURL = oItem.cfg.getProperty("url");
var oSubmenu = oItem.cfg.getProperty("submenu");
bCurrentPageURL = (sURL.substr((sURL.length-1),1) == "#");
/*
ACCESSIBILITY FEATURE FOR SCREEN READERS: Expand/collapse the
submenu when the user clicks on the submenu indicator image.
*/
if(oTarget == oItem.submenuIndicator && oSubmenu) {
if(oSubmenu.cfg.getProperty("visible")) {
oSubmenu.hide();
}
else {
var oActiveItem = this.activeItem;
// Hide any other submenus that might be visible
if(oActiveItem && oActiveItem != this) {
this.clearActiveItem();
}
this.activeItem = oItem;
oItem.cfg.setProperty("selected", true);
oSubmenu.show();
}
}
else if(oTarget.tagName != "A" && !bCurrentPageURL) {
/*
Follow the URL of the item regardless of whether or
not the user clicked specifically on the
HTMLAnchorElement (<A<) node.
*/
document.location = sURL;
}
}
switch(oTarget.tagName) {
case "A":
if(bCurrentPageURL) {
// Don't follow URLs that are equal to "#"
Event.preventDefault(p_oEvent);
}
else {
/*
Break if the anchor's URL is something other than "#"
to prevent the call to "stopPropagation" from be
executed. This is required for Safari to be able to
follow the URL.
*/
break;
}
default:
/*
Stop the propagation of the event at each MenuModule
instance since Menus can be embedded in eachother.
*/
Event.stopPropagation(p_oEvent);
break;
}
// Fire the associated "click" Custom Event for the MenuModule instance
this.clickEvent.fire(p_oEvent);
};
// Private Custom Event handlers
/**
* "beforerender" Custom Event handler for a MenuModule instance. Appends all
* of the HTMLUListElement (<UL<s) nodes (and their child
* HTMLLIElement (<LI<)) nodes and their accompanying title nodes to
* the body of the MenuModule instance.
* @private
* @param {String} p_sType The name of the event that was fired.
* @param {Array} p_aArgs Collection of arguments sent when the event
* was fired.
* @param {YAHOO.widget.MenuModule} p_oMenuModule The MenuModule instance that
* fired the event.
*/
YAHOO.widget.MenuModule.prototype._onBeforeRender =
function(p_sType, p_aArgs, p_oMenuModule) {
var Dom = this._oDom;
var oConfig = this.cfg;
var oEl = this.element;
var nListElements = this._aListElements.length;
if(oConfig.getProperty("position") == "static") {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?