Skip to content

Commit

Permalink
initial development
Browse files Browse the repository at this point in the history
  • Loading branch information
couchand committed Nov 2, 2017
0 parents commit 8025bfd
Show file tree
Hide file tree
Showing 15 changed files with 1,191 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
/build/
/node_modules/
9 changes: 9 additions & 0 deletions LICENSE
@@ -0,0 +1,9 @@
The MIT License (MIT)

Copyright (c) 2017 Andrew Couch

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
81 changes: 81 additions & 0 deletions README.md
@@ -0,0 +1,81 @@
# d3-redux

[Redux](https://github.com/reactjs/redux) offers a simple yet powerful
state management solution, but it's not immediately obvious how to
idiomatically make use of it from a complex [D3](https://d3js.org/)
application. With a few judicious extensions to d3-selection,
*d3-redux* makes wiring up state changes simple and easy, the D3 way.

Provide the Redux store at the root of your application:

```js
const initialState = {
todos: [
{ id: 1, title: "Write amazing code", completed: false }
]
};
const store = Redux.createStore(rootReducer, initialState);

const app = d3.select("#app")
.provide(store);
```

Then in some nested component make use of the store's state, just as
you would any other data join:

```js
let todos = main.select("ul")
.selectAll("li")
.dataFromState(state => state.todos);

todos = todos.enter()
.append("li")
.merge(todos);
```

Finally, attach handlers that will dispatch actions back to the store:

```js
const destroyTodo = id => ({ type: "DESTROY", payload: { id } });

trashCan.dispatchOn("click", d => destroyTodo(d.id));
```

## Installing

If you use NPM, `npm install d3-redux`. Otherwise, download the
[latest release](https://github.com/couchand/d3-redux/releases/latest).

## API Reference

All methods return the current selection, to facilitate D3's idiomatic
method chaining style.

<a href="#selection_provide" name="selection_provide">#</a> <i>selection</i>.<b>provide</b>(<i>store</i>)

Provides the Redux *store* to nested components.

<a href="#selection_dataFromState" name="selection_dataFromState">#</a> <i>selection</i>.<b>dataFromState</b>(<i>selector</i>[, <i>key</i>])

Calls the *selector*, passing in the current state from the previously-
provided store, and forwards the result (as well as the *key*, if
provided) to [`selection.data()`](https://github.com/d3/d3-selection#selection_data).
Computes a data join. _Note:_ this is a "selector" in the Redux and
[reselect](https://github.com/reactjs/reselect) sense, not in the D3
one - it is a unary function of the store's state.

<a href="#selection_datumFromState" name="selection_datumFromState">#</a> <i>selection</i>.<b>datumFromState</b>(<i>selector</i>)

Calls the *selector*, passing in the current state from the previously-
provided store, and forwards the result to
[`selection.datum()`](https://github.com/d3/d3-selection#selection_datum).
Does not computes a data join. _Note:_ this is a "selector" in the
Redux and [reselect](https://github.com/reactjs/reselect) sense, not
in the D3 one - it is a unary function of the store's state.

<a href="#selection_dispatchOn" name="selection_dispatchOn">#</a> <i>selection</i>.<b>dispatchOn</b>(<i>typenames</i>, <i>actionCreator</i>[, <i>capture</i>])

Attaches an event listener for the given *typenames* using
[`selection.on()`](https://github.com/d3/d3-selection#selection_on).
The return value of *actionCreator* is forwarded on to dispatch from
the previously-provided store.
10 changes: 10 additions & 0 deletions index.js
@@ -0,0 +1,10 @@
import { selection } from "d3-selection";
import provide from "./src/provide";
import dataFromState from "./src/dataFromState";
import datumFromState from "./src/datumFromState";
import dispatchOn from "./src/dispatchOn";

selection.prototype.provide = provide;
selection.prototype.dataFromState = dataFromState;
selection.prototype.datumFromState = datumFromState;
selection.prototype.dispatchOn = dispatchOn;
33 changes: 33 additions & 0 deletions package.json
@@ -0,0 +1,33 @@
{
"name": "d3-redux",
"version": "0.0.1",
"description": "D3.js bindings for Redux",
"keywords": [
"d3",
"d3-module",
"redux"
],
"license": "MIT",
"main": "build/d3-redux.js",
"jsnext:main": "index",
"homepage": "https://github.com/couchand/d3-redux",
"repository": {
"type": "git",
"url": "https://github.com/couchand/d3-redux.git"
},
"scripts": {
"pretest": "rm -rf build && mkdir build && rollup -f umd -n d3 -g d3-selection:d3 -o build/d3-redux.js -- index.js",
"test": "tape 'test/**/*-test.js'",
"prepublish": "npm run test && uglifyjs build/d3-redux.js -c -m -o build/d3-redux.min.js",
"postpublish": "zip -j build/d3-redux.zip -- LICENSE README.md build/d3-redux.js build/d3-redux.min.js"
},
"devDependencies": {
"jsdom": "11",
"rollup": "0.27",
"tape": "4",
"uglify-js": "2"
},
"dependencies": {
"d3-selection": "^1.1.0"
}
}
8 changes: 8 additions & 0 deletions src/dataFromState.js
@@ -0,0 +1,8 @@
import storeLocal from './local';

export default function (selector, key) {
return this.data(function () {
var store = storeLocal.get(this);
return selector.call(this, store.getState());
}, key);
}
10 changes: 10 additions & 0 deletions src/datumFromState.js
@@ -0,0 +1,10 @@
import { select } from 'd3-selection';
import storeLocal from './local';

export default function (selector) {
return this.each(function () {
var store = storeLocal.get(this);
select(this)
.datum(selector.call(this, store.getState()));
});
}
12 changes: 12 additions & 0 deletions src/dispatchOn.js
@@ -0,0 +1,12 @@
import { select } from 'd3-selection';
import storeLocal from './local';

export default function (event, handler, capture) {
return this.on(event, function (d, i, g) {
var action = handler.call(this, d, i, g);
if (action) {
var store = storeLocal.get(this);
store.dispatch(action);
}
}, capture);
}
3 changes: 3 additions & 0 deletions src/local.js
@@ -0,0 +1,3 @@
import { local } from 'd3-selection';

export default local();
5 changes: 5 additions & 0 deletions src/provide.js
@@ -0,0 +1,5 @@
import storeLocal from './local';

export default function (store) {
return this.property(storeLocal, store);
}
60 changes: 60 additions & 0 deletions test/dataFromState-test.js
@@ -0,0 +1,60 @@
var tape = require('tape');
var jsdom = require('./jsdom');
var d3 = require('d3-selection');

require('../');

function storeOf(state) {
return {
getState: function () { return state; }
};
}

tape('selection.dataFromState(selector) gets state from the provided store', function (test) {
var state = { foo: [0, 1, 2] };
var document = jsdom('');
var sel = d3.select(document.body)
.provide(storeOf(state))
.selectAll('div')
.dataFromState(function (d) { return d.foo; })
.enter().append('div');
test.equal(sel.size(), state.foo.length);
sel.each(function (d, i) { test.equal(d, i) });
test.end();
});

tape('selection.dataFromState(selector) calls the selector in the context of the selected element', function (test) {
var me;
var document = jsdom('<div></div>');
var el = document.querySelector('div');
d3.select(el)
.provide(storeOf([]))
.selectAll('.child')
.dataFromState(function () { me = this; return []; });
test.equal(me, el);
test.end();
});

tape('selection.data(selector, key) joins data to element using the computed keys', function(test) {
var body = jsdom('<node id="one"></node><node id="two"></node><node id="three"></node>').body,
one = body.querySelector('#one'),
two = body.querySelector('#two'),
three = body.querySelector('#three'),
selection = d3.select(body)
.provide(storeOf({}))
.selectAll('node')
.dataFromState(function () { return ['one', 'four', 'three']; }, function(d) { return d || this.id; });
test.deepEqual(selection, {
_groups: [[one,, three]],
_parents: [body],
_enter: [[, {
__data__: 'four',
_next: three,
_parent: body,
namespaceURI: 'http://www.w3.org/1999/xhtml',
ownerDocument: body.ownerDocument
}, ]],
_exit: [[, two, ]]
});
test.end();
});
34 changes: 34 additions & 0 deletions test/datumFromState-test.js
@@ -0,0 +1,34 @@
var tape = require('tape');
var jsdom = require('./jsdom');
var d3 = require('d3-selection');

require('../');

function storeOf(state) {
return {
getState: function () { return state; }
};
}

tape('selection.datumFromState(selector) gets state from the provided store', function (test) {
var state = { foo: {} };
var document = jsdom('<div></div>');
var sel = d3.select(document.body)
.provide(storeOf(state))
.select('div')
.datumFromState(function (d) { return d.foo; });
test.equal(sel.datum(), state.foo);
test.end();
});

tape('selection.datumFromState(selector) calls the selector in the context of the selected element', function (test) {
var me;
var document = jsdom('<div></div>');
var el = document.querySelector('div');
d3.select(document.body)
.provide(storeOf({}))
.select('div')
.datumFromState(function () { me = this; });
test.equal(me, el);
test.end();
});
58 changes: 58 additions & 0 deletions test/dispatchOn-test.js
@@ -0,0 +1,58 @@
var tape = require('tape');
var jsdom = require('./jsdom');
var d3 = require('d3-selection');

require('../');

tape('selection.dispatchOn(type, listener) dispatches on event', function(test) {
var document = jsdom('<div></div>');
var div = document.querySelector('div');
var actions = [];
var store = {
dispatch: function (action) { actions.push(action);}
};
var sel = d3.select(document.body)
.provide(store)
.select('div')
.dispatchOn('click', function () { return 42; });
sel.dispatch('click');

test.deepEqual(actions, [42]);
test.end();
});

tape('selection.dispatchOn(type, listener, capture) passes along the capture flag', function (test) {
var result;
var sel = d3.select({
addEventListener: function(type, listener, capture) { result = capture; }
});
test.equal(sel.dispatchOn("click", function() {}, true), sel);
test.equal(result, true);
test.end();
});

tape('selection.dispatchOn(type, listener) passes the listener data, index and group', function(test) {
var document = jsdom('<parent id="one"><child id="three"></child><child id="four"></child></parent><parent id="two"><child id="five"></child></parent>'),
one = document.querySelector('#one'),
two = document.querySelector('#two'),
three = document.querySelector('#three'),
four = document.querySelector('#four'),
five = document.querySelector('#five'),
results = [];

var selection = d3.selectAll([one, two])
.provide({ dispatch: function () {} })
.datum(function(d, i) { return 'parent-' + i; })
.selectAll('child')
.data(function(d, i) { return [0, 1].map(function(j) { return 'child-' + i + '-' + j; }); })
.dispatchOn('foo', function(d, i, nodes) { results.push([this, d, i, nodes]); });

test.deepEqual(results, []);
selection.dispatch('foo');
test.deepEqual(results, [
[three, 'child-0-0', 0, [three, four]],
[four, 'child-0-1', 1, [three, four]],
[five, 'child-1-0', 0, [five, ]]
]);
test.end();
});
5 changes: 5 additions & 0 deletions test/jsdom.js
@@ -0,0 +1,5 @@
var jsdom = require('jsdom');

module.exports = function (html) {
return (new jsdom.JSDOM(html)).window.document;
};

0 comments on commit 8025bfd

Please sign in to comment.