summaryrefslogtreecommitdiff
path: root/public/js/app.js
diff options
context:
space:
mode:
Diffstat (limited to 'public/js/app.js')
-rw-r--r--public/js/app.js68
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 ();
+});