|
1 d3_selectionPrototype.classed = function(name, value) { |
|
2 var names = name.split(d3_selection_classedWhitespace), |
|
3 n = names.length, |
|
4 i = -1; |
|
5 if (arguments.length > 1) { |
|
6 while (++i < n) d3_selection_classed.call(this, names[i], value); |
|
7 return this; |
|
8 } else { |
|
9 while (++i < n) if (!d3_selection_classed.call(this, names[i])) return false; |
|
10 return true; |
|
11 } |
|
12 }; |
|
13 |
|
14 var d3_selection_classedWhitespace = /\s+/g; |
|
15 |
|
16 function d3_selection_classed(name, value) { |
|
17 var re = new RegExp("(^|\\s+)" + d3.requote(name) + "(\\s+|$)", "g"); |
|
18 |
|
19 // If no value is specified, return the first value. |
|
20 if (arguments.length < 2) { |
|
21 var node = this.node(); |
|
22 if (c = node.classList) return c.contains(name); |
|
23 var c = node.className; |
|
24 re.lastIndex = 0; |
|
25 return re.test(c.baseVal != null ? c.baseVal : c); |
|
26 } |
|
27 |
|
28 function classedAdd() { |
|
29 if (c = this.classList) return c.add(name); |
|
30 var c = this.className, |
|
31 cb = c.baseVal != null, |
|
32 cv = cb ? c.baseVal : c; |
|
33 re.lastIndex = 0; |
|
34 if (!re.test(cv)) { |
|
35 cv = d3_collapse(cv + " " + name); |
|
36 if (cb) c.baseVal = cv; |
|
37 else this.className = cv; |
|
38 } |
|
39 } |
|
40 |
|
41 function classedRemove() { |
|
42 if (c = this.classList) return c.remove(name); |
|
43 var c = this.className, |
|
44 cb = c.baseVal != null, |
|
45 cv = cb ? c.baseVal : c; |
|
46 cv = d3_collapse(cv.replace(re, " ")); |
|
47 if (cb) c.baseVal = cv; |
|
48 else this.className = cv; |
|
49 } |
|
50 |
|
51 function classedFunction() { |
|
52 (value.apply(this, arguments) |
|
53 ? classedAdd |
|
54 : classedRemove).call(this); |
|
55 } |
|
56 |
|
57 return this.each(typeof value === "function" |
|
58 ? classedFunction : value |
|
59 ? classedAdd |
|
60 : classedRemove); |
|
61 } |