Subversion Repositories SmartDukaan

Rev

Rev 23379 | Rev 23494 | Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

function badRequestAlert(response){
        console.log(response.responseText);
        var errorObject = JSON.parse(response.responseText);
        errorObject = errorObject.response;
        alert('Bad Request\n'+
                        'rejectedType : '+errorObject.rejectedType+'\n'+
                        'rejectedValue : '+errorObject.rejectedValue+'\n'+
                        'message : '+errorObject.message);
}
        
function internalServerErrorAlert(response){
        console.log(response.responseText);
        var errorObject = JSON.parse(response.responseText);
        errorObject = errorObject.response;
        alert('Internal Server Error\n'+
                'rejectedType : '+errorObject.rejectedType+'\n'+
                'rejectedValue : '+errorObject.rejectedValue+'\n'+
                'message : '+errorObject.message);
}

$( document ).ajaxError(function(event, jqxhr, settings, thrownError) {
        //$( ".log" ).text( "Triggered ajaxError handler." );
        if(jqxhr.status == 400){
                //$('#error-prompt-model').modal();
                badRequestAlert(jqxhr); 
        }else{
                internalServerErrorAlert(jqxhr);
        }
});


function doAjaxRequestWithParamsHandler(urlString, httpType, params, callback_function){
        $.ajax({
           url: urlString,
           async: true,
           cache: false,
           data: params,
       //dataType:'json',
           type: httpType,
           success: function(response) {
              //console.log("response"+JSON.stringify(data));
              callback_function(response);
           }
        });
}

function doAjaxRequestWithJsonHandler(urlString, httpType, json, callback_function){
        $.ajax({
           url: urlString,
           async: true,
           cache: false,
           processData: false,
           data: json,
       contentType:'application/json',
           type: httpType,
           success: function(response) {
              //console.log("response"+JSON.stringify(data));
              callback_function(response);
           }
        });
}


function doAjaxRequestHandler(urlString, httpType, callback_function){
        $.ajax({
           url: urlString,
           async: true,
           cache: false,
           type: httpType,
           success: function(response) {
              //console.log("response"+JSON.stringify(data));
              callback_function(response);
           }
        });
}

function doAjaxUploadRequest(urlString, httpType, file){
        var response;
        doAjaxUploadRequestHandler(urlString, httpType, file, function(ajaxResponse){
                response = ajaxResponse;
        });
        return response;
}

function doAjaxUploadRequestHandler(urlString, httpType, file, callback_function){
        var formData = new FormData();
        formData.append("file", file);
        $.ajax({
           url: urlString,
           type: httpType,
           data: formData,
       dataType: 'json',
       async: true,
       cache: false,
       contentType: false,
       enctype: 'multipart/form-data',
       processData: false,
           success: function(response) {
              //console.log("response"+JSON.stringify(data));
              callback_function(response);
           }
        });
}

function uploadDocument(file){
        var url = 'http://'+ webApiHost + ':' + webApiPort + '/profitmandi-web/document-upload';
        doAjaxUploadRequestHandler(url, 'POST', file, function(response){
                var documentId = response.response.document_id; 
                console.log("documentId : "+documentId);
                return documentId;
        });
}

function doAjaxGetDownload(urlString, fileName){
        console.log("fileName : "+fileName);
        doAjaxDownload(urlString, "GET", null, fileName);
}

function doAjaxPostDownload(urlString, data, fileName){
        doAjaxDownload(urlString, "POST", data, fileName);
}

function doAjaxDownload(urlString, httpType, data, fileName){
        xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function() {
            var a;
            if (xhttp.readyState === 4 && xhttp.status === 200) {
                // Trick for making downloadable link
                a = document.createElement('a');
                a.href = window.URL.createObjectURL(xhttp.response);
                // Give filename you wish to download
                a.download = fileName;
                a.style.display = 'none';
                document.body.appendChild(a);
                a.click();
            }
            if(xhttp.readyState == 4 && xhttp.status === 400){
                        badRequestAlert(xhttp.responseText);
            }
            if(xhttp.readyState == 4 && xhttp.status === 500){
                        internalServerErrorAlert(xhttp.responseText);
            }
        };
        // Post data to URL which handles post request
        xhttp.open(httpType, urlString);
        if(httpType == "POST"){
                xhttp.setRequestHeader("Content-Type", "application/json");
        }
        // You should set responseType as blob for binary responses
        xhttp.responseType = 'blob';
        xhttp.send(data);
}


function numberToComma(x) {
        x=x.toString();
        var lastThree = x.substring(x.length-3);
        var otherNumbers = x.substring(0,x.length-3);
        if(otherNumbers != '')
            lastThree = ',' + lastThree;
        return otherNumbers.replace(/\B(?=(\d{2})+(?!\d))/g, ",") + lastThree;
}