Browse files

initial board list implementation

  • Loading branch information...
1 parent 3588bbc commit 9fe9dc07292bcac2dfbf19a8fa2612fec217bdf6 @mnutt mnutt committed Dec 22, 2011
Showing with 122 additions and 2 deletions.
  1. +58 −0 lib/cube/client/boards.js
  2. +22 −0 lib/cube/client/home.html
  3. +42 −2 lib/cube/server/visualizer.js
View
58 lib/cube/client/boards.js
@@ -0,0 +1,58 @@
+cube.boards = function(url) {
+ var socket,
+ interval;
+
+ var boardList = document.getElementById("boards");
+
+ function message(message) {
+ var data = JSON.parse(message.data);
+
+ switch(data.type) {
+ case "add": {
+ var board = data.board;
+ var id = board._id.toString(36);
+
+ var li = document.createElement("li");
+
+ var selection = d3.select(li)
+ .attr("class", "board-item");
+
+
+ var link = selection.append("a")
+ .text(id)
+ .attr("href", "http://" + document.location.host + "/" + id);
+
+ var count = selection.append("span")
+ .text(" (" + board.pieces.length + ")");
+
+ boardList.appendChild(selection.node());
+ }
+ }
+ }
+
+ function reopen() {
+ if (socket) {
+ socket.close();
+ }
+ socket = new WebSocket(url);
+ socket.onopen = load;
+ socket.onmessage = message;
+ if (!interval) interval = setInterval(ping, 5000);
+ }
+
+ function ping() {
+ if (socket.readyState == 1) {
+ socket.send(JSON.stringify({type: "ping"}));
+ } else if (socket.readyState > 1) {
+ reopen();
+ }
+ }
+
+ function load() {
+ if (socket && socket.readyState == 1) {
+ socket.send(JSON.stringify({type: "load"}));
+ }
+ }
+
+ reopen();
+};
View
22 lib/cube/client/home.html
@@ -0,0 +1,22 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>Cube</title>
+ <script type="text/javascript" src="/d3/d3.js"></script>
+ <script type="text/javascript" src="/cube.js"></script>
+ <link type="text/css" rel="stylesheet" href="/cube.css"/>
+ </head>
+ <body>
+ <h1>Boards</h1>
+ <ol id="boards"></ol>
+
+ <a href="/new">New Board</a>
+
+ <script type="text/javascript">
+
+ var mode = "list",
+ boards = cube.boards("ws://" + location.host + "/boards");
+
+ </script>
+ </body>
+</html>
View
44 lib/cube/server/visualizer.js
@@ -4,10 +4,12 @@ var url = require("url"),
exports.register = function(db, endpoints) {
endpoints.ws.push(
- endpoint.exact("/board", viewBoard(db))
+ endpoint.exact("/board", viewBoard(db)),
+ endpoint.exact("/boards", listBoards(db))
);
endpoints.http.push(
- endpoint.exact("/", createBoard(db)),
+ endpoint.exact("/", home(db)),
+ endpoint.exact("/new", createBoard(db)),
endpoint.re(/^\/[0-9][0-9a-z]{5}(\/edit)?$/, loadBoard(db)),
endpoint.exact("/cube.js", endpoint.file(
resolve("start.js"),
@@ -19,6 +21,7 @@ exports.register = function(db, endpoints) {
resolve("palette.js"),
resolve("squares.js"),
resolve("board.js"),
+ resolve("boards.js"),
resolve("header.js"),
resolve("end.js")
)),
@@ -42,6 +45,14 @@ exports.register = function(db, endpoints) {
);
};
+function home(db) {
+ var file = endpoint.file(resolve("home.html"));
+
+ return function random(request, response) {
+ file(request, response);
+ };
+};
+
function createBoard(db) {
var boards, max = parseInt("9zzzzy", 36);
@@ -87,6 +98,35 @@ function loadBoard(db) {
};
}
+function listBoards(db) {
+ var boards;
+
+ db.collection("boards", function(error, collection) {
+ boards = collection;
+ });
+
+ function dispatch(request, callback) {
+ switch (request.type) {
+ case "load": load(request, callback); break;
+ default: callback({type: "error", status: 400}); break;
+ }
+ }
+
+ function load(request, callback) {
+ boards.find({}, function(error, cursor) {
+ if(error) { throw error; }
+
+ cursor.each(function(error, board) {
+ if(board && board.pieces) {
+ callback({type: "add", board: board});
+ }
+ });
+ });
+ }
+
+ return dispatch;
+}
+
function viewBoard(db) {
var boards,
boardsByCallback = {},

0 comments on commit 9fe9dc0

Please sign in to comment.