Create a Menu Button whose Menu instance displays a Calendar : DropDown Button « YUI Library « JavaScript DHTML






Create a Menu Button whose Menu instance displays a Calendar

 




<!--
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>Simple Calendar Menu Button</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/calendar/assets/skins/sam/calendar.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/calendar/calendar-min.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/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">

    /*
        Set the "zoom" property to "normal" since it is set to "1" by the 
        ".example-container .bd" rule in yui.css and this causes a Menu
        instance's width to expand to 100% of the browser viewport.
    */
    
    div.yuimenu .bd {
    
        zoom: normal;
    
    }

    /*
        Restore default padding of 10px for the calendar containtainer 
        that is overridden by the ".example-container .bd .bd" rule 
        in yui.css.
    */

    #calendarcontainer {

        padding:10px;

    }

    #calendarmenu {
    
        position: absolute;
    
    }

    #calendarpicker button {

        background: url(yui_2.7.0b-assets/button-assets/calendar_icon.gif) center center no-repeat;
        text-align: left;
        text-indent: -10em;
        overflow: hidden;
        *margin-left: 10em; /* For IE */
        *padding: 0 3em;    /* For IE */
        white-space: nowrap;

    }

    #month-field,
    #day-field {
    
        width: 2em;
    
    }
    
    #year-field {
    
        width: 3em;
    
    }

  #datefields {
  
    border: solid 1px #666;
    padding: .5em;
  
  }
  
  #calendarpicker  {
  
    vertical-align: baseline;
    
  }

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

</head>

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


<h1>Simple Calendar Menu Button</h1>

<div class="exampleIntro">
  <p>
This example demonstrates how to create a Menu Button whose Menu instance displays a Calendar.
</p>      
</div>

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

<script type="text/javascript">

  (function () {
  
    var Event = YAHOO.util.Event,
      Dom = YAHOO.util.Dom;


    Event.onDOMReady(function () {
  
      var oCalendarMenu;
  
      var onButtonClick = function () {
        
        // Create a Calendar instance and render it into the body 
        // element of the Overlay.
  
        var oCalendar = new YAHOO.widget.Calendar("buttoncalendar", oCalendarMenu.body.id);
  
        oCalendar.render();
  
  
        // Subscribe to the Calendar instance's "select" event to 
        // update the month, day, year form fields when the user
        // selects a date.
  
        oCalendar.selectEvent.subscribe(function (p_sType, p_aArgs) {
  
          var aDate;
  
          if (p_aArgs) {
              
            aDate = p_aArgs[0][0];
              
            Dom.get("month-field").value = aDate[1];
            Dom.get("day-field").value = aDate[2];
            Dom.get("year-field").value = aDate[0];
  
          }
          
          oCalendarMenu.hide();
        
        });
  
  
        // Pressing the Esc key will hide the Calendar Menu and send focus back to 
        // its parent Button
  
        Event.on(oCalendarMenu.element, "keydown", function (p_oEvent) {
        
          if (Event.getCharCode(p_oEvent) === 27) {
            oCalendarMenu.hide();
            this.focus();
          }
        
        }, null, this);
        
        
        var focusDay = function () {

          var oCalendarTBody = Dom.get("buttoncalendar").tBodies[0],
            aElements = oCalendarTBody.getElementsByTagName("a"),
            oAnchor;

          
          if (aElements.length > 0) {
          
            Dom.batch(aElements, function (element) {
            
              if (Dom.hasClass(element.parentNode, "today")) {
                oAnchor = element;
              }
            
            });
            
            
            if (!oAnchor) {
              oAnchor = aElements[0];
            }


            // Focus the anchor element using a timer since Calendar will try 
            // to set focus to its next button by default
            
            YAHOO.lang.later(0, oAnchor, function () {
              try {
                oAnchor.focus();
              }
              catch(e) {}
            });
          
          }
          
        };


        // Set focus to either the current day, or first day of the month in 
        // the Calendar  when it is made visible or the month changes
  
        oCalendarMenu.subscribe("show", focusDay);
        oCalendar.renderEvent.subscribe(focusDay, oCalendar, true);
  

        // Give the Calendar an initial focus
        
        focusDay.call(oCalendar);
  
  
        // Re-align the CalendarMenu to the Button to ensure that it is in the correct
        // position when it is initial made visible
        
        oCalendarMenu.align();
        
  
        // Unsubscribe from the "click" event so that this code is 
        // only executed once
  
        this.unsubscribe("click", onButtonClick);
      
      };
  
  
      // Create an Overlay instance to house the Calendar instance
  
      oCalendarMenu = new YAHOO.widget.Overlay("calendarmenu", { visible: false });
  
  
      // Create a Button instance of type "menu"
  
      var oButton = new YAHOO.widget.Button({ 
                        type: "menu", 
                        id: "calendarpicker", 
                        label: "Choose A Date", 
                        menu: oCalendarMenu, 
                        container: "datefields" });
  
  
      oButton.on("appendTo", function () {
  
        // Create an empty body element for the Overlay instance in order 
        // to reserve space to render the Calendar instance into.
    
        oCalendarMenu.setBody("&#32;");
    
        oCalendarMenu.body.id = "calendarcontainer";
      
      });
      
  
      // Add a "click" event listener that will render the Overlay, and 
      // instantiate the Calendar the first time the Button instance is 
      // clicked.
  
      oButton.on("click", onButtonClick);
    
    });  

  }());

</script>

<div>

    <form id="button-example-form" name="button-example-form" method="post">
    
        <fieldset id="datefields">
    
            <legend>Date</legend>
    
            <label for="month-field">Month: </label> <input id="month-field" type="text" name="month">
            <label for="day-field">Day:</label> <input id="day-field" type="text" name="day">
            <label for="year-field">Year: </label> <input id="year-field" type="text" name="year">
    
        </fieldset>
    
    </form>

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

</body>
</html>
<!-- presentbright.corp.yahoo.com uncompressed/chunked Thu Feb 19 10:53:06 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.Lazy load on/off and dropdown button
8.Dropdown button with slider
9.Combine a Split Button with a Slider to create an opacity slider button, similar to Adobe Photoshop
10.Replace the content of a dropdown Button's Menu on the fly