<commit_before>/*
 * jquery.gpopover
 * http://github.com/markembling/jquery-gpopover
 *
 * A simple jQuery plugin for creating popover elements similar to Google's 
 * new 'apps' launcher/switcher.
 *
 * Copyright (c) 2013 <NAME> (<EMAIL>)
 * Licensed under the BSD (3 clause) license.
 */

;(function($){
    
    $.fn.gpopover = function(options) {
        
        var settings = $.extend({}, $.fn.gpopover.defaults, options);
        
        return this.each(function(){
        
            var $trigger = $(this),
                $popover = $('#' + $trigger.data('popover'));
        
            var arrows = _addArrowElements($popover);
            
            if (settings.preventHide) {
                _preventHideClickPropagation($popover);
            }
        
            $trigger.click(function(e){
            
                if (! $popover.is(":visible")) {
                    // Set width before showing
                    $popover.width(settings.width);
                
                    $popover.fadeIn(settings.fadeInDuration);
                    
                    // Set up hiding
                    $(document).one('click.popoverHide', function() { 
                        $popover.fadeOut(settings.fadeOutDuration);
                    });
                
                    // Sort out the position (must be done after showing)
                    var triggerPos = $trigger.offset();
                    $popover.offset({
                        left: (triggerPos.left + ($trigger.outerWidth() / 2)) - ($popover.outerWidth() / 2),
                        top: triggerPos.top + $trigger.outerHeight() + 10  
                        // the final 10 above allows room for the arrow above it
                    });
                
                    // Check and reposition if out of the viewport
                    var positionXCorrection = _repositionForViewportSides($popover, settings.viewportSideMargin);
                
                    // Set the position of the arrow elements
                    _setArrowPosition(arrows, $popover, positionXCorrection);

                    // Prevent this event from having any further effect
                    e.preventDefault();
                    e.stopPropagation();
                }
            });
        
        });
        
    };
    
    // Default settings
    $.fn.gpopover.defaults = {
        width: 250,             // Width of the popover
        fadeInDuration: 65,     // Duration of popover fade-in animation
        fadeOutDuration: 65,    // Duration of popover fade-out animation
        viewportSideMargin: 10, // Space to leave the side if out the viewport
        preventHide: false      // Prevent hide when clicking within popover
    };
    
    // Private functions
    
    function _addArrowElements($popover) {
        var $arrow = $('<div class="gpopover-arrow"></div>');
        var $arrowShadow = $('<div class="gpopover-arrow-shadow"></div>');
        
        $popover.append($arrow);
        $popover.append($arrowShadow);
        
        return { $arrow: $arrow, $shadow: $arrowShadow };
    }
    
    function _preventHideClickPropagation($popover) {
        /* Prevent clicks within the popover from being propagated 
           to the document (and thus stop the popover from being 
           hidden) */
        $popover.click(function(e) { e.stopPropagation(); });
    }
    
    function _repositionForViewportSides($popover, sideMargin) {
        var popoverOffsetLeft = $popover.offset().left;
        var positionXCorrection = 0;
        
        // Right edge
        if (popoverOffsetLeft + $popover.outerWidth() + sideMargin > $(window).width()) {
            var rightEdgeCorrection = -((popoverOffsetLeft + $popover.outerWidth() + sideMargin) - $(window).width());
            popoverOffsetLeft = popoverOffsetLeft + rightEdgeCorrection
        
            positionXCorrection = rightEdgeCorrection;
        }
        
        // Left edge
        if (popoverOffsetLeft < sideMargin) {
            var leftEdgeCorrection = sideMargin - popoverOffsetLeft;
            popoverOffsetLeft = popoverOffsetLeft + leftEdgeCorrection
            
            positionXCorrection += leftEdgeCorrection;
        }
        
        // Reposition the popover element if necessary
        if (positionXCorrection !== 0) {
            $popover.offset({ left: popoverOffsetLeft });
        }<commit_msg>Move show and hide to separate functions<commit_after>/*
 * jquery.gpopover
 * http://github.com/markembling/jquery-gpopover
 *
 * A simple jQuery plugin for creating popover elements similar to Google's 
 * new 'apps' launcher/switcher.
 *
 * Copyright (c) 2013 <NAME> (<EMAIL>)
 * Licensed under the BSD (3 clause) license.
 */

;(function($){
    
    $.fn.gpopover = function(options) {
        
        var settings = $.extend({}, $.fn.gpopover.defaults, options);
        
        return this.each(function(){
        
            var $trigger = $(this),
                $popover = $('#' + $trigger.data('popover'));
        
            var arrows = _addArrowElements($popover);
            
            if (settings.preventHide) {
                _preventHideClickPropagation($popover);
            }
        
            $trigger.click(function(e){
            
                if (! $popover.is(":visible")) {
                    // Set width before showing
                    $popover.width(settings.width);
                
                    _showPopover($popover, settings);
                    
                    // Set up hiding
                    $(document).one('click.popoverHide', function() {
                        _hidePopover($popover, settings);
                    });
                
                    // Sort out the position (must be done after showing)
                    var triggerPos = $trigger.offset();
                    $popover.offset({
                        left: (triggerPos.left + ($trigger.outerWidth() / 2)) - ($popover.outerWidth() / 2),
                        top: triggerPos.top + $trigger.outerHeight() + 10  
                        // the final 10 above allows room for the arrow above it
                    });
                
                    // Check and reposition if out of the viewport
                    var positionXCorrection = _repositionForViewportSides($popover, settings.viewportSideMargin);
                
                    // Set the position of the arrow elements
                    _setArrowPosition(arrows, $popover, positionXCorrection);

                    // Prevent this event from having any further effect
                    e.preventDefault();
                    e.stopPropagation();
                }
            });
        
        });
        
    };
    
    // Default settings
    $.fn.gpopover.defaults = {
        width: 250,             // Width of the popover
        fadeInDuration: 65,     // Duration of popover fade-in animation
        fadeOutDuration: 65,    // Duration of popover fade-out animation
        viewportSideMargin: 10, // Space to leave the side if out the viewport
        preventHide: false      // Prevent hide when clicking within popover
    };
    
    // Private functions
    
    function _addArrowElements($popover) {
        var $arrow = $('<div class="gpopover-arrow"></div>');
        var $arrowShadow = $('<div class="gpopover-arrow-shadow"></div>');
        
        $popover.append($arrow);
        $popover.append($arrowShadow);
        
        return { $arrow: $arrow, $shadow: $arrowShadow };
    }
    
    function _preventHideClickPropagation($popover) {
        /* Prevent clicks within the popover from being propagated 
           to the document (and thus stop the popover from being 
           hidden) */
        $popover.click(function(e) { e.stopPropagation(); });
    }
    
    function _showPopover($popover, settings) {
        // Show the popover
        $popover.fadeIn(settings.fadeInDuration);
    }
    
    function _hidePopover($popover, settings) {
        // Hide the popover
        $popover.fadeOut(settings.fadeOutDuration);
    }
    
    function _repositionForViewportSides($popover, sideMargin) {
        var popoverOffsetLeft = $popover.offset().left;
        var positionXCorrection = 0;
        
        // Right edge
        if (popoverOffsetLeft + $popover.outerWidth() + sideMargin > $(window).width()) {
            var rightEdgeCorrection = -((popoverOffsetLeft + $popover.outerWidth() + sideMargin) - $(window).width());
            popoverOffsetLeft = popoverOffsetLeft + rightEdgeCorrection
        
            positionXCorrection = rightEdgeCorrection;
        }
        
        // Left edge
        if (popoverOffsetLeft < sideMargin) {
            var leftEdgeCorrection = sideMargin - popoverOffsetLeft;
            popoverOffsetLeft = popoverOffsetLeft + leftEdgeCorrection
            
            positionXCorrection += leftEdgeCorrection;
        }
        
        // Reposition the popover element if necessary
        if (positionXCorrection !== 0) {
            $popover.offset({ left: popoverOffsetLeft });
        }
{'fraction_non_alphanumeric': 0.10165794066317627, 'fraction_numerical': 0.004799301919720768, 'mean_word_length': 1.6092771770062606, 'pattern_counts': {'":': 2, '<': 17, '<?xml version=': 0, '>': 17, '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': '6156176', 'n_tokens_mistral': 2383, 'n_tokens_neox': 2273, 'n_words': 558}