<commit_before>/**
 * A Keyboard module
 * @param $ jQuery
 * @returns {{className: string}}
 * @constructor
 * @memberof svl
 */


function Keyboard ($) {
    var self = {
            className : 'Keyboard'
        };
    var status = {
        focusOnTextField: false,
        shiftDown: false
    };

    var $textareaComment;

    function init () {
        if ('ui' in svl && 'form' in svl.ui) {
            $textareaComment = (svl.ui.form.commentField.length) > 0 ? svl.ui.form.commentField : null;
        }

        if ($textareaComment) {
          $textareaComment.bind('focus', textFieldFocus);
          $textareaComment.bind('blur', textFieldBlur);
        }


        $(document).bind('keyup', documentKeyUp);
        $(document).bind('keydown', documentKeyDown);
    }
     // taken from 
    // http://stackoverflow.com/questions/7799722/how-do-i-implement-move-forward-action-for-google-street-view
    function headingDifference(link, angle) {
    
        var diff = Math.abs((svl.panorama.pov.heading % 360) - link.heading + angle);
        if(diff>180)
            diff=Math.abs(360-diff);

        return diff;

    }

    function movePano(angle) {
        var curr;
        var found = false;
        var len =  svl.panorama.links.length;
        for(var i = 0; i < len; i++) {
            var diffLast = headingDifference(svl.panorama.links[i], angle);
            if(curr == undefined) {
                curr = svl.panorama.links[i];
            }
            if(diffLast <= 90){
                found = true;
            }
            var diffCurr = headingDifference(curr, angle);
            if(diffCurr > diffLast && diffLast <= 90) {
                curr = svl.panorama.links[i];
                found = true;
            }
        }
        if(found){
            svl.panorama.setPano(curr.pano);
        }
    }

    function rotatePov(degree){
        var heading =  svl.panorama.pov.heading;
        var pitch = svl.panorama.pov.pitch;
        var zoom = svl.panorama.pov.zoom;
        heading += degree;
        if(heading < 0)
            heading += 360;
        heading = heading % 360;
        svl.panorama.setPov({heading: heading, pitch: pitch, zoom: zoom});

    }

    /**
     * This is a callback for a key down event
     * @param {object} e An event object
     * @private
     */
    function documentKeyDown(e) {
        if([32, 37, 38, 39, 40].indexOf(e.keyCode) > -1) { 
            e.preventDefault();
        }
    
        switch (e.keyCode) {
            // "left"
            // rotate pano
            case 37:
                rotatePov(-2);
                break;
             // "right"
            // rotate pano
            case 39:
                rotatePov(2);
                break;

        }
        // The callback method that is triggered with a keyUp event.
        if (!status.focusOnTextField) {
            switch (e.keyCode) {
                case 16:
                    // "Shift"
                    status.shiftDown = true;
                    break;
            }
        }
    }

    function documentKeyPress (e) {

    }
    /**
     * This is a callback for a key up event.
     * @param {object} e An event object
     * @private
     */
    function documentKeyUp (e) {
        switch (e.keyCode) {
            // "up"
            case 38:
                movePano(0);
                break;
            // "down"
            case 40:
                movePano(180);
                break;
        }
        if ("onboarding" in svl && svl.onboarding && svl.onboarding.isOnboarding()) {
            // Don't allow users to use keyboard shortcut during the onboarding.<commit_msg>Clean up code and consdense direction math<commit_after>/**
 * A Keyboard module
 * @param $ jQuery
 * @returns {{className: string}}
 * @constructor
 * @memberof svl
 */
function Keyboard ($) {
    var self = {
            className : 'Keyboard'
        };
    var status = {
        focusOnTextField: false,
        shiftDown: false
    };

    var $textareaComment;

    function init () {
        if ('ui' in svl && 'form' in svl.ui) {
            $textareaComment = (svl.ui.form.commentField.length) > 0 ? svl.ui.form.commentField : null;
        }

        if ($textareaComment) {
          $textareaComment.bind('focus', textFieldFocus);
          $textareaComment.bind('blur', textFieldBlur);
        }

        $(document).bind('keyup', documentKeyUp);
        $(document).bind('keydown', documentKeyDown);
    }
    function toRadians (angle) {
        return angle * (Math.PI / 180);
    }
    // Move in the direction of a link closest to a given angle
    function movePano(angle) {
        // take the cosine of the difference for each link to the current heading in radians and stores them to an array
        var cosines = svl.panorama.links.map(function(link) { return Math.cos(toRadians(svl.panorama.pov.heading + angle) - toRadians(link.heading))});
        // finds index of greatest value in cosines array
        var maxVal = Math.max.apply(null, cosines);
        var maxIndex = cosines.indexOf(maxVal);
        //in the case of one link, this prevents you from moving in that direction if you press the opposite
        if(cosines[maxIndex] > 0.5){
            // transitions panorama to the link of the greatest cosine (closest to direction)
            svl.panorama.setPano(svl.panorama.links[maxIndex].pano);
        }
    }
    // abstract movePano with more meaningful function name for the following two
    function moveForward(){
        movePano(0);
    }

    function moveBackward(){
        movePano(180);
    }
    // change the heading of the current panorama point of view by a particular degree value
    function rotatePov(degree){
        var heading =  svl.panorama.pov.heading;
        // pitch and zoom arent changed but are needed for the setPov call
        var pitch = svl.panorama.pov.pitch;
        var zoom = svl.panorama.pov.zoom;
        heading = (heading + degree + 360) % 360;
        svl.panorama.setPov({heading: heading, pitch: pitch, zoom: zoom});
    }

    /**
     * This is a callback for a key down event
     * @param {object} e An event object
     * @private
     */
    function documentKeyDown(e) {
        // lock scrolling in response to key pressing
        if([32, 37, 38, 39, 40].indexOf(e.keyCode) > -1) { 
            e.preventDefault();
        }
    
        switch (e.keyCode) {
            // "left"
            // rotate pano
            case 37:
                rotatePov(-2);
                break;
             // "right"
            // rotate pano
            case 39:
                rotatePov(2);
                break;

        }
        // The callback method that is triggered with a keyUp event.
        if (!status.focusOnTextField) {
            switch (e.keyCode) {
                case 16:
                    // "Shift"
                    status.shiftDown = true;
                    break;
            }
        }
    }

    /**
     * This is a callback for a key up event.
     * @param {object} e An event object
     * @private
     */
    function documentKeyUp (e) {
        switch (e.keyCode) {
            // "up"
            case 38:
                moveForward();
                break;
            // "down"
            case 40:
                moveBackward();
                break;
        }
        if ("onboarding" in svl && svl.onboarding && svl.onboarding.isOnboarding()) {
            // Don't allow users to use keyboard shortcut during the onboarding.
{'fraction_non_alphanumeric': 0.111140618775727, 'fraction_numerical': 0.0126145266232904, 'mean_word_length': 1.5575551782682513, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 10, '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': '2007188', 'n_tokens_mistral': 2303, 'n_tokens_neox': 2133, 'n_words': 579}