Subversion Repositories SmartDukaan

Rev

Rev 23786 | Rev 23870 | 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
	      callback_function(response);
23786 amit.gupta 42
	      $('.currency').each(function(index,ele){
43
	    	  if(!isNaN(parseInt($(ele).html()))) {
44
	    		  $(ele).html(numberToComma($(ele).html()));  
45
	    	  }
46
	      });
23193 ashik.ali 47
	   }
48
	});
49
}
50
 
23500 ashik.ali 51
function doGetAjaxRequestWithParamsHandler(urlString, params, callback_function){
52
	doAjaxRequestWithParamsHandler(urlString, "GET", params, callback_function);
53
}
54
 
55
function doPostAjaxRequestWithParamsHandler(urlString, params, callback_function){
56
	doAjaxRequestWithParamsHandler(urlString, "POST", params, callback_function);
57
}
58
 
23032 ashik.ali 59
function doAjaxRequestWithJsonHandler(urlString, httpType, json, callback_function){
60
	$.ajax({
61
	   url: urlString,
23343 ashik.ali 62
	   async: true,
23032 ashik.ali 63
	   cache: false,
64
	   processData: false,
65
	   data: json,
66
       contentType:'application/json',
67
	   type: httpType,
68
	   success: function(response) {
69
	      //console.log("response"+JSON.stringify(data));
70
	      callback_function(response);
23786 amit.gupta 71
	      $('.currency').each(function(index,ele){
72
	    	  if(!isNaN(parseInt($(ele).html()))) {
73
	    		  $(ele).html(numberToComma($(ele).html()));  
74
	    	  }
75
	      });
23032 ashik.ali 76
	   }
77
	});
78
}
79
 
23500 ashik.ali 80
function doPostAjaxRequestWithJsonHandler(urlString, json, callback_function){
81
	doAjaxRequestWithJsonHandler(urlString, "POST", json, callback_function);
82
}
23032 ashik.ali 83
 
23500 ashik.ali 84
function doPutAjaxRequestWithJsonHandler(urlString, json, callback_function){
85
	doAjaxRequestWithJsonHandler(urlString, "PUT", json, callback_function);
86
}
87
 
23026 ashik.ali 88
function doAjaxRequestHandler(urlString, httpType, callback_function){
22982 ashik.ali 89
	$.ajax({
90
	   url: urlString,
23343 ashik.ali 91
	   async: true,
23026 ashik.ali 92
	   cache: false,
22982 ashik.ali 93
	   type: httpType,
94
	   success: function(response) {
95
	      callback_function(response);
23786 amit.gupta 96
	      $('.currency').each(function(index,ele){
97
	    	  if(!isNaN(parseInt($(ele).html()))) {
98
	    		  $(ele).html(numberToComma($(ele).html()));  
99
	    	  }
100
	      });
22982 ashik.ali 101
	   }
102
	});
103
}
104
 
23500 ashik.ali 105
function doGetAjaxRequestHandler(urlString, callback_function){
106
	doAjaxRequestHandler(urlString, "GET", callback_function);
107
}
108
 
109
function doPutAjaxRequestHandler(urlString, callback_function){
110
	doAjaxRequestHandler(urlString, "PUT", callback_function);
111
}
112
 
23629 ashik.ali 113
function doPostAjaxRequestHandler(urlString, callback_function){
114
	doAjaxRequestHandler(urlString, "POST", callback_function);
115
}
116
 
23783 ashik.ali 117
function doDeleteAjaxRequestHandler(urlString, callback_function){
118
	doAjaxRequestHandler(urlString, "DELETE", callback_function);
119
}
120
 
23347 ashik.ali 121
function doAjaxUploadRequest(urlString, httpType, file){
122
	var response;
123
	doAjaxUploadRequestHandler(urlString, httpType, file, function(ajaxResponse){
124
		response = ajaxResponse;
125
	});
126
	return response;
127
}
128
 
23026 ashik.ali 129
function doAjaxUploadRequestHandler(urlString, httpType, file, callback_function){
22982 ashik.ali 130
	var formData = new FormData();
131
	formData.append("file", file);
132
	$.ajax({
133
	   url: urlString,
134
	   type: httpType,
135
	   data: formData,
136
       dataType: 'json',
23343 ashik.ali 137
       async: true,
22982 ashik.ali 138
       cache: false,
139
       contentType: false,
140
       enctype: 'multipart/form-data',
141
       processData: false,
142
	   success: function(response) {
143
	      //console.log("response"+JSON.stringify(data));
144
	      callback_function(response);
145
	   }
146
	});
147
}
148
 
23347 ashik.ali 149
function uploadDocument(file){
23379 ashik.ali 150
	var url = 'http://'+ webApiHost + ':' + webApiPort + '/profitmandi-web/document-upload';
23377 ashik.ali 151
	doAjaxUploadRequestHandler(url, 'POST', file, function(response){
152
		var documentId = response.response.document_id; 
153
		console.log("documentId : "+documentId);
154
		return documentId;
155
	});
23347 ashik.ali 156
}
157
 
23377 ashik.ali 158
function doAjaxGetDownload(urlString, fileName){
159
	console.log("fileName : "+fileName);
23343 ashik.ali 160
	doAjaxDownload(urlString, "GET", null, fileName);
22982 ashik.ali 161
}
162
 
23377 ashik.ali 163
function doAjaxPostDownload(urlString, data, fileName){
23343 ashik.ali 164
	doAjaxDownload(urlString, "POST", data, fileName);
21627 kshitij.so 165
}
166
 
