import {activeLoader, desactiveLoader} from 'Global/Ajax/loader'

export function refreshPage() {
    window.location = window.location.pathname;
}


export function refreshFonction(id, content) {
    let selector = '#'+id;
    if (id.length == 0) {
        selector = '.contenu';
    }

    var result = $('<div />').append(content).find(selector).html();

    $(selector).html(result);
}

export function refrechDiv(path, id, form, menu, paramJson)
{
    let data = null;
    activeLoader();
    if (
        (paramJson != null || paramJson != 'undefined' )
        && (form == 'undefined' || form == null || form == '')
    ) {
        data = paramJson;
    } else if (form) {
        data = $('#'+form).serialize();
    } else {
        data = '';
    }

    $.ajax({
        url: path,
        type: 'POST',
        dataType: 'html',
        data : data,
        success: function (htmlData) {
            refreshFonction(id, htmlData);
            desactiveLoader();
            return true;
        }
    });	
    
    return false;

}

export function redirectJSWithPost(url, values, method, target, traditional, redirectTop)
{
    if(values)
    {
        values[$('#tokenCSRF').attr('name')] = $('#tokenCSRF').val();
    }
    $.redirect(url, values, method, target, traditional, redirectTop);
}