Posts Tagged ‘JQuery’

Simple WordPress Embedded Content, JUnit Updates and introducing Tohu!

Monday, December 21st, 2009

With my dad and step-mum currently visiting from the UK I have found it hard to find blogging time of late. A lot has been going on in my technical areas of interest lately, Java has announced closures in Java 7, WordPress and JUnit have been updated, JUnit has also had a bug release update pushed out already! In this entry I will outline the best WordPress functionality added by the update, the main reason for the JUnit releases and let you know about exciting developments at work.

WordPress has recently been updated to version 2.9. This new version includes a super easy way to include embeddable media in posts via Embeds. Thought I would give this new feature a work out by embedding one of my favourite television moments of all time…from classic BBC Comedy Only Fools and Horses – Del Boy prepares to make a move on some hot chicks whilst out on the town with Trigger…

Am sure I will find more uses for this great new feature. To find out what other new features are included check out the release blog post.

JUnit has also recently been updated to version 4.8.1. This version  follows close on the heals of the 4.8 release which introduced Categories to JUnit. Check out the Category changes and implementations  in the release notes.  The Categories implementation is marked by the authors as preliminary, not sure what is meant by that – as surely it would be difficult to remove it once people start using it! 4.8.1 fixes bugs in the Categories documentation and implementation reported by early implementors. I shall be taking a look at the implementation ASAP.

Lately I have been working on an interesting project at work that implemented a Grails/Groovy server and a .NET Extraction client. Most of my time was spent on the .NET client extractor which has been an interesting, if somewhat frustrating experience.

Today I got moved to a new project which looks like it could be really interesting. It involves the use of Tohu, an open source JBoss Drools based library for use in dynamic questionnaire based implementations. Solnet Solutions are the main developers of this open source implementation. I have been interested in making commercial use of Drools for a long time, and this project will give me the opportunity to get some real experience in this area. Tohu also currently only supports a JQuery UI. With JQuery being another of the technologies I have dabbled in but never really used in earnest as yet.

I have set myself a few goals for the coming months. Among them is blogging about the JUnit Categories updates and completing my review of the 3.7 changes. I will outline more of my plans and goals for the year in upcoming blog posts.

Picasa Photo Stream Slivers using JQuery – Part 2

Sunday, August 16th, 2009

This post continues on from Part 1. So if you have not read that, you may want to do so before continuing.

After completing the steps in the previous blog we now have a set of photo album thumbnail images. We shall now add the links that will then open up the Picasa Web Album

Appending the following to the displayed images code does the job for Picasa Web Albums:

.wrap("<div class='imgholder'><a href=" + item.link[1].href + "></a></div>");

So the complete code for the displaying of the album thumbnail images and links to them becomes this:

$jq("<img/>").attr("src",item.media$group.media$thumbnail[0].url)
  .appendTo("#images")
  .wrap("<div class='imgholder'><a href=" + item.link[1].href + "></a></div>");

Next we will add some styles to the images and the holder. Add the following code:

#images {
  overflow:hidden;
  height: 160px;
}
.imgholder {
  overflow:hidden;
  width: auto;
  float: left;
  border-right: 1px solid white;
  background: black;
  height: 160px;
}
.imgholder a,
.imghover a:hover,
.imghover a:hover img,
.imgholder a img {
  margin: 0;
  padding: 0;
  border: 0;
}

This code adds a fixed height to the images panel, floats the image container divs to the left inside the images panel and prevent margin/padding inheritence. When adding this widget to any other page you may need to clear:left at the bottom of the images panel. If you need to do this add an empty div with the id of imgholderend and add this code at the end of the styles:

.imgholderend {
  clear:left;
}

Now it is time to animate the images. First up we set the images that are not the first one to slivers of 10px wide and then add the JQuery code that animates the slivers. This code comes after the image loop code.

  // Set up the image container divs to collapse
  $jq(".imgholder").not(":first").css("width","10px");

  // Add the container mouseover event function
  $jq(".imgholder").mouseover(function() {

    // Make every image other than the one being mousedover small
    $jq(".imgholder").stop().not(this).animate({width:"10px"});

    // Make the currently active image big (set width to image width)
    $jq(this).animate({width:$jq(this).find("img").width()+"px"});

  });

Loading this code into the browser now should show you the Picasa Photo Stream slivers. Mouse over the images and they should animate to expand the currently moused over sliver.

Now that this code is working we can add it to a WordPress sidebar widget.

The first thing to check is that JQuery is enabled for your theme. For the default theme this means updating the call PHP in the header.php where wp_head() is called to the following:

