Section 14.3. Script.aculo.us: More Than the Sum of Its Periods


14.3. Script.aculo.us: More Than the Sum of Its Periods

The script.aculo.us library is one of several that's built on top of Prototype. It extends the available functionality and provides a higher level of interaction, as well as increasingly sophisticated effects.

You'll find documentation for script.aculo.us, which includes a usage page, at http://wiki.script.aculo.us/scriptaculous/show/Usage. This covers where to get the library and how to install it. The library consists of multiple JavaScript files (scriptaculous.js, builder.js, effects.js, dragdrop.js, slider.js, and control.js), which need to be placed in your script directory, along with prototype.js and any other JavaScript file.

14.3.1. Usage

To use script.aculo.us, you'll need to link prototype as well as the new library:

<script type="text/javascript" src="/books/4/327/1/html/2/prototype.js"></script> <script type="text/javascript" src="/books/4/327/1/html/2/scriptaculous.js"></script>

The scriptaculous.js file loads in all the other JS files. If you want only certain effects, though, you can specify this on the same line as the scriptaculous.js load, using the following syntax:

<script type="text/javascript" src="/books/4/327/1/html/2/scriptaculous.js?load=effects,controls">

Once loaded, you can then use any of the libraries' specialized UI (user interface) effects.

Script.aculo.us' libraries of effects, drag and drop, and auto-completion are integrated as a Ruby on Rails Ajax helper. This means you can automatically manage an effect using a tag such as the following:

<%= text_field_with_auto_complete :contact, :name %>

You don't have to be developing in Ruby on Rails to use script.aculo.us, but the documentation for doing so is sparse. Still, let's look at a couple of script.aculous.effects.


14.3.2. A Gander at Effects

One of the script.aculu.os libraries includes several visual effects: fades, clippings, and so on. These are extremely easy to use and quite fun to watch. In Example 14-3, I tried out several of the different effects, including ones to puff, squish, and pulsate a DIV element.

Example 14-3. Taking script.aculo.us visual effects for a run

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>I want to have fun!</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script type="text/javascript" src="/books/4/327/1/html/2/prototype.js"></script> <script type="text/javascript" src="/books/4/327/1/html/2/scriptaculous.js"></script> <style type="text/css"> div.elem { margin: 20px; padding: 10px;            background-color: #C6B3FF;            width: 400px; height: 200px;           } .elem a { text-decoration: none; font-size: larger; color: #6A38FF } </style> <script type="text/javascript"> //<![CDATA[ function pulsate(  ) {    new Effect.Pulsate($('theblock')); } function shake(  ) {    new Effect.Shake($('theblock')); } function slideup(  ) {    new Effect.SlideUp($('theblock')); } function slidedown(  ) {    new Effect.SlideDown($('theblock')); } function dropout(  ) {    new Effect.DropOut($('theblock')); } function appear(  ) {    new Effect.Appear($('theblock')); } function puff(  ) {    new Effect.Puff($('theblock')); } function squish(  ) {    new Effect.Squish($('theblock')); } function highlight(  ) {    new Effect.Highlight($('theblock')); } //]]> </script> </head> <body> <div  > <p>Testing the scriptaculous effects</p> </div> <div > <a href="javascript:pulsate(  )">new Effect.Pulsate(obj)</a><br /> <a href="javascript:shake(  )">new Effect.Shake(obj)</a><br /> <a href="javascript:slideup(  )">new Effect.SlideUp(obj)</a><br /> <a href="javascript:slidedown(  )">new Effect.SlideDown(obj)</a><br /> <a href="javascript:dropout(  )">new Effect.DropOut(obj)</a><br /> <a href="javascript:appear(  )">new Effect.Appear(obj)</a><br /> <a href="javascript:puff(  )">new Effect.Puff(obj)</a><br /> <a href="javascript:squish(  )">new Effect.Squish(obj)</a><br /> <a href="javascript:highlight(  )">new Effect.Highlight(obj)</a> </div> </body> </html>

Notice in the code that I pass the element in using the Prototype helper function, $.

The title of the example page says it all: I want to have fun. There's nothing wrong with making your web pages fun, but these effects go beyond just the coolness factor.

The Pulsate effect can be used to grab attention. Other means can still be used, such as an alert dialog when scripting is turned off. However, I find something like Pulsate preferable to using an alert to get attention.

The Shake effect can be used when a person enters a wrong value, and I've seen this used in login pages. If there's text that also provides feedback, the use of this effect is also accessible. The SlideDown and SlideUp provide the functionality demonstrated in Chapter 12 for creating an accordion effect. Again, if the layers are open when scripting is not supported, the page is accessible.

The Puff and Squish effects can show and hide a note to the web-page reader. I like this rather than using straight visibility because there's a warning that something is coming and something is going away, rather than just having them appear and disappear. One rule of DHTML is: don't disconcert your user too much.

The Appear function is a way to undo some of the other disappearing effects, and it also has a nice "here I come" feel to it. As for Highlight, this is the infamous blue-to-yellow fade that people are implementing in their applications to denote a successful form action. I'm still out on this one.

The point is how easy these effects were to use. Other script.aculo.us effects include the autocompletion, the sortables, and the slider. The library also implements drag and drop, though as discussed earlier in the book, use this effect sparingly.

Script.aculo.us isn't the only library built on top of Prototype. Another is Rico, discussed next.

Take a look under the covers at how script.aculo.us creates its effects. This, combined with looking at Prototype's code, is a good demonstration of clever JavaScript object management.





Learning JavaScript
Learning JavaScript, 2nd Edition
ISBN: 0596521871
EAN: 2147483647
Year: 2006
Pages: 151

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net