<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>David Janes&#039; Code Weblog &#187; yui</title>
	<atom:link href="http://code.davidjanes.com/blog/category/yui/feed/" rel="self" type="application/rss+xml" />
	<link>http://code.davidjanes.com/blog</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Sun, 11 Apr 2010 12:32:10 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Make INPUT fields select dates using the YUI Calendar Control</title>
		<link>http://code.davidjanes.com/blog/2009/12/25/make-input-fields-select-dates-using-the-yui-calendar-control/</link>
		<comments>http://code.davidjanes.com/blog/2009/12/25/make-input-fields-select-dates-using-the-yui-calendar-control/#comments</comments>
		<pubDate>Fri, 25 Dec 2009 20:57:44 +0000</pubDate>
		<dc:creator>David Janes</dc:creator>
				<category><![CDATA[code fragments]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[yui]]></category>

		<guid isPermaLink="false">http://code.davidjanes.com/blog/?p=641</guid>
		<description><![CDATA[Here&#8217;s a lengthy Javascript code segment that can be dropped into any of your existing forms that provide for a date selection range. It has the following functionality:

clicking a date field pops up a YUI Calendar control to change the date
the date field is made read only by this code
pressing ESC while a Calendar is [...]]]></description>
			<content:encoded><![CDATA[<p><img style="float: right; margin-left: 5px;" src="http://i.discoveranywheremobile.com/2009-12-25/YUICalendar.png" alt="" />Here&#8217;s a lengthy Javascript code segment that can be dropped into any of your existing forms that provide for a date selection range. It has the following functionality:</p>
<ul>
<li>clicking a date field pops up a <a href="http://developer.yahoo.com/yui/calendar/">YUI Calendar</a> control to change the date</li>
<li>the date field is made read only by this code</li>
<li>pressing <code>ESC</code> while a Calendar is displaying dismisses it</li>
<li>pressing <code>DEL</code> while a Calendar is displaying clears the input field</li>
<li>there is an Input field for the start date and an Input field for the end date</li>
<li>you cannot make the end date before the start date, and you cannot make the start date after the end date</li>
<li>that the dates are optional</li>
<li>dates are displayed in the standard <a href="http://en.wikipedia.org/wiki/ISO_8601">ISO</a> <code>YYYY-MM-DD</code> format</li>
</ul>
<p>This code sample demonstrates:</p>
<ul>
<li>the YUI Calendar control, including
<ul>
<li>setting the date in the control</li>
<li>getting the date from the control (after selection)</li>
<li>dynamically displaying and hiding the control</li>
<li>manipulating ISO dates into the format that YUI requires</li>
<li>setting min and max dates on the fly &#8211; specifically, this shows you how to manipulate YUI &#8220;config&#8221; parameters which the documentation implies are readonly (look for functions in source code named like <code>configMinDate</code>)</li>
<li>simple <a href="http://developer.yahoo.com/yui/animation/">YUI animations</a> (to show that a field value has been changed)</li>
<li>detecting keystrokes (DEL and ESC) using YUI</li>
<li>adding functionality using Javascript, after the DOM loads</li>
</ul>
<p>This assumes:</p>
<ul>
<li>you have an Input field for the start date with DOM <code>id="id_event_start"</code></li>
<li>you have an Input field for the end date with DOM <code>id="id_event_end"</code></li>
<li>that the Form (or a parent element of the Form, up to Body) has DOM <code>class="yui-skin-sam"</code></li>
</ul>
<p>Add the following Script inclusions to your HTML (note: not all of these are probably needed, and there&#8217;s more efficient ways to include YUI JS, especially for production deployments):</p>
<pre>&lt;script type="text/javascript"
 src="http://yui.yahooapis.com/2.8.0r4/build/json/json-min.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript"
 src="http://yui.yahooapis.com/2.8.0r4/build/event/event-min.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript"
 src="http://yui.yahooapis.com/2.8.0r4/build/connection/connection-min.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript"
 src="http://yui.yahooapis.com/2.8.0r4/build/dom/dom-min.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript"
 src="http://yui.yahooapis.com/2.8.0r4/build/element/element-min.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript"
 src="http://yui.yahooapis.com/2.8.0r4/build/button/button-min.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript"
 src="http://yui.yahooapis.com/2.8.0r4/build/dragdrop/dragdrop-min.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript"
 src="http://yui.yahooapis.com/2.8.0r4/build/container/container-min.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript"
 src="http://yui.yahooapis.com/2.8.0r4/build/yuiloader/yuiloader-min.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript"
 src="http://yui.yahooapis.com/2.8.0r4/build/container/container_core-min.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript"
 src="http://yui.yahooapis.com/2.8.0r4/build/calendar/calendar-min.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript"
 src="http://yui.yahooapis.com/2.8.0r4/build/animation/animation-min.js"&gt;&lt;/script&gt;
</pre>
<p>Add the following CSS inclusions to your HTML:</p>
<pre>&lt;link rel="stylesheet" type="text/css"
 href="http://yui.yahooapis.com/2.8.0r4/build/button/assets/skins/sam/button.css" /&gt;
&lt;link rel="stylesheet" type="text/css"
 href="http://yui.yahooapis.com/2.8.0r4/build/container/assets/skins/sam/container.css" /&gt;
&lt;link rel="stylesheet" type="text/css"
 href="http://yui.yahooapis.com/2.8.0r4/build/calendar/assets/skins/sam/calendar.css" /&gt;
</pre>
<p>Add the following CSS to your HTML:</p>
<pre>&lt;style type="text/css"&gt;

#id_calendar {
 position: absolute;
 z-index: 15;
}
.yui-skin-sam .yui-panel .bd, .yui-skin-sam .yui-panel .ft {
 background-color:#FFFFFF;
}
&lt;/style&gt;
</pre>
<p>Add the following HTML fragment:</p>
<pre>&lt;div id="id_calendar_wrapper"&gt;
 &lt;div id="id_calendar"&gt;&lt;/div&gt;
&lt;/div&gt;
</pre>
<p>And finally, here&#8217;s the JS that makes it all work</p>
<pre>&lt;script type="text/javascript"&gt;
calendar_js = {
 c : null,
 start_e : null,
 end_e : null,
 current_e : 0,
 supress : 0,

 create : function() {
 calendar_js.start_e = YAHOO.util.Dom.get("id_event_start");
 calendar_js.end_e = YAHOO.util.Dom.get("id_event_end");

 YAHOO.util.Event.addListener(calendar_js.start_e, "click", calendar_js.onclick, this);
 YAHOO.util.Event.addListener(calendar_js.end_e, "click", calendar_js.onclick, this);

 YAHOO.util.Dom.get(calendar_js.start_e).readOnly = true;
 YAHOO.util.Dom.get(calendar_js.end_e).readOnly = true;

 (new YAHOO.util.KeyListener(document, { keys:27 }, { fn:calendar_js.onesc })).enable();
 (new YAHOO.util.KeyListener(document, { keys:8 }, { fn:calendar_js.ondel })).enable();
 },

 onesc : function() {
 calendar_js.current_e = null;
 if (calendar_js.c) {
 calendar_js.c.hide();
 }

 },

 ondel : function() {
 if (calendar_js.current_e) {
 calendar_js.current_e.value = "";
 calendar_js.current_e = null;
 }
 if (calendar_js.c) {
 calendar_js.c.hide();
 }

 },

 toydate : function(d) {
 if (!d) {
 return null;
 }
 if (d.value) {
 d = d.value;
 }
 if (d &amp;&amp; d.length) {
 d = d.replace(/(\d\d\d\d)-(\d\d)-(\d\d)/, "$2/$3/$1");
 if (d.indexOf('/') == 2) {
 return d;
 }
 }
 return null;
 },

 onclick : function(evt, obj) {
 var e_e = YAHOO.util.Event.getTarget(evt);

 if (calendar_js.c == null) {
 calendar_js.c = new YAHOO.widget.Calendar(null, "id_calendar");
 calendar_js.c.render(); 

 calendar_js.c.selectEvent.subscribe(calendar_js.ondate, calendar_js.c, true);
 }

 var c_e = YAHOO.util.Dom.get("id_calendar");
 if (calendar_js.current_e == e_e) {
 calendar_js.current_e = null;
 calendar_js.c.hide();
 } else {
 YAHOO.util.Dom.setAttribute(c_e, "display", "block");
 calendar_js.current_e = e_e;

 var region = YAHOO.util.Region.getRegion(e_e);

 calendar_js.c.show();

 if (e_e.value.length) {
 var d = calendar_js.toydate(e_e);
 if (d) {
 calendar_js.supress = 1;
 calendar_js.c.select(d);
 }
 }

 if (e_e == calendar_js.start_e) {
 var d = calendar_js.toydate(calendar_js.end_e);
 if (d) {
 calendar_js.c.configMaxDate(null, [ d ], null);
 calendar_js.c.configMinDate(null, [ "01/01/1970" ], null);
 }
 } else {
 var d = calendar_js.toydate(calendar_js.start_e);
 if (d) {
 calendar_js.c.configMinDate(null, [ d ], null);
 calendar_js.c.configMaxDate(null, [ "01/01/2200" ], null);
 }
 }

 calendar_js.c.render();
 YAHOO.util.Dom.setXY(c_e, [ region.right + 2, region.top ]);
 }
 },

 ondate : function(type, args, obj) {
 if (calendar_js.supress) {
 calendar_js.supress = 0;
 return;
 }

 var date = this.toDate(args[0][0]);

 var year = "" + date.getFullYear();
 var month = "" + ( 1 + date.getMonth() );
 while (month.length &lt; 2) {
 month = "0" + month;
 }
 var day = "" + date.getDate();
 while (day.length &lt; 2) {
 day = "0" + day;
 }

 var animate = new YAHOO.util.ColorAnim(calendar_js.current_e,
 {backgroundColor: { from: '#ffff99', to: '#FFFFFF' } }
 );
 animate.duration = 0.75;
 animate.method = YAHOO.util.Easing.easeOut;
 animate.animate(); 

 calendar_js.current_e.value = year + "-" + month + "-" + day;
 calendar_js.c.hide();
 calendar_js.current_e = null;
 },

 end : 0
};
YAHOO.util.Event.onDOMReady(calendar_js.oninit);
&lt;/script&gt;
</pre>
<p>Notes:</p>
<ul>
<li>no apologies for verboseness &#8211; I write code so I can understand it in 24 months time</li>
<li>my original code is a lot more &#8220;tabby&#8221; &#8211; I flattened to a single space so they&#8217;d fit in the space constraints here</li>
<li>the code should be fairly self explanatory if you step through it &#8211; the hard stuff is probably YUI weirdness and you can probably just accept that &#8220;that&#8217;s just the way it is&#8221;</li>
</ul>
</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://code.davidjanes.com/blog/2009/12/25/make-input-fields-select-dates-using-the-yui-calendar-control/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
