64x64 icon dark hosting
Choose a hosting plan here and get a free year's subscription to Tuts+ (worth $180).

Create a Scalable Widget Using YUI3: Part 3


Start a hosting plan from $3.92/mo and get a free year on Tuts+ (normally $180)

In the last part of this series, we looked at the life-cycle methods, automatic methods and the custom methods that our widget requires or can make use of. In this part, we're going to finish defining the widget's class by adding the attribute change-handling methods that we attached in the bindUI() life-cycle method.

Let's get started right away!

Attribute Change Handlers

The attribute change-handling group of methods are called when some of our attributes change values. We'll start by adding the method that is called when the showTitle attribute changes; add the following code directly after the _uiSetTitle() method:

We first get a reference to the contentBox, and then use this to select the title node. Remember this is the container in which reside the title and subtitle in the header of the widget.

If the title node already exists, we remove it using YUI's remove() method. We also set the _titleNode of the widget to null. If the node doesn't exist, we simple call the _createTitle() method of our widget to generate and display it.

Next we can handle the showUI attribute changing:

This method is almost identical to the last one -- all that changes is that we are looking for the change of a different attribute, and either removing or creating a different group of elements. Again, we set the _uiNode property of our widget to null, so that the widget is aware of the latest state of its UI.

Our next method is called after the term attribute changes:

When the term attribute changes, we first remove any previous search results from the viewer by calling YUI's (specifically the Node module's) empty() method followed by the hide() method. We also show our loader node for some visual feedback that something is happening.

We then call our _retrieveTweets() method to initiate a new request to Twitter's search API. This will trigger a cascade of additional methods to be called, that result ultimately in the viewer being updated with a new set of tweets. Finally, we check whether the widget currently has a _titleNode, and if so we call the _uiSetTitle() method in order to update the subtitle with the new search term.

Our last attribute change-handler is by far the largest and deals with the tweets attribute changes, which will occur as a result of the request to Twitter being made:

First up, we set the variables we'll need within the method including a counter variable for use in the for loop, the results array from the response that is stored in the tweets attribute, the value of the numberOfTweets attribute and the limit, which is either the number of results in the results array, or the configured number of tweets if there are fewer items in the array than the number of tweets.

The remaining code for this method is encased within an if conditional which checks to see if there are actually results, which may not be the case if there were no tweets containing the search term. If there are results in the array, we iterate over each of them using a for loop. On each iteration, we get the current tweet and pass it to a _formatTweet() utility method that will add any links, usernames or hash tags found within the text, and then create a new node for the tweet using the same principles that we looked at in the last part of this tutorial.

When the searchUI is not visible, we should alter the styling of the widget slightly to prevent a double border at the bottom of the widget. We check whether the showUI attribute is set to false, and is the last tweet being processed, and if so add the class name last to the tweet using YUI's addClass() method. We then add the newly created node to the viewer node to display it in the widget.

After the for loop has completed, we hide the loading node, which will at this point be visible having already been displayed earlier on, and then show the viewer node.

If the results array does not have a length, it means that the search did not return any results. In this case, we create an error node to display to the user and append it to the viewer node, then hide the loading node and show the viewer node as before.

A Final Utility Method

We've added all of the methods that support changing attribute values. At this point, we have just one further method to add; the _formatTweet() method that we reference from the within the for loop of the method we just added. This method is as follows:

This method accepts a single argument, which is the text from the 'current' item of the results array that we want to linkify/atify/hashify. We start by defining three regular expressions, the first will match any links within the text that start with http, https or ftp and contain any characters that are allowed within URLs. The second will match any Twitter usernames (any strings that start with the @ symbol), and the last will match any strings that start with the # symbol.

We then set a variable called string which is used to contain the transformed text. First, we add the links. JavaScript's replace() function accepts the regular expression for matching links as the first argument and a function as the second argument -- the function will be executed each time a match is found and is passed the matching text as an argument. The function then returns the match having converted it to a link element using JavaScript's link() function. This function accepts a URL that is used for the href of the resulting link. The matching text is used for the href.

We then use the replace() function on the string once again, but this time we pass in the @ matching regular expression as the first argument. This function works in the same way as before, but also adds Twitter's URL to the start of the href that is used to wrap the matching text. The string variable is then operated on in the same way to match and convert any hashed words, but this time Twitter's search API URL is used to create the link(s). After the text has been operated on, we return the resulting string.

