Subversion Repositories SmartDukaan

Rev

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