Skip to content

Commit

Permalink
Merge branch 'order'
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Dec 9, 2011
2 parents fe671a7 + b3848af commit 05d871b
Show file tree
Hide file tree
Showing 16 changed files with 137 additions and 30 deletions.
1 change: 1 addition & 0 deletions Makefile
Expand Up @@ -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 \
Expand Down
7 changes: 7 additions & 0 deletions d3.geo.js
Expand Up @@ -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,
Expand All @@ -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]);
Expand Down
2 changes: 1 addition & 1 deletion d3.geo.min.js

Large diffs are not rendered by default.

38 changes: 26 additions & 12 deletions d3.js
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions d3.min.js

Large diffs are not rendered by default.

25 changes: 25 additions & 0 deletions examples/hello-world/hello-order.html
@@ -0,0 +1,25 @@
<!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>
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "d3",
"version": "2.6.1",
"version": "2.7.0",
"description": "A small, free JavaScript library for manipulating documents based on data.",
"keywords": [
"dom",
Expand Down
2 changes: 1 addition & 1 deletion src/core/core.js
@@ -1 +1 @@
d3 = {version: "2.6.1"}; // semver
d3 = {version: "2.7.0"}; // semver
9 changes: 8 additions & 1 deletion src/core/selection-filter.js
@@ -1,10 +1,11 @@
// 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;
Expand All @@ -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);
};
}
11 changes: 11 additions & 0 deletions src/core/selection-order.js
@@ -0,0 +1,11 @@
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;
};
2 changes: 1 addition & 1 deletion src/core/selection-root.js
@@ -1,6 +1,6 @@
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)
Expand Down
12 changes: 2 additions & 10 deletions src/core/selection-sort.js
@@ -1,15 +1,7 @@
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) {
Expand Down
6 changes: 5 additions & 1 deletion src/core/selection.js
Expand Up @@ -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 = [];
Expand Down
7 changes: 7 additions & 0 deletions src/geo/bounds.js
Expand Up @@ -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,
Expand All @@ -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]);
Expand Down
7 changes: 7 additions & 0 deletions test/core/selection-filter-test.js
Expand Up @@ -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);
}
Expand Down
32 changes: 32 additions & 0 deletions test/core/selection-order-test.js
@@ -0,0 +1,32 @@
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);

0 comments on commit 05d871b

Please sign in to comment.