SOURCE CODE: Uize.Dom.Classes

VIEW REFERENCE

/*______________
|       ______  |   U I Z E    J A V A S C R I P T    F R A M E W O R K
|     /      /  |   ---------------------------------------------------
|    /    O /   |    MODULE : Uize.Dom.Classes Package
|   /    / /    |
|  /    / /  /| |    ONLINE : http://uize.com
| /____/ /__/_| | COPYRIGHT : (c)2010-2014 UIZE
|          /___ |   LICENSE : Available under MIT License or GNU General Public License
|_______________|             http://uize.com/license.html
*/

/* Module Meta Data
  type: Package
  importance: 5
  codeCompleteness: 100
  docCompleteness: 100
*/

/*?
  Introduction
    The =Uize.Dom.Classes= module facilitates manipulation of the value of the =className= property of DOM nodes, with support for adding classes, removing classes, toggling classes, and more.

    *DEVELOPERS:* `Chris van Rensburg`

    Key Benefits
      The =Uize.Dom.Classes= module provides a number of key benefits.

      More Elegant, Easier to Read
        It is certainly possible to manipulate classes using regular expressions, but the methods of the =Uize.Dom.Classes= module make it easier to do and make one's code easier to read.

        An Example of Adding a Class
          To illustrate the ease of use of the =Uize.Dom.Classes= module, consider the following example of adding a CSS class to a node's =className= property...

          BEFORE
          ....................................................
          var node = Uize.Node.getById ('recommendationsPod');
          if (!/\bpodPopulated\b/.test (node.className)) {
            node.className += ' podPopulated';
          }
          ....................................................

          AFTER
          ................................................................
          Uize.Dom.Classes.addClass ('recommendationsPod','podPopulated');
          ................................................................

          As you can see, the code using the =Uize.Dom.Classes.addClass= is much easier to read and understand.

        An Example of Toggling a Class
          In another illustration of the ease of use of the =Uize.Dom.Classes= module, consider the following example of toggling a CSS class in a node's =className= property...

          BEFORE
          ...................................................................
          var node = Uize.Node.getById ('recommendationsPod');
          if (/\bpodPopulated\b/.test (node.className)) {
            node.className = node.className.replace (/\bpodPopulated\b/,'');
          } else {
            node.className += ' podPopulated';
          }
          ...................................................................

          AFTER
          ...................................................................
          Uize.Dom.Classes.toggleClass ('recommendationsPod','podPopulated');
          ...................................................................

      Powerful State Paradigm
        In addition to ease of use, the =Uize.Dom.Classes= module introduces a powerful, higher level construct called a state.

        The `state related methods` that support the state paradigm make it much easier to manage the classes in the =className= property of a node to reflect application state. Consider an example where you want the CSS class "selected" to be present in the =className= property of a node with the =id= of ='recommendationsPod'= when the application variable =podIsSelected= is set to =true=, and to be absent when the variable is set to =false=. Your application will have updater code to update the =className= of the node to reflect the state of the =podIsSelected= variable. This updater code may not know or trust what the current state of the =className= property of the node might be, so it may test first to see if the "selected" class should be added or removed. You might write the code as follows...

        BEFORE
        ..................................................................
        if (podIsSelected) {
          Uize.Dom.Classes.addClass ('recommendationsPod','selected');
        } else {
          Uize.Dom.Classes.removeClass ('recommendationsPod','selected');
        }
        ..................................................................

        Well, the =Uize.Dom.Classes.setState= static method makes this type of update operation a lot simpler. Instead of conditionally adding or removing the class, you simply set its state as follows...

        AFTER
        ..........................................................................
        Uize.Dom.Classes.setState ('recommendationsPod','selected',podIsSelected);
        ..........................................................................

        This is a very simple use case, but the `state related methods` of the =Uize.Dom.Classes= module also support more sophisticated cases.

    Adding a Class
      A CSS class can be added to a node's =className= property using the =Uize.Dom.Classes.addClass= static method.

      This method only adds the specified CSS class to the node's =className= property if the class isn't already present, so you will never have the same class occurring multiple times in the =className= as a result of calling this method.

      The =Uize.Dom.Classes.addClass= method follows these basic rules...

      +. If the =className= property is empty, then it will be set to the class being added.
      +. If the class being added is already present in the =className= property, then the value of the =className= property will be unchanged (see important note on `case sensitivity`).
      +. If the class being added is not present but there are other classes in the =className= property, then the class being added will be appended, with a single space separating it from the other classes (if present).

      To illustrate the above rules, consider the following example...

      HTML - BEFORE
      .........................................................................