27 Aug 2011

Some Simple jQuery Plugins

Below are some really simple jQuery plugins that I've thrown together over the past couple of years that I've found useful.

Need to scroll to element with animation? Here you go:

  // scroll to animation
  // example usage: $('.scroll_to_here').scrollTo();

  jQuery.fn.scrollTo = function(speed) {
    if(speed === undefined ){
      speed = 'slow';
    }
    $('html,body').animate({scrollTop: this.offset().top},speed);
  };

Ajax polling is pretty easy with jQuery, but it is nice to have reusable function so you don't repeat yourself. The plugin below is based on another simple ajax polling plugin that scales the amount of time between requests. This plugin uses a simple interval with a maximum number of attempts.

  //Simple AJAX Polling jQuery Plugin
  // example usage:
  /* $.ajaxPoll({
          url: "/path",
          type: "GET",
          interval: 250,
          maxAttempts: 25,
          successCondition: function(result) {
              return result != "processing"; 
          },
          success: function(data) {
              $('#container').replaceWith(data);
          }
      });
  */

  jQuery.ajaxPoll = function(user_options){

    var options = {
      interval: 30000,
      maxAttempts: 15
    }
  
    jQuery.extend(options, user_options);
  
    var attempts = 0;

    options.success = function(data, status) {

      if (options.successCondition(data)) {
          if (options.successCallback){
            options.successCallback(data, status);
          }          
          return;
      }
    
      attempts++;

      if (attempts > options.maxAttempts) {
          return;
      }
    
      setTimeout(function() { jQuery.ajax(options) }, options.interval);
    };

    jQuery.ajax(options);
  }

The HTML5 placeholder attribute is awesome, but some browsers (IE and ... maybe just IE?) do not support it. Here is a jQuery plugin that solves that problem by clearing and restoring a field's placeholder value in browsers that don't handle that silliness for you.

  // clears and restores a field's default value
  // example usage (js): $('input.has_default').hasDefaultValue();
  // example usage (html): <input class="has_default" placeholder="This is displayed by default" type="text"/>

  jQuery.fn.hasDefaultValue = function() {
    function supports_input_placeholder() {
      var i = document.createElement('input');
      return 'placeholder' in i;
    }
    if(!supports_input_placeholder()){
      this.each(function(){
        if(this.value === ""){
          this.value = $(this).attr("placeholder")
        }
      });
      this.focus(function(event){
        if(this.value === $(this).attr("placeholder")){
          this.value = ""
        }
      })
      this.blur(function(event){
        if($(this).attr("placeholder") && this.value === ""){
          this.value = $(this).attr("placeholder");
        } 
      })
    }
  };
27 Aug 2011

Roku 2

Roku 2 BoxOne month ago I got a Roku 2. This was my fourth Roku. I bought a first generation Roku in June 2009 and I loved it. I eventually bought one for every TV in my house. My third box was the second generation Roku 1. It was much smaller than the first gen, had a better remote, and also had a cool little fabric tag on it. The Roku 2 follows in the second gen Roku 1's footsteps by being even smaller, having an even better remote, and still having the little purple tag.

A new feature of the Roku 2 is the ability to play games. The only game that I've tried is the included Angry Birds. It's a good version of the game, and the motion controls are surprisingly good. But, for me, gaming on the Roku is just a nice secondary feature. I think I'll stick to either my Xbox, Playstation or Wii for the majority of game playing.

The Roku 2 is small

The best thing about the Roku is the number of 'channels' available. There is one for Netflix, Amazon Instant Video, Hulu, and many more. The first generation boxes seem to have some trouble with channels like Hulu that sport a flashy interface. The Roku 2 doesn't have this problem. This is definitely the best Roku yet.

22 Aug 2011

Minimalist Business Card

Boris Smus' Business Card I came across the business card to the left via Boing Boing. Even though the original post is over one year old, it inspired me to try to design a simple business card (although I suppose it is more of a calling card). Graphic design is one of my weaker skills so I like to practice when I can. My take on the concept is below:

My Business Card

I kind of like it. I ordered twenty from Zazzle for $7 just to see what they look like printed on actual paper.

21 Aug 2011

Bastion

Bastion ScreenshotI've been playing Bastion for the Xbox 360. I love this game. The art is great, the dynamic narration is unlike anything I've ever seen in a game before, and the music is amazing. The game narrates the main character's actions while you play--a feature that would certainly be annoying if poorly executed. Luckily here it is wonderfully done. The narrator simultaneously injects both humor and a sense of weight to the story. The music is great and evocative. I even bought the soundtrack.

Bastion Screenshot

And on top of all that the game is really fun. Every weapon featured in the game is fun, looks cool and is useful. I'm starting my second play through and I'm looking forward to experimenting with some of the weapons that I only briefly used on my first try.

Bastion is one of the best games I've played this year. If you have a 360 download the Bastion demo and give it a try.

14 Aug 2011

Automating New Post Creation in Jekyll

After automating deployment, my next goal was to automate creating a new post. After looking around a bit I decided to slightly modify the rake task used by Octopress, a Jekyll based blogging framework.

  # adapted from https://github.com/imathis/octopress/blob/master/Rakefile   
  # usage rake new_post['My New Post'] or rake new_post (defaults to "My New Post")
  desc "Start a new post"
  task :new_post, :title do |t, args|
   args.with_defaults(:title => 'My New Post')
   title = args.title
   filename = "_posts/#{Time.now.strftime('%Y-%m-%d')}-#{title.downcase.gsub(/&/,'and').gsub(/[,'":\?!\(\)\[\]]/,'').gsub(/[\W\.]/, '-').gsub(/-+$/,'')}.html"
   puts "Creating new post: #{filename}"
   open(filename, 'w') do |post|
     system "mkdir -p _posts";
     post.puts "---"
     post.puts "layout: post"
     post.puts "title: \"#{title.gsub(/&/,'&amp;')}\""
     post.puts "date: #{Time.now.strftime('%Y-%m-%d %H:%M')}"
     post.puts "published: false"
     post.puts "---"
   end
  end

Now that the above is in my rakefile, I'm just a rake new_post['This is Awesome'] away from starting a new post.