This also tweaks the array conversion slightly so that it is called by
selection.selectAll rather than d3_selectAll; this guarantees that the selection
groups are always arrays, even when a function selector is used.
This commit is contained in:
Mike Bostock 2011-09-27 08:45:06 -07:00
Родитель 5d0dbe29cf
Коммит 80fafd527c
8 изменённых файлов: 54 добавлений и 10 удалений

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

@ -11,6 +11,24 @@ try {
};
}
d3 = {version: "2.2.0"}; // semver
var d3_array = d3_arraySlice; // conversion for NodeLists
function d3_arrayCopy(pseudoarray) {
var i = -1, n = pseudoarray.length, array = [];
while (++i < n) array.push(pseudoarray[i]);
return array;
}
function d3_arraySlice(pseudoarray) {
return Array.prototype.slice.call(pseudoarray);
}
try {
d3_array(document.documentElement.childNodes)[0].nodeType;
} catch(e) {
d3_array = d3_arrayCopy;
}
var d3_arraySubclass = [].__proto__?
// Until ECMAScript supports array subclassing, prototype injection works well.
@ -1199,7 +1217,7 @@ function d3_selection(groups) {
}
var d3_select = function(s, n) { return n.querySelector(s); },
d3_selectAll = function(s, n) { return Array.prototype.slice.call(n.querySelectorAll(s)); };
d3_selectAll = function(s, n) { return n.querySelectorAll(s); };
// Prefer Sizzle, if available.
if (typeof Sizzle === "function") {
@ -1254,7 +1272,7 @@ d3_selectionPrototype.selectAll = function(selector) {
for (var j = -1, m = this.length; ++j < m;) {
for (var group = this[j], i = -1, n = group.length; ++i < n;) {
if (node = group[i]) {
subgroups.push(subgroup = selector.call(node, node.__data__, i));
subgroups.push(subgroup = d3_array(selector.call(node, node.__data__, i)));
subgroup.parentNode = node;
}
}
@ -1738,16 +1756,18 @@ var d3_selectionRoot = d3_selection([[document]]);
d3_selectionRoot[0].parentNode = document.documentElement;
// TODO fast singleton implementation!
// TODO select(function)
d3.select = function(selector) {
return typeof selector === "string"
? d3_selectionRoot.select(selector)
: d3_selection([[selector]]); // assume node
};
// TODO selectAll(function)
d3.selectAll = function(selector) {
return typeof selector === "string"
? d3_selectionRoot.selectAll(selector)
: d3_selection([selector]); // assume node[]
: d3_selection([d3_array(selector)]); // assume node[]
};
function d3_transition(groups, id) {
d3_arraySubclass(groups, d3_transitionPrototype);
@ -3401,7 +3421,7 @@ function d3_svg_mousePoint(container, e) {
};
d3.svg.touches = function(container) {
var touches = d3.event.touches;
return touches ? Array.prototype.map.call(touches, function(touch) {
return touches ? d3_array(touches).map(function(touch) {
var point = d3_svg_mousePoint(container, touch);
point.identifier = touch.identifier;
return point;

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

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

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

@ -1,3 +1,21 @@
var d3_array = d3_arraySlice; // conversion for NodeLists
function d3_arrayCopy(pseudoarray) {
var i = -1, n = pseudoarray.length, array = [];
while (++i < n) array.push(pseudoarray[i]);
return array;
}
function d3_arraySlice(pseudoarray) {
return Array.prototype.slice.call(pseudoarray);
}
try {
d3_array(document.documentElement.childNodes)[0].nodeType;
} catch(e) {
d3_array = d3_arrayCopy;
}
var d3_arraySubclass = [].__proto__?
// Until ECMAScript supports array subclassing, prototype injection works well.

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

@ -3,14 +3,16 @@ var d3_selectionRoot = d3_selection([[document]]);
d3_selectionRoot[0].parentNode = document.documentElement;
// TODO fast singleton implementation!
// TODO select(function)
d3.select = function(selector) {
return typeof selector === "string"
? d3_selectionRoot.select(selector)
: d3_selection([[selector]]); // assume node
};
// TODO selectAll(function)
d3.selectAll = function(selector) {
return typeof selector === "string"
? d3_selectionRoot.selectAll(selector)
: d3_selection([selector]); // assume node[]
: d3_selection([d3_array(selector)]); // assume node[]
};

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

@ -8,7 +8,7 @@ d3_selectionPrototype.selectAll = function(selector) {
for (var j = -1, m = this.length; ++j < m;) {
for (var group = this[j], i = -1, n = group.length; ++i < n;) {
if (node = group[i]) {
subgroups.push(subgroup = selector.call(node, node.__data__, i));
subgroups.push(subgroup = d3_array(selector.call(node, node.__data__, i)));
subgroup.parentNode = node;
}
}

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

@ -4,7 +4,7 @@ function d3_selection(groups) {
}
var d3_select = function(s, n) { return n.querySelector(s); },
d3_selectAll = function(s, n) { return Array.prototype.slice.call(n.querySelectorAll(s)); };
d3_selectAll = function(s, n) { return n.querySelectorAll(s); };
// Prefer Sizzle, if available.
if (typeof Sizzle === "function") {

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

@ -1,6 +1,6 @@
d3.svg.touches = function(container) {
var touches = d3.event.touches;
return touches ? Array.prototype.map.call(touches, function(touch) {
return touches ? d3_array(touches).map(function(touch) {
var point = d3_svg_mousePoint(container, touch);
point.identifier = touch.identifier;
return point;

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

@ -35,6 +35,10 @@ suite.addBatch({
assert.isTrue(div[0][0] === document.body.lastChild);
assert.length(div, 1);
assert.length(div[0], 1);
},
"groups are not instances of NodeList": function() {
var div = d3.select("body").selectAll(function() { return this.getElementsByClassName("div"); });
assert.isFalse(div[0] instanceof window.NodeList);
}
}
});