QScript Functions for Moving Variables

From Q
Jump to navigation Jump to search
This page is currently under construction, or it refers to features which are under development and not yet available for use.
This page is under construction. Its contents are only visible to developers!
includeWeb('QScript Selection Functions'); // for requestOneDataFileFromProject
includeWeb('QScript Utility Functions');   // for inDisplayr

// tag_type: string for the variable type to move, one of either filter, hidden or weight
// position: string, either top or bottom
moveTaggedVariablesToTopOrBottom = function(tag_type = 'Filter', position = 'top') {
    let data_file = requestOneDataFileFromProject(last_as_default = false);
    const allowed_tag_types = ['Filter', 'Hidden', 'Weight'];
    let all_questions = data_file.questions;
    if (!allowed_tag_types.includes(tag_type))
        throw new UserError('Possible tag types are "' + allowed_tag_types.join('", "') + '" ');
    let all_relevant_questions = all_questions.filter(vs => vs['is' + tag_type]);
    let data_file_name = data_file.name;
    if (all_relevant_questions.length === 0)
        log(correctTerminology('No ' + tag_type + ' questions found in the ' + data_file_name + ' Data Set'));
    else {
        let relevant_array = getVariablesFromQuestions(all_relevant_questions);
        if (!['top', 'bottom'].includes(position))
            throw new UserError('Possible position arguments are top or bottom');
        let final_position = position === 'top' ? null : data_file.variables[data_file.variables.length - 1];
        data_file.moveAfter(relevant_array, final_position);
        if (!inDisplayr())
        {
            let variable_tag = tag_type === 'filter' ? tag_type : (tag_type + ' variable');
            variable_tag = variable_tag.toLowerCase();
            variable_tag += relevant_array.length === 1 ? '' : 's';
            log('Moved ' + relevant_array.length + ' ' + variable_tag + ' to the ' + position +
                ' in the Variables and Questions tab for ' + data_file_name + '.');
        }
    }
}