Skip to content

Commit

Permalink
Fixes related to d3.dispatch API change.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Nov 19, 2011
1 parent 1b8779e commit 70e9ee6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 42 deletions.
42 changes: 18 additions & 24 deletions lib/cube/client/board.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,22 @@ cube.board = function(url, id) {
d3.select(svg)
.attr("class", "board");

event.size.add(resize);
event.squareSize.add(resize);
event.squareRadius.add(resize);
event.padding.add(resize);
event.on("size.board", resize);
event.on("squareSize.board", resize);
event.on("squareRadius.board", resize);
event.on("padding.board", resize);

function message(message) {
var e = JSON.parse(message.data);
switch (e.type) {
case "view": {
event.view.dispatch.call(board, e);
event.view.call(board, e);
break;
}
case "add": {
var piece = board.add(cube.piece.type[e.piece.type])
.fromJSON(e.piece)
.off("move", add)
.on("move", move);
.on("move.board", move);

d3.select(piece.node())
.style("opacity", 1e-6)
Expand All @@ -55,12 +54,12 @@ cube.board = function(url, id) {
pieces.some(function(piece) {
if (piece.id == e.piece.id) {
piece
.off("move", move)
.on("move.board", null)
.transition(d3.transition().duration(500))
.fromJSON(e.piece);

// Renable events after transition starts.
d3.timer(function() { piece.on("move", move); }, 250);
d3.timer(function() { piece.on("move.board", move); }, 250);
return true;
}
});
Expand All @@ -70,7 +69,7 @@ cube.board = function(url, id) {
pieces.some(function(piece) {
if (piece.id == e.piece.id) {
piece
.off("move", move)
.on("move.board", null)
.transition(d3.transition().duration(500))
.size(e.piece.size)
.position(e.piece.position);
Expand All @@ -79,7 +78,7 @@ cube.board = function(url, id) {
svg.parentNode.appendChild(piece.node());

// Renable events after transition starts.
d3.timer(function() { piece.on("move", move); }, 250);
d3.timer(function() { piece.on("move.board", move); }, 250);
return true;
}
});
Expand Down Expand Up @@ -125,7 +124,7 @@ cube.board = function(url, id) {
// A one-time listener to send an add event on mouseup.
function add() {
socket.send(JSON.stringify({type: "add", id: id, piece: this}));
this.off("move", add).on("move", move);
this.on("move.board", move);
}

function move() {
Expand Down Expand Up @@ -153,50 +152,45 @@ cube.board = function(url, id) {
};

board.on = function(type, listener) {
event[type].add(listener);
return board;
};

board.off = function(type, listener) {
event[type].remove(listener);
event.on(type, listener);
return board;
};

board.size = function(x) {
if (!arguments.length) return size;
event.size.dispatch.call(board, size = x);
event.size.call(board, size = x);
return board;
};

board.squareSize = function(x) {
if (!arguments.length) return squareSize;
event.squareSize.dispatch.call(board, squareSize = x);
event.squareSize.call(board, squareSize = x);
return board;
};

board.squareRadius = function(x) {
if (!arguments.length) return squareRadius;
event.squareRadius.dispatch.call(board, squareRadius = x);
event.squareRadius.call(board, squareRadius = x);
return board;
};

board.padding = function(x) {
if (!arguments.length) return padding;
event.padding.dispatch.call(board, padding = x);
event.padding.call(board, padding = x);
return board;
};

board.add = function(type) {
var piece = type(board);
piece.id = ++pieceId;
piece.on("move", add).on("edit", edit);
piece.on("move.board", add).on("edit.board", edit);
svg.parentNode.appendChild(piece.node());
pieces.push(piece);
return piece;
};

board.remove = function(piece, silent) {
piece.off("move", move).off("edit", edit);
piece.on("move.board", null).on("edit.board", null);
if (silent) {
d3.select(piece.node())
.style("opacity", 1)
Expand Down
29 changes: 12 additions & 17 deletions lib/cube/client/piece.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ cube.piece = function(board) {
}

board
.on("padding", resize)
.on("squareSize", resize);
.on("padding.piece", resize)
.on("squareSize.piece", resize);

event.position.add(resize);
event.size.add(resize);
event.deserialize.add(deserialize);
event.on("position.piece", resize);
event.on("size.piece", resize);
event.on("deserialize.piece", deserialize);

function resize() {
var squareSize = board.squareSize(),
Expand Down Expand Up @@ -76,18 +76,13 @@ cube.piece = function(board) {
};

piece.on = function(type, listener) {
event[type].add(listener);
return piece;
};

piece.off = function(type, listener) {
event[type].remove(listener);
event.on(type, listener);
return piece;
};

piece.size = function(x) {
if (!arguments.length) return size;
event.size.dispatch.call(piece, size = x);
event.size.call(piece, size = x);
return piece;
};

Expand All @@ -101,23 +96,23 @@ cube.piece = function(board) {

piece.position = function(x) {
if (!arguments.length) return position;
event.position.dispatch.call(piece, position = x);
event.position.call(piece, position = x);
return piece;
};

piece.toJSON = function() {
var x = {id: piece.id, size: size, position: position};
event.serialize.dispatch.call(piece, x);
event.serialize.call(piece, x);
return x;
};

piece.fromJSON = function(x) {
event.deserialize.dispatch.call(piece, x);
event.deserialize.call(piece, x);
return piece;
};

piece.edit = function() {
event.edit.dispatch.call(piece);
event.edit.call(piece);
return piece;
};

Expand Down Expand Up @@ -149,7 +144,7 @@ cube.piece = function(board) {
} else {
transition = x.select(function() { return div; });
d3.timer(function() {
event.move.dispatch.call(piece);
event.move.call(piece);
return transition = selection;
});
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cube",
"version": "0.0.10",
"version": "0.0.11",
"description": "A system for time series visualization using MongoDB, Node and D3.",
"keywords": ["time series", "visualization"],
"homepage": "http://square.github.com/cube/",
Expand Down

0 comments on commit 70e9ee6

Please sign in to comment.