summaryrefslogtreecommitdiff
path: root/public/js/app.js
blob: caca9f86651aafd450adcc869382eaf663a1fc43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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 ();
});