Subversion Repositories SmartDukaan

Rev

Rev 23914 | Rev 23919 | 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) {
23872 amit.gupta 22
	// $( ".log" ).text( "Triggered ajaxError handler." );
22956 ashik.ali 23
	if(jqxhr.status == 400){
23872 amit.gupta 24
		// $('#error-prompt-model').modal();
22956 ashik.ali 25
		badRequestAlert(jqxhr);	
26
	}else{
27
		internalServerErrorAlert(jqxhr);
28
	}
29
});
30
 
23918 amit.gupta 31
$( document ).ajaxStart(function() {
32
		if(typeof showAlert !="undefined" && showAlert) {
33
			bootbox.dialog({
34
				title: "<b>Low on Investments Alert!</b>",
35
				message : "<h4>Dear Partner, your investment is below 90%</h4>"
36
					+  "<dl>"
37
					+  "<dt>Total Investment Required</dt></dd>- " + minimumInvestmentAmount +"</dd>"
38
					+ "<dt>Total Invested Amount</dt></dd>- " + totalInvestedAmount +"</dd>"
39
					+  "<dt>Amount Short </dt><dd>- " + shortAmount+"</dd></dl>"
40
			 		+ "It is required to ensure minimum investment with us!"
41
			 		+ '<br><span class="small">Note - Please login again if your wallet/investments are updated!</span>',
42
			 	buttons:{
43
			 			cancel: {
44
			 				label: "OK",
45
			 		        className: 'btn-danger'
46
			 			}
47
			 		}
48
			});
49
		}
50
	});
23026 ashik.ali 51
 
23918 amit.gupta 52
 
53
 
23193 ashik.ali 54
function doAjaxRequestWithParamsHandler(urlString, httpType, params, callback_function){
55
	$.ajax({
56
	   url: urlString,
23343 ashik.ali 57
	   async: true,
23193 ashik.ali 58
	   cache: false,
59
	   data: params,
23872 amit.gupta 60
       // dataType:'json',
23193 ashik.ali 61
	   type: httpType,
62
	   success: function(response) {
63
	      callback_function(response);
23786 amit.gupta 64
	      $('.currency').each(function(index,ele){
65
	    	  if(!isNaN(parseInt($(ele).html()))) {
66
	    		  $(ele).html(numberToComma($(ele).html()));  
67
	    	  }
68
	      });
23193 ashik.ali 69
	   }
70
	});
71
}
72
 
23500 ashik.ali 73
function doGetAjaxRequestWithParamsHandler(urlString, params, callback_function){
74
	doAjaxRequestWithParamsHandler(urlString, "GET", params, callback_function);
75
}
76
 
77
function doPostAjaxRequestWithParamsHandler(urlString, params, callback_function){
78
	doAjaxRequestWithParamsHandler(urlString, "POST", params, callback_function);
79
}
80
 
23032 ashik.ali 81
function doAjaxRequestWithJsonHandler(urlString, httpType, json, callback_function){
82
	$.ajax({
83
	   url: urlString,
23343 ashik.ali 84
	   async: true,
23032 ashik.ali 85
	   cache: false,
86
	   processData: false,
87
	   data: json,
88
       contentType:'application/json',
89
	   type: httpType,
90
	   success: function(response) {
23872 amit.gupta 91
	      // console.log("response"+JSON.stringify(data));
23032 ashik.ali 92
	      callback_function(response);
23786 amit.gupta 93
	      $('.currency').each(function(index,ele){
94
	    	  if(!isNaN(parseInt($(ele).html()))) {
95
	    		  $(ele).html(numberToComma($(ele).html()));  
96
	    	  }
97
	      });
23032 ashik.ali 98
	   }
99
	});
100
}
101
 
23500 ashik.ali 102
function doPostAjaxRequestWithJsonHandler(urlString, json, callback_function){
103
	doAjaxRequestWithJsonHandler(urlString, "POST", json, callback_function);
104
}
23032 ashik.ali 105
 
23500 ashik.ali 106
function doPutAjaxRequestWithJsonHandler(urlString, json, callback_function){
107
	doAjaxRequestWithJsonHandler(urlString, "PUT", json, callback_function);
108
}
109
 
23026 ashik.ali 110
function doAjaxRequestHandler(urlString, httpType, callback_function){
22982 ashik.ali 111
	$.ajax({
112
	   url: urlString,
23343 ashik.ali 113
	   async: true,
23026 ashik.ali 114
	   cache: false,
22982 ashik.ali 115
	   type: httpType,
116
	   success: function(response) {
117
	      callback_function(response);
23786 amit.gupta 118
	      $('.currency').each(function(index,ele){
119
	    	  if(!isNaN(parseInt($(ele).html()))) {
120
	    		  $(ele).html(numberToComma($(ele).html()));  
121
	    	  }
122
	      });
22982 ashik.ali 123
	   }
124
	});
125
}
126
 
23500 ashik.ali 127
function doGetAjaxRequestHandler(urlString, callback_function){
128
	doAjaxRequestHandler(urlString, "GET", callback_function);
129
}
130
 
131
function doPutAjaxRequestHandler(urlString, callback_function){
132
	doAjaxRequestHandler(urlString, "PUT", callback_function);
133
}
134
 
23629 ashik.ali 135
function doPostAjaxRequestHandler(urlString, callback_function){
136
	doAjaxRequestHandler(urlString, "POST", callback_function);
137
}
138
 
23783 ashik.ali 139
function doDeleteAjaxRequestHandler(urlString, callback_function){
140
	doAjaxRequestHandler(urlString, "DELETE", callback_function);
141
}
142
 
23347 ashik.ali 143
function doAjaxUploadRequest(urlString, httpType, file){
144
	var response;
145
	doAjaxUploadRequestHandler(urlString, httpType, file, function(ajaxResponse){
146
		response = ajaxResponse;
147
	});
148
	return response;
149
}
150
 
23026 ashik.ali 151
function doAjaxUploadRequestHandler(urlString, httpType, file, callback_function){
22982 ashik.ali 152
	var formData = new FormData();
153
	formData.append("file", file);
154
	$.ajax({
155
	   url: urlString,
156
	   type: httpType,
157
	   data: formData,
158
       dataType: 'json',
23343 ashik.ali 159
       async: true,
22982 ashik.ali 160
       cache: false,
161
       contentType: false,
162
       enctype: 'multipart/form-data',
163
       processData: false,
164
	   success: function(response) {
23872 amit.gupta 165
	      // console.log("response"+JSON.stringify(data));
22982 ashik.ali 166
	      callback_function(response);
167
	   }
168
	});
169
}
170
 
23347 ashik.ali 171
function uploadDocument(file){
23379 ashik.ali 172
	var url = 'http://'+ webApiHost + ':' + webApiPort + '/profitmandi-web/document-upload';
23377 ashik.ali 173
	doAjaxUploadRequestHandler(url, 'POST', file, function(response){
174
		var documentId = response.response.document_id; 
175
		console.log("documentId : "+documentId);
176
		return documentId;
177
	});
23347 ashik.ali 178
}
179
 
23377 ashik.ali 180
function doAjaxGetDownload(urlString, fileName){
181
	console.log("fileName : "+fileName);
23343 ashik.ali 182
	doAjaxDownload(urlString, "GET", null, fileName);
22982 ashik.ali 183
}
184
 
23377 ashik.ali 185
function doAjaxPostDownload(urlString, data, fileName){
23343 ashik.ali 186
	doAjaxDownload(urlString, "POST", data, fileName);
21627 kshitij.so 187
}
188
 
