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

Untitled

A Pen By Captain Anonymous

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

Looking for quick-add? Try the external resource search, it's quicker and gives you access to the most recent version of thousands of libraries. ☝️

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Looking for quick-add? Try the external resource search, it's quicker and gives you access to the most recent version of thousands of libraries. ☝️

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console