Wednesday, July 21, 2010

Prevent users from selecting weekends in jQuery UI’s DatePicker

The jQuery UI DatePicker widget has a built in function for selecting all days that aren’t weekends.  As we can see here starting on line 910 (as of this writing) of jquery.ui.datepicker.js.

/* Set as beforeShowDay function to prevent selection of weekends.
    @param date Date - the date to customize
    @return [boolean, string] - is this date selectable?, what is its CSS class?
*/
noWeekends: function(date) {
    var day = date.getDay();
    return [(day > 0 && day < 6), ''];
}

We can provide the noWeekends function into the beforeShowDay event which will calculate all the weekdays and provide an array of true/false values indicating whether a date is selectable.

<!DOCTYPE html>
<html>
<head>
   <meta charset=utf-8 />
   <title>Prevent users from selecting weekends in jQuery UI’s DatePicker</title>
   <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/themes/overcast/jquery-ui.css" rel="stylesheet" type="text/css" />
</head>
<body style="font-size: 67.5%">
    <input id="datepicker" />
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/jquery-ui.min.js"></script>
    <script type="text/javascript">
      $(function() {
        $("#datepicker").datepicker({
          beforeShowDay: $.datepicker.noWeekends
       });
      });
    </script>
</body>
</html>

rwfb3601

Demo Source

I don't see the noWeekends Utility function documented anywhere so I submitted a ticket and I'll see about getting that taken care of.

Update: I’ve taken care of the documentation and it now shows up on the datepicker page.

This is a test from Windows Live Writer

I am trying out Windows Live Writer.  Hoping that it makes it easier to write and publish blog posts.  I find the biggest deterrent to blogging is logging in and using the crappy online tools of my blog engine.  (I am running an older version of dasBlog.  I need to update to the latest version I hear it has better comment spam protection).

live-writer-screenshot.png

What’s cool about this so far is that I put in my blog credentials and it went and analyzed my blog and came back with all my styles and template look and feel.  As you can see in the screenshot above it put the background image shadow on the right side of the blog post.  This allows me to see exactly what I will see when I hit Publish.

So let’s do just that!  Hitting Publish.

EDIT: OK I hit publish and it posted to my blog and it looks great.  Now trying a edit. It works!! 

Monday, June 21, 2010

Using jQuery and Templating to Pull and Display Your Twitter Updates

A while back, I wrote a blog post on how to pull in your last three twitter posts into your web page via jQuery and JSON. It's still today a very popular post.

A lot has happened since that post and I wanted to re-address it and show you an alternative way of pulling in your Twitter feed via jQuery and the new Templating plugin developed by Microsoft. On the right is a new Twitter widget I put together using the new jQuery templating plugin.

Microsoft's Templating Plugin

During MIX10, John Resig and Microsoft announced that Microsoft would be contributing code back to the jQuery Project. One of their first contributions was the Templating plugin. Microsoft created templates for jQuery so that JavaScript developers could use jQuery to easily display a set of data.

The source code for the plugin can be found in the jQuery-tmpl GitHub repository. The plugin requires jQuery 1.4.2 or higher.

Getting Setup

Before we start you'll need the following:

Create a html page, and include jQuery, the template plugin and the supporting_methods.js. Then include the style.css doc so that your code from the demo we do looks nice.

The HTML

Since each tweet will be listed as a list item in an unordered list the only required HTML we need is an empty ul:

		<ul id="renderTweets"></ul>
	

In our demo though we want to make it a complete widget. We need some extra layout html to be able to provide a heading and a link to take you to more tweets. Here is the full html we'll use:

		<div id="Tweets">
			<h3>@RedWolves Tweets</h3>
		  	<ul id="renderTweets"></ul>
			<div id="more">
				<a href="http://twitter.com/redwolves">More &gt;</a>
			</div>
		</div>
	

Make sure you replace my twitter name, RedWolves, with yours.

Making the Ajax request

Now let's get the data from Twitter. We'll use a JSONP ajax call to get back the JSON data of the last three tweets from Twitter. To get the JSON data we will use the user timeline of the Twitter API.

		$("document").ready(function(){
			var url = "http://api.twitter.com/1/statuses/user_timeline.json?id=RedWolves&count=3&callback=?"
			$.ajax({
				dataType: 'jsonp',
				url: url,
				jsonpCallback: "renderTweets" 
			});
		});
	

At the end of the Twitter API url that we declare we attach the user id of the user timeline we want to see, in this case mine, RedWolves, so make sure to update with the username you wish to see. We then declare the url and call the .ajax() method and provide it with the dataType 'jsonp' so that we can pull the data via cross-domain. We set the url and then tell it which function we will call after the data is loaded. After the data is loaded we will call the function 'renderTweets', defined below, which will run the data through the template.

Defining the Template

Now let's define the template that will display the data we get back from the Ajax request. We define the template in a script tag and we provide it with a type of "text/template" and a unique ID.

		<script type="text/template" id="twitterTemplate">
			<li>
					<img src="${ user['profile_image_url'] }" /> 
					${ text.linkify().atify() } 
					<span class="created_at">
						${ relative_time(created_at) } via ${ source }
					</span>
			</li>
		</script>
	

On each JSON item we will run it through the template and create an li element with the profile photo, tweet text, when it was created and what source the tweet came from (e.g. Twitter for iPhone, web, etc.).

We are using a couple of supporting methods to help us in formatting. .linkify which will make any link a hyper link, atify which will link up any @screenname to their twitter profile and relative_time which will turn the created time into Twitter style time. You can find these methods in the supporting_methods.js file.

Putting it all together