This brings us to the end of our widget's class; at this point we should have an almost fully functioning widget (we haven't yet added the paging, this will be the subject of the next and final instalment in this series). We should be able to run the page and get results:

Nettuts+ Image

Styling the Widget

We should provide at least 2 style sheets for our widget; a base style sheet that contains the basic styles that the widget requires in order to display correctly, and a theme style sheet that controls how the widget appears visually. We'll look at the base style sheet first; add the following code to a new file:

Save this style sheet as tweet-search-base.css in the css folder. As you can see, we target all of the elements within the widget using the class names we generated in part one. There may be multiple instances of the widget on a single page and we don't want our styles to affect any other elements on the page outside of our widget, so using class names in this way is really the only reliable solution.

The styling has been kept as light as possible, using only the barest necessary styles. The widget has no fixed width and uses percentages for things like padding and margins so that it can be put into any sized container by the implementing developer.

Next, we can add the skin file; add the following code in another new file:

Save this file as tweet-search-skin.css in the css folder. Although we also use our generated class names here, each rule is prefixed with the yui3-skin-sam class name so that the rules are only applied when the default Sam theme is in use. This makes it very easy for the overall look of the widget to be changed. This does mean however that the implementing developer will need to add the yui3-skin-sam class name to an element on the page, usually the , but this is likely to be in use already if other modules of the library are being used.

Like before, we've added quite light styling, although we do have a little more freedom of expression with a skin file, hence the subtle niceties such as the rounded-corners and css-gradients. We should also recommended that the css-reset, css-fonts and css-base YUI style sheets are also used when implementing our widget, as doing so is part of the reason the custom style sheets used by the widget are nice and small.

Implementing the Widget

Our work as widget builders is complete (for now), but we should spend a little while looking at how the widget is actually used. Create the following HTML page in your text editor:

The only YUI script file we need to link to is the YUI seed file which sets up the YUI global object and loads the required modules.

Save this file in the root project directory. First of all we link to the CDN hosted YUI reset, base and fonts combined style sheet, as well as our two custom style sheets that we just created. We also add the yui3-skin-sam class name to the of the page to pick up the theme styling for our widget. On the page, we add a container for our widget and give it an id attribute for easy selecting.

The only YUI script file we need to link to is the YUI seed file; this file sets up the YUI global object and contains the YUI loader which dynamically loads the modules required by the page. We also link to our plugin's script file, of course.

Within the final script element we instantiate the YUI global object and call the use() method specifying our widget's name (not the static NAME used internally by our widget, but the name specified in the add() method of our widget's class wrapper) as the first argument.

Each YUI instance is a self-contained sandbox in which only the named modules are accessible.

The second argument is an anonymous function in which the initialisation code for our widget is added. This function accepts a single argument which refers to the current YUI instance. We can use any number of YUI objects on the page, each with its own modules. Each YUI instance is a self-contained sandbox in which only the named modules (and their dependencies) are accessible. This means we can have any number of self-contained blocks of code, all independent from each other on the same page.

Within the callback function, we create a new instance of our widget stored in a variable. Our widget's constructor is available via the namespace we specified in the widget's class, which is attached to the YUI instance as a property. Our widget's constructor accepts a configuration object as an argument; we use this to specify the container that we want to render our widget into, in this case the empty <div> we added to the page. The specified element will become the contentBox of our widget. Finally, we call the render() method on the variable our widget instance is stored in, which renders the HTML for our widget into the specified container.

In the configuration object, we can override any of the default attributes of our widget, so if we wanted to disable the title of the widget and the search UI, we could pass the following configuration object into our widget's constructor:

I mentioned in an earlier part of the widget that by including all of the text strings used by the widget in an attribute, we could easily enable extremely easy internationalization. To render the widget in Spanish, for example, all we need to do is override the strings attribute, like this:

Now when we run the widget, all of the visible text (aside from the tweets of course) for the widget is in Spanish:

Nettuts+ Image


In this part of the tutorial, we completed our widget by adding the attribute change-handling methods and a small utility method for formatting the flat text of each tweet into mark-up. We also looked at the styling required by our widget and how the styles should be categorized, i.e. whether they are base styles or skin styles.

We also saw how easy it is to initialise and configure the widget and how it can easily be converted into display in another language. In the next part of this tutorial, we'll look at a close relative to the widget – the plugin and add a paging feature to our widget.