Wednesday, February 20, 2008

Rotator

Introduction

This small Javascript class, serves as a generic rotator for different kind of collections.

Arguments

The Rotator class constructor expects 3 arguments, the last 2 are optional.
  • collection
    Can be a string, an array or an array-like object( arguments, nodelist, numeric hash ).
    If it's a string, it will get split by character.
  • setter
    A function that receives a value, an element of the collection and it's index.
    It must, either set the value internally or return the new value.
    The scope (this) will be the element, the arguments are the value and then the index.
    If it's a string, it means you want to set the value to the attribute of the element with that name.
    If none is given, then Rotator.defaultSetter is used, which, by default, returns the new value.
  • getter:
    A function that receives an element of the collection and the index.
    It must return the desired information from them.
    The scope (this) will be the element and the first argument, the index.
    If it's a string, it means you want the attribute of the element with that name.
    If none is given, then Rotator.defaultGetter is assigned, which, by default, returns the element.

How to use

Once you create an instance (obj) of the class, you can get/set the collection using the method obj.collection().
To rotate the elements, call obj.left() or obj.right().
With each call, the elements will be rotated one position in the direction you specified, and the functions will be called.
Note that the collection held by the object won't be the same that you gave it. Yours remains untouched.

Check the demo to see it working. If you view the source, you'll see 3 different instances.

Links

Downloads

0 comments: