Commit 05d871b7 authored by Mike Bostock's avatar Mike Bostock

Merge branch 'order'

parents fe671a70 b3848afc
......@@ -99,6 +99,7 @@ d3.core.js: \
src/core/selection-data.js \
src/core/selection-filter.js \
src/core/selection-map.js \
src/core/selection-order.js \
src/core/selection-sort.js \
src/core/selection-on.js \
src/core/selection-each.js \
......
......@@ -652,6 +652,7 @@ function d3_geo_bounds(o, f) {
var d3_geo_boundsTypes = {
Feature: d3_geo_boundsFeature,
FeatureCollection: d3_geo_boundsFeatureCollection,
GeometryCollection: d3_geo_boundsGeometryCollection,
LineString: d3_geo_boundsLineString,
MultiLineString: d3_geo_boundsMultiLineString,
MultiPoint: d3_geo_boundsLineString,
......@@ -670,6 +671,12 @@ function d3_geo_boundsFeatureCollection(o, f) {
}
}
function d3_geo_boundsGeometryCollection(o, f) {
for (var a = o.geometries, i = 0, n = a.length; i < n; i++) {
d3_geo_bounds(a[i], f);
}
}
function d3_geo_boundsLineString(o, f) {
for (var a = o.coordinates, i = 0, n = a.length; i < n; i++) {
f.apply(null, a[i]);
......
This diff is collapsed.
......@@ -10,7 +10,7 @@ try {
d3_style_setProperty.call(this, name, value + "", priority);
};
}
d3 = {version: "2.6.1"}; // semver
d3 = {version: "2.7.0"}; // semver
var d3_array = d3_arraySlice; // conversion for NodeLists
function d3_arrayCopy(pseudoarray) {
......@@ -1346,12 +1346,16 @@ function d3_selection(groups) {
}
var d3_select = function(s, n) { return n.querySelector(s); },
d3_selectAll = function(s, n) { return n.querySelectorAll(s); };
d3_selectAll = function(s, n) { return n.querySelectorAll(s); },
d3_selectRoot = document.documentElement,
d3_selectMatcher = d3_selectRoot.matchesSelector || d3_selectRoot.webkitMatchesSelector || d3_selectRoot.mozMatchesSelector || d3_selectRoot.msMatchesSelector || d3_selectRoot.oMatchesSelector,
d3_selectMatches = function(n, s) { return d3_selectMatcher.call(n, s); };
// Prefer Sizzle, if available.
if (typeof Sizzle === "function") {
d3_select = function(s, n) { return Sizzle(s, n)[0]; };
d3_selectAll = function(s, n) { return Sizzle.uniqueSort(Sizzle(s, n)); };
d3_selectMatches = Sizzle.matchesSelector;
}
var d3_selectionPrototype = [];
......@@ -1735,13 +1739,14 @@ d3_selectionPrototype.data = function(data, join) {
function d3_selection_dataNode(data) {
return {__data__: data};
}
// TODO preserve null elements to maintain index?
d3_selectionPrototype.filter = function(filter) {
var subgroups = [],
subgroup,
group,
node;
if (typeof filter !== "function") filter = d3_selection_filter(filter);
for (var j = 0, m = this.length; j < m; j++) {
subgroups.push(subgroup = []);
subgroup.parentNode = (group = this[j]).parentNode;
......@@ -1754,24 +1759,33 @@ d3_selectionPrototype.filter = function(filter) {
return d3_selection(subgroups);
};
function d3_selection_filter(selector) {
return function() {
return d3_selectMatches(this, selector);
};
}
d3_selectionPrototype.map = function(map) {
return this.each(function() {
this.__data__ = map.apply(this, arguments);
});
};
d3_selectionPrototype.sort = function(comparator) {
comparator = d3_selection_sortComparator.apply(this, arguments);
for (var j = 0, m = this.length; j < m; j++) {
for (var group = this[j].sort(comparator), i = 1, n = group.length, prev = group[0]; i < n; i++) {
var node = group[i];
if (node) {
if (prev) prev.parentNode.insertBefore(node, prev.nextSibling);
prev = node;
d3_selectionPrototype.order = function() {
for (var j = -1, m = this.length; ++j < m;) {
for (var group = this[j], i = group.length - 1, next = group[i], node; --i >= 0;) {
if (node = group[i]) {
if (next) next.parentNode.insertBefore(node, next);
next = node;
}
}
}
return this;
};
d3_selectionPrototype.sort = function(comparator) {
comparator = d3_selection_sortComparator.apply(this, arguments);
for (var j = -1, m = this.length; ++j < m;) this[j].sort(comparator);
return this.order();
};
function d3_selection_sortComparator(comparator) {
if (!arguments.length) comparator = d3.ascending;
......@@ -1863,7 +1877,7 @@ d3_selectionPrototype.transition = function() {
};
var d3_selectionRoot = d3_selection([[document]]);
d3_selectionRoot[0].parentNode = document.documentElement;
d3_selectionRoot[0].parentNode = d3_selectRoot;
// TODO fast singleton implementation!
// TODO select(function)
......
This source diff could not be displayed because it is too large. You can view the blob instead.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
</head>
<body>
<script type="text/javascript" src="../../d3.js"></script>
<script type="text/javascript">
var div = d3.select("body").selectAll("div")
.data(["a", "b", "f"])
.enter().append("div")
.text(String);
var div = d3.select("body").selectAll("div")
.data(["a", "b", "c", "d", "e", "f"], String);
div.enter().append("div")
.text(String);
div.order();
</script>
</body>
</html>
{
"name": "d3",
"version": "2.6.1",
"version": "2.7.0",
"description": "A small, free JavaScript library for manipulating documents based on data.",
"keywords": [
"dom",
......
d3 = {version: "2.6.1"}; // semver
d3 = {version: "2.7.0"}; // semver
// TODO preserve null elements to maintain index?
d3_selectionPrototype.filter = function(filter) {
var subgroups = [],
subgroup,
group,
node;
if (typeof filter !== "function") filter = d3_selection_filter(filter);
for (var j = 0, m = this.length; j < m; j++) {
subgroups.push(subgroup = []);
subgroup.parentNode = (group = this[j]).parentNode;
......@@ -17,3 +18,9 @@ d3_selectionPrototype.filter = function(filter) {
return d3_selection(subgroups);
};
function d3_selection_filter(selector) {
return function() {
return d3_selectMatches(this, selector);
};
}
d3_selectionPrototype.order = function() {
for (var j = -1, m = this.length; ++j < m;) {
for (var group = this[j], i = group.length - 1, next = group[i], node; --i >= 0;) {
if (node = group[i]) {
if (next) next.parentNode.insertBefore(node, next);
next = node;
}
}
}
return this;
};
var d3_selectionRoot = d3_selection([[document]]);
d3_selectionRoot[0].parentNode = document.documentElement;
d3_selectionRoot[0].parentNode = d3_selectRoot;
// TODO fast singleton implementation!
// TODO select(function)
......
d3_selectionPrototype.sort = function(comparator) {
comparator = d3_selection_sortComparator.apply(this, arguments);
for (var j = 0, m = this.length; j < m; j++) {
for (var group = this[j].sort(comparator), i = 1, n = group.length, prev = group[0]; i < n; i++) {
var node = group[i];
if (node) {
if (prev) prev.parentNode.insertBefore(node, prev.nextSibling);
prev = node;
}
}
}
return this;
for (var j = -1, m = this.length; ++j < m;) this[j].sort(comparator);
return this.order();
};
function d3_selection_sortComparator(comparator) {
......
......@@ -4,12 +4,16 @@ function d3_selection(groups) {
}
var d3_select = function(s, n) { return n.querySelector(s); },
d3_selectAll = function(s, n) { return n.querySelectorAll(s); };
d3_selectAll = function(s, n) { return n.querySelectorAll(s); },
d3_selectRoot = document.documentElement,
d3_selectMatcher = d3_selectRoot.matchesSelector || d3_selectRoot.webkitMatchesSelector || d3_selectRoot.mozMatchesSelector || d3_selectRoot.msMatchesSelector || d3_selectRoot.oMatchesSelector,
d3_selectMatches = function(n, s) { return d3_selectMatcher.call(n, s); };
// Prefer Sizzle, if available.
if (typeof Sizzle === "function") {
d3_select = function(s, n) { return Sizzle(s, n)[0]; };
d3_selectAll = function(s, n) { return Sizzle.uniqueSort(Sizzle(s, n)); };
d3_selectMatches = Sizzle.matchesSelector;
}
var d3_selectionPrototype = [];
......
......@@ -25,6 +25,7 @@ function d3_geo_bounds(o, f) {
var d3_geo_boundsTypes = {
Feature: d3_geo_boundsFeature,
FeatureCollection: d3_geo_boundsFeatureCollection,
GeometryCollection: d3_geo_boundsGeometryCollection,
LineString: d3_geo_boundsLineString,
MultiLineString: d3_geo_boundsMultiLineString,
MultiPoint: d3_geo_boundsLineString,
......@@ -43,6 +44,12 @@ function d3_geo_boundsFeatureCollection(o, f) {
}
}
function d3_geo_boundsGeometryCollection(o, f) {
for (var a = o.geometries, i = 0, n = a.length; i < n; i++) {
d3_geo_bounds(a[i], f);
}
}
function d3_geo_boundsLineString(o, f) {
for (var a = o.coordinates, i = 0, n = a.length; i < n; i++) {
f.apply(null, a[i]);
......
......@@ -54,6 +54,13 @@ suite.addBatch({
assert.isTrue(some[0][0] === span[0][3]);
assert.equal(some.length, 1);
},
"can be specified as a selector": function(span) {
span.classed("foo", function(d, i) { return d & 1; });
var some = span.filter(".foo");
assert.equal(some.length, 2);
assert.equal(some[0].length, 1);
assert.equal(some[1].length, 1);
},
"returns a new selection": function(span) {
assert.isFalse(span.filter(function() { return 1; }) === span);
}
......
require("../env");
require("../../d3");
var vows = require("vows"),
assert = require("assert");
var suite = vows.describe("selection.order");
suite.addBatch({
"selectAll(div)": {
topic: function() {
return d3.select("body").html("").selectAll("div")
.data([1, 2, 10, 20])
.enter().append("div")
.attr("id", String);
},
"orders elements by data": function(div) {
div = div.data([1, 10, 20, 2], String).order();
assert.domNull(div[0][0].previousSibling);
assert.domEqual(div[0][1].previousSibling, div[0][0]);
assert.domEqual(div[0][2].previousSibling, div[0][1]);
assert.domEqual(div[0][3].previousSibling, div[0][2]);
assert.domNull(div[0][3].nextSibling);
},
"returns the current selection": function(span) {
span = d3.select("body"); // https://github.com/tmpvar/jsdom/issues/277
assert.isTrue(span.order() === span);
}
}
});
suite.export(module);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment