diff options
| author | Jules Laplace <jules@okfoc.us> | 2012-09-24 16:22:07 -0400 |
|---|---|---|
| committer | Jules Laplace <jules@okfoc.us> | 2012-09-24 16:22:07 -0400 |
| commit | 686106d544ecc3b6ffd4db2b665d3bc879a58d8c (patch) | |
| tree | a5b5e50237cef70e12f0745371896e96f5f6d578 /public/js/app.js | |
ok
Diffstat (limited to 'public/js/app.js')
| -rw-r--r-- | public/js/app.js | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/public/js/app.js b/public/js/app.js new file mode 100644 index 0000000..caca9f8 --- /dev/null +++ b/public/js/app.js @@ -0,0 +1,68 @@ +function App(){ + var app = this; + var socket = io.connect(window.location.hostname); + + // Initialize all the modules of this app + function init(){ + app.user = new User (app); + app.chat = new Chat (app); + app.grid = new Grid (app); + bind(); + } + + // Bind events that should be handled by the app generically + function bind(){ + $(window).on({ + blur: function(){ + $("#message").blur(); + }, + focus: function(){ + // $message.focus(); + }, + keydown: function(e) { + // console.log(e.keyCode); + switch(e.keyCode) { + case 32: // space + if (! app.chat.focused) { + app.grid.toggle(); + return false; + } + break; + case 9: // tab + $("#message").focus(); + return false; + } + } + }); + } + + // Send a JSON object to the other clients + app.send = function(ns, message){ + socket.emit(ns, JSON.stringify(message)); + } + + // Listen for events from other clients + app.receive = function(ns, callback){ + // socket.on(ns, callback); + socket.on(ns, function(json){ + var data; + console.log(ns, json); + try { + console.log(typeof json); + data = (typeof json === "string") ? JSON.parse(json) : json; + } + catch (e) { + console.log("ERROR PARSING JSON"); + data = {}; + } + callback(data); + }); + } + + init(); +} + +// Load the app when the DOM is ready +$(function(){ + var app = new App (); +}); |
