<commit_before><!DOCTYPE html>
<html>
<head>
<title>Chat</title>
<script src="sockjs-0.3.4.js"></script>
<script src="stomp.js"></script>
<script src="jquery-2.1.0.min.js"></script>
<link href="chat.css" rel="stylesheet" type="text/css">
</head>
<body>
  <div>
    <div id="userList">
    </div>
  </div>
  <script type="text/javascript">
      var stompClient = null;
      var socket = null;
      var whoami = null;
      
      function connect() {
        socket = new SockJS('/hello');
        stompClient = Stomp.over(socket);
        stompClient.connect('', '', function(frame) {
          whoami = frame.headers['user-name'];
          console.log('Connected: ' + frame);
          stompClient.subscribe('/user/queue/greetings', function(greeting) {
            showGreeting(JSON.parse(greeting.body));
          });
          stompClient.subscribe('/topic/active', function(activeMembers) {
            showActive(activeMembers);
          });
        });
      }
      
      function showActive(activeMembers) {
        renderActive(activeMembers.body);
        stompClient.send('/app/activeUsers', {}, '');
      }
      
      function renderActive(activeMembers) {
        var previouslySelected = $('.user-selected').text();
        var usersWithPendingMessages = new Object();
        $.each($('.pending-messages'), function(index, value) {
          usersWithPendingMessages[value.id.substring(5)] = true; // strip the user-
        });
        var members = $.parseJSON(activeMembers);
        var userDiv = $('<div>', {id: 'users'});
        $.each(members, function(index, value) {
          if (value === whoami) {
            return true;
          }
          var userLine = $('<div>', {id: 'user-' + value});
          userLine.addClass('user-entry');
          if (previouslySelected === value) {
            userLine.addClass('user-selected');
          }
          else {
            userLine.addClass('user-unselected');
          }
          var userNameDisplay = $('<span>');
          userNameDisplay.html(value);
          userLine.append(userNameDisplay);
          userLine.click(function() {
            var foo = this;
            $('.chat-container').hide();
            $('.user-entry').removeClass('user-selected');
            $('.user-entry').addClass('user-unselected');
            userLine.removeClass('user-unselected');
            userLine.removeClass('pending-messages');
            userLine.addClass('user-selected');
            userLine.children('.newmessage').remove();
            var chatWindow = getChatWindow(value);
            chatWindow.show();
          });
          if (value in usersWithPendingMessages) {
            userLine.append(newMessageIcon());
            userLine.addClass('pending-messages');
          }
          userDiv.append(userLine);
        });
        $('#userList').html(userDiv);
      }
      
      function disconnect() {
        stompClient.disconnect();
        console.log("Disconnected");
      }

      function sendMessageTo(user) {
        var chatInput = '#input-chat-' + user;
        var message = $(chatInput).val();
        if (!message.length) {
          return;
        }
        stompClient.send("/app/hello", {}, JSON.stringify({
          'recipient': user,
          'message' : message
        }));
        $(chatInput).val('');
        $(chatInput).focus();
      }
      
      function getChatWindow(userName) {
        var elementId = 'chat-' + userName;
        var containerId = elementId + '-container';
        var selector = '#' + containerId;
        var inputId = 'input-' + elementId;
        if (!$(selector).length) {
          var chatContainer = $('<div>', {id: containerId, class: 'chat-container'});
          var chatWindow = $('<div>', {id: elementId, class: 'chat'});
          var chatInput = $('<textarea>', {id: inputId, type: 'text', class: 'chat-input', rows: '2', cols: '75', 
            placeholder: 'Enter a message.  Something deep and meaningful.  Something you can be proud of.'});
          var chatSubmit = $('<button>', {id: 'submit-' + elementId, type: 'submit', class: 'chat-submit'})
          chatSubmit.html('Send');
          
          chatInput.keypress(function(event) {
            if (event.which == 13) {
              var user = event.currentTarget.id.substring(11); // gets rid of 'input-chat-'
              event.preventDefault();
              sendMessageTo(user);
            }
          });
          
          chatSubmit.click(function(event) {
            var user = event.currentTarget.id.substring(12); // gets rid of 'submit-chat-'
            sendMessageTo(user);
          });
          
          chatContainer.append(chatWindow);
          chatContainer.append(chatInput);
          chatContainer.append(chatSubmit);
          
          $('body').append(chatContainer);
        }
        return $(selector);
      }
      
      function showGreeting(message) {
        var chatWindowTarget = (message.recipient === whoami) ? message.sender : message.recipient;
        var chatContainer = getChatWindow(chatWindowTarget);
        var chatWindow = chatContainer.children('.chat');
        var userDisplay = $('<span>', {class: (message.sender === whoami ? 'chat-sender' : 'chat-recipient')});
        userDisplay.html(message.sender + ' says: ');
        var messageDisplay = $('<span>');
        messageDisplay.html(message.message);
        //chatWindow.append('<span>' + message.sender + ' says: </span><span>' + message.message + '</span><br/>');
        chatWindow.append(userDisplay).append(messageDisplay).append('<br/>');
        chatWindow.animate({ scrollTop: chatWindow[0].scrollHeight}, 1);
        if (message.sender !== whoami) {
          var sendingUser = $('#user-' + message.sender);
          if (!sendingUser.hasClass('user-selected')) {
            sendingUser.append(newMessageIcon());
            sendingUser.addClass('pending-messages');
          }
        }
      }
      
      function newMessageIcon() {
        var newMessage = $('<span>', {class: 'newmessage'});
        newMessage.html('&#x2709;');
        return newMessage;
      }
      
      $(document).ready(function() {
        connect();
      });
  </script>
