Subversion Repositories SmartDukaan

Rev

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