Multi-value map support for property.

This commit is contained in:
Mike Bostock 2011-08-28 17:07:55 -07:00
Родитель 5c1dd6d904
Коммит ce8852b9b4
4 изменённых файлов: 81 добавлений и 12 удалений

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

@ -1503,7 +1503,22 @@
return value == null ? styleNull : typeof value === "function" ? styleFunction : styleConstant;
}
d3_selectionPrototype.property = function(name, value) {
if (arguments.length < 2) return this.node()[name];
if (arguments.length < 2) {
if ((value = typeof name) === "object") {
for (value in name) this.property(value, name[value]);
return this;
}
if (value === "function") {
return this.each(function() {
var x = name.apply(this, arguments);
for (value in x) d3_selection_property(value, x[value]).apply(this, arguments);
});
}
return this.node()[name];
}
return this.each(d3_selection_property(name, value));
};
function d3_selection_property(name, value) {
function propertyNull() {
delete this[name];
}
@ -1514,8 +1529,8 @@
var x = value.apply(this, arguments);
if (x == null) delete this[name]; else this[name] = x;
}
return this.each(value == null ? propertyNull : typeof value === "function" ? propertyFunction : propertyConstant);
};
return value == null ? propertyNull : typeof value === "function" ? propertyFunction : propertyConstant;
}
d3_selectionPrototype.text = function(value) {
return arguments.length < 1 ? this.node().textContent : this.each(typeof value === "function" ? function() {
var v = value.apply(this, arguments);

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

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

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

@ -1,7 +1,31 @@
d3_selectionPrototype.property = function(name, value) {
if (arguments.length < 2) {
// If no value is specified, return the first value.
if (arguments.length < 2) return this.node()[name];
// map:object
if ((value = typeof name) === "object") {
for (value in name) this.property(value, name[value]);
return this;
}
// map:function
if (value === "function") {
return this.each(function() {
var x = name.apply(this, arguments);
for (value in x) d3_selection_property(value, x[value]).apply(this, arguments);
});
}
// name:string
return this.node()[name];
}
// name:string, value:constant
// name:string, value:null
// name:string, value:function
return this.each(d3_selection_property(name, value));
};
function d3_selection_property(name, value) {
function propertyNull() {
delete this[name];
@ -17,7 +41,7 @@ d3_selectionPrototype.property = function(name, value) {
else this[name] = x;
}
return this.each(value == null
return value == null
? propertyNull : (typeof value === "function"
? propertyFunction : propertyConstant));
};
? propertyFunction : propertyConstant);
}

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

@ -22,6 +22,21 @@ suite.addBatch({
body.property("bgcolor", function() { return "orange"; });
assert.equal(document.body.bgcolor, "orange");
},
"sets properties as a map of constants": function(body) {
body.property({bgcolor: "purple", opacity: .41});
assert.equal(document.body.bgcolor, "purple");
assert.equal(document.body.opacity, .41);
},
"sets properties as a map of functions": function(body) {
body.data(["cyan"]).property({bgcolor: String, opacity: function(d, i) { return i; }});
assert.equal(document.body.bgcolor, "cyan");
assert.equal(document.body.opacity, 0);
},
"sets properties as a function that returns a map": function(body) {
body.data([.14]).property(function(d, i) { return {bgcolor: "black", opacity: d}; });
assert.equal(document.body.bgcolor, "black");
assert.equal(document.body.opacity, .14);
},
"gets a property value": function(body) {
document.body.bgcolor = "yellow";
assert.equal(body.property("bgcolor"), "yellow");
@ -34,6 +49,21 @@ suite.addBatch({
body.property("bgcolor", "yellow").property("bgcolor", function() { return null });
assert.isFalse("bgcolor" in document.body);
},
"removes properties as a map of nulls": function(body) {
document.body.bgcolor = "red";
body.property({bgcolor: null});
assert.isFalse("bgcolor" in document.body);
},
"removes properties as a map of functions that return null": function(body) {
document.body.bgcolor = "red";
body.property({bgcolor: function() {}});
assert.isFalse("bgcolor" in document.body);
},
"removes properties as a function that returns a map of nulls": function(body) {
document.body.bgcolor = "red";
body.property(function() { return {bgcolor: null}; });
assert.isFalse("bgcolor" in document.body);
},
"returns the current selection": function(body) {
assert.isTrue(body.property("bgcolor", "yellow") === body);
}