| 1 | import {namespace} from "d3-selection";
|
|---|
| 2 |
|
|---|
| 3 | function attrInterpolate(name, i) {
|
|---|
| 4 | return function(t) {
|
|---|
| 5 | this.setAttribute(name, i.call(this, t));
|
|---|
| 6 | };
|
|---|
| 7 | }
|
|---|
| 8 |
|
|---|
| 9 | function attrInterpolateNS(fullname, i) {
|
|---|
| 10 | return function(t) {
|
|---|
| 11 | this.setAttributeNS(fullname.space, fullname.local, i.call(this, t));
|
|---|
| 12 | };
|
|---|
| 13 | }
|
|---|
| 14 |
|
|---|
| 15 | function attrTweenNS(fullname, value) {
|
|---|
| 16 | var t0, i0;
|
|---|
| 17 | function tween() {
|
|---|
| 18 | var i = value.apply(this, arguments);
|
|---|
| 19 | if (i !== i0) t0 = (i0 = i) && attrInterpolateNS(fullname, i);
|
|---|
| 20 | return t0;
|
|---|
| 21 | }
|
|---|
| 22 | tween._value = value;
|
|---|
| 23 | return tween;
|
|---|
| 24 | }
|
|---|
| 25 |
|
|---|
| 26 | function attrTween(name, value) {
|
|---|
| 27 | var t0, i0;
|
|---|
| 28 | function tween() {
|
|---|
| 29 | var i = value.apply(this, arguments);
|
|---|
| 30 | if (i !== i0) t0 = (i0 = i) && attrInterpolate(name, i);
|
|---|
| 31 | return t0;
|
|---|
| 32 | }
|
|---|
| 33 | tween._value = value;
|
|---|
| 34 | return tween;
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | export default function(name, value) {
|
|---|
| 38 | var key = "attr." + name;
|
|---|
| 39 | if (arguments.length < 2) return (key = this.tween(key)) && key._value;
|
|---|
| 40 | if (value == null) return this.tween(key, null);
|
|---|
| 41 | if (typeof value !== "function") throw new Error;
|
|---|
| 42 | var fullname = namespace(name);
|
|---|
| 43 | return this.tween(key, (fullname.local ? attrTweenNS : attrTween)(fullname, value));
|
|---|
| 44 | }
|
|---|