client/main.js
/* renders an autocomplete form given an element to append form to and the array
of form fields */
renders an autocomplete form given an element to append form to and the array
of form fields
renders an autocomplete form given an element to append form to and the array of form fields
function autocomplete(inp, arr) {
    var currentFocus;
    inp.addEventListener("input", function(e) {
        var a, b, i, val = this.value;
        closeAllLists();
        if (!val) { return false;}
        currentFocus = -1;
        a = document.createElement("DIV");
        a.setAttribute("id", this.id + "autocomplete-list");
        a.setAttribute("class", "autocomplete-items");
        this.parentNode.appendChild(a);
        for (i = 0; i < arr.length; i++) {
          if (arr[i].substr(0, val.length).toUpperCase() == val.toUpperCase()) {
            b = document.createElement("DIV");
            b.addEventListener("click", function() {
                lobbyName = b.textContent || "";
                enterLobby();
                closeAllLists();
            });
            b.innerHTML = "<strong>" + arr[i].substr(0, val.length) + "</strong>";
            b.innerHTML += arr[i].substr(val.length);
            b.innerHTML += "<input type='hidden' value='" + arr[i] + "'>";
                b.addEventListener("click", function(e) {
                inp.value = this.getElementsByTagName("input")[0].value;
                closeAllLists();
            });
            a.appendChild(b);
          }
        }
    });
    inp.addEventListener("keydown", function(e) {
        var x = document.getElementById(this.id + "autocomplete-list");
        if (x) x = x.getElementsByTagName("div");
        if (e.keyCode == 40) {
          currentFocus++;
          addActive(x);
        } else if (e.keyCode == 38) { 
          currentFocus--;
          addActive(x);
        } else if (e.keyCode == 13) {
          e.preventDefault();
          if (currentFocus > -1) {
            if (x) x[currentFocus].click();
            lobbyName = x[currentFocus].textContent || ""
            enterLobby()
          }
        }
    });
    function addActive(x) {
      if (!x) return false;
      removeActive(x);
      if (currentFocus >= x.length) currentFocus = 0;
      if (currentFocus < 0) currentFocus = (x.length - 1);
      x[currentFocus].classList.add("autocomplete-active");
    }
    function removeActive(x) {
      for (var i = 0; i < x.length; i++) {
        x[i].classList.remove("autocomplete-active");
      }
    }
    function closeAllLists(elmnt) {
      var x = document.getElementsByClassName("autocomplete-items");
      for (var i = 0; i < x.length; i++) {
        if (elmnt != x[i] && elmnt != inp) {
        x[i].parentNode.removeChild(x[i]);
      }
    }
  }
  document.addEventListener("click", function (e) {
      closeAllLists(e.target);
  });
}
function autocomplete ( inp , arr ) { var currentFocus ; inp . addEventListener ( "input" , function ( e ) { var a , b , i , val = this . value ; closeAllLists ( ) ; if ( ! val ) { return false ; } currentFocus = - 1 ; a = document . createElement ( "DIV" ) ; a . setAttribute ( "id" , this . id + "autocomplete-list" ) ; a . setAttribute ( "class" , "autocomplete-items" ) ; this . parentNode . appendChild ( a ) ; for ( i = 0 ; i < arr . length ; i ++ ) { if ( arr [ i ] . substr ( 0 , val . length ) . toUpperCase ( ) == val . toUpperCase ( ) ) { b = document . createElement ( "DIV" ) ; b . addEventListener ( "click" , function ( ) { lobbyName = b . textContent || "" ; enterLobby ( ) ; closeAllLists ( ) ; } ) ; b . innerHTML = "<strong>" + arr [ i ] . substr ( 0 , val . length ) + "</strong>" ; b . innerHTML += arr [ i ] . substr ( val . length ) ; b . innerHTML += "<input type='hidden' value='" + arr [ i ] + "'>" ; b . addEventListener ( "click" , function ( e ) { inp . value = this . getElementsByTagName ( "input" ) [ 0 ] . value ; closeAllLists ( ) ; } ) ; a . appendChild ( b ) ; } } } ) ; inp . addEventListener ( "keydown" , function ( e ) { var x = document . getElementById ( this . id + "autocomplete-list" ) ; if ( x ) x = x . getElementsByTagName ( "div" ) ; if ( e . keyCode == 40 ) { currentFocus ++ ; addActive ( x ) ; } else if ( e . keyCode == 38 ) { currentFocus -- ; addActive ( x ) ; } else if ( e . keyCode == 13 ) { e . preventDefault ( ) ; if ( currentFocus > - 1 ) { if ( x ) x [ currentFocus ] . click ( ) ; lobbyName = x [ currentFocus ] . textContent || "" enterLobby ( ) } } } ) ; function addActive ( x ) { if ( ! x ) return false ; removeActive ( x ) ; if ( currentFocus >= x . length ) currentFocus = 0 ; if ( currentFocus < 0 ) currentFocus = ( x . length - 1 ) ; x [ currentFocus ] . classList . add ( "autocomplete-active" ) ; } function removeActive ( x ) { for ( var i = 0 ; i < x . length ; i ++ ) { x [ i ] . classList . remove ( "autocomplete-active" ) ; } } function closeAllLists ( elmnt ) { var x = document . getElementsByClassName ( "autocomplete-items" ) ; for ( var i = 0 ; i < x . length ; i ++ ) { if ( elmnt != x [ i ] && elmnt != inp ) { x [ i ] . parentNode . removeChild ( x [ i ] ) ; } } } document . addEventListener ( "click" , function ( e ) { closeAllLists ( e . target ) ; } ) ; }
renders an autocomplete form given an element to append form to and the array
of form fields
renders an autocomplete form given an element to append form to and the array of form fields
/*the autocomplete function takes two arguments,
    the text field element and an array of possible autocompleted values:*/ /*execute a function when someone writes in the text field:*/ /*close any already open lists of autocompleted values*/ /*create a DIV element that will contain the items (values):*/ /*append the DIV element as a child of the autocomplete container:*/ /*for each item in the array...*/ /*check if the item starts with the same letters as the text field value:*/ /*create a DIV element for each matching element:*/ /*make the matching letters bold:*/ /*insert a input field that will hold the current array item's value:*/ /*execute a function when someone clicks on the item value (DIV element):*/ /*insert the value for the autocomplete text field:*/ /*close the list of autocompleted values,
                (or any other open lists of autocompleted values:*/ /*execute a function presses a key on the keyboard:*/ /*If the arrow DOWN key is pressed,
          increase the currentFocus variable:*/ /*and and make the current item more visible:*/ //up /*If the arrow UP key is pressed,
          decrease the currentFocus variable:*/ /*and and make the current item more visible:*/ /*If the ENTER key is pressed, prevent the form from being submitted,*/ /*and simulate a click on the "active" item:*/ // Get text from field and submit /*a function to classify an item as "active":*/ /*start by removing the "active" class on all items:*/ /*add class "autocomplete-active":*/ /*a function to remove the "active" class from all autocomplete items:*/ /*close all autocomplete lists in the document,
      except the one passed as an argument:*/ /*execute a function when someone clicks in the document:*/