prototype: Remove the dependency on Element.ClassNames from Element#addClassName/removeClassName/toggleClassName, and deprecate Element.ClassNames. Closes #9073.

This commit is contained in:
Sam Stephenson 2007-07-24 20:42:21 +00:00
parent 59c3dfd87a
commit a529bcf590
3 changed files with 47 additions and 42 deletions

View File

@ -1,5 +1,7 @@
*SVN*
* Remove the dependency on Element.ClassNames from Element#addClassName/removeClassName/toggleClassName, and deprecate Element.ClassNames. Closes #9073. [Tobie Langel]
* Make Element#wrap accept a second argument for setting attributes on the wrapper. Allow wrapping elements which are not part of the document. Closes #9071. [Tobie Langel]
* Improvements for Element#replace, Element#update and Element#insert. Closes #7429, #9060. [Tobie Langel]

View File

@ -20,7 +20,7 @@ var Insertion = {
After: function(element, content) {
return Element.insert(element, {after:content});
}
}
};
var $continue = new Error('"throw $continue" is deprecated, use "return" instead');
@ -109,5 +109,41 @@ var Position = {
options = options || {};
return Element.clonePosition(target, source, options);
}
}
};
/*--------------------------------------------------------------------------*/
Element.ClassNames = Class.create();
Element.ClassNames.prototype = {
initialize: function(element) {
this.element = $(element);
},
_each: function(iterator) {
this.element.className.split(/\s+/).select(function(name) {
return name.length > 0;
})._each(iterator);
},
set: function(className) {
this.element.className = className;
},
add: function(classNameToAdd) {
if (this.include(classNameToAdd)) return;
this.set($A(this).concat(classNameToAdd).join(' '));
},
remove: function(classNameToRemove) {
if (!this.include(classNameToRemove)) return;
this.set($A(this).without(classNameToRemove).join(' '));
},
toString: function() {
return $A(this).join(' ');
}
};
Object.extend(Element.ClassNames.prototype, Enumerable);
/*--------------------------------------------------------------------------*/

View File

@ -285,7 +285,7 @@ Element.Methods = {
},
classNames: function(element) {
return new Element.ClassNames(element);
return $w(element.className);
},
hasClassName: function(element, className) {
@ -297,20 +297,22 @@ Element.Methods = {
addClassName: function(element, className) {
if (!(element = $(element))) return;
Element.classNames(element).add(className);
if (!element.hasClassName(className))
element.className += (element.className ? ' ' : '') + className;
return element;
},
removeClassName: function(element, className) {
if (!(element = $(element))) return;
Element.classNames(element).remove(className);
element.className = element.className.replace(
new RegExp("(^|\\s+)" + className + "(\\s+|$)"), ' ').strip();
return element;
},
toggleClassName: function(element, className) {
if (!(element = $(element))) return;
Element.classNames(element)[element.hasClassName(className) ? 'remove' : 'add'](className);
return element;
return element[element.hasClassName(className) ?
'removeClassName' : 'addClassName'](className);
},
observe: function() {
@ -1158,38 +1160,3 @@ Element.addMethods = function(methods) {
if (Element.extend.refresh) Element.extend.refresh();
Element.cache = {};
};
/*--------------------------------------------------------------------------*/
Element.ClassNames = Class.create();
Element.ClassNames.prototype = {
initialize: function(element) {
this.element = $(element);
},
_each: function(iterator) {
this.element.className.split(/\s+/).select(function(name) {
return name.length > 0;
})._each(iterator);
},
set: function(className) {
this.element.className = className;
},
add: function(classNameToAdd) {
if (this.include(classNameToAdd)) return;
this.set($A(this).concat(classNameToAdd).join(' '));
},
remove: function(classNameToRemove) {
if (!this.include(classNameToRemove)) return;
this.set($A(this).without(classNameToRemove).join(' '));
},
toString: function() {
return $A(this).join(' ');
}
};
Object.extend(Element.ClassNames.prototype, Enumerable);