23343 ashik.ali 189
function doAjaxDownload(urlString, httpType, data, fileName){
22488 ashik.ali 190
	xhttp = new XMLHttpRequest();
191
	xhttp.onreadystatechange = function() {
192
	    var a;
23494 ashik.ali 193
	    if(xhttp.readyState === 2){
194
	    	if(xhttp.status == 200){
195
	    		xhttp.responseType = "blob";
196
            } else {
197
                xhttp.responseType = "text";
198
            }
199
	    }else if (xhttp.readyState === 4 && xhttp.status === 200) {
22488 ashik.ali 200
	        // Trick for making downloadable link
201
	        a = document.createElement('a');
202
	        a.href = window.URL.createObjectURL(xhttp.response);
203
	        // Give filename you wish to download
23343 ashik.ali 204
	        a.download = fileName;
22488 ashik.ali 205
	        a.style.display = 'none';
206
	        document.body.appendChild(a);
207
	        a.click();
23494 ashik.ali 208
	    }else if(xhttp.readyState == 4 && xhttp.status === 400){
209
    		badRequestAlert(xhttp);
210
	    }else if(xhttp.readyState == 4 && xhttp.status === 500){
211
	    	internalServerErrorAlert(xhttp);
22488 ashik.ali 212
	    }
213
	};
214
	// Post data to URL which handles post request
23343 ashik.ali 215
	xhttp.open(httpType, urlString);
216
	if(httpType == "POST"){
217
		xhttp.setRequestHeader("Content-Type", "application/json");
218
	}
22488 ashik.ali 219
	// You should set responseType as blob for binary responses
23872 amit.gupta 220
	// xhttp.responseType = 'blob';
22488 ashik.ali 221
	xhttp.send(data);
23405 amit.gupta 222
}
223
 
23629 ashik.ali 224
function loadPaginatedNextItems(url, params, paginatedIdentifier, tableIdentifier, detailsContainerIdentifier){
225
	var start = $("#"+paginatedIdentifier+" .start").text();
226
	var end = $("#"+paginatedIdentifier+" .end").text();
227
	url = context + url + "?offset=" + end;
228
 
229
	if(params != null){
230
		for (var key in params) {
231
			if (params.hasOwnProperty(key)) {
23872 amit.gupta 232
				// console.log(key + " -> " + p[key]);
23629 ashik.ali 233
				url = url +"&" + key + "=" + params[key];
234
			}
235
		}
236
	}
237
 
238
	doGetAjaxRequestHandler(url, function(response){
239
		var size = $("#"+paginatedIdentifier+" .size").text();
240
		if((parseInt(end) + 10) > parseInt(size)){
241
			console.log("(end + 10) > size == true");
242
			$("#"+paginatedIdentifier+" .end").text(size);
243
		}else{
244
			console.log("(end + 10) > size == false");
245
			$("#"+paginatedIdentifier+" .end").text(+end + +10);
246
		}
247
		$("#"+paginatedIdentifier+" .start").text(+start + +10);
248
		var last = $("#"+paginatedIdentifier+" .end").text();
249
		var temp = $("#"+paginatedIdentifier+" .size").text();
250
		if (parseInt(last) >= parseInt(temp)){
251
			$("#"+paginatedIdentifier+" .next").prop('disabled', true);
23872 amit.gupta 252
			// $( "#good-inventory-paginated .end" ).text(temp);
23629 ashik.ali 253
		}
254
	    $('#'+tableIdentifier).html(response);
255
	    if(detailsContainerIdentifier != null){
256
			$('#'+detailsContainerIdentifier).html('');
257
		}
258
	    $("#"+paginatedIdentifier+" .previous").prop('disabled', false);
259
	});
260
 
261
}
23405 amit.gupta 262
 
23629 ashik.ali 263
 
