Subversion Repositories SmartDukaan

Rev

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