/* * uvm_directory_find_people.js * Code for the "Search Directory" option on uvm.edu * * Dillon Beliveau * * Works with both MagicScript and Drupal */ jQuery(document).ready(function($) { var people_input_suggestions = $("
").attr("id","people-input-suggestions"); people_input_suggestions.css({ "postion":"absolute", "display":"none", }); $("body").append(people_input_suggestions); $("div.people-clear-button").click(function() { $("input.people-input").val(""); do_update(); }); $("input.people-input").keydown(on_people_input_modified); $("input.people-input").click(on_people_input_click); $("input.people-input").blur(on_people_input_blur); $("input.people-search-button").click(do_search); $(window).resize(reposition_results); window.people_input_request_num = 1; window.people_input_suggestions_displaying = 0; function on_people_input_modified(event) { var pis = $("#people-input-suggestions"); pis.show(); // Ensure this is visible var pi = $("input.people-input"); var curSelected = $("#people-input-suggestions ul li.selected").first(); var prevSelected = curSelected.prev(); var nextSelected = curSelected.next(); var itemSelected = (curSelected.length == 1); // Are there any selected? if (event["keyCode"] == 13) // Enter key pressed { do_search(); // Redirect to the normal directory search page return; } if (event["keyCode"] == 9) // Tab key pressed { event.preventDefault(); // Complete the text box with the first suggestion if there are no items // selected if (!itemSelected) { var first = pis.find("ul li").first().data("name"); pi.val(first); } else // Otherwise complete with the name of the person selected. { pi.val(curSelected.data("name")); } } // Arrow key pressed (up or down) // UP Down if (event["keyCode"] == 38 || event["keyCode"] == 40) { event.preventDefault(); var down = (event["keyCode"] == 40); // To determine up/down in this block // If there are no items selected, if (!itemSelected) { pis.data("origval",pi.val()); // Set the original value of the text box so we can restore it later. // Go different ways depending on whether up/down was pressed. var newSelected = down ? pis.find("ul li").first() : pis.find("ul li").last(); pi.val(newSelected.text()); // Select the new item newSelected.addClass("selected"); } else { // Again, different directions based on whether up or down was pressed var newSelected = down ? nextSelected : prevSelected; if (newSelected.length == 1) { // Select the new element newSelected.addClass("selected"); pi.val(newSelected.text()); } else { // Deselected all elements, set the text box value back to the // original pi.val(pis.data("origval")); //console.log("Clearing origval PLACE#1"); pis.data("origval",null); } curSelected.removeClass("selected"); } // Return so we don't search again return; } // Clear origval as input has changed //console.log("Clearing origval PLACE#2"); pis.data("origval",null); // Grab suggestions and drop down a list window.clearTimeout(window.findPeopleTimeout); window.findPeopleTimeout = window.setTimeout(do_update,200); } function on_people_input_blur() { // Clear all suggestions setTimeout(function(){ $("#people-input-suggestions").hide();},200); } function on_people_input_click() { //do_update(); } // Recreate the positioning CSS for the results - useful for when the window // resizes function reposition_results() { var pis = $("#people-input-suggestions"); var pi = $("input.people-input"); pis.css({ "position":"absolute", "width": pi.outerWidth(), "top": pi.offset().top+pi.outerHeight()+10, "left": pi.offset().left, }); } function do_update() { var pis = $("#people-input-suggestions"); var pi = $("input.people-input"); var pi_offset = pi.offset(); if (pis.data("origval") == null) { var query = $("input.people-input").val(); } else { var query = pis.data("origval"); } if (query == "") { //pis.css({"display":"none"}); pis.hide(); return; } reposition_results(); //console.log("Sending out people search request #"+window.people_input_request_num); $.getJSON(("//www.uvm.edu/directory/api/query_suggestions.php"), { "name": query, "request_num": window.people_input_request_num++, }, function(data) { //console.log("Received people search callback #"+data["request_num"]); if (parseInt(data["request_num"]) > window.people_input_suggestions_displaying) { window.people_input_suggestions_displaying = parseInt(data["request_num"]); } else { //console.log("Threw away old data."); return; } var pis = $("#people-input-suggestions"); pis.html(""); pis.show(); var list = $("