📄 button-beta.js
字号:
/*Copyright (c) 2007, Yahoo! Inc. All rights reserved.Code licensed under the BSD License:http://developer.yahoo.net/yui/license.txtversion: 2.3.0*//*** @module button* @description <p>The Button Control enables the creation of rich, graphical * buttons that function like traditional HTML form buttons. <em>Unlike</em> * tradition HTML form buttons, buttons created with the Button Control can have * a label that is different from its value. With the inclusion of the optional * <a href="module_menu.html">Menu Control</a>, the Button Control can also be* used to create menu buttons and split buttons, controls that are not * available natively in HTML. The Button Control can also be thought of as a * way to create more visually engaging implementations of the browser's * default radio-button and check-box controls.</p>* <p>The Button Control supports the following types:</p>* <dl>* <dt>push</dt>* <dd>Basic push button that can execute a user-specified command when * pressed.</dd>* <dt>link</dt>* <dd>Navigates to a specified url when pressed.</dd>* <dt>submit</dt>* <dd>Submits the parent form when pressed.</dd>* <dt>reset</dt>* <dd>Resets the parent form when pressed.</dd>* <dt>checkbox</dt>* <dd>Maintains a "checked" state that can be toggled on and off.</dd>* <dt>radio</dt>* <dd>Maintains a "checked" state that can be toggled on and off. Use with * the ButtonGroup class to create a set of controls that are mutually * exclusive; checking one button in the set will uncheck all others in * the group.</dd>* <dt>menu</dt>* <dd>When pressed will show/hide a menu.</dd>* <dt>split</dt>* <dd>Can execute a user-specified command or display a menu when pressed.</dd>* </dl>* @title Button* @namespace YAHOO.widget* @requires yahoo, dom, element, event* @optional container, menu* @beta*/(function () { /** * The Button class creates a rich, graphical button. * @param {String} p_oElement String specifying the id attribute of the * <code><input></code>, <code><button></code>, * <code><a></code>, or <code><span></code> element to * be used to create the button. * @param {<a href="http://www.w3.org/TR/2000/WD-DOM-Level-1-20000929/level- * one-html.html#ID-6043025">HTMLInputElement</a>|<a href="http://www.w3.org * /TR/2000/WD-DOM-Level-1-20000929/level-one-html.html#ID-34812697"> * HTMLButtonElement</a>|<a href=" * http://www.w3.org/TR/2000/WD-DOM-Level-1-20000929/level-one-html.html# * ID-33759296">HTMLElement</a>} p_oElement Object reference for the * <code><input></code>, <code><button></code>, * <code><a></code>, or <code><span></code> element to be * used to create the button. * @param {Object} p_oElement Object literal specifying a set of * configuration attributes used to create the button. * @param {Object} p_oAttributes Optional. Object literal specifying a set * of configuration attributes used to create the button. * @namespace YAHOO.widget * @class Button * @constructor * @extends YAHOO.util.Element */ // Shorthard for utilities var Dom = YAHOO.util.Dom, Event = YAHOO.util.Event, Lang = YAHOO.lang, Overlay = YAHOO.widget.Overlay, Menu = YAHOO.widget.Menu, // Private member variables m_oButtons = {}, // Collection of all Button instances m_oOverlayManager = null, // YAHOO.widget.OverlayManager instance m_oSubmitTrigger = null, // The button that submitted the form m_oFocusedButton = null; // The button that has focus // Private methods /** * @method createInputElement * @description Creates an <code><input></code> element of the * specified type. * @private * @param {String} p_sType String specifying the type of * <code><input></code> element to create. * @param {String} p_sName String specifying the name of * <code><input></code> element to create. * @param {String} p_sValue String specifying the value of * <code><input></code> element to create. * @param {String} p_bChecked Boolean specifying if the * <code><input></code> element is to be checked. * @return {<a href="http://www.w3.org/TR/2000/WD-DOM-Level-1-20000929/level- * one-html.html#ID-6043025">HTMLInputElement</a>} */ function createInputElement(p_sType, p_sName, p_sValue, p_bChecked) { var oInput, sInput; if (Lang.isString(p_sType) && Lang.isString(p_sName)) { if (YAHOO.env.ua.ie) { /* For IE it is necessary to create the element with the "type," "name," "value," and "checked" properties set all at once. */ sInput = "<input type=\"" + p_sType + "\" name=\"" + p_sName + "\""; if (p_bChecked) { sInput += " checked"; } sInput += ">"; oInput = document.createElement(sInput); } else { oInput = document.createElement("input"); oInput.name = p_sName; oInput.type = p_sType; if (p_bChecked) { oInput.checked = true; } } oInput.value = p_sValue; return oInput; } } /** * @method setAttributesFromSrcElement * @description Gets the values for all the attributes of the source element * (either <code><input></code> or <code><a></code>) that * map to Button configuration attributes and sets them into a collection * that is passed to the Button constructor. * @private * @param {<a href="http://www.w3.org/TR/2000/WD-DOM-Level-1-20000929/level- * one-html.html#ID-6043025">HTMLInputElement</a>|<a href="http://www.w3.org/ * TR/2000/WD-DOM-Level-1-20000929/level-one-html.html#ID- * 48250443">HTMLAnchorElement</a>} p_oElement Object reference to the HTML * element (either <code><input></code> or <code><span> * </code>) used to create the button. * @param {Object} p_oAttributes Object reference for the collection of * configuration attributes used to create the button. */ function setAttributesFromSrcElement(p_oElement, p_oAttributes) { var sSrcElementNodeName = p_oElement.nodeName.toUpperCase(), me = this, oAttribute, oRootNode, sText; /** * @method setAttributeFromDOMAttribute * @description Gets the value of the specified DOM attribute and sets it * into the collection of configuration attributes used to configure * the button. * @private * @param {String} p_sAttribute String representing the name of the * attribute to retrieve from the DOM element. */ function setAttributeFromDOMAttribute(p_sAttribute) { if ( !(p_sAttribute in p_oAttributes) ) { /* Need to use "getAttributeNode" instead of "getAttribute" because using "getAttribute," IE will return the innerText of a <code><button></code> for the value attribute rather than the value of the "value" attribute. */ oAttribute = p_oElement.getAttributeNode(p_sAttribute); if (oAttribute && ("value" in oAttribute)) { p_oAttributes[p_sAttribute] = oAttribute.value; } } } /** * @method setFormElementProperties * @description Gets the value of the attributes from the form element * and sets them into the collection of configuration attributes used to * configure the button. * @private */ function setFormElementProperties() { setAttributeFromDOMAttribute("type"); if (p_oAttributes.type == "button") { p_oAttributes.type = "push"; } if ( !("disabled" in p_oAttributes) ) { p_oAttributes.disabled = p_oElement.disabled; } setAttributeFromDOMAttribute("name"); setAttributeFromDOMAttribute("value"); setAttributeFromDOMAttribute("title"); } switch (sSrcElementNodeName) { case "A": p_oAttributes.type = "link"; setAttributeFromDOMAttribute("href"); setAttributeFromDOMAttribute("target"); break; case "INPUT": setFormElementProperties(); if ( !("checked" in p_oAttributes) ) { p_oAttributes.checked = p_oElement.checked; } break; case "BUTTON": setFormElementProperties(); oRootNode = p_oElement.parentNode.parentNode; if (Dom.hasClass(oRootNode, this.CSS_CLASS_NAME + "-checked")) { p_oAttributes.checked = true; } if (Dom.hasClass(oRootNode, this.CSS_CLASS_NAME + "-disabled")) { p_oAttributes.disabled = true; } p_oElement.removeAttribute("value"); p_oElement.setAttribute("type", "button"); break; } p_oElement.removeAttribute("id"); p_oElement.removeAttribute("name"); if ( !("tabindex" in p_oAttributes) ) { p_oAttributes.tabindex = p_oElement.tabIndex; } if ( !("label" in p_oAttributes) ) { // Set the "label" property sText = sSrcElementNodeName == "INPUT" ? p_oElement.value : p_oElement.innerHTML; if (sText && sText.length > 0) { p_oAttributes.label = sText; } } } /** * @method initConfig * @description Initializes the set of configuration attributes that are * used to instantiate the button. * @private * @param {Object} Object representing the button's set of * configuration attributes. */ function initConfig(p_oConfig) { var oAttributes = p_oConfig.attributes, oSrcElement = oAttributes.srcelement, sSrcElementNodeName = oSrcElement.nodeName.toUpperCase(), me = this; if (sSrcElementNodeName == this.NODE_NAME) { p_oConfig.element = oSrcElement; p_oConfig.id = oSrcElement.id; Dom.getElementsBy(function (p_oElement) { switch (p_oElement.nodeName.toUpperCase()) { case "BUTTON": case "A": case "INPUT": setAttributesFromSrcElement.call(me, p_oElement, oAttributes); break; } }, "*", oSrcElement); } else { switch (sSrcElementNodeName) { case "BUTTON": case "A": case "INPUT": setAttributesFromSrcElement.call(this, oSrcElement, oAttributes); break; } } } // Constructor YAHOO.widget.Button = function (p_oElement, p_oAttributes) { var fnSuperClass = YAHOO.widget.Button.superclass.constructor, oConfig, oElement; if (arguments.length == 1 && !Lang.isString(p_oElement) && !p_oElement.nodeName) { if (!p_oElement.id) { p_oElement.id = Dom.generateId(); } fnSuperClass.call(this, (this.createButtonElement(p_oElement.type)), p_oElement); } else { oConfig = { element: null, attributes: (p_oAttributes || {}) }; if (Lang.isString(p_oElement)) { oElement = Dom.get(p_oElement); if (oElement) { if (!oConfig.attributes.id) { oConfig.attributes.id = p_oElement; } oConfig.attributes.srcelement = oElement; initConfig.call(this, oConfig); if (!oConfig.element) { oConfig.element = this.createButtonElement(oConfig.attributes.type); } fnSuperClass.call(this, oConfig.element, oConfig.attributes); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -