Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 3d32022567
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 180 lines (179 sloc) 5.299 kb
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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>Bejesus IRC Channel</title>
  <link rel="stylesheet" href="css/irc.css" >
</head>
<body onload="body_loaded();">
  <div id="top_section">
    <div id="nick_list">
      <ul id="nick_ul">
      </ul>
    </div>
    <table id="chat_body">
    </table>
  </div>
  <form id="chat_bar" onsubmit="handleSubmit(); return false;">
    <input type="text" id="text_input" size="80">
    <button type="submit">Send</button>
  </form>

<div id="js">
<script src="/socket.io/socket.io.js"></script>
  <script>
    var sock = null;
    var rv = null;
    var nickname = null;
    var chatBody = null;
    var textInput = null;
    var nick_ul = null;
    var nick_lis = {};
    var body_loaded = function () {
      sock = new io.Socket();
      sock.on('message', handleMessage);
      sock.connect();
      chatBody = document.getElementById('chat_body');
      textInput = document.getElementById('text_input');
      nick_ul = document.getElementById('nick_ul');
    };
    var getNickname = function () {
      var name = prompt("Please enter your nickname:", "");
      switch (name) {
        case "":
          alert("You did not input a nickname, please reload if you wish to connect.");
          sock.disconnect();
          return null;
          break;
        case null:
          alert("Login cancelled, please reload if you wish to connect.");
          sock.disconnect();
          return null;
          break;
        default:
          return name;
          break;
      }
    };
    var appendMessage = function (from, message, s) {
      var row = document.createElement('tr');
      if (typeof s != 'undefined' && s == true) {
        row.className = 'me';
      }
      row.innerHTML = ''
          + '<th class="author">' + from + '</th>'
          + '<td class="msg">' + message + '</td>';
      chatBody.appendChild(row);
      scrollBody();
    };
    var appendEvent = function (from, event, s) {
      var row = document.createElement('tr');
      if (typeof s != 'undefined' && s == true) {
        row.className = 'me';
      }
      switch (event) {
        case "join":
          var message = "<u>joined the channel</u>";
          break;
        case "quit":
          var message = "<u>left the channel</u>";
          break;
        default:
          var message = "<u>unknown event type oO</u>";
          break;
      }
      row.innerHTML = ''
          + '<th class="author">' + from + '</th>'
          + '<td class="msg">' + message + '</td>';
      chatBody.appendChild(row);
      scrollBody();
    };
    var addNickToList = function (nick) {
      if (!nick_lis.hasOwnProperty(nick)) {
        var li = document.createElement('li');
        li.value = nick;
        li.textContent = nick;
        nick_lis[nick] = li;
        nick_ul.appendChild(li);
        sortList(nick_ul);
      }
    };
    var delNickFromList = function (nick) {
      if (nick_lis.hasOwnProperty(nick)) {
        nick_ul.removeChild(nick_lis[nick]);
        delete nick_lis[nick];
        sortList(nick_ul);
      }
    };
    var handleMessage = function (data) {
      var obj = JSON.parse(data);
      if (nickname === null) {
        var tmp = getNickname();
        if (tmp !== null) {
          nickname = tmp;
          rv = sock.send(JSON.stringify({ nickname: nickname }));
        }
      } else {
        if (obj.hasOwnProperty('messagetype')) {
          var s = (obj.from == nickname) ? true : false;
          switch (obj.messagetype) {
            case "message":
              appendMessage(obj.from, obj.message, false);
              break;
            case "join":
              appendEvent(obj.from, obj.messagetype, s);
              addNickToList(obj.from);
              break;
            case "quit":
              appendEvent(obj.from, obj.messagetype, s);
              delNickFromList(obj.from);
              break;
            default:
              alert(data);
              break;
          }
        } else {
          alert(data);
        }
      }
    };
    var sendMessage = function () {
      appendMessage(nickname, textInput.value, true);
      sock.send(JSON.stringify({
        messagetype: "message",
        message: textInput.value
      }));
      textInput.value = "";
    };
    var handleSubmit = function (e) {
      if (textInput.value !== '') {
        sendMessage();
      }
    };
    var scrollBody = function() {
      document.body.scrollTop = document.body.clientHeight;
    };
    var sortList = function (oUl) {
      for(var i in oUl.childNodes) {
        var x = oUl.childNodes[i];
        for(var j in oUl.childNodes) {
       var y = oUl.childNodes[j];
       if((x.innerText != 'undefined' || y.innerText != 'undefined')
       && x.innerText > y.innerText) {
       if(oUl.firstChild != x)
       oUl.insertBefore(y, x);
       }
       }
      }
    }
    /* Define innerText for Mozilla based browsers */
    if((typeof HTMLElement != 'undefined') && (HTMLElement.prototype.__defineGetter__ != 'undefined')) {
     HTMLElement.prototype.__defineGetter__("innerText", function () {
       var r = this.ownerDocument.createRange();
       r.selectNodeContents(this);
       return r.toString();
     });
    }
  </script>
</div>
</body>
</html>
Something went wrong with that request. Please try again.