<commit_before>/*
 * js code for Drop-Down Menu
 */

export function hide_menu($element) {
    $element.animate({'opacity': 0}, 100, () => {
        $element.css('visibility', 'hidden')
            .css('display', 'none');
    });
}

export function show_menu($element) {
    $element.css('visibility', 'visible')
        .css('display', 'block')
        .animate({'opacity': 1}, 100);
}

export function navMenuListener() {
    $('.nav-menu').on('click', (event) => {
        event.stopPropagation();
        hide_menu($('.top-nav-menu li ul'));
        hide_menu($('#language_select, #select_language'));
        const $el = $('#all-accounts, #all-accounts-top');
        if (+$el.css('opacity') === 1 ) {
            hide_menu($el);
        } else {
            show_menu($el);
        }
    });
}

export function selectDropdown(selector, has_label) {
    if (!selector) return;

    const $selector = $(selector);
    let $select_dropdown, $list, $list_items;

    function init() {
        const is_initialized = $selector.hasClass('select-hidden');
        if (!is_initialized) {
            $selector.addClass('select-hidden')
                .wrap('<div class="select"></div>')
                .after('<div class="select-dropdown" tabindex="0"></div>');
        }

        $select_dropdown = $selector.next('div.select-dropdown');
        $select_dropdown.text($selector.find(':selected').text()).wrapInner('<span></span>');

        $list = $select_dropdown.parent().find('.select-options');
        if ($list.length) {
            // empty list to repopulate
            $list.empty();
        } else {
            // create list
            $list = $('<ul />', { class: 'select-options' }).insertAfter($select_dropdown);
        }

        const optgroups = $selector.children('optgroup');
        if (optgroups.length) {
            // break down group into labels with its list items
            optgroups.each((idx, el) => {
                const options = $(el).children();
                const label   = $(el).attr('label');
                appendToList(options, label);
            });
        } else {
            const options = $selector.children('option');
            appendToList(options);
        }

        // Attach event listeners
        $select_dropdown.off('click').on('click', (e) => {
            e.stopPropagation();
            // expand dropdown expand/collapse
            const $siblings = $('.select-dropdown').not(e.target);
            if ($siblings.hasClass('show')) {
                $siblings.removeClass('show');
            }
            $select_dropdown.toggleClass('show');
        });

        $list_items = $list.children('li');
        $list_items.off('click').on('click', (e) => {
            e.stopPropagation();
            const $target = $(e.target);
            if ($target.hasClass('disabled') || $target.hasClass('label')) return;
            $select_dropdown.text($target.text()).removeClass('show').wrapInner('<span></span>');

            const selected_value = $selector.val();
            const dropdown_value = $target.attr('value');

            // sync original select with selected dropdown value
            if (selected_value !== dropdown_value) {
                triggerEventChange(dropdown_value);
                $list_items.not(e.target).each((idx, el) => {
                    $(el).removeClass('selected');
                });
                $target.addClass('selected');
                removeActiveClasses();
            }
        });

        const removeActiveClasses = () => {
            const list = $select_dropdown.parent().find('.select-options');
            list.find('li.select-items').removeClass('active');

        };

        const triggerEventChange = (value) => {
            const event = new Event('change');
            // dispatch event to trigger onChange value
            $selector.val(value).get(0).dispatchEvent(event);
        };

        // attach focus event/class
        $select_dropdown.on('focusin', () => $select_dropdown.addClass('focused'));
        $select_dropdown.on('focusout', () => $select_dropdown.removeClass('focused'));

        // attach keypress events
        $select_dropdown.off('keydown').on('keydown', (e) => {

            const selected_value = $selector.val();
            const key            =  e.keyCode || e.which;
            const key_string     = String.fromCharCode(key);
            const $target        = $(e.target).next('ul.select-options');
            const $active        = $target.children().filter('.active');
            const active_target  = $target.find('li.select-items.active').first();
            let $current;

            const key_matching_item = $target.find('li.select-items').filter((idx, item) => {
                const found_item = $(item).not('.selected').not('.active').not('.disabled').text().charAt(0).toLowerCase() === key_string.toLowerCase();
                return found_item;
            });

            const isAlphaNumeric = /^[a-z0-9]+$/i.test(key_string);
            const isActiveFocus  = ($select_dropdown.hasClass('show') && $select_dropdown.hasClass('focused'));

            if (!key === 9 || isActiveFocus) {
                e.preventDefault();
            }

            switch (key) {
                case 9:
                    if ($select_dropdown.hasClass('show') && $select_dropdown.hasClass('focused')) {
                        removeActiveClasses();
                        $select_dropdown.removeClass('show');
                    }
                    break;
                case 13:
                    if (active_target && active_target.attr('value') && (active_target.attr('value') !== selected_value) || active_target.attr('value') === '') {
                        $target.find('li.select-items').removeClass('selected');
                        triggerEventChange(active_target.attr('value'));
                        active_target.addClass('selected');
                        $select_dropdown.text(active_target.text()).removeClass('show').wrapInner('<span></span>');
                    }
                    removeActiveClasses();
                    $select_dropdown.removeClass('show');
                    break;
                case 38:
                    removeActiveClasses();
                    $current = $active.prevAll(':not(.disabled):not(.selected)').eq(0);
                    if (!$active.length || $active.is(':first-child')) {
                        $current = $target.children().not('.disabled').not('.selected').last();
                    }
                    $current.addClass('active');
                    break;
                case 40:
                    if ($select_dropdown.hasClass('focused')) {
                        event.preventDefault();
                    }
                    removeActiveClasses();
                    if (!$select_dropdown.hasClass('show')) {
                        $select_dropdown.addClass('show');
                    }
                    $current = $active.nextAll(':not(.disabled):not(.selected)').eq(0);
                    if (!$active.length || $active.is(':last-child')) {
                        $current = $target.children().not('.disabled').not('.selected').first();
                    }
                    $current.addClass('active');
                    break;
                default:
                    if (isAlphaNumeric && key_matching_item.length) {
                        removeActiveClasses();
                        key_matching_item.first().addClass('active');
                    }
            }
        });

        // collapse dropdown when clicking outside
        $(document).click((e) => {
            if ((!$list_items.is(e.target) && !$list_items.has(e.target).length)
                 && $select_dropdown.hasClass('show')) {
                $select_dropdown.removeClass('show');
                removeActiveClasses();
            }
        });
    };

    function appendToList(options, label) {
        if (has_label && label) {
            $('<li />', {
                text : label,
                class: 'select-items label',
            }).appendTo($list);
        }

        $.map(options, (el) => {
            const $el = $(el);
            const is_disabled = $el.is(':disabled');
            const is_selected = $el.is(':selected');
            const className   = `select-items${is_selected ? ' selected': ''}${is_disabled ? ' disabled': ''}`;<commit_msg>fixed incorrect syntax and check if dropdown is shown before accepting keypresses<commit_after>/*
 * js code for Drop-Down Menu
 */

export function hide_menu($element) {
    $element.animate({'opacity': 0}, 100, () => {
        $element.css('visibility', 'hidden')
            .css('display', 'none');
    });
}

export function show_menu($element) {
    $element.css('visibility', 'visible')
        .css('display', 'block')
        .animate({'opacity': 1}, 100);
}

export function navMenuListener() {
    $('.nav-menu').on('click', (event) => {
        event.stopPropagation();
        hide_menu($('.top-nav-menu li ul'));
        hide_menu($('#language_select, #select_language'));
        const $el = $('#all-accounts, #all-accounts-top');
        if (+$el.css('opacity') === 1 ) {
            hide_menu($el);
        } else {
            show_menu($el);
        }
    });
}

export function selectDropdown(selector, has_label) {
    if (!selector) return;

    const $selector = $(selector);
    let $select_dropdown, $list, $list_items;

    function init() {
        const is_initialized = $selector.hasClass('select-hidden');
        if (!is_initialized) {
            $selector.addClass('select-hidden')
                .wrap('<div class="select"></div>')
                .after('<div class="select-dropdown" tabindex="0"></div>');
        }

        $select_dropdown = $selector.next('div.select-dropdown');
        $select_dropdown.text($selector.find(':selected').text()).wrapInner('<span></span>');

        $list = $select_dropdown.parent().find('.select-options');
        if ($list.length) {
            // empty list to repopulate
            $list.empty();
        } else {
            // create list
            $list = $('<ul />', { class: 'select-options' }).insertAfter($select_dropdown);
        }

        const optgroups = $selector.children('optgroup');
        if (optgroups.length) {
            // break down group into labels with its list items
            optgroups.each((idx, el) => {
                const options = $(el).children();
                const label   = $(el).attr('label');
                appendToList(options, label);
            });
        } else {
            const options = $selector.children('option');
            appendToList(options);
        }

        // Attach event listeners
        $select_dropdown.off('click').on('click', (e) => {
            e.stopPropagation();
            // expand dropdown expand/collapse
            const $siblings = $('.select-dropdown').not(e.target);
            if ($siblings.hasClass('show')) {
                $siblings.removeClass('show');
            }
            $select_dropdown.toggleClass('show');
        });

        $list_items = $list.children('li');
        $list_items.off('click').on('click', (e) => {
            e.stopPropagation();
            const $target = $(e.target);
            if ($target.hasClass('disabled') || $target.hasClass('label')) return;
            $select_dropdown.text($target.text()).removeClass('show').wrapInner('<span></span>');

            const selected_value = $selector.val();
            const dropdown_value = $target.attr('value');

            // sync original select with selected dropdown value
            if (selected_value !== dropdown_value) {
                triggerEventChange(dropdown_value);
                $list_items.not(e.target).each((idx, el) => {
                    $(el).removeClass('selected');
                });
                $target.addClass('selected');
                removeActiveClasses();
            }
        });

        const removeActiveClasses = () => {
            const list = $select_dropdown.parent().find('.select-options');
            list.find('li.select-items').removeClass('active');

        };

        const triggerEventChange = (value) => {
            const event = new Event('change');
            // dispatch event to trigger onChange value
            $selector.val(value).get(0).dispatchEvent(event);
        };

        // attach focus event/class
        $select_dropdown.on('focusin', () => $select_dropdown.addClass('focused'));
        $select_dropdown.on('focusout', () => $select_dropdown.removeClass('focused'));

        // attach keypress events
        $select_dropdown.off('keydown').on('keydown', (e) => {

            const selected_value = $selector.val();
            const key            =  e.keyCode || e.which;
            const key_string     = String.fromCharCode(key);
            const $target        = $(e.target).next('ul.select-options');
            const $active        = $target.children().filter('.active');
            const active_target  = $target.find('li.select-items.active').first();
            let $current;

            const key_matching_item = $target.find('li.select-items').filter((idx, item) => {
                const found_item = $(item).not('.selected').not('.active').not('.disabled').text().charAt(0).toLowerCase() === key_string.toLowerCase();
                return found_item;
            });

            const isAlphaNumeric = /^[a-z0-9]+$/i.test(key_string);
            const isActiveFocus  = ($select_dropdown.hasClass('show') && $select_dropdown.hasClass('focused'));

            if (!key === 9 || isActiveFocus) {
                e.preventDefault();
            }

            switch (key) {
                case 9:
                    if ($select_dropdown.hasClass('show') && $select_dropdown.hasClass('focused')) {
                        removeActiveClasses();
                        $select_dropdown.removeClass('show');
                    }
                    break;
                case 13:
                    if (active_target && active_target.attr('value') && (active_target.attr('value') !== selected_value) || active_target.attr('value') === '') {
                        $target.find('li.select-items').removeClass('selected');
                        triggerEventChange(active_target.attr('value'));
                        active_target.addClass('selected');
                        $select_dropdown.text(active_target.text()).removeClass('show').wrapInner('<span></span>');
                    }
                    removeActiveClasses();
                    $select_dropdown.removeClass('show');
                    break;
                case 38:
                    removeActiveClasses();
                    $current = $active.prevAll(':not(.disabled):not(.selected)').eq(0);
                    if (!$active.length || $active.is(':first-child')) {
                        $current = $target.children().not('.disabled').not('.selected').last();
                    }
                    $current.addClass('active');
                    break;
                case 40:
                    if ($select_dropdown.hasClass('focused')) {
                        e.preventDefault();
                    }
                    removeActiveClasses();
                    if (!$select_dropdown.hasClass('show')) {
                        $select_dropdown.addClass('show');
                    }
                    $current = $active.nextAll(':not(.disabled):not(.selected)').eq(0);
                    if (!$active.length || $active.is(':last-child')) {
                        $current = $target.children().not('.disabled').not('.selected').first();
                    }
                    $current.addClass('active');
                    break;
                default:
                    if (isAlphaNumeric && key_matching_item.length && $select_dropdown.hasClass('show') && $select_dropdown.hasClass('focused')) {
                        removeActiveClasses();
                        key_matching_item.first().addClass('active');
                    }
            }
        });

        // collapse dropdown when clicking outside
        $(document).click((e) => {
            if ((!$list_items.is(e.target) && !$list_items.has(e.target).length)
                 && $select_dropdown.hasClass('show')) {
                $select_dropdown.removeClass('show');
                removeActiveClasses();
            }
        });
    };

    function appendToList(options, label) {
        if (has_label && label) {
            $('<li />', {
                text : label,
                class: 'select-items label',
            }).appendTo($list);
        }

        $.map(options, (el) => {
            const $el = $(el);
            const is_disabled = $el.is(':disabled');
            const is_selected = $el.is(':selected');
            const className   = `select-items${is_selected ? ' selected': ''}${is_disabled ? ' disabled': ''}`;
{'fraction_non_alphanumeric': 0.15991109551383284, 'fraction_numerical': 0.0028075100894893843, 'mean_word_length': 1.7479909996785599, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 55, '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': '758798', 'n_tokens_mistral': 4475, 'n_tokens_neox': 4273, 'n_words': 717}