The last bit of jQuery code we need to do is to actually call the plugin and combine the template with the data and put it into the DOM. Here we will define the renderTweets callback function.

		function renderTweets(data) {
			$("#twitterTemplate")
				.render(data)
				.appendTo("#renderTweets");
		}
	

What we do is select our template, call the template plugin method, .render, which takes in the data from the Ajax call and renders the results against the template, and then append the results to the desired element in the HTML, the ul with id of renderTweets.

The whole HTML

		<!DOCTYPE html>
		<html>
			<head>
				<meta charset=utf-8 />
				<title>twitter</title>
				<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
				<script src="jquery.tmpl.js"></script>
				<script src="supporting_methods.js"></script>
				<link rel="stylesheet" href="style.css" type="text/css" media="screen" title="no title" charset="utf-8">
				<script type="text/template" id="twitterTemplate">
					<li>
							<img src="${ user['profile_image_url'] }" /> 
							${ text.linkify().atify() } 
							<span class="created_at">
								${ relative_time(created_at) } via ${ source }
							</span>
					</li>
				</script>
				<script type="text/javascript">
					$("document").ready(function(){
						var url = "http://api.twitter.com/1/statuses/user_timeline.json?id=RedWolves&count=3&callback=?"
						$.ajax({
							dataType: 'jsonp',
							url: url,
							jsonpCallback: "renderTweets" 
						});
					});

					function renderTweets(data) {
						$("#twitterTemplate")
							.render(data)
							.appendTo("#renderTweets");
					}
				</script>
			</head>
		<body>
		  <div id="Tweets">
			<h3>@RedWolves Tweets</h3>
		  	<ul id="renderTweets"></ul>
			<div id="more">
				<a href="http://twitter.com/redwolves">More ></a>
			</div>
		  </div>
		</body>
		</html>​
		
	



Source Code Demo

Saturday, April 03, 2010

Barcamp Rochester 2010 - Overview of jQuery UI and jQuery UI 1.8

Today was Barcamp Rochester 5. I gave a 30 minute overview of jQuery UI and went over the new features of jQuery UI 1.8. Here is the video of my talk I took with my flip video. Sorry about the sound quality, use headphones ;)

You can follow along with my slides. The slides are based off John Resig's HTML presentations and are heaviily modified to be jQuery UI themed and a showcase for what jQuery UI can do. (Note: you should use Safari/Chrome to view the slides. Also click the orange header text to advance the slide.)

If you attended my talk, please rate me on speaker rate.

Friday, January 22, 2010

jQuery 1.4 Give Us a New Way to Zebra Stripe

In jQuery 1.4 all setter methods have been extended to take in a setter function.  Before only .attr() had the ability to use a function to return the value to set. Now you can pass a setter function to .css(), .attr(), .val(), .html(), .text(), .append(), .prepend(), .before(), .after(), .replaceWith(), .wrap(), .wrapInner(), .offset(), .addClass(), .removeClass(), and .toggleClass().

The setter function can take two arguments, the index position of the element in the set and the old value of the element.

.css( propertyName, function(index, value))

With setter functions now available we can use this in a new way to zebra stripe a set of elements.  In this example we'll stripe a unordered list:

HTML:

<ul>
<li>item 1</li>
<li>item 2</li>
<li>item 3</li>
<li>item 4</li>
<li>item 5</li>
<li>item 6</li>
<li>item 7</li>
</ul>
jQuery 1.4:

$(document).ready(function(){
$("li").css("background-color", function(i){
return (i % 2 === 0) ? "#cccccc": "#FFFFFF";
});
});

We select all the LIs and call the .css() setter method. We give it the property name we want to update, background-image and we pass a function that will return the value we want to set.

The function tests if the index that we passed in i is MOD 2 (simply is it even or odd), if even set the color to #cccccc else set it to #FFFFFF.

Demo: (jsbin)

Note: Obviously, this isn't the best way to do zebra stripping with jQuery but I like to explore different ways to do the same thing to learn the techniques.

Monday, January 18, 2010

14 Days of jQuery - Behind the Scenes - yayQuery

14 Days of jQuery - Behind the Scenes - yayQuery from Ralph Whitbeck on Vimeo.

yayQueries Paul Irish steals my flip and records some video while we were out eating after day two of video recording.

Saturday, January 16, 2010

14 Days of jQuery - Behind the Scenes - Part 7

14 Days of jQuery - Behind the Scenes - Part 7 from Ralph Whitbeck on Vimeo.

The team feverishly works to get the Day 2 blog out before the stroke of midnight, everyone that is except...

Friday, January 15, 2010

14 Days of jQuery - Behind the Scenes - Part 6

14 Days of jQuery - Behind the Scenes - Part 6 from Ralph Whitbeck on Vimeo.

Ralph and Elijah are recording 6 podcasts while everyone is together. Here we are talking with Mike Hostetler and Jonathan Sharp about appendTo for a future episode.

14 Days of jQuery - Behind the Scenes - Part 5

14 Days of jQuery - Behind the Scenes - Part 5 from Ralph Whitbeck on Vimeo.

John Resig, Jonathan Sharp and Richard Worth are doing final sound checks. Here's a look at the set up.

Thursday, January 14, 2010

14 Days of jQuery - Behind the Scenes - Keynote

14 Days of jQuery - Behind the Scenes - Keynote from Ralph Whitbeck on Vimeo.

This is a look at the production team behind the keynote.

Blog Posts by:

The Official jQuery Podcast

with Ralph Whitbeck & Rey Bango

You can subscribe to the show in iTunes or via the raw RSS feed

My Twitter Updates

View Twitter Page