Lazy load on/off and dropdown button : DropDown Button « YUI Library « JavaScript DHTML






Lazy load on/off and dropdown button

 

<!--
Software License Agreement (BSD License)
Copyright (c) 2009, Yahoo! Inc.
All rights reserved.

Redistribution and use of this software in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this list of conditions and the
      following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
    * Neither the name of Yahoo! Inc. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission of Yahoo! Inc.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Sources of Intellectual Property Included in the YUI Library

YUI is issued by Yahoo! under the BSD license above. Below is a list of certain publicly available software that is the source of intellectual property in YUI, along with the licensing terms that pertain to thosesources of IP. This list is for informational purposes only and is not intended to represent an exhaustive list of third party contributions to the YUI.

    * Douglas Crockford's JSON parsing and stringifying methods: In the JSON Utility, Douglas Crockford's JSON parsing and stringifying methods are adapted from work published at JSON.org. The adapted work is in the public domain.
    * Robert Penner's animation-easing algorithms: In the Animation Utility, YUI makes use of Robert Penner's algorithms for easing.
    * Geoff Stearns's SWFObject: In the Charts Control and the Uploader versions through 2.7.0, YUI makes use of Geoff Stearns's SWFObject v1.5 for Flash Player detection and embedding. More information on SWFObject can be found here (http://blog.deconcept.com/swfobject/). SWFObject is (c) 2007 Geoff Stearns and is released under the MIT License (http://www.opensource.org/licenses/mit-license.php).
    * Diego Perini's IEContentLoaded technique: The Event Utility employs a technique developed by Diego Perini and licensed under GPL. YUI's use of this technique is included under our BSD license with the author's permission.

-->

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>


    <meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Using A Menu Button To Replace A &#60;select&#62; Element</title>

<style type="text/css">
/*margin and padding on body element
  can introduce errors in determining
  element position and are not recommended;
  we turn them off as a foundation for YUI
  CSS treatments. */
body {
  margin:0;
  padding:0;
}
</style>

<link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/fonts/fonts-min.css" />
<link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/menu/assets/skins/sam/menu.css" />
<link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/button/assets/skins/sam/button.css" />
<script type="text/javascript" src="yui_2.7.0b-lib/yahoo-dom-event/yahoo-dom-event.js"></script>
<script type="text/javascript" src="yui_2.7.0b-lib/container/container_core-min.js"></script>
<script type="text/javascript" src="yui_2.7.0b-lib/menu/menu-min.js"></script>
<script type="text/javascript" src="yui_2.7.0b-lib/element/element-min.js"></script>
<script type="text/javascript" src="yui_2.7.0b-lib/button/button-min.js"></script>


<!--begin custom header content for this example-->
<style type="text/css">

  /*  Style the <fieldset> since the Reset CSS removes the default style. */
    #button-example-form fieldset {

        border: 2px groove #ccc;
        margin: .5em;
        padding: .5em;

    }

  pre {
    border: solid 1px #000;
    background-color: #ccc;
    padding: 10px;
    margin: 10px;
  }
  
  .yui-menu-button em.yui-button-label {

    font-style: normal;
    display: block;
    text-align: left;
    white-space: nowrap;

    /*  Restrict the width of the label to 5em. */
    width: 5em;

    /* Hide the overflow if the text label exceeds 5em in width. */
    overflow: hidden;

    /* 
      IE, Safari and Opera support the ability to add ellipsis when the text 
      label exceeds 10em in width.
    */
    text-overflow: ellipsis;
    -o-text-overflow: ellipsis;

  }

  li.yui-button-selectedmenuitem {
    background: url(yui_2.7.0b-assets/button-assets/checkbox.png) left center no-repeat;
  }

</style>
<!--end custom header content for this example-->

</head>

<body class=" yui-skin-sam">


<h1>Using A Menu Button To Replace A &#60;select&#62; Element</h1>

<div class="exampleIntro">
  <p>
This example illustrates how to use Button to mimic the behavior of an 
HTML <code>&#60;select&#62;</code> element.
</p>      
</div>

<!--BEGIN SOURCE CODE FOR EXAMPLE  -->