23343 ashik.ali 167
function doAjaxDownload(urlString, httpType, data, fileName){
22488 ashik.ali 168
	xhttp = new XMLHttpRequest();
169
	xhttp.onreadystatechange = function() {
170
	    var a;
23494 ashik.ali 171
	    if(xhttp.readyState === 2){
172
	    	if(xhttp.status == 200){
173
	    		xhttp.responseType = "blob";
174
            } else {
175
                xhttp.responseType = "text";
176
            }
177
	    }else if (xhttp.readyState === 4 && xhttp.status === 200) {
22488 ashik.ali 178
	        // Trick for making downloadable link
179
	        a = document.createElement('a');
180
	        a.href = window.URL.createObjectURL(xhttp.response);
181
	        // Give filename you wish to download
23343 ashik.ali 182
	        a.download = fileName;
22488 ashik.ali 183
	        a.style.display = 'none';
184
	        document.body.appendChild(a);
185
	        a.click();
23494 ashik.ali 186
	    }else if(xhttp.readyState == 4 && xhttp.status === 400){
187
    		badRequestAlert(xhttp);
188
	    }else if(xhttp.readyState == 4 && xhttp.status === 500){
189
	    	internalServerErrorAlert(xhttp);
22488 ashik.ali 190
	    }
191
	};
192
	// Post data to URL which handles post request
23343 ashik.ali 193
	xhttp.open(httpType, urlString);
194
	if(httpType == "POST"){
195
		xhttp.setRequestHeader("Content-Type", "application/json");
196
	}
22488 ashik.ali 197
	// You should set responseType as blob for binary responses
23494 ashik.ali 198
	//xhttp.responseType = 'blob';
22488 ashik.ali 199
	xhttp.send(data);
23405 amit.gupta 200
}
201
 
23629 ashik.ali 202
function loadPaginatedNextItems(url, params, paginatedIdentifier, tableIdentifier, detailsContainerIdentifier){
203
	var start = $("#"+paginatedIdentifier+" .start").text();
204
	var end = $("#"+paginatedIdentifier+" .end").text();
205
	url = context + url + "?offset=" + end;
206
 
207
	if(params != null){
208
		for (var key in params) {
209
			if (params.hasOwnProperty(key)) {
210
				//console.log(key + " -> " + p[key]);
211
				url = url +"&" + key + "=" + params[key];
212
			}
213
		}
214
	}
215
 
216
	doGetAjaxRequestHandler(url, function(response){
217
		var size = $("#"+paginatedIdentifier+" .size").text();
218
		if((parseInt(end) + 10) > parseInt(size)){
219
			console.log("(end + 10) > size == true");
220
			$("#"+paginatedIdentifier+" .end").text(size);
221
		}else{
222
			console.log("(end + 10) > size == false");
223
			$("#"+paginatedIdentifier+" .end").text(+end + +10);
224
		}
225
		$("#"+paginatedIdentifier+" .start").text(+start + +10);
226
		var last = $("#"+paginatedIdentifier+" .end").text();
227
		var temp = $("#"+paginatedIdentifier+" .size").text();
228
		if (parseInt(last) >= parseInt(temp)){
229
			$("#"+paginatedIdentifier+" .next").prop('disabled', true);
230
			//$( "#good-inventory-paginated .end" ).text(temp);
231
		}
232
	    $('#'+tableIdentifier).html(response);
233
	    if(detailsContainerIdentifier != null){
234
			$('#'+detailsContainerIdentifier).html('');
235
		}
236
	    $("#"+paginatedIdentifier+" .previous").prop('disabled', false);
237
	});
238
 
239
}
23405 amit.gupta 240
 
23629 ashik.ali 241
 
242
function loadPaginatedPreviousItems(url, params, paginatedIdentifier, tableIdentifier, detailsContainerIdentifier){
243
	var start = $("#"+paginatedIdentifier+" .start").text();
244
	var end =  $("#"+paginatedIdentifier+" .end").text();
245
	var size = $("#"+paginatedIdentifier+" .size").text();
23819 govind 246
	if(parseInt(end) == parseInt(size) && parseInt(end)%10 !=0){
23629 ashik.ali 247
		var mod = parseInt(end) % 10;
248
		end = parseInt(end) + (10 - mod); 
249
	}
250
	var pre = end - 20;
251
 
252
	url = context + url + "?offset=" + pre;
253
 
254
	if(params != null){
255
		for (var key in params) {
256
			if (params.hasOwnProperty(key)) {
257
				url = url +"&" + key + "=" + params[key];
258
			}
259
		}
260
	}
261
 
262
	doGetAjaxRequestHandler(url, function(response){
263
		$("#"+paginatedIdentifier+" .end").text(+end - +10);
264
		$("#"+paginatedIdentifier+" .start").text(+start - +10);
265
		$('#'+tableIdentifier).html(response);
266
		if(detailsContainerIdentifier != null){
267
			$('#'+detailsContainerIdentifier).html('');
268
		}
269
		$("#"+paginatedIdentifier+" .next").prop('disabled', false);
270
		if (parseInt(pre)==0){
271
			$("#"+paginatedIdentifier+" .previous").prop('disabled', true);
272
		}
273
	});	
274
 
275
}
276
 
277
 
23405 amit.gupta 278
function numberToComma(x) {
23786 amit.gupta 279
	x=parseInt(x);
23405 amit.gupta 280
	x=x.toString();
281
	var lastThree = x.substring(x.length-3);
282
	var otherNumbers = x.substring(0,x.length-3);
283
	if(otherNumbers != '')
284
	    lastThree = ',' + lastThree;
285
	return otherNumbers.replace(/\B(?=(\d{2})+(?!\d))/g, ",") + lastThree;
23786 amit.gupta 286
}