forked from d3/d3
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Multi-value map support for classed.
- Loading branch information
Showing
8 changed files
with
241 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
function d3_collapse(s) { | ||
return s.replace(/^\s+|\s+$/g, "").replace(/\s+/g, " "); | ||
return s.trim().replace(/\s+/g, " "); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,87 @@ | ||
d3_selectionPrototype.classed = function(name, value) { | ||
var names = d3_collapse(name).split(" "), | ||
n = names.length, | ||
i = -1; | ||
if (arguments.length > 1) { | ||
while (++i < n) d3_selection_classed.call(this, names[i], value); | ||
if (arguments.length < 2) { | ||
|
||
// For classed(function), the function must return an object for each | ||
// element, specifying the names of classes to add or remove. The values | ||
// must be constants, not functions. | ||
if ((value = typeof name) === "function") { | ||
return this.each(function() { | ||
var x = name.apply(this, arguments); | ||
for (value in x) d3_selection_classed(value, x[value]).apply(this, arguments); | ||
}); | ||
} | ||
|
||
// For classed(string), return true only if the first node has the specified | ||
// class or classes. Note that even if the browser supports DOMTokenList, it | ||
// probably doesn't support it on SVG elements (which can be animated). | ||
if (value === "string") { | ||
var node = this.node(), | ||
n = (name = name.trim().split(/^|\s+/g)).length, | ||
i = -1; | ||
if (value = node.classList) { | ||
while (++i < n) if (!value.contains(name[i])) return false; | ||
} else { | ||
value = node.className; | ||
if (value.baseVal != null) value = value.baseVal; | ||
while (++i < n) if (!d3_selection_classedRe(name[i]).test(value)) return false; | ||
} | ||
return true; | ||
} | ||
|
||
// For classed(object), the object specifies the names of classes to add or | ||
// remove. The values may be functions that are evaluated for each element. | ||
for (value in name) this.each(d3_selection_classed(value, name[value])); | ||
return this; | ||
} else { | ||
while (++i < n) if (!d3_selection_classed.call(this, names[i])) return false; | ||
return true; | ||
} | ||
|
||
// Otherwise, both a name and a value are specified, and are handled as below. | ||
return this.each(d3_selection_classed(name, value)); | ||
}; | ||
|
||
function d3_selection_classedRe(name) { | ||
return new RegExp("(?:^|\\s+)" + d3.requote(name) + "(?:\\s+|$)", "g"); | ||
} | ||
|
||
// Multiple class names are allowed (e.g., "foo bar"). | ||
function d3_selection_classed(name, value) { | ||
var re = new RegExp("(^|\\s+)" + d3.requote(name) + "(\\s+|$)", "g"); | ||
name = name.trim().split(/\s+/).map(d3_selection_classedName); | ||
var n = name.length; | ||
|
||
// If no value is specified, return the first value. | ||
if (arguments.length < 2) { | ||
var node = this.node(); | ||
if (c = node.classList) return c.contains(name); | ||
var c = node.className; | ||
re.lastIndex = 0; | ||
return re.test(c.baseVal != null ? c.baseVal : c); | ||
function classedConstant() { | ||
var i = -1; | ||
while (++i < n) name[i](this, value); | ||
} | ||
|
||
function classedAdd() { | ||
if (c = this.classList) return c.add(name); | ||
var c = this.className, | ||
cb = c.baseVal != null, | ||
cv = cb ? c.baseVal : c; | ||
re.lastIndex = 0; | ||
if (!re.test(cv)) { | ||
cv = d3_collapse(cv + " " + name); | ||
if (cb) c.baseVal = cv; | ||
else this.className = cv; | ||
} | ||
// When the value is a function, the function is still evaluated only once per | ||
// element even if there are multiple class names. | ||
function classedFunction() { | ||
var i = -1, x = value.apply(this, arguments); | ||
while (++i < n) name[i](this, x); | ||
} | ||
|
||
function classedRemove() { | ||
if (c = this.classList) return c.remove(name); | ||
var c = this.className, | ||
return typeof value === "function" | ||
? classedFunction | ||
: classedConstant; | ||
} | ||
|
||
function d3_selection_classedName(name) { | ||
var re = d3_selection_classedRe(name); | ||
return function(node, value) { | ||
if (c = node.classList) return value ? c.add(name) : c.remove(name); | ||
var c = node.className, | ||
cb = c.baseVal != null, | ||
cv = cb ? c.baseVal : c; | ||
if (cv) { | ||
if (value) { | ||
re.lastIndex = 0; | ||
if (!re.test(cv)) { | ||
cv = d3_collapse(cv + " " + name); | ||
if (cb) c.baseVal = cv; | ||
else node.className = cv; | ||
} | ||
} else if (cv) { | ||
cv = d3_collapse(cv.replace(re, " ")); | ||
if (cb) c.baseVal = cv; | ||
else this.className = cv; | ||
else node.className = cv; | ||
} | ||
} | ||
|
||
function classedFunction() { | ||
(value.apply(this, arguments) | ||
? classedAdd | ||
: classedRemove).call(this); | ||
} | ||
|
||
return this.each(typeof value === "function" | ||
? classedFunction : value | ||
? classedAdd | ||
: classedRemove); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.