<script type="text/javascript">

  YAHOO.util.Event.onDOMReady(function () {
  
    var Button = YAHOO.widget.Button;
  

    //  "render" event handler for a Button's Menu - responsible for setting
    //   the default value for the Button's "selectedMenuItem" attribute.

    var onMenuRender = function (type, args, button) {

      button.set("selectedMenuItem", this.getItem(0));

    };


    //  "selectedMenuItemChange" event handler for a Button that will set 
    //  the Button's "label" attribute to the value of the "text" 
    //  configuration property of the MenuItem that was clicked.

    var onSelectedMenuItemChange = function (event) {

      var oMenuItem = event.newValue;

      this.set("label", ("<em class=\"yui-button-label\">" + 
            oMenuItem.cfg.getProperty("text") + "</em>"));

    };


    //  "submit" event handler for a Button's parent form - repsonsible for 
    //  rendering a Menu that was to be lazy loaded, but never clicked on, 
    //  and therefore never rendered.

    var onFormSubmit = function (event, button) {

      var oMenuItem = button.get("selectedMenuItem"),
        UA = YAHOO.env.ua,
        oEvent,
        oMenu;
      
      if (!oMenuItem) {

        //  Pause submission of the form until the Button's Menu 
        //  is rendered
        YAHOO.util.Event.preventDefault(event);

        oMenu = button.getMenu();

        oMenu.addItems(oMenu.itemData);

        oMenu.subscribe("render", function () {

          var bSubmitForm;

          if (UA.ie) {
            bSubmitForm = this.fireEvent("onsubmit");
          }
          else {  // Gecko, Opera, and Safari
      
            oEvent = document.createEvent("HTMLEvents");
            oEvent.initEvent("submit", true, true);
            bSubmitForm = this.dispatchEvent(oEvent);
      
          }

          //  In IE and Safari, dispatching a "submit" event to a form 
          //  WILL cause the form's "submit" event to fire, but WILL  
          //  NOT submit the form.  Therefore, we need to call the 
          //  "submit" method as well.
          
          if ((UA.ie || UA.webkit) && bSubmitForm) {
            this.submit();
          }

        }, this, true);

        oMenu.render(oMenu.cfg.getProperty("container"));

      }
    
    };


    var oMenuButton1 = new Button({ 
              id: "menubutton-1", 
              name: "menubutton-1",
              label: "<em class=\"yui-button-label\">Option 1</em>",
              type: "menu",  
              menu: "select-1", 
              container: "select-1-container"
    });

    //  Register a "selectedMenuItemChange" event handler that will sync the 
    //  Button's "label" attribute to the MenuItem that was clicked.

    oMenuButton1.on("selectedMenuItemChange", onSelectedMenuItemChange);
    


    var oMenuButton2 = new Button({ 
              id: "menubutton-2", 
              name: "menubutton-2",
              label: "<em class=\"yui-button-label\">Option 1</em>",
              type: "menu",
              lazyloadmenu: false,
              menu: "select-2", 
              container: "select-2-container"
    });      

    //  Register a "selectedMenuItemChange" event handler that will sync the 
    //  Button's "label" attribute to the MenuItem that was clicked.

    oMenuButton2.on("selectedMenuItemChange", onSelectedMenuItemChange);


    var aMenuButton3MenuData = [
      { text: "Option 1", value: "menubutton-3-1" },
      { text: "Option 2", value: "menubutton-3-2" },
      { text: "Option 3", value: "menubutton-3-3" }
    ];

    var oMenuButton3 = new Button({ 
                id: "menubutton-3", 
                name: "menubutton-3",
                label: "<em class=\"yui-button-label\">Option 1</em>",
                type: "menu",  
                menu: aMenuButton3MenuData, 
                container: "menubutton-3-container" });

    //  Register a "selectedMenuItemChange" event handler that will sync the 
    //  Button's "label" attribute to the MenuItem that was clicked.

    oMenuButton3.on("selectedMenuItemChange", onSelectedMenuItemChange);

    oMenuButton3.on("appendTo", function () {

      var oMenu = this.getMenu();

      //  If a Button's "selectedMenuItem" attribute is set, the selected 
      //  MenuItem's name and value will be part of the form's data set 
      //  when its parent form is submitted.  For Buttons with Menus built
      //  entirely from script, the "selectedMenuItem" property is not 
      //  set by default.  To set the "selectedMenuItem" to a default 
      //  value, simply register a "render" event handler for the Button's
      //  Menu that sets the Button's "selectedMenuItem" attribute to the
      //  desired item in the Menu.

      oMenu.subscribe("render", onMenuRender, this);
      

      //  The items in a Button's Menu are lazy loaded by default: loaded 
      //  when the Button is initially clicked.  If the user never clicks 
      //  on the Button, its Menu will never be rendered, meaning the 
      //  "render" event handler registered above will never be called, 
      //  and the default value for the Button's "selectMenuItem"
      //  attribute will never be set.  Therefore, add a "submit" event 
      //  handler to the Button's parent form that will render the Menu 
      //  if the Button's "selectedMenuItem" attribute is not set.

      YAHOO.util.Event.on(this.getForm(), "submit", onFormSubmit, this);

    });



    var aMenuButton4MenuData = [
      { text: "Option 1", value: "menubutton-4-1" },
      { text: "Option 2", value: "menubutton-4-2" },
      { text: "Option 3", value: "menubutton-4-3" }
    ];

    var oMenuButton4 = new Button({ 
                id: "menubutton-4", 
                name: "menubutton-4",
                label: "<em class=\"yui-button-label\">Option 1</em>",
                type: "menu",  
                lazyloadmenu: false,
                menu: aMenuButton4MenuData, 
                container: "menubutton-4-container" });

    var oMenuButton4Menu = oMenuButton4.getMenu();


    //  If a Button's "selectedMenuItem" attribute is set, the selected 
    //  MenuItem's name and value will be part of the form's data set 
    //  when its parent form is submitted.  For Buttons with Menus built 
    //  entirely from script, the "selectedMenuItem" property is not set by 
    //  default.  To set the "selectedMenuItem" to a default value, simply 
    //  register a "render" event handler for the Button's Menu that sets 
    //  the Button's "selectedMenuItem" attribute to the desired item in 
    //  the Menu.

    oMenuButton4Menu.subscribe("render", onMenuRender, oMenuButton4);

    //  Register a "selectedMenuItemChange" event handler that will sync the 
    //  Button's "label" attribute to the MenuItem that was clicked.

    oMenuButton4.on("selectedMenuItemChange", onSelectedMenuItemChange);



    var oMenuButton5 = new Button({ 
              id: "menubutton-5", 
              name: "menubutton-5",
              label: "<em class=\"yui-button-label\">None</em>",
              type: "menu",  
              menu: "select-3", 
              container: "select-3-container" });

    //  Register a "selectedMenuItemChange" event handler that will sync the 
    //  Button's "label" attribute to the MenuItem that was clicked.

    oMenuButton5.on("selectedMenuItemChange", onSelectedMenuItemChange);
              

    var oMenuButton6 = new Button({ 
              id: "menubutton-6", 
              name: "menubutton-6",
              label: "<em class=\"yui-button-label\">None</em>",
              type: "menu",
              lazyloadmenu: false,
              menu: "select-4", 
              container: "select-4-container" });

    //  Register a "selectedMenuItemChange" event handler that will sync the 
    //  Button's "label" attribute to the MenuItem that was clicked.

    oMenuButton6.on("selectedMenuItemChange", onSelectedMenuItemChange);

    var aMenuButton7MenuData = [
      "None",      
      { text: "Option 1", value: "menubutton-7-1" },
      { text: "Option 2", value: "menubutton-7-2" },
      { text: "Option 3", value: "menubutton-7-3" }
    ];

    var oMenuButton7 = new Button({ 
                id: "menubutton-7", 
                name: "menubutton-7",
                label: "<em class=\"yui-button-label\">None</em>",
                type: "menu",  
                menu: aMenuButton7MenuData, 
                container: "menubutton-7-container" });

    //  Register a "selectedMenuItemChange" event handler that will sync the 
    //  Button's "label" attribute to the MenuItem that was clicked.

    oMenuButton7.on("selectedMenuItemChange", onSelectedMenuItemChange);


    var aMenuButton8MenuData = [
      "None",
      { text: "Option 1", value: "menubutton-8-1" },
      { text: "Option 2", value: "menubutton-8-2" },
      { text: "Option 3", value: "menubutton-8-3" }
    ];

    var oMenuButton8 = new Button({ 
                id: "menubutton-8", 
                name: "menubutton-8",
                label: "<em class=\"yui-button-label\">None</em>",
                type: "menu", 
                lazyloadmenu: false,
                menu: aMenuButton8MenuData, 
                container: "menubutton-8-container" });


    //  Register a "selectedMenuItemChange" event handler that will sync the 
    //  Button's "label" attribute to the MenuItem that was clicked.

    oMenuButton8.on("selectedMenuItemChange", onSelectedMenuItemChange);

  });

