Cleverly compacted `classed` code.

We can take advantage of JavaScript's invisible hoisting of variables to set
them before they are defined. This simplifies the code for the various special
cases in the `classed` operator, such as for SVG elements and browsers that
support the tokenized class list.
This commit is contained in:
Mike Bostock 2011-05-07 11:21:59 -07:00
Родитель 8d2bc4b1b3
Коммит 99e2a55614
3 изменённых файлов: 32 добавлений и 34 удалений

31
d3.js поставляемый
Просмотреть файл

@ -1305,37 +1305,36 @@ function d3_selection(groups) {
// If no value is specified, return the first value.
if (arguments.length < 2) {
return first(function() {
var c;
if (c = this.classList) return c.contains(name);
var c = this.className;
re.lastIndex = 0;
return re.test((c = this.className).baseVal != null
? c.baseVal : c);
return re.test(c.baseVal != null ? c.baseVal : c);
});
}
/** @this {Element} */
function classedAdd() {
var c;
if (c = this.classList) return c.add(name);
var isAnimatedString = (c = this.className).baseVal != null,
classes = isAnimatedString ? c.baseVal : c;
var c = this.className,
cb = c.baseVal != null,
cv = cb ? c.baseVal : c;
re.lastIndex = 0;
if (!re.test(classes)) {
classes = d3_collapse(classes + " " + name);
if (isAnimatedString) c.baseVal = classes;
else this.className = classes;
if (!re.test(cv)) {
cv = d3_collapse(cv + " " + name);
if (cb) c.baseVal = cv;
else this.className = cv;
}
}
/** @this {Element} */
function classedRemove() {
var c;
if (c = this.classList) return c.remove(name);
var isAnimatedString = (c = this.className).baseVal != null,
classes = isAnimatedString ? c.baseVal : c;
classes = d3_collapse(classes.replace(re, " "));
if (isAnimatedString) c.baseVal = classes;
else this.className = classes;
var c = this.className,
cb = c.baseVal != null,
cv = cb ? c.baseVal : c;
cv = d3_collapse(cv.replace(re, " "));
if (cb) c.baseVal = cv;
else this.className = cv;
}
/** @this {Element} */

4
d3.min.js поставляемый

Различия файлов скрыты, потому что одна или несколько строк слишком длинны

Просмотреть файл

@ -305,37 +305,36 @@ function d3_selection(groups) {
// If no value is specified, return the first value.
if (arguments.length < 2) {
return first(function() {
var c;
if (c = this.classList) return c.contains(name);
var c = this.className;
re.lastIndex = 0;
return re.test((c = this.className).baseVal != null
? c.baseVal : c);
return re.test(c.baseVal != null ? c.baseVal : c);
});
}
/** @this {Element} */
function classedAdd() {
var c;
if (c = this.classList) return c.add(name);
var isAnimatedString = (c = this.className).baseVal != null,
classes = isAnimatedString ? c.baseVal : c;
var c = this.className,
cb = c.baseVal != null,
cv = cb ? c.baseVal : c;
re.lastIndex = 0;
if (!re.test(classes)) {
classes = d3_collapse(classes + " " + name);
if (isAnimatedString) c.baseVal = classes;
else this.className = classes;
if (!re.test(cv)) {
cv = d3_collapse(cv + " " + name);
if (cb) c.baseVal = cv;
else this.className = cv;
}
}
/** @this {Element} */
function classedRemove() {
var c;
if (c = this.classList) return c.remove(name);
var isAnimatedString = (c = this.className).baseVal != null,
classes = isAnimatedString ? c.baseVal : c;
classes = d3_collapse(classes.replace(re, " "));
if (isAnimatedString) c.baseVal = classes;
else this.className = classes;
var c = this.className,
cb = c.baseVal != null,
cv = cb ? c.baseVal : c;
cv = d3_collapse(cv.replace(re, " "));
if (cb) c.baseVal = cv;
else this.className = cv;
}
/** @this {Element} */