cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

mstgy

A Pen By Captain Anonymous

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              Google+ user search with keyboard navigation<br />
(REPLACE API-KEY FOR EXTERN USAGE)<br />
&copy <a href="http://kollinger.it">Gerhard Kollinger</a>

<br />
<br />


<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<input name="gplus_user_id" value="" id="user_id" type="text" autocomplete="off" />
<div id="foundPersons"></div>
            
          
!
            
              #user_id {
    width: 250px;
    border-radius: 3px 3px 3px 3px;
    border: 1px solid #AAAAAA;
    box-shadow: 1px 1px 2px rgba(0, 0, 0, 0.1);
}

#foundPersons {
    display: none; 
    margin-top: -1px;
    border: 1px solid #AAAAAA;
    border-radius: 3px 3px 3px 3px;
    overflow: hidden;
    width: 250px;
    position: absolute;
    background: #ffffff;
    box-shadow: 1px 1px 2px rgba(0, 0, 0, 0.1);
}

.personLink {
    display: block;
    height: 25px;
    color: #000000;
    text-decoration: none;
    padding: 5px 0 0 5px;
}

.personLink.lastPerson {
    padding-bottom: 5px;
}

.personLink img {
    position: absolute;
    border-radius: 2px 2px 2px 2px;
}

.personLink span {
    font-size: 9px;
    margin-left: 32px;
    display: block;
    padding-top: 4px;
}

.personLink:hover, .personLink.active {
    background: #FAFAFA;
    color: #D54E21;
}
            
          
!
            
              jQuery(document).ready(function () {
    activePersonNr        = "";
    var actualActivePersonNr;
    apiKey = "AIzaSyCgrfVktQSMSJcgKKpaycktF5bOCLjfuXc";
     
    // Listen to user-id input and perform live search of users via g+ API, and if a person was selected, the user-id will be written in the input
    jQuery("#user_id").keypress(function(e){
       doLookup = actionHandler(e);
    });
    jQuery("#user_id").keyup(function(){
        if(doLookup) {
            userLookup();
        }
    });

    // close found persons box if a click was performed
    jQuery(document).click(function() {
        jQuery("#foundPersons").css("display", "none");
    });

    // very fucked up style, but it seems to work perfect
    function actionHandler(e) {
        userIdInput = jQuery("#user_id").val();

        if(userIdInput.length >= 2) {
            // if up, down or enter was pressed then set a new person to active state or select the active one
            personLinks             = jQuery(".personLink");
            keyCodeArrowUp          = 38;
            keyCodeArrowDown        = 40;
            keyCodeEnter            = 13;
            pressedKeyKeyCode       = e.keyCode;
            foundPersonsBoxdisplay  = jQuery("#foundPersons").css("display");

            if
                (
                    foundPersonsBoxdisplay == "block" && personLinks && pressedKeyKeyCode == keyCodeArrowUp      ||
                    foundPersonsBoxdisplay == "block" && personLinks && pressedKeyKeyCode == keyCodeArrowDown    ||
                    foundPersonsBoxdisplay == "block" && personLinks && pressedKeyKeyCode == keyCodeEnter
                ) {
                personLinksCount    = personLinks.size();

                for(i = 0; i < personLinksCount; i++) {
                    actualPersonLinkActive = personLinks[i].className.match(/active/);

                    if(actualPersonLinkActive) {
                        actualActivePersonNr = i;
                    }
                }

                for(i = 0; i < personLinksCount; i++) {
                    if(actualActivePersonNr == i) {
                        switch(pressedKeyKeyCode) {
                            case keyCodeArrowUp:
                                if( ! activePersonNr) {
                                    jQuery(personLinks[0]).removeClass("active");
                                    jQuery(personLinks[personLinksCount - 1]).addClass("active");
                                    activePersonNr = personLinksCount - 1;
                                }else {
                                    jQuery(personLinks[i]).removeClass("active");
                                    jQuery(personLinks[i-1]).addClass("active");
                                    activePersonNr--;
                                }
                                return false;

                            case keyCodeArrowDown:
                                if(activePersonNr == personLinksCount - 1) {
                                    jQuery(personLinks[personLinksCount - 1]).removeClass("active");
                                    jQuery(personLinks[0]).addClass("active");
                                    activePersonNr = 0;
                                }else {
                                    jQuery(personLinks[i]).removeClass("active");
                                    jQuery(personLinks[i+1]).addClass("active");
                                    activePersonNr++;
                                }
                                return false;
                        }
                    }

                    if(pressedKeyKeyCode == keyCodeEnter) {
                        if(activePersonNr !== "") {
                            activePersonId = personLinks[activePersonNr].getAttribute("onclick").replace(/return personClicked\(/, "").replace(/\)/, "");
                            personClicked(activePersonId);
                            jQuery("#foundPersons").css("display", "none");
                            return false;
                        }
                    }
                }

                if(activePersonNr == "") {
                    if(pressedKeyKeyCode == keyCodeArrowUp) {
                        activePersonNr = personLinksCount - 1;
                        jQuery(personLinks[personLinksCount - 1]).addClass("active");
                    }else {
                        activePersonNr = 0;
                        jQuery(personLinks[0]).addClass("active");
                    }
                    return false;
                }
            }

            // if we land here no navigation key was pressed so lookup for new persons
            return true;
        }
    }


    function userLookup() {
        userIdInput = jQuery("#user_id").val();

        // create new person links
        jQuery.getJSON("https://www.googleapis.com/plus/v1/people/?key="+apiKey+"&query="+userIdInput, function(response) {
            if(response) {
                persons = response.items;
                foundPersonsContent = "";

                for(i = 0; i < persons.length; i++) {
                    person = persons[i];

                    if(persons.length - 1 == i) {
                        lastClass = " lastPerson";
                    }else {
                        lastClass = "";
                    }

                    personName = person.displayName.length > 40 ? person.displayName.substring(0, 40) + "..." : person.displayName;

                    jQuery("#foundPersons").css("display", "block");
                    foundPersonsContent = foundPersonsContent+"<a onclick=\"return personClicked("+person.id+")\" href=\"#\" class=\"personLink"+lastClass+"\" title=\""+person.displayName+"\"><img src=\""+person.image.url.replace(/sz=50/, "sz=25")+"\" /><span>"+personName+"</span></a>";
                }

                activePersonNr       = "";
                actualActivePersonNr = -1;
                jQuery("#foundPersons").html(foundPersonsContent);
            }
        });
    }


    function personClicked(clickedPersonId) {
        jQuery("#user_id").val(clickedPersonId);
        return false;
    }
});
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console