</script>



<form name="button-example-form" id="button-example-form" method="post" action="#">
    
  <fieldset>
    <legend>With A Default Value</legend>

    <fieldset>
      <legend>Using Existing Markup</legend>

      <fieldset>
        <legend>Lazy load on</legend>

        <label id="select-1-container">
          Choose a value: 
          <select id="select-1" name="select-1">
            <option value="select-1-1" selected>Option 1</option>
            <option value="select-1-2">Option 2</option>
            <option value="select-1-3">Option 3</option>      
          </select>
        </label>

      </fieldset>

      <fieldset>
        <legend>Lazy load off</legend>
        <label id="select-2-container">
          Choose a value: 
          <select id="select-2" name="select-2">
            <option value="select-2-1" selected>Option 1</option>
            <option value="select-2-2">Option 2</option>
            <option value="select-2-3">Option 3</option>      
          </select>
        </label>
      </fieldset>

    </fieldset>

    <fieldset>
      <legend>Without Existing Markup</legend>

      <fieldset>
        <legend>Lazy load on</legend>
        <label id="menubutton-3-container">Choose a value: </label>
      </fieldset>

      <fieldset>        
        <legend>Lazy load off</legend>
        <label id="menubutton-4-container">Choose a value: </label>
      </fieldset>

    </fieldset>

  </fieldset>

  <fieldset>
    <legend>Without A Default Value</legend>

    <fieldset>
      <legend>Using Existing Markup</legend>

      <fieldset>
        <legend>Lazy load on</legend>
        <label id="select-3-container">
          Choose a value:
          <select id="select-3" name="select-3">
            <option value="">None</option>
            <option value="select-3-1">Option 1</option>
            <option value="select-3-2">Option 2</option>
            <option value="select-3-3">Option 3</option>      
          </select>
        </label>
      </fieldset>

      <fieldset>
        <legend>Lazy load off</legend>
        <label id="select-4-container">
          Choose a value
          <select id="select-4" name="select-4">
            <option value="">None</option>
            <option value="select-4-1">Option 1</option>
            <option value="select-4-2">Option 2</option>
            <option value="select-4-3">Option 3</option>      
          </select>
        </label>
      </fieldset>

    </fieldset>


    <fieldset>
      <legend>Without Existing Markup</legend>
      
      <fieldset>
        <legend>Lazy load on</legend>
        <label id="menubutton-7-container">Choose a value: </label>
      </fieldset>

      <fieldset>        
        <legend>Lazy load off</legend>
        <label id="menubutton-8-container">Choose a value: </label>
      </fieldset>        
      
    </fieldset>

  </fieldset>

  <div>
    <input type="submit" id="submit-button" name="submit-button" value="Submit Form">
  </div>
  
</form>

<!--END SOURCE CODE FOR EXAMPLE  -->

</body>
</html>
<!-- presentbright.corp.yahoo.com uncompressed/chunked Thu Feb 19 10:53:07 PST 2009 -->

   
  








yui_2.7.0b.zip( 4,431 k)

Related examples in the same category

1.Create and use a Menu Button from HTML markup
2.Create and use a Menu Button from Javascript
3.Create and use a Split Button from HTML markup
4.Create and use a Split Button from Javascript
5.Dropdown Button based color picker
6.Fixed Width Menu Button
7.Dropdown button with slider
8.Combine a Split Button with a Slider to create an opacity slider button, similar to Adobe Photoshop
9.Create a Menu Button whose Menu instance displays a Calendar
10.Replace the content of a dropdown Button's Menu on the fly