dom-class.js revision 0fa21f434cf7d4ed63d790f8fb2a9c68e656d00e
82d0cf8c731b23f6a2fbb31e3e696e629444363eTrippvar addClass, hasClass, removeClass;
82d0cf8c731b23f6a2fbb31e3e696e629444363eTripp
82d0cf8c731b23f6a2fbb31e3e696e629444363eTrippY.mix(Y.DOM, {
a75ebc38c1de401b679953a9b87bd323f0f48d02Tripp /**
82d0cf8c731b23f6a2fbb31e3e696e629444363eTripp * Determines whether a DOM element has the given className.
82d0cf8c731b23f6a2fbb31e3e696e629444363eTripp * @method hasClass
82d0cf8c731b23f6a2fbb31e3e696e629444363eTripp * @param {HTMLElement} element The DOM element.
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp * @param {String} className the class name to search for
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp * @return {Boolean} Whether or not the element has the given class.
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp */
82d0cf8c731b23f6a2fbb31e3e696e629444363eTripp hasClass: function(node, className) {
82d0cf8c731b23f6a2fbb31e3e696e629444363eTripp var re = Y.DOM._getRegExp('(?:^|\\s+)' + className + '(?:\\s+|$)');
82d0cf8c731b23f6a2fbb31e3e696e629444363eTripp return re.test(node.className);
87a49173dbc22a145cb87e605cca83fd42524377Tripp },
87a49173dbc22a145cb87e605cca83fd42524377Tripp
87a49173dbc22a145cb87e605cca83fd42524377Tripp /**
87a49173dbc22a145cb87e605cca83fd42524377Tripp * Adds a class name to a given DOM element.
87a49173dbc22a145cb87e605cca83fd42524377Tripp * @method addClass
87a49173dbc22a145cb87e605cca83fd42524377Tripp * @param {HTMLElement} element The DOM element.
87a49173dbc22a145cb87e605cca83fd42524377Tripp * @param {String} className the class name to add to the class attribute
87a49173dbc22a145cb87e605cca83fd42524377Tripp */
87a49173dbc22a145cb87e605cca83fd42524377Tripp addClass: function(node, className) {
87a49173dbc22a145cb87e605cca83fd42524377Tripp if (!Y.DOM.hasClass(node, className)) { // skip if already present
87a49173dbc22a145cb87e605cca83fd42524377Tripp node.className = Y.Lang.trim([node.className, className].join(' '));
82d0cf8c731b23f6a2fbb31e3e696e629444363eTripp }
82d0cf8c731b23f6a2fbb31e3e696e629444363eTripp },
82d0cf8c731b23f6a2fbb31e3e696e629444363eTripp
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp /**
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp * Removes a class name from a given element.
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp * @method removeClass
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp * @param {HTMLElement} element The DOM element.
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp * @param {String} className the class name to remove from the class attribute
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp */
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp removeClass: function(node, className) {
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp if (className && hasClass(node, className)) {
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp node.className = Y.Lang.trim(node.className.replace(Y.DOM._getRegExp('(?:^|\\s+)' +
82d0cf8c731b23f6a2fbb31e3e696e629444363eTripp className + '(?:\\s+|$)'), ' '));
82d0cf8c731b23f6a2fbb31e3e696e629444363eTripp
82d0cf8c731b23f6a2fbb31e3e696e629444363eTripp if ( hasClass(node, className) ) { // in case of multiple adjacent
82d0cf8c731b23f6a2fbb31e3e696e629444363eTripp removeClass(node, className);
82d0cf8c731b23f6a2fbb31e3e696e629444363eTripp }
f69d245bb21be88752420e834a6b6be37e9b525fTripp }
82d0cf8c731b23f6a2fbb31e3e696e629444363eTripp },
82d0cf8c731b23f6a2fbb31e3e696e629444363eTripp
82d0cf8c731b23f6a2fbb31e3e696e629444363eTripp /**
82d0cf8c731b23f6a2fbb31e3e696e629444363eTripp * Replace a class with another class for a given element.
82d0cf8c731b23f6a2fbb31e3e696e629444363eTripp * If no oldClassName is present, the newClassName is simply added.
82d0cf8c731b23f6a2fbb31e3e696e629444363eTripp * @method replaceClass
82d0cf8c731b23f6a2fbb31e3e696e629444363eTripp * @param {HTMLElement} element The DOM element.
82d0cf8c731b23f6a2fbb31e3e696e629444363eTripp * @param {String} oldClassName the class name to be replaced
87a49173dbc22a145cb87e605cca83fd42524377Tripp * @param {String} newClassName the class name that will be replacing the old class name
8209f3939e32e0e5bde64192267fdaf9db6f4fbcTripp */
8209f3939e32e0e5bde64192267fdaf9db6f4fbcTripp replaceClass: function(node, oldC, newC) {
87a49173dbc22a145cb87e605cca83fd42524377Tripp //Y.log('replaceClass replacing ' + oldC + ' with ' + newC, 'info', 'Node');
82d0cf8c731b23f6a2fbb31e3e696e629444363eTripp addClass(node, newC);
82d0cf8c731b23f6a2fbb31e3e696e629444363eTripp removeClass(node, oldC);
82d0cf8c731b23f6a2fbb31e3e696e629444363eTripp },
f69d245bb21be88752420e834a6b6be37e9b525fTripp
82d0cf8c731b23f6a2fbb31e3e696e629444363eTripp /**
82d0cf8c731b23f6a2fbb31e3e696e629444363eTripp * If the className exists on the node it is removed, if it doesn't exist it is added.
82d0cf8c731b23f6a2fbb31e3e696e629444363eTripp * @method toggleClass
82d0cf8c731b23f6a2fbb31e3e696e629444363eTripp * @param {HTMLElement} element The DOM element.
82d0cf8c731b23f6a2fbb31e3e696e629444363eTripp * @param {String} className the class name to be toggled
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp */
82d0cf8c731b23f6a2fbb31e3e696e629444363eTripp toggleClass: function(node, className) {
82d0cf8c731b23f6a2fbb31e3e696e629444363eTripp if (hasClass(node, className)) {
87a49173dbc22a145cb87e605cca83fd42524377Tripp removeClass(node, className);
87a49173dbc22a145cb87e605cca83fd42524377Tripp } else {
87a49173dbc22a145cb87e605cca83fd42524377Tripp addClass(node, className);
87a49173dbc22a145cb87e605cca83fd42524377Tripp }
87a49173dbc22a145cb87e605cca83fd42524377Tripp }
82d0cf8c731b23f6a2fbb31e3e696e629444363eTripp});
82d0cf8c731b23f6a2fbb31e3e696e629444363eTripp
82d0cf8c731b23f6a2fbb31e3e696e629444363eTripphasClass = Y.DOM.hasClass;
f69d245bb21be88752420e834a6b6be37e9b525fTrippremoveClass = Y.DOM.removeClass;
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTrippaddClass = Y.DOM.addClass;
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp