Rob Kinyon - DOM.Element-0.02

Documentation | Source

NAME

DOM.Element

DESCRIPTION

This provides a group of useful functions for manipulating specific elements within the DOM.

DEPENDENCIES

This requires JSAN to be installed.

STATIC CLASSES

These are classes that are not instantiable, but instead provide a useful set of operations. (In Javascript terms, they do not provide a prototype member, so you cannot call new() upon them.)

DOM.Element

This provides a set of functions that do useful small things to DOM elements. Every function (unless otherwise noted) passes all arguments through $() (from DOM.Utils). Please see that documentation for more details.

Unless otherwise noted, every function accepts a variable number of arguments.

SUPPORT

Currently, there is no mailing list or IRC channel. Please send bug reports and patches to the author.

AUTHOR

Rob Kinyon (rob.kinyon@iinteractive.com)

Originally written by Sam Stephenson (sam@conio.net)

My time is generously donated by Infinity Interactive, Inc. http://www.iinteractive.com

/*

*/

try {
    JSAN.use( 'DOM.Utils' );
} catch (e) {
    throw "DOM.Element requires JSAN to be loaded";
}

if ( typeof( DOM ) == 'undefined' ) {
    DOM = {};
}

/*

*/

/*

*/

DOM.Element = {

    /*

    =item * hide()

    This function make sure that every element passed to it is hidden via use of CSS.

    =cut

    */

    hide: function() {
        for (var i = 0; i < arguments.length; i++) {
            var element = $(arguments[i]);
            if ( element && element.nodeType == 1 )
                element.style.display = 'none';
        }
    }

    /*

    =item * show()

    This function make sure that every element passed to it is visible via use of CSS.

    =cut

    */

   ,show: function() {
        for (var i = 0; i < arguments.length; i++) {
            var element = $(arguments[i]);
            if ( element && element.nodeType == 1 )
                element.style.display = '';
        }
    }

    /*

    =item * toggle()

    For each element passed to it, this function calls hide() if the element is visible and show() if it's hidden.

    =cut

    */

   ,toggle: function() {
        for (var i = 0; i < arguments.length; i++) {
            var element = $(arguments[i]);
            if ( element && element.nodeType == 1 )
                element.style.display = 
                    (element.style.display == 'none' ? '' : 'none');
        }
    }

    /*

    =item * remove()

    This function removes() all the elements specified from the DOM tree.

    =cut

    */

   ,remove: function() {
        for (var i = 0; i < arguments.length; i++) {
            element = $(arguments[i]);
            if ( element )
                element.parentNode.removeChild(element);
        }
    }
   
    /*

    =item * getHeight()

    This function returns the offsetHeight.

    This function only accepts one argument.

    =cut

    */

   ,getHeight: function(element) {
        element = $(element);
        if ( !element ) return;
        return element.offsetHeight; 
    }

    /*

    =item * hasClassName()

    This function returns true or false depending on if the element has the classname.

    This function takes two arguments - the element and the classname.

    =cut

    */

   ,hasClassName: function(element, className) {
        element = $(element);
        if ( !element || element.nodeType != 1 ) return;
        var a = element.className.split(' ');
        for (var i = 0; i < a.length; i++) {
            if (a[i] == className)
                return true;
        }
        return false;
    }

    /*

    =item * addClassName()

    This function adds the classname to the element classlist.

    This function takes two arguments - the element and the classname.

    =cut

    */

   ,addClassName: function(element, className) {
        element = $(element);
        if ( !element || element.nodeType != 1 ) return;
        DOM.Element.removeClassName(element, className);
        element.className += ' ' + className;
    }

    /*

    =item * removeClassName()

    This function removes the classname from the element classlist.

    This function takes two arguments - the element and the classname.

    =cut

    */

   ,removeClassName: function(element, className) {
        element = $(element);
        if ( !element || element.nodeType != 1 ) return;

        var newClassnames = new Array();
        var a = element.className.split(' ');
        for (var i = 0; i < a.length; i++) {
            if (a[i] != className) {
                newClassnames.push( a[i] );
            }
        }
        element.className = newClassnames.join(' ');
    }
  
    /*

    =item * cleanWhitespace()

    This function returns true or false dependeing on if the element has the classname.

    This function takes two arguments - the element and the classname.

    =cut

    */

   ,cleanWhitespace: function() {
        var element = $(element);
        if ( !element ) return;
        for (var i = 0; i < element.childNodes.length; i++) {
            var node = element.childNodes[i];
            if (node.nodeType == 3 && !/\S/.test(node.nodeValue)) 
                DOM.Element.remove(node);
        }
    }

/*

*/

};

/*

*/