<?php
  wp_enqueue_script('jquery');
  wp_head();
?>

The next thing is to add a text widget and add the Javascript code and the image holding divs we have developed over the 2 blogs:

<script type="text/javascript">
var picasaFeed = "http://picasaweb.google.com/data/feed/base/user/psellars?alt=json&kind=album&hl=en_US&callback=?";
var $jq = jQuery.noConflict();

$jq.getJSON(picasaFeed,function(data) {

  $jq.each(data.feed.entry, function(i,item) {
    $jq("<img/>").attr("src",item.media$group.media$thumbnail[0].url)
      .appendTo("#images")
      .wrap("<div class='imgholder'><a href='"+item.link[1].href+"'</a></div>");
  });

  // Set up the image container divs to collapse
  $jq(".imgholder").not(":first").css("width","10px");

  // Add the container mouseover event function
  $jq(".imgholder").mouseover(function() {

    // Make every image other than the one being mousedover small
    $jq(".imgholder").stop().not(this).animate({width:"10px"});

    // Make the currently active image big (set width to image width)
    $jq(this).animate({width:$jq(this).find("img").width()+"px"});

  });

});
</script>
<div id="images"></div>
<div id="imgholderend"></div>

..then add the CSS to your theme CSS. On WordPress this can be found in Appearance then Editor from the menu. The CSS to add is thus:

/* Picasa Web Albums Extra */
#images {
  overflow:hidden;
  height: 160px;
}
.imgholder {
  overflow:hidden;
  width: auto;
  float: left;
  border-right: 1px solid white;
  background: black;
  height: 160px;
}
.imgholder a,
.imghover a:hover,
.imghover a:hover img,
.imgholder a img {
  margin: 0;
  padding: 0;
  border: 0;
}
.imgholderend {
  clear:left;
}

That is it, you should now have a Picasa Web Album widget like that in the sidebar of this blog’s main page.

Hopefully you found this a useful blog – having written it I am considering writing such blogs more as articles on my website rather than blogs going forward. Makes more sense to me to blog about issues/little bits and pieces I have experimented with rather than these article type blogs.

Picasa Photo Stream Slivers using JQuery – Part 1

Sunday, August 16th, 2009

[ The Picasa Photo Stream Slivers can be seen on the main page of this blog]

Inspired by an article I read in issue 155 of Web Designer and the fact I have recently been taking lots more digital photos I recently undertook to implement a photo stream sliver display using JQuery from my Picasa web albums.

So after creating a base html test page my first task was to get the JSON feed from Picasa. This turned out to be:


http://picasaweb.google.com/data/feed/base/user/psellars?alt=json&amp;kind=album&amp;hl=en_US

Obtaining this JSON field was quite  simple, being almost the same as with the Flickr feed the magazine article used. For Flickr it is a simple case of grabbing the RSS feed for the album you want to use and replacing format=rss_200 with format=JSON. Obtaining the Picasa feed just as simple, replacing alt=rss with alt=json.

First things first is to include the JQuery script into the page. When I was creating this example I was using JQuery 1.3.2 so using the compressed version this is a simple matter of including this library in a script tag.

<script type="text/javascript"
  src="js/jquery-1.3.2.min.js"></script>

The slivers work by loading data dynamically into the page. I chose to do as per the article and include the images into a div element. This element was provided with id ‘images’ and added to the body of the page.

<div id="images"></div>

Now to the JQuery code in earnest. The first thing to do is grab the raw data from the feed.  JQuery has a built-in function to grab a JSON feed dynamically which loads the information from the feed and creates instances of the objects contained inside. The getJSON JQuery method call required looks like the code below, another thing to keep in mind is that this code is intended for a blog post – so we will not be using the ‘$’ for JQuery.  (Click here for more information on using JSON with Google Data API’s)

var $jq = jQuery.noConflict();
$jq.getJSON("INSERT_YOUR_FEED_URL_HERE&callback=?",
  function(data) {
    $jq.each(data.feed.entry, function(i,item) {
      // do something with each item!
    };
});

As can be seen from the code extract above the feed.entry element of the Google JSON feed contains the items we want to do things with. In this case each entry represents a Picasa web album.

In order to display the actual web album cover photos we use the following line in the script executed for each entry in the feed.

$jq("<img/>")
  .attr("src",item.media$group.media$thumbnail[0].url)
  .appendTo("#images");

This code creates an image and assigns the value of the group.media thumbnail url to the src of this image. This adds the album cover as the source of the image.

Loading this page into your web browser should display you the album covers related to your feed.

In Part 2 of this blog links to the albums will be added, some styling applied  and  a website widget created.