<commit_before>var settings_user_groups = (function () {

var exports = {};

var meta = {
    loaded: false,
};

exports.reset = function () {
    meta.loaded = false;
};

exports.reload = function () {
    var user_groups_section = $('#user-groups').expectOne();
    user_groups_section.html('');
    exports.populate_user_groups();
};

exports.populate_user_groups = function () {
    if (!meta.loaded) {
        return;
    }

    var user_groups_section = $('#user-groups').expectOne();
    var user_groups_array = user_groups.get_realm_user_groups();

    _.each(user_groups_array, function (data) {
        user_groups_section.append(templates.render('admin_user_group_list', {
            user_group: {
                name: data.name,
                id: data.id,
                description: data.description,
            },
        }));

        var pill_container = $('.pill-container[data-group-pills="' + data.name + '"]');
        var pills = input_pill(pill_container);

        data.members.forEach(function (user_id) {
            var user = people.get_person_from_user_id(user_id);

            if (user) {
                pills.pill.append(user.full_name, user_id);
            } else {
                blueslip.warn('Unknown user ID ' + user_id + ' in members of user group ' + data.name);
            }
        });

        function update_save_state(draft_group) {
            var original_group = user_groups.get_user_group_from_id(data.id).members;
            var same_groups = _.isEqual(_.sortBy(draft_group), _.sortBy(original_group));
            var save_changes = pill_container.siblings('.save-member-changes');
            var save_hidden = save_changes.css('display') === 'none';

            if ((!draft_group.length || same_groups) && !save_hidden) {
                save_changes.fadeOut();
            } else if (!same_groups && draft_group.length && save_hidden) {
                save_changes.css({display: 'inline-block', opacity: '0'}).fadeTo(400, 1);
            }
        }

        pills.onPillCreate(function (value, reject) {
            var person = people.get_by_email(value);
            var draft_group = pills.keys();

            if (!person || draft_group.includes(person.user_id)) {
                return reject();
            }

            draft_group.push(person.user_id);
            update_save_state(draft_group);

            return { key: person.user_id, value: person.full_name };
        });

        pills.onPillRemove(function () {
            update_save_state(pills.keys());
        });

        $('#user-groups #' + data.id).on('click', '.save-member-changes', function () {
            var draft_group = pills.keys();
            var group_data = user_groups.get_user_group_from_id(data.id);
            var original_group = group_data.members;
            var added = _.difference(draft_group, original_group);
            var removed = _.difference(original_group, draft_group);
            var btn = $(this);<commit_msg>org settings: Add typeahead to user group member inputs.<commit_after>var settings_user_groups = (function () {

var exports = {};

var meta = {
    loaded: false,
};

exports.reset = function () {
    meta.loaded = false;
};

exports.reload = function () {
    var user_groups_section = $('#user-groups').expectOne();
    user_groups_section.html('');
    exports.populate_user_groups();
};

exports.populate_user_groups = function () {
    if (!meta.loaded) {
        return;
    }

    var user_groups_section = $('#user-groups').expectOne();
    var user_groups_array = user_groups.get_realm_user_groups();

    _.each(user_groups_array, function (data) {
        user_groups_section.append(templates.render('admin_user_group_list', {
            user_group: {
                name: data.name,
                id: data.id,
                description: data.description,
            },
        }));

        var pill_container = $('.pill-container[data-group-pills="' + data.name + '"]');
        var pills = input_pill(pill_container);

        data.members.forEach(function (user_id) {
            var user = people.get_person_from_user_id(user_id);

            if (user) {
                pills.pill.append(user.full_name, user_id);
            } else {
                blueslip.warn('Unknown user ID ' + user_id + ' in members of user group ' + data.name);
            }
        });

        function update_save_state(draft_group) {
            var original_group = user_groups.get_user_group_from_id(data.id).members;
            var same_groups = _.isEqual(_.sortBy(draft_group), _.sortBy(original_group));
            var save_changes = pill_container.siblings('.save-member-changes');
            var save_hidden = save_changes.css('display') === 'none';

            if ((!draft_group.length || same_groups) && !save_hidden) {
                save_changes.fadeOut();
            } else if (!same_groups && draft_group.length && save_hidden) {
                save_changes.css({display: 'inline-block', opacity: '0'}).fadeTo(400, 1);
            }
        }

        var input = pill_container.children('.input');

        input.typeahead({
            items: 5,
            fixed: true,
            dropup: true,
            source: people.get_realm_persons,
            highlighter: function (item) {
                return typeahead_helper.render_person(item);
            },
            matcher: function (item) {
                if (pills.keys().includes(item.user_id)) {
                    return false;
                }

                var person = people.get_person_from_user_id(item.user_id);
                var query = this.query.toLowerCase();

                return (person.email.toLowerCase().indexOf(query) !== -1
                        || person.full_name.toLowerCase().indexOf(query) !== -1);
            },
            sorter: function (matches) {
                return typeahead_helper.sort_recipientbox_typeahead(
                    this.query, matches, "");
            },
            updater: function (user) {
                pills.pill.append(user.full_name, user.user_id);
                input.text('');
                update_save_state(pills.keys());
            },
            stopAdvance: true,
        });

        pills.onPillCreate(function (value, reject) {
            var person = people.get_by_email(value);
            var draft_group = pills.keys();

            if (!person || draft_group.includes(person.user_id)) {
                return reject();
            }

            draft_group.push(person.user_id);
            update_save_state(draft_group);

            return { key: person.user_id, value: person.full_name };
        });

        pills.onPillRemove(function () {
            update_save_state(pills.keys());
        });

        $('#user-groups #' + data.id).on('click', '.save-member-changes', function () {
            var draft_group = pills.keys();
            var group_data = user_groups.get_user_group_from_id(data.id);
            var original_group = group_data.members;
            var added = _.difference(draft_group, original_group);
            var removed = _.difference(original_group, draft_group);
            var btn = $(this);
{'fraction_non_alphanumeric': 0.12654192654192653, 'fraction_numerical': 0.0018018018018018018, 'mean_word_length': 2.0537452391028355, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4312494', 'n_tokens_mistral': 2097, 'n_tokens_neox': 2031, 'n_words': 352}