<commit_before>/*globals $, COMMCAREHQ, _, ko, django */
$(function () {
    var initial_page_data = hqImport('hqwebapp/js/initial_page_data.js').get,
        v2 = COMMCAREHQ.toggleEnabled('APP_MANAGER_V2'),
        moduleBrief = initial_page_data('module_brief'),
        moduleType = moduleBrief.module_type,
        options = initial_page_data('js_options') || {};

    hqImport('app_manager/js/app_manager.js').setAppendedPageTitle(django.gettext("Module Settings"));

    // Set up details
    if (!v2 || moduleBrief.case_type) {
        var state = hqImport('app_manager/js/detail-screen-config.js').state;
        var DetailScreenConfig = hqImport('app_manager/js/detail-screen-config.js').DetailScreenConfig;
        state.requires_case_details(moduleBrief.requires_case_details);

        var details = initial_page_data('details');
        for (var i = 0; i < details.length; i++) {
            var detail = details[i];
            var detailScreenConfig = DetailScreenConfig.init({
                module_id: moduleBrief.id,
                moduleUniqueId: moduleBrief.unique_id,
                state: {
                    type: detail.type,
                    short: detail.short,
                    long: detail.long,
                },
                sortRows: detail.sort_elements,
                model: detail.model,
                properties: detail.properties,
                lang: moduleBrief.lang,
                langs: moduleBrief.langs,
                saveUrl: hqImport('hqwebapp/js/urllib.js').reverse('edit_module_detail_screens'),
                parentModules: initial_page_data('parent_modules'),
                childCaseTypes: detail.subcase_types,
                fixture_columns_by_type: options.fixture_columns_by_type || {},
                parentSelect: detail.parent_select,
                fixtureSelect: detail.fixture_select,
                contextVariables: state,
                multimedia: initial_page_data('multimedia_object_map'),
                searchProperties: options.search_properties || [],
                includeClosed: options.include_closed,
                defaultProperties: options.default_properties || [],
                searchButtonDisplayCondition: options.search_button_display_condition,
                blacklistedOwnerIdsExpression: options.blacklisted_owner_ids_expression,
            });

            var $list_home = $("#" + detail.type + "-detail-screen-config-tab");
            $list_home.koApplyBindings(detailScreenConfig);

            if (detail.long !== undefined) {
                var $detail_home = $("#" + detail.type + "-detail-screen-detail-config-tab");
                $detail_home.koApplyBindings(detailScreenConfig);
            }
        }
    }

    // Validation for case type
    var showCaseTypeError = function(message) {
        var $caseTypeError = $('#case_type_error');
        $caseTypeError.css('display', 'block');
        $caseTypeError.text(message);
    };
    var hideCaseTypeError = function() {
        $('#case_type_error').css('display', 'none');
    };
    $('#case_type').on('textchange', function () {
        var $el = $(this),
            value = $el.val(),
            valueNoSpaces = value.replace(/ /g, '_');
        if (value !== valueNoSpaces) {
            var pos = $el.caret('pos');
            $el.val(valueNoSpaces);
            $el.caret('pos', pos);
        }
        if (v2 && !valueNoSpaces) {
            $el.closest('.form-group').addClass('has-error');
            showCaseTypeError(
                gettext("Case type is required.")
            );
            return;
        }
        if (!valueNoSpaces.match(/^[\w-]*$/g)) {
            $el.closest('.form-group').addClass('has-error');
            showCaseTypeError(
                gettext("Case types can only include the characters a-z, 0-9, '-' and '_'")
            );
        } else if (valueNoSpaces === 'commcare-user' && moduleType !== 'advanced') {
            $el.closest('.form-group').addClass('has-error');
            showCaseTypeError(
                gettext("'commcare-user' is a reserved case type. Please change the case type")
            );

        } else {
            $el.closest('.form-group').removeClass('has-error');
            hideCaseTypeError();
        }
    });

    // Module filter
    $(function () {
        var $moduleFilter = $('#module-filter');
        if ($moduleFilter.length) {
            $moduleFilter.koApplyBindings({xpath: initial_page_data("module_filter") || ''});
        }
    });

    // Registration in case list
    if ($('#case-list-form').length) {
        var CaseListForm = function (originalFormId, formOptions) {
            var self = this;

            self.caseListForm = ko.observable(originalFormId);
            self.formMissing = ko.computed(function() {
                return self.caseListForm() && !formOptions[self.caseListForm()];
            });

            // Show or hide associated multimedia. Not done in knockout because
            // the multimedia section has its own separate set of knockout bindings
            self.caseListForm.subscribe(function(form_id) {
                if (form_id) {
                    $("#case_list_media").show();
                } else {
                    $("#case_list_media").hide();
                }
            });
        };
        var case_list_form_options = initial_page_data('case_list_form_options');
            caseListForm = new CaseListForm(
                case_list_form_options ? case_list_form_options.form.form_id : null,
                case_list_form_options ? case_list_form_options.options : []
            );
        $('#case-list-form').koApplyBindings(caseListForm);

        // Reset save button after bindings
        // see http://manage.dimagi.com/default.asp?145851
        var $form = $('#case-list-form').closest('form'),
            $button = $form.find('.save-button-holder').data('button');
        $button.setStateWhenReady('saved');
    }

    if (moduleType === 'shadow') {
        // Shadow module checkboxes for including/excluding forms
        var ShadowModule = hqImport('app_manager/js/shadow-module-settings.js').ShadowModule,
            shadowOptions = initial_page_data('shadow_module_options');<commit_msg>Show/hide multimedia properly on page load<commit_after>/*globals $, COMMCAREHQ, _, ko, django */
$(function () {
    var initial_page_data = hqImport('hqwebapp/js/initial_page_data.js').get,
        v2 = COMMCAREHQ.toggleEnabled('APP_MANAGER_V2'),
        moduleBrief = initial_page_data('module_brief'),
        moduleType = moduleBrief.module_type,
        options = initial_page_data('js_options') || {};

    hqImport('app_manager/js/app_manager.js').setAppendedPageTitle(django.gettext("Module Settings"));

    // Set up details
    if (!v2 || moduleBrief.case_type) {
        var state = hqImport('app_manager/js/detail-screen-config.js').state;
        var DetailScreenConfig = hqImport('app_manager/js/detail-screen-config.js').DetailScreenConfig;
        state.requires_case_details(moduleBrief.requires_case_details);

        var details = initial_page_data('details');
        for (var i = 0; i < details.length; i++) {
            var detail = details[i];
            var detailScreenConfig = DetailScreenConfig.init({
                module_id: moduleBrief.id,
                moduleUniqueId: moduleBrief.unique_id,
                state: {
                    type: detail.type,
                    short: detail.short,
                    long: detail.long,
                },
                sortRows: detail.sort_elements,
                model: detail.model,
                properties: detail.properties,
                lang: moduleBrief.lang,
                langs: moduleBrief.langs,
                saveUrl: hqImport('hqwebapp/js/urllib.js').reverse('edit_module_detail_screens'),
                parentModules: initial_page_data('parent_modules'),
                childCaseTypes: detail.subcase_types,
                fixture_columns_by_type: options.fixture_columns_by_type || {},
                parentSelect: detail.parent_select,
                fixtureSelect: detail.fixture_select,
                contextVariables: state,
                multimedia: initial_page_data('multimedia_object_map'),
                searchProperties: options.search_properties || [],
                includeClosed: options.include_closed,
                defaultProperties: options.default_properties || [],
                searchButtonDisplayCondition: options.search_button_display_condition,
                blacklistedOwnerIdsExpression: options.blacklisted_owner_ids_expression,
            });

            var $list_home = $("#" + detail.type + "-detail-screen-config-tab");
            $list_home.koApplyBindings(detailScreenConfig);

            if (detail.long !== undefined) {
                var $detail_home = $("#" + detail.type + "-detail-screen-detail-config-tab");
                $detail_home.koApplyBindings(detailScreenConfig);
            }
        }
    }

    // Validation for case type
    var showCaseTypeError = function(message) {
        var $caseTypeError = $('#case_type_error');
        $caseTypeError.css('display', 'block');
        $caseTypeError.text(message);
    };
    var hideCaseTypeError = function() {
        $('#case_type_error').css('display', 'none');
    };
    $('#case_type').on('textchange', function () {
        var $el = $(this),
            value = $el.val(),
            valueNoSpaces = value.replace(/ /g, '_');
        if (value !== valueNoSpaces) {
            var pos = $el.caret('pos');
            $el.val(valueNoSpaces);
            $el.caret('pos', pos);
        }
        if (v2 && !valueNoSpaces) {
            $el.closest('.form-group').addClass('has-error');
            showCaseTypeError(
                gettext("Case type is required.")
            );
            return;
        }
        if (!valueNoSpaces.match(/^[\w-]*$/g)) {
            $el.closest('.form-group').addClass('has-error');
            showCaseTypeError(
                gettext("Case types can only include the characters a-z, 0-9, '-' and '_'")
            );
        } else if (valueNoSpaces === 'commcare-user' && moduleType !== 'advanced') {
            $el.closest('.form-group').addClass('has-error');
            showCaseTypeError(
                gettext("'commcare-user' is a reserved case type. Please change the case type")
            );

        } else {
            $el.closest('.form-group').removeClass('has-error');
            hideCaseTypeError();
        }
    });

    // Module filter
    $(function () {
        var $moduleFilter = $('#module-filter');
        if ($moduleFilter.length) {
            $moduleFilter.koApplyBindings({xpath: initial_page_data("module_filter") || ''});
        }
    });

    // Registration in case list
    if ($('#case-list-form').length) {
        var CaseListForm = function (originalFormId, formOptions) {
            var self = this;

            self.caseListForm = ko.observable(originalFormId);
            self.formMissing = ko.computed(function() {
                return self.caseListForm() && !formOptions[self.caseListForm()];
            });

            var showMedia = function(formId) {
                if (formId) {
                    $("#case_list_media").show();
                } else {
                    $("#case_list_media").hide();
                }
            };

            // Show or hide associated multimedia. Not done in knockout because
            // the multimedia section has its own separate set of knockout bindings
            showMedia(originalFormId);
            self.caseListForm.subscribe(showMedia);
        };
        var case_list_form_options = initial_page_data('case_list_form_options');
            caseListForm = new CaseListForm(
                case_list_form_options ? case_list_form_options.form.form_id : null,
                case_list_form_options ? case_list_form_options.options : []
            );
        $('#case-list-form').koApplyBindings(caseListForm);

        // Reset save button after bindings
        // see http://manage.dimagi.com/default.asp?145851
        var $form = $('#case-list-form').closest('form'),
            $button = $form.find('.save-button-holder').data('button');
        $button.setStateWhenReady('saved');
    }

    if (moduleType === 'shadow') {
        // Shadow module checkboxes for including/excluding forms
        var ShadowModule = hqImport('app_manager/js/shadow-module-settings.js').ShadowModule,
            shadowOptions = initial_page_data('shadow_module_options');
{'fraction_non_alphanumeric': 0.12707444286391656, 'fraction_numerical': 0.002054686265212581, 'mean_word_length': 2.081324567811054, 'pattern_counts': {'":': 0, '<': 5, '<?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': '6718558', 'n_tokens_mistral': 3514, 'n_tokens_neox': 3379, 'n_words': 582}