Subversion Repositories SmartDukaan

Rev

Rev 23786 | Rev 23872 | 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) {
              callback_function(response);
              $('.currency').each(function(index,ele){
                  if(!isNaN(parseInt($(ele).html()))) {
                          $(ele).html(numberToComma($(ele).html()));  
                  }
              });
           }
        });
}

function doGetAjaxRequestWithParamsHandler(urlString, params, callback_function){
        doAjaxRequestWithParamsHandler(urlString, "GET", params, callback_function);
}

function doPostAjaxRequestWithParamsHandler(urlString, params, callback_function){
        doAjaxRequestWithParamsHandler(urlString, "POST", params, callback_function);
}

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);
              $('.currency').each(function(index,ele){
                  if(!isNaN(parseInt($(ele).html()))) {
                          $(ele).html(numberToComma($(ele).html()));  
                  }
              });
           }
        });
}

function doPostAjaxRequestWithJsonHandler(urlString, json, callback_function){
        doAjaxRequestWithJsonHandler(urlString, "POST", json, callback_function);
}

function doPutAjaxRequestWithJsonHandler(urlString, json, callback_function){
        doAjaxRequestWithJsonHandler(urlString, "PUT", json, callback_function);
}

function doAjaxRequestHandler(urlString, httpType, callback_function){
        $.ajax({
           url: urlString,
           async: true,
           cache: false,
           type: httpType,
           success: function(response) {
              callback_function(response);
              $('.currency').each(function(index,ele){
                  if(!isNaN(parseInt($(ele).html()))) {
                          $(ele).html(numberToComma($(ele).html()));  
                  }
              });
           }
        });
}

function doGetAjaxRequestHandler(urlString, callback_function){
        doAjaxRequestHandler(urlString, "GET", callback_function);
}

function doPutAjaxRequestHandler(urlString, callback_function){
        doAjaxRequestHandler(urlString, "PUT", callback_function);
}

function doPostAjaxRequestHandler(urlString, callback_function){
        doAjaxRequestHandler(urlString, "POST", callback_function);
}

function doDeleteAjaxRequestHandler(urlString, callback_function){
        doAjaxRequestHandler(urlString, "DELETE", callback_function);
}

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 === 2){
                if(xhttp.status == 200){
                        xhttp.responseType = "blob";
            } else {
                xhttp.responseType = "text";
            }
            }else 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();
            }else if(xhttp.readyState == 4 && xhttp.status === 400){
                badRequestAlert(xhttp);
            }else if(xhttp.readyState == 4 && xhttp.status === 500){
                internalServerErrorAlert(xhttp);
            }
        };
        // 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 loadPaginatedNextItems(url, params, paginatedIdentifier, tableIdentifier, detailsContainerIdentifier){
        var start = $("#"+paginatedIdentifier+" .start").text();
        var end = $("#"+paginatedIdentifier+" .end").text();
        url = context + url + "?offset=" + end;
        
        if(params != null){
                for (var key in params) {
                        if (params.hasOwnProperty(key)) {
                                //console.log(key + " -> " + p[key]);
                                url = url +"&" + key + "=" + params[key];
                        }
                }
        }
        
        doGetAjaxRequestHandler(url, function(response){
                var size = $("#"+paginatedIdentifier+" .size").text();
                if((parseInt(end) + 10) > parseInt(size)){
                        console.log("(end + 10) > size == true");
                        $("#"+paginatedIdentifier+" .end").text(size);
                }else{
                        console.log("(end + 10) > size == false");
                        $("#"+paginatedIdentifier+" .end").text(+end + +10);
                }
                $("#"+paginatedIdentifier+" .start").text(+start + +10);
                var last = $("#"+paginatedIdentifier+" .end").text();
                var temp = $("#"+paginatedIdentifier+" .size").text();
                if (parseInt(last) >= parseInt(temp)){
                        $("#"+paginatedIdentifier+" .next").prop('disabled', true);
                        //$( "#good-inventory-paginated .end" ).text(temp);
                }
            $('#'+tableIdentifier).html(response);
            if(detailsContainerIdentifier != null){
                        $('#'+detailsContainerIdentifier).html('');
                }
            $("#"+paginatedIdentifier+" .previous").prop('disabled', false);
        });
    
}


function loadPaginatedPreviousItems(url, params, paginatedIdentifier, tableIdentifier, detailsContainerIdentifier){
        var start = $("#"+paginatedIdentifier+" .start").text();
        var end =  $("#"+paginatedIdentifier+" .end").text();
        var size = $("#"+paginatedIdentifier+" .size").text();
        if(parseInt(end) == parseInt(size) && parseInt(end)%10 !=0){
                var mod = parseInt(end) % 10;
                end = parseInt(end) + (10 - mod); 
        }
        var pre = end - 20;
        
        url = context + url + "?offset=" + pre;
        
        if(params != null){
                for (var key in params) {
                        if (params.hasOwnProperty(key)) {
                                url = url +"&" + key + "=" + params[key];
                        }
                }
        }
        
        doGetAjaxRequestHandler(url, function(response){
                $("#"+paginatedIdentifier+" .end").text(+end - +10);
                $("#"+paginatedIdentifier+" .start").text(+start - +10);
                $('#'+tableIdentifier).html(response);
                if(detailsContainerIdentifier != null){
                        $('#'+detailsContainerIdentifier).html('');
                }
                $("#"+paginatedIdentifier+" .next").prop('disabled', false);
                if (parseInt(pre)==0){
                        $("#"+paginatedIdentifier+" .previous").prop('disabled', true);
                }
        });     
        
}


function numberToComma(x) {
        x=parseInt(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;
}