</body>
</html><commit_msg>fixed problem with double chat windows when none were previously opened and two people instant message<commit_after><!DOCTYPE html>
<html>
<head>
<title>Chat</title>
<script src="sockjs-0.3.4.js"></script>
<script src="stomp.js"></script>
<script src="jquery-2.1.0.min.js"></script>
<link href="chat.css" rel="stylesheet" type="text/css">
</head>
<body>
  <div>
    <div id="userList">
    </div>
  </div>
  <script type="text/javascript">
      var stompClient = null;
      var socket = null;
      var whoami = null;
      
      function connect() {
        socket = new SockJS('/hello');
        stompClient = Stomp.over(socket);
        stompClient.connect('', '', function(frame) {
          whoami = frame.headers['user-name'];
          console.log('Connected: ' + frame);
          stompClient.subscribe('/user/queue/greetings', function(greeting) {
            showGreeting(JSON.parse(greeting.body));
          });
          stompClient.subscribe('/topic/active', function(activeMembers) {
            showActive(activeMembers);
          });
        });
      }
      
      function showActive(activeMembers) {
        renderActive(activeMembers.body);
        stompClient.send('/app/activeUsers', {}, '');
      }
      
      function renderActive(activeMembers) {
        var previouslySelected = $('.user-selected').text();
        var usersWithPendingMessages = new Object();
        $.each($('.pending-messages'), function(index, value) {
          usersWithPendingMessages[value.id.substring(5)] = true; // strip the user-
        });
        var members = $.parseJSON(activeMembers);
        var userDiv = $('<div>', {id: 'users'});
        $.each(members, function(index, value) {
          if (value === whoami) {
            return true;
          }
          var userLine = $('<div>', {id: 'user-' + value});
          userLine.addClass('user-entry');
          if (previouslySelected === value) {
            userLine.addClass('user-selected');
          }
          else {
            userLine.addClass('user-unselected');
          }
          var userNameDisplay = $('<span>');
          userNameDisplay.html(value);
          userLine.append(userNameDisplay);
          userLine.click(function() {
            var foo = this;
            $('.chat-container').hide();
            $('.user-entry').removeClass('user-selected');
            $('.user-entry').addClass('user-unselected');
            userLine.removeClass('user-unselected');
            userLine.removeClass('pending-messages');
            userLine.addClass('user-selected');
            userLine.children('.newmessage').remove();
            var chatWindow = getChatWindow(value);
            chatWindow.show();
          });
          if (value in usersWithPendingMessages) {
            userLine.append(newMessageIcon());
            userLine.addClass('pending-messages');
          }
          userDiv.append(userLine);
        });
        $('#userList').html(userDiv);
      }
      
      function disconnect() {
        stompClient.disconnect();
        console.log("Disconnected");
      }

      function sendMessageTo(user) {
        var chatInput = '#input-chat-' + user;
        var message = $(chatInput).val();
        if (!message.length) {
          return;
        }
        stompClient.send("/app/hello", {}, JSON.stringify({
          'recipient': user,
          'message' : message
        }));
        $(chatInput).val('');
        $(chatInput).focus();
      }
      
      function getChatWindow(userName) {
        var existingChats = $('.chat-container');
        var elementId = 'chat-' + userName;
        var containerId = elementId + '-container';
        var selector = '#' + containerId;
        var inputId = 'input-' + elementId;
        if (!$(selector).length) {
          var chatContainer = $('<div>', {id: containerId, class: 'chat-container'});
          var chatWindow = $('<div>', {id: elementId, class: 'chat'});
          var chatInput = $('<textarea>', {id: inputId, type: 'text', class: 'chat-input', rows: '2', cols: '75', 
            placeholder: 'Enter a message.  Something deep and meaningful.  Something you can be proud of.'});
          var chatSubmit = $('<button>', {id: 'submit-' + elementId, type: 'submit', class: 'chat-submit'})
          chatSubmit.html('Send');
          
          chatInput.keypress(function(event) {
            if (event.which == 13) {
              var user = event.currentTarget.id.substring(11); // gets rid of 'input-chat-'
              event.preventDefault();
              sendMessageTo(user);
            }
          });
          
          chatSubmit.click(function(event) {
            var user = event.currentTarget.id.substring(12); // gets rid of 'submit-chat-'
            sendMessageTo(user);
          });
          
          chatContainer.append(chatWindow);
          chatContainer.append(chatInput);
          chatContainer.append(chatSubmit);
          
          if (existingChats.length) {
            chatContainer.hide();
          }
          
          $('body').append(chatContainer);
        }
        return $(selector);
      }
      
      function showGreeting(message) {
        var chatWindowTarget = (message.recipient === whoami) ? message.sender : message.recipient;
        var chatContainer = getChatWindow(chatWindowTarget);
        var chatWindow = chatContainer.children('.chat');
        var userDisplay = $('<span>', {class: (message.sender === whoami ? 'chat-sender' : 'chat-recipient')});
        userDisplay.html(message.sender + ' says: ');
        var messageDisplay = $('<span>');
        messageDisplay.html(message.message);
        //chatWindow.append('<span>' + message.sender + ' says: </span><span>' + message.message + '</span><br/>');
        chatWindow.append(userDisplay).append(messageDisplay).append('<br/>');
        chatWindow.animate({ scrollTop: chatWindow[0].scrollHeight}, 1);
        if (message.sender !== whoami) {
          var sendingUser = $('#user-' + message.sender);
          if (!sendingUser.hasClass('user-selected')) {
            sendingUser.append(newMessageIcon());
            sendingUser.addClass('pending-messages');
          }
        }
      }
      
      function newMessageIcon() {
        var newMessage = $('<span>', {class: 'newmessage'});
        newMessage.html('&#x2709;');
        return newMessage;
      }
      
      $(document).ready(function() {
        connect();
      });
  </script>
</body>
</html>
{'fraction_non_alphanumeric': 0.15915868780411238, 'fraction_numerical': 0.0034531470726730496, 'mean_word_length': 2.132497541789577, 'pattern_counts': {'":': 0, '<': 79, '<?xml version=': 0, '>': 79, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4506874', 'n_tokens_mistral': 3483, 'n_tokens_neox': 3471, 'n_words': 656}