// Copyright 2006 Google Inc. // All Rights Reserved. // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions // are met: // // * Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above copyright // notice, this list of conditions and the following disclaimer in // the documentation and/or other materials provided with the // distribution. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // POSSIBILITY OF SUCH DAMAGE. /** * @fileoverview Utilities for adding, removing and setting classes. * */ goog.provide('goog.dom.classes'); goog.require('goog.array'); /** * Sets the entire class name of an element. * @param {Element} element DOM node to set class of. * @param {string} className Class name(s) to apply to element. */ goog.dom.classes.set = function(element, className) { element.className = className; }; /** * Gets an array of class names on an element * @param {Element} element DOM node to get class of. * @return {Array} Class names on {@code element}. */ goog.dom.classes.get = function(element) { var className = element.className; // Some types of elements don't have a className in IE (e.g. iframes). // Furthermore, in Firefox, className is not a string when the element is // an SVG element. return className && typeof className.split == 'function' ? className.split(' ') : []; }; /** * Adds a class or classes to an element. Does not add multiples of class names. * @param {Element} element DOM node to add class to. * @param {string} var_args Class names to add. * @return {boolean} Whether class was added (or all classes were added). */ goog.dom.classes.add = function(element, var_args) { var classes = goog.dom.classes.get(element); var args = goog.array.slice(arguments, 1); var b = goog.dom.classes.add_(classes, args); element.className = classes.join(' '); return b; }; /** * Removes a class or classes from an element. * @param {Element} element DOM node to remove class from. * @param {string} var_args Class name(s) to remove. * @return {boolean} Whether all classes in {@code var_args} were found and * removed. */ goog.dom.classes.remove = function(element, var_args) { var classes = goog.dom.classes.get(element); var args = goog.array.slice(arguments, 1); var b = goog.dom.classes.remove_(classes, args); element.className = classes.join(' '); return b; }; /** * Helper method for {@link goog.dom.classes.add} and * {@link goog.dom.classes.addRemove}. Adds one or more classes to the supplied * classes array. * @param {Array.} classes All class names for the element, will be * updated to have the classes supplied in {@code args} added. * @param {Array.} args Class names to add. * @return {boolean} Whether all classes in were added. * @private */ goog.dom.classes.add_ = function(classes, args) { var rv = 0; for (var i = 0; i < args.length; i++) { if (!goog.array.contains(classes, args[i])) { classes.push(args[i]); rv++; } } return rv == args.length; }; /** * Helper method for {@link goog.dom.classes.remove} and * {@link goog.dom.classes.addRemove}. Removes one or more classes from the * supplied classes array. * @param {Array.} classes All class names for the element, will be * updated to have the classes supplied in {@code args} removed. * @param {Array.} args Class names to remove. * @return {boolean} Whether all classes in were found and removed. * @private */ goog.dom.classes.remove_ = function(classes, args) { var rv = 0; for (var i = 0; i < classes.length; i++) { if (goog.array.contains(args, classes[i])) { goog.array.splice(classes, i--, 1); rv++; } } return rv == args.length; }; /** * Switches a class on an element from one to another without disturbing other * classes. If the fromClass isn't removed, the toClass won't be added. * @param {Element} element DOM node to swap classes on. * @param {string} fromClass Class to remove. * @param {string} toClass Class to add. * @return {boolean} Whether classes were switched. */ goog.dom.classes.swap = function(element, fromClass, toClass) { var classes = goog.dom.classes.get(element); var removed = false; for (var i = 0; i < classes.length; i++) { if (classes[i] == fromClass) { goog.array.splice(classes, i--, 1); removed = true; } } if (removed) { classes.push(toClass); element.className = classes.join(' '); } return removed; }; /** * Adds zero or more classes to and element and and removes zero or more as a * single operation. Unlike calling {@link goog.dom.classes.add} and * {@link goog.dom.classes.remove} separately this is more efficient as it only * parses the class property once. * @param {Element} element DOM node to swap classes on. * @param {string|Array.|null} classesToRemove Class or classes to * remove, if null no classes are removed. * @param {string|Array.|null} classesToAdd Class or classes to add, if * null no classes are added. */ goog.dom.classes.addRemove = function(element, classesToRemove, classesToAdd) { var classes = goog.dom.classes.get(element); if (goog.isString(classesToRemove)) { goog.array.remove(classes, classesToRemove); } else if (goog.isArray(classesToRemove)) { goog.dom.classes.remove_(classes, classesToRemove); } if (goog.isString(classesToAdd) && !goog.array.contains(classes, classesToAdd)) { classes.push(classesToAdd); } else if (goog.isArray(classesToAdd)) { goog.dom.classes.add_(classes, classesToAdd); } element.className = classes.join(' '); }; /** * Returns true if an element has a class. * @param {Element} element DOM node to test. * @param {string} className Class name to test for. * @return {boolean} Whether element has the class. */ goog.dom.classes.has = function(element, className) { return goog.array.contains(goog.dom.classes.get(element), className); }; /** * Adds or removes a class depending on the enabled argument. * @param {Element} element DOM node to add or remove the class on. * @param {string} className Class name to add or remove. * @param {boolean} enabled Whether to add or remove the class (true adds, * false removes). */ goog.dom.classes.enable = function(element, className, enabled) { if (enabled) { goog.dom.classes.add(element, className); } else { goog.dom.classes.remove(element, className); } }; /** * Removes a class if an element has it, and adds it the element doesn't have * it. Won't affect other classes on the node. * @param {Element} element DOM node to toggle class on. * @param {string} className Class to toggle. * @return {boolean} True if class was added, false if it was removed * (in other words, whether element has the class after this function has * been called). */ goog.dom.classes.toggle = function(element, className) { var add = !goog.dom.classes.has(element, className); goog.dom.classes.enable(element, className, add); return add; };