264
function loadPaginatedPreviousItems(url, params, paginatedIdentifier, tableIdentifier, detailsContainerIdentifier){
265
	var start = $("#"+paginatedIdentifier+" .start").text();
266
	var end =  $("#"+paginatedIdentifier+" .end").text();
267
	var size = $("#"+paginatedIdentifier+" .size").text();
23819 govind 268
	if(parseInt(end) == parseInt(size) && parseInt(end)%10 !=0){
23629 ashik.ali 269
		var mod = parseInt(end) % 10;
270
		end = parseInt(end) + (10 - mod); 
271
	}
272
	var pre = end - 20;
273
 
274
	url = context + url + "?offset=" + pre;
275
 
276
	if(params != null){
277
		for (var key in params) {
278
			if (params.hasOwnProperty(key)) {
279
				url = url +"&" + key + "=" + params[key];
280
			}
281
		}
282
	}
283
 
284
	doGetAjaxRequestHandler(url, function(response){
285
		$("#"+paginatedIdentifier+" .end").text(+end - +10);
286
		$("#"+paginatedIdentifier+" .start").text(+start - +10);
287
		$('#'+tableIdentifier).html(response);
288
		if(detailsContainerIdentifier != null){
289
			$('#'+detailsContainerIdentifier).html('');
290
		}
291
		$("#"+paginatedIdentifier+" .next").prop('disabled', false);
292
		if (parseInt(pre)==0){
293
			$("#"+paginatedIdentifier+" .previous").prop('disabled', true);
294
		}
295
	});	
296
 
297
}
298
 
299
 
23405 amit.gupta 300
function numberToComma(x) {
23786 amit.gupta 301
	x=parseInt(x);
23405 amit.gupta 302
	x=x.toString();
303
	var lastThree = x.substring(x.length-3);
304
	var otherNumbers = x.substring(0,x.length-3);
305
	if(otherNumbers != '')
306
	    lastThree = ',' + lastThree;
307
	return otherNumbers.replace(/\B(?=(\d{2})+(?!\d))/g, ",") + lastThree;
23786 amit.gupta 308
}
23870 amit.gupta 309
 
310
function dateRangeCallback(start, end) {
23886 amit.gupta 311
	startMoment = start;
312
	endMoment = end;
313
	startDate = start.format(moment.HTML5_FMT.DATETIME_LOCAL_SECONDS);
314
	endDate = end.format(moment.HTML5_FMT.DATETIME_LOCAL_SECONDS);
23872 amit.gupta 315
}
316
 
317
function getSingleDatePicker(){
318
	var singleDatePicker =  
319
	{ 
320
		"todayHighlight": true, 
23886 amit.gupta 321
		"startDate": startMoment || moment(),
23872 amit.gupta 322
		"autoclose": true,
323
		"autoUpdateInput": true,
324
		"singleDatePicker": true,
325
    "locale": {
326
		'format': 'DD/MM/YYYY'
327
	}
328
  };
329
	return singleDatePicker;
23886 amit.gupta 330
}
331
 
332
function getRangedDatePicker(showRanges){
333
	if(typeof showRanges=="undefined") {
334
		showRanges = false;
335
	}
336
	var rangedDatePicker =  
337
	{ 
338
			"todayHighlight": true,
23914 govind 339
			"opens": "right",
23886 amit.gupta 340
			"startDate": startMoment || moment(),
341
			"endDate": endMoment || moment(),
342
			"autoclose": true,
343
			"alwaysShowCalendars": false,
344
			"autoUpdateInput": true,
345
			"locale": {
346
				'format': 'DD/MM/YYYY'
347
			}
348
	};
349
	if(showRanges) {
350
		rangedDatePicker['ranges'] = 
351
		{
352
            'Today': [moment(), moment()],
353
            'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
354
            'Last 7 Days': [moment().subtract(6, 'days'), moment()],
355
            'Last 30 Days': [moment().subtract(29, 'days'), moment()],
356
            'This Month': [moment().startOf('month'), moment().endOf('month')],
357
            'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
358
         }
359
	}
360
	return rangedDatePicker;
23892 amit.gupta 361
}
23893 amit.gupta 362
$(function(){
363
	dateRangeCallback(moment(),moment());
364
})