Subversion Repositories SmartDukaan

Rev

Rev 23405 | Rev 23500 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
22956 ashik.ali 1
function badRequestAlert(response){
2
	console.log(response.responseText);
3
	var errorObject = JSON.parse(response.responseText);
4
	errorObject = errorObject.response;
5
	alert('Bad Request\n'+
6
			'rejectedType : '+errorObject.rejectedType+'\n'+
7
			'rejectedValue : '+errorObject.rejectedValue+'\n'+
8
			'message : '+errorObject.message);
9
}
23347 ashik.ali 10
 
22956 ashik.ali 11
function internalServerErrorAlert(response){
12
	console.log(response.responseText);
13
	var errorObject = JSON.parse(response.responseText);
14
	errorObject = errorObject.response;
15
	alert('Internal Server Error\n'+
16
		'rejectedType : '+errorObject.rejectedType+'\n'+
17
		'rejectedValue : '+errorObject.rejectedValue+'\n'+
18
		'message : '+errorObject.message);
19
}
20
 
21
$( document ).ajaxError(function(event, jqxhr, settings, thrownError) {
22
	//$( ".log" ).text( "Triggered ajaxError handler." );
23
	if(jqxhr.status == 400){
22982 ashik.ali 24
		//$('#error-prompt-model').modal();
22956 ashik.ali 25
		badRequestAlert(jqxhr);	
26
	}else{
27
		internalServerErrorAlert(jqxhr);
28
	}
29
});
30
 
23026 ashik.ali 31
 
23193 ashik.ali 32
function doAjaxRequestWithParamsHandler(urlString, httpType, params, callback_function){
33
	$.ajax({
34
	   url: urlString,
23343 ashik.ali 35
	   async: true,
23193 ashik.ali 36
	   cache: false,
37
	   data: params,
38
       //dataType:'json',
39
	   type: httpType,
40
	   success: function(response) {
41
	      //console.log("response"+JSON.stringify(data));
42
	      callback_function(response);
43
	   }
44
	});
45
}
46
 
23032 ashik.ali 47
function doAjaxRequestWithJsonHandler(urlString, httpType, json, callback_function){
48
	$.ajax({
49
	   url: urlString,
23343 ashik.ali 50
	   async: true,
23032 ashik.ali 51
	   cache: false,
52
	   processData: false,
53
	   data: json,
54
       contentType:'application/json',
55
	   type: httpType,
56
	   success: function(response) {
57
	      //console.log("response"+JSON.stringify(data));
58
	      callback_function(response);
59
	   }
60
	});
61
}
62
 
63
 
23026 ashik.ali 64
function doAjaxRequestHandler(urlString, httpType, callback_function){
22982 ashik.ali 65
	$.ajax({
66
	   url: urlString,
23343 ashik.ali 67
	   async: true,
23026 ashik.ali 68
	   cache: false,
22982 ashik.ali 69
	   type: httpType,
70
	   success: function(response) {
71
	      //console.log("response"+JSON.stringify(data));
72
	      callback_function(response);
73
	   }
74
	});
75
}
76
 
23347 ashik.ali 77
function doAjaxUploadRequest(urlString, httpType, file){
78
	var response;
79
	doAjaxUploadRequestHandler(urlString, httpType, file, function(ajaxResponse){
80
		response = ajaxResponse;
81
	});
82
	return response;
83
}
84
 
23026 ashik.ali 85
function doAjaxUploadRequestHandler(urlString, httpType, file, callback_function){
22982 ashik.ali 86
	var formData = new FormData();
87
	formData.append("file", file);
88
	$.ajax({
89
	   url: urlString,
90
	   type: httpType,
91
	   data: formData,
92
       dataType: 'json',
23343 ashik.ali 93
       async: true,
22982 ashik.ali 94
       cache: false,
95
       contentType: false,
96
       enctype: 'multipart/form-data',
97
       processData: false,
98
	   success: function(response) {
99
	      //console.log("response"+JSON.stringify(data));
100
	      callback_function(response);
101
	   }
102
	});
103
}
104
 
23347 ashik.ali 105
function uploadDocument(file){
23379 ashik.ali 106
	var url = 'http://'+ webApiHost + ':' + webApiPort + '/profitmandi-web/document-upload';
23377 ashik.ali 107
	doAjaxUploadRequestHandler(url, 'POST', file, function(response){
108
		var documentId = response.response.document_id; 
109
		console.log("documentId : "+documentId);
110
		return documentId;
111
	});
23347 ashik.ali 112
}
113
 
23377 ashik.ali 114
function doAjaxGetDownload(urlString, fileName){
115
	console.log("fileName : "+fileName);
23343 ashik.ali 116
	doAjaxDownload(urlString, "GET", null, fileName);
22982 ashik.ali 117
}
118
 
23377 ashik.ali 119
function doAjaxPostDownload(urlString, data, fileName){
23343 ashik.ali 120
	doAjaxDownload(urlString, "POST", data, fileName);
21627 kshitij.so 121
}
122
 
23343 ashik.ali 123
function doAjaxDownload(urlString, httpType, data, fileName){
22488 ashik.ali 124
	xhttp = new XMLHttpRequest();
125
	xhttp.onreadystatechange = function() {
126
	    var a;
23494 ashik.ali 127
	    if(xhttp.readyState === 2){
128
	    	if(xhttp.status == 200){
129
	    		xhttp.responseType = "blob";
130
            } else {
131
                xhttp.responseType = "text";
132
            }
133
	    }else if (xhttp.readyState === 4 && xhttp.status === 200) {
22488 ashik.ali 134
	        // Trick for making downloadable link
135
	        a = document.createElement('a');
136
	        a.href = window.URL.createObjectURL(xhttp.response);
137
	        // Give filename you wish to download
23343 ashik.ali 138
	        a.download = fileName;
22488 ashik.ali 139
	        a.style.display = 'none';
140
	        document.body.appendChild(a);
141
	        a.click();
23494 ashik.ali 142
	    }else if(xhttp.readyState == 4 && xhttp.status === 400){
143
    		badRequestAlert(xhttp);
144
	    }else if(xhttp.readyState == 4 && xhttp.status === 500){
145
	    	internalServerErrorAlert(xhttp);
22488 ashik.ali 146
	    }
147
	};
148
	// Post data to URL which handles post request
23343 ashik.ali 149
	xhttp.open(httpType, urlString);
150
	if(httpType == "POST"){
151
		xhttp.setRequestHeader("Content-Type", "application/json");
152
	}
22488 ashik.ali 153
	// You should set responseType as blob for binary responses
23494 ashik.ali 154
	//xhttp.responseType = 'blob';
22488 ashik.ali 155
	xhttp.send(data);
23405 amit.gupta 156
}
157
 
158
 
159
function numberToComma(x) {
160
	x=x.toString();
161
	var lastThree = x.substring(x.length-3);
162
	var otherNumbers = x.substring(0,x.length-3);
163
	if(otherNumbers != '')
164
	    lastThree = ',' + lastThree;
165
	return otherNumbers.replace(/\B(?=(\d{2})+(?!\d))/g, ",") + lastThree;
23347 ashik.ali 166
}