Subversion Repositories SmartDukaan

Rev

Rev 23494 | Rev 23629 | 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
 
23500 ashik.ali 47
function doGetAjaxRequestWithParamsHandler(urlString, params, callback_function){
48
	doAjaxRequestWithParamsHandler(urlString, "GET", params, callback_function);
49
}
50
 
51
function doPostAjaxRequestWithParamsHandler(urlString, params, callback_function){
52
	doAjaxRequestWithParamsHandler(urlString, "POST", params, callback_function);
53
}
54
 
23032 ashik.ali 55
function doAjaxRequestWithJsonHandler(urlString, httpType, json, callback_function){
56
	$.ajax({
57
	   url: urlString,
23343 ashik.ali 58
	   async: true,
23032 ashik.ali 59
	   cache: false,
60
	   processData: false,
61
	   data: json,
62
       contentType:'application/json',
63
	   type: httpType,
64
	   success: function(response) {
65
	      //console.log("response"+JSON.stringify(data));
66
	      callback_function(response);
67
	   }
68
	});
69
}
70
 
23500 ashik.ali 71
function doPostAjaxRequestWithJsonHandler(urlString, json, callback_function){
72
	doAjaxRequestWithJsonHandler(urlString, "POST", json, callback_function);
73
}
23032 ashik.ali 74
 
23500 ashik.ali 75
function doPutAjaxRequestWithJsonHandler(urlString, json, callback_function){
76
	doAjaxRequestWithJsonHandler(urlString, "PUT", json, callback_function);
77
}
78
 
23026 ashik.ali 79
function doAjaxRequestHandler(urlString, httpType, callback_function){
22982 ashik.ali 80
	$.ajax({
81
	   url: urlString,
23343 ashik.ali 82
	   async: true,
23026 ashik.ali 83
	   cache: false,
22982 ashik.ali 84
	   type: httpType,
85
	   success: function(response) {
86
	      //console.log("response"+JSON.stringify(data));
87
	      callback_function(response);
88
	   }
89
	});
90
}
91
 
23500 ashik.ali 92
function doGetAjaxRequestHandler(urlString, callback_function){
93
	doAjaxRequestHandler(urlString, "GET", callback_function);
94
}
95
 
96
function doPutAjaxRequestHandler(urlString, callback_function){
97
	doAjaxRequestHandler(urlString, "PUT", callback_function);
98
}
99
 
23347 ashik.ali 100
function doAjaxUploadRequest(urlString, httpType, file){
101
	var response;
102
	doAjaxUploadRequestHandler(urlString, httpType, file, function(ajaxResponse){
103
		response = ajaxResponse;
104
	});
105
	return response;
106
}
107
 
23026 ashik.ali 108
function doAjaxUploadRequestHandler(urlString, httpType, file, callback_function){
22982 ashik.ali 109
	var formData = new FormData();
110
	formData.append("file", file);
111
	$.ajax({
112
	   url: urlString,
113
	   type: httpType,
114
	   data: formData,
115
       dataType: 'json',
23343 ashik.ali 116
       async: true,
22982 ashik.ali 117
       cache: false,
118
       contentType: false,
119
       enctype: 'multipart/form-data',
120
       processData: false,
121
	   success: function(response) {
122
	      //console.log("response"+JSON.stringify(data));
123
	      callback_function(response);
124
	   }
125
	});
126
}
127
 
23347 ashik.ali 128
function uploadDocument(file){
23379 ashik.ali 129
	var url = 'http://'+ webApiHost + ':' + webApiPort + '/profitmandi-web/document-upload';
23377 ashik.ali 130
	doAjaxUploadRequestHandler(url, 'POST', file, function(response){
131
		var documentId = response.response.document_id; 
132
		console.log("documentId : "+documentId);
133
		return documentId;
134
	});
23347 ashik.ali 135
}
136
 
23377 ashik.ali 137
function doAjaxGetDownload(urlString, fileName){
138
	console.log("fileName : "+fileName);
23343 ashik.ali 139
	doAjaxDownload(urlString, "GET", null, fileName);
22982 ashik.ali 140
}
141
 
23377 ashik.ali 142
function doAjaxPostDownload(urlString, data, fileName){
23343 ashik.ali 143
	doAjaxDownload(urlString, "POST", data, fileName);
21627 kshitij.so 144
}
145
 
23343 ashik.ali 146
function doAjaxDownload(urlString, httpType, data, fileName){
22488 ashik.ali 147
	xhttp = new XMLHttpRequest();
148
	xhttp.onreadystatechange = function() {
149
	    var a;
23494 ashik.ali 150
	    if(xhttp.readyState === 2){
151
	    	if(xhttp.status == 200){
152
	    		xhttp.responseType = "blob";
153
            } else {
154
                xhttp.responseType = "text";
155
            }
156
	    }else if (xhttp.readyState === 4 && xhttp.status === 200) {
22488 ashik.ali 157
	        // Trick for making downloadable link
158
	        a = document.createElement('a');
159
	        a.href = window.URL.createObjectURL(xhttp.response);
160
	        // Give filename you wish to download
23343 ashik.ali 161
	        a.download = fileName;
22488 ashik.ali 162
	        a.style.display = 'none';
163
	        document.body.appendChild(a);
164
	        a.click();
23494 ashik.ali 165
	    }else if(xhttp.readyState == 4 && xhttp.status === 400){
166
    		badRequestAlert(xhttp);
167
	    }else if(xhttp.readyState == 4 && xhttp.status === 500){
168
	    	internalServerErrorAlert(xhttp);
22488 ashik.ali 169
	    }
170
	};
171
	// Post data to URL which handles post request
23343 ashik.ali 172
	xhttp.open(httpType, urlString);
173
	if(httpType == "POST"){
174
		xhttp.setRequestHeader("Content-Type", "application/json");
175
	}
22488 ashik.ali 176
	// You should set responseType as blob for binary responses
23494 ashik.ali 177
	//xhttp.responseType = 'blob';
22488 ashik.ali 178
	xhttp.send(data);
23405 amit.gupta 179
}
180
 
181
 
182
function numberToComma(x) {
183
	x=x.toString();
184
	var lastThree = x.substring(x.length-3);
185
	var otherNumbers = x.substring(0,x.length-3);
186
	if(otherNumbers != '')
187
	    lastThree = ',' + lastThree;
188
	return otherNumbers.replace(/\B(?=(\d{2})+(?!\d))/g, ",") + lastThree;
23347 ashik.ali 189
}