<commit_before>
dhis2.util.namespace( 'dhis2.appr' );

dhis2.appr.currentPeriodOffset = 0;
dhis2.appr.permissions = null;
dhis2.appr.dataSets = {};
dhis2.appr.dataApprovalLevels = {};

/**
 * Object with properties: ds, pe, ou, array of approvals with aoc, ou.
 */
dhis2.appr.uiState = {};

//------------------------------------------------------------------------------
// Report
//------------------------------------------------------------------------------

/**
 * Page init.
 */
$( document ).ready( function() 
{
	$.getJSON( "../api/dataSets.json?fields=id,displayName,periodType,workflow,categoryCombo[id,displayName]&paging=false", function( json ) {
				
		var dsHtml = "<option value=''>[ Select ]</option>";
		
		$.each( json.dataSets, function( inx, ds ) {
			if ( ds.workflow ) {
				ds.hasCategoryCombo = !!( ds.categoryCombo.displayName !== "default" );
				dsHtml += "<option value='" + ds.id + "'>" + ds.displayName + "</option>";
				dhis2.appr.dataSets[ds.id] = ds;
			}
		} );
		
		$( "#dataSetId" ).html( dsHtml );
	} );
	
	$.getJSON( "../api/dataApprovalLevels.json", function( json ) {
		$.each( json.dataApprovalLevels, function( inx, al ) {
			dhis2.appr.dataApprovalLevels[al.id] = al;
		} );
	} );
} );

/**
 * Callback for changes in data set. Displays a list of period types starting
 * with the data set's period as the shortest type, and including all longer
 * types so that approvals can be made for multiple periods. If there is a
 * current period type selection, and it is still on the new list of period
 * types, keep it. Otherwise choose the period type for the data set.
 */
dhis2.appr.dataSetSelected = function()
{
    var ds = dhis2.appr.dataSets[$( "#dataSetId :selected" ).val()];
    var dataSetPeriodType = dhis2.appr.dataSets[ds.id].periodType;

    if ( $( "#periodType" ).val() != dataSetPeriodType ) {
        var periodTypeToSelect = $( "#periodType" ).val() || dataSetPeriodType;
        var foundDataSetPeriodType = false;
        var html = "<option value=''>[ " + i18n_select_period_type + " ]</option>";

        $.each( dhis2.appr.metaData.periodTypes, function () {
            if ( foundDataSetPeriodType || this == dataSetPeriodType ) {
                var selected = ( this == periodTypeToSelect ) ? " selected" : "";
                html += "<option value='" + this + "'" + selected + ">" + this + "</option>";
                foundDataSetPeriodType = true;
            } 
            else if ( this == periodTypeToSelect ) {
                periodTypeToSelect = dataSetPeriodType;
            }
        } );

        $( "#periodType" ).html( html );
        $( "#periodType" ).removeAttr( "disabled" );
        dhis2.appr.displayPeriods();
    }
    
    dhis2.appr.setCategoryComboDiv( ds );
    dhis2.appr.clearItemsDialog();
};

/**
 * Sets the category combo div HTML content. 
 * @param ds a data set object.
 */
dhis2.appr.setCategoryComboDiv = function( ds )
{
	if ( ds.hasCategoryCombo ) {
		var cc = ds.categoryCombo;
		var html = cc.displayName + "<br><a href='javascript:dhis2.appr.showItemsDialog()'>" + i18n_select_items + "</a>";<commit_msg>Force approval UI periodType to match workflow periodType DHIS2-2738 (#1890)<commit_after>
dhis2.util.namespace( 'dhis2.appr' );

dhis2.appr.currentPeriodOffset = 0;
dhis2.appr.permissions = null;
dhis2.appr.dataSets = {};
dhis2.appr.dataApprovalLevels = {};

/**
 * Object with properties: ds, pe, ou, array of approvals with aoc, ou.
 */
dhis2.appr.uiState = {};

//------------------------------------------------------------------------------
// Report
//------------------------------------------------------------------------------

/**
 * Page init.
 */
$( document ).ready( function() 
{
	$.getJSON( "../api/dataSets.json?fields=id,displayName,workflow[id,periodType],categoryCombo[id,displayName]&paging=false", function( json ) {
				
		var dsHtml = "<option value=''>[ Select ]</option>";
		
		$.each( json.dataSets, function( inx, ds ) {
			if ( ds.workflow ) {
				ds.hasCategoryCombo = !!( ds.categoryCombo.displayName !== "default" );
				dsHtml += "<option value='" + ds.id + "'>" + ds.displayName + "</option>";
				dhis2.appr.dataSets[ds.id] = ds;
			}
		} );
		
		$( "#dataSetId" ).html( dsHtml );
	} );
	
	$.getJSON( "../api/dataApprovalLevels.json", function( json ) {
		$.each( json.dataApprovalLevels, function( inx, al ) {
			dhis2.appr.dataApprovalLevels[al.id] = al;
		} );
	} );
} );

/**
 * Callback for changes in data set. Displays the period type of the workflow
 * associated with the data set.
 */
dhis2.appr.dataSetSelected = function()
{
    var ds = dhis2.appr.dataSets[$( "#dataSetId :selected" ).val()];
    var workflowPeriodType = dhis2.appr.dataSets[ds.id].workflow.periodType;

    if ( $( "#periodType" ).val() != workflowPeriodType ) {
        var html = "<option value='" + workflowPeriodType + "' selected>" + workflowPeriodType + "</option>"
        $( "#periodType" ).html( html );
        dhis2.appr.displayPeriods();
    }
    
    dhis2.appr.setCategoryComboDiv( ds );
    dhis2.appr.clearItemsDialog();
};

/**
 * Sets the category combo div HTML content. 
 * @param ds a data set object.
 */
dhis2.appr.setCategoryComboDiv = function( ds )
{
	if ( ds.hasCategoryCombo ) {
		var cc = ds.categoryCombo;
		var html = cc.displayName + "<br><a href='javascript:dhis2.appr.showItemsDialog()'>" + i18n_select_items + "</a>";
{'fraction_non_alphanumeric': 0.2155444258950102, 'fraction_numerical': 0.009645705805972918, 'mean_word_length': 2.512703583061889, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, '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': '6351191', 'n_tokens_mistral': 1819, 'n_tokens_neox': 1724, 'n_words': 384}