Subversion Repositories SmartDukaan

Rev

Rev 14773 | Rev 14782 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
14386 anikendra 1
var livePriceCalls = [];
14432 anikendra 2
var outOfStockCount = 0;
14773 anikendra 3
function getcashbackstring(type,value) {
4
	console.log(type+' '+value);
5
	if(value==0){
6
		return '';
7
	}
8
	if(type==2){
14781 anikendra 9
		return '+'+value+' Cashbk';
14773 anikendra 10
	}else if(type==1){
14781 anikendra 11
		return '+'+value+'% Cashbk';
14773 anikendra 12
	}else{
13
		return '';
14
	}
15
}
16
 
13984 anikendra 17
$(function(){	
18
	if($('.scroll > .card').length>0) {
19
		setTimeout(function(){ document.location.reload(); }, 1000*60*35);
13820 anikendra 20
		$('.scroll').jscroll({
21
			loadingHtml: '<center><img src="/img/ajax-loader.gif" alt="Loading" /></center>',
22
		    autoTriggerUntil: 3,
23
		    padding: 20,
24
		});
25
	}
13901 anikendra 26
	if($('.storeminprice').length>0) {
27
		var globalminprice = 9999999;
14773 anikendra 28
		var globalminsku;
29
		var globalmincashback = '';		
14345 anikendra 30
		var calls = [];
13901 anikendra 31
		$('.storeminprice').each(function(){
14345 anikendra 32
			var temp = {'sort':$(this).data('searchorder'),'val':$(this)};
33
			calls.push(temp);
34
		});
35
		calls = sortByKey(calls,'sort');		
36
		for(var i in calls){
37
			fetchLivePrice(calls[i]['val'])
14432 anikendra 38
		}		
14345 anikendra 39
	};
40
	function sortByKey(array, key) {
41
	    return array.sort(function(a, b) {
42
	        var x = a[key]; var y = b[key];
43
	        return ((x < y) ? -1 : ((x > y) ? 1 : 0));
44
	    });
45
	};
14432 anikendra 46
	function fetchLivePrice(obj){		
47
		var that = obj;	
48
		var inStock = false;		
14386 anikendra 49
		var req = $.ajax({
14345 anikendra 50
			url: '/store_products/getliveprice/'+$(that).data('bundle_id')+'/'+$(that).data('id'),
51
			// Tell jQuery we're expecting JSONP
52
			dataType: "json",
53
			// Tell YQL what we want and that we want JSON			
54
			method: 'get',
14430 anikendra 55
			// timeout: 30000,
14345 anikendra 56
			// Work with the response
57
			success: function( response ) {
14685 anikendra 58
				// console.log(response);
14345 anikendra 59
				if(response.success){
60
					var i = 0;	
61
					var minpriceindex = 0;				
62
					if(response.products[i].in_stock == 1){inStock = true;}
63
					var minprice = response.products[0]['available_price'];
64
					var minpriceurl = response.products[0]['marketPlaceUrl'];
65
					if(response.products.length>0){
66
						//More than one products in store
67
						var variants = [];
68
						// var i = 0;
69
						for(var i in response.products){	
14685 anikendra 70
							if(response.products[i].in_stock == 1){
71
								inStock = true;
72
								// console.log(response.products[i]);
73
								if(response.products[i].available_price && response.products[i].available_price < globalminprice) {
74
									globalminprice = Math.round(response.products[i].available_price);
75
									globalminsku = response.products[i]._id;
76
									var globalminsource = response.products[i].source_id;
14773 anikendra 77
									var globalminurl = response.products[i].marketPlaceUrl;
78
									var globalmincashback = getcashbackstring(response.products[i].cash_back_type,response.products[i].cash_back);
14685 anikendra 79
								}
80
								if(response.products[i].available_price <= minprice) {
81
									minprice = Math.round(response.products[i].available_price);
82
									minpriceindex = i;
14700 anikendra 83
								}
14773 anikendra 84
								variants.push({'name' : response.products[i].source_product_name, 'source_id' : response.products[i].source_id, 'available_price' : Math.round(response.products[i].available_price), 'url' : response.products[i].marketPlaceUrl,'source_product_name' : response.products[i].source_product_name,'id' : response.products[i]._id,'cash_back_type' : response.products[i].cash_back_type,'cash_back' : response.products[i].cash_back});
14685 anikendra 85
							}						
13901 anikendra 86
						}
14685 anikendra 87
						if(variants.length>1){
14700 anikendra 88
							variants.splice(minpriceindex,1);
14713 anikendra 89
							var variantslink = $('<span class="variants"> '+(variants.length+1)+' Options</span>');
90
							$(that).parent().parent().find('.pull-left',0).append(variantslink);							
14345 anikendra 91
							$(variantslink).data('variants',variants)
13948 anikendra 92
						}
14345 anikendra 93
					}
94
					if(inStock){
14773 anikendra 95
						$(that).html(minprice).siblings('.cashbackamount').html(getcashbackstring(response.products[minpriceindex].cash_back_type,response.products[minpriceindex].cash_back));
14689 anikendra 96
						$('#name-'+$(that).data('id')).html(response.products[minpriceindex]['source_product_name']);
14685 anikendra 97
						// console.log(response.products[minpriceindex]);
98
						$('#sku-'+$(that).data('id')).data('id',response.products[minpriceindex]['_id']).data('source',response.products[minpriceindex]['source_id']).data('price',response.products[minpriceindex]['available_price']).data('url',response.products[minpriceindex]['marketPlaceUrl']).removeClass('hidden');
14357 anikendra 99
						// console.log(globalminprice+' '+globalminsku);
14345 anikendra 100
						if(globalminprice != 9999999) {
14773 anikendra 101
							$('#bestprice').html(globalminprice).siblings('.cashbackamount').html(globalmincashback);
14685 anikendra 102
							$('#beststorelink').data('id',globalminsku).data('source',globalminsource).data('price',globalminprice).data('url',globalminurl).removeClass('hidden');
14345 anikendra 103
							$('#bestpricecontainer').removeClass('hidden');
14432 anikendra 104
						} 
14386 anikendra 105
					} else{
14432 anikendra 106
						outOfStockCount++;
14345 anikendra 107
						$(that).parent().parent().remove();	
13901 anikendra 108
					}
14345 anikendra 109
					if(!response.products[0].available_price){
14434 anikendra 110
						// outOfStockCount++;
111
						// $(that).parent().parent().remove();	
112
						var btn = $("<a class='tryagainforliveprice btn btn-xs btn-warning' title='Try Again'>Try Again</a>");
113
						$(that).html(btn);
14345 anikendra 114
					}
14386 anikendra 115
				} else{
14434 anikendra 116
					// outOfStockCount++;
117
					// $(that).parent().parent().remove();
118
					var btn = $("<a class='tryagainforliveprice btn btn-xs btn-warning' title='Try Again'>Try Again</a>");
119
					$(that).html(btn);
13901 anikendra 120
				}
14432 anikendra 121
				checkForOutStocks(outOfStockCount);
14345 anikendra 122
			},
14386 anikendra 123
			error: function(request, status, err) {
124
				//console.log(status+' '+err+' '+request);
14430 anikendra 125
            	// if (status == "timeout") {
126
            		var btn = $("<a class='tryagainforliveprice btn btn-xs btn-warning' title='Try Again'>Try Again</a>");
14434 anikendra 127
					$(that).html(btn);
14430 anikendra 128
				// } else {
129
					// $(that).parent().parent().remove();
130
				// }
14345 anikendra 131
			}
13901 anikendra 132
		});
14386 anikendra 133
		livePriceCalls.push(req);
14357 anikendra 134
	};
14432 anikendra 135
	function checkForOutStocks(count){
136
		if(count>=livePriceCalls.length){
137
			$('#bestpricecontainer').html('<h4>Out of stock</h4>').removeClass('hidden');	
138
		}
139
	};
14687 anikendra 140
	$(document).on('click','.showless',function(){
141
		$(this).addClass('hidden').siblings('.varnts').hide();
142
		$(this).parent().find('.variants',0).removeClass('hidden').addClass('justshow');
143
	});
13901 anikendra 144
	$(document).on('click','.variants',function(){
145
		$('.storeproductinfo').empty();
14685 anikendra 146
		var variants = $(this).data('variants');
147
		// console.log(variants);
14026 anikendra 148
		$('#variantscount').html(variants.length);
13901 anikendra 149
		for(var i in variants){
14392 anikendra 150
			// console.log(variants[i]);
14685 anikendra 151
			// var row = $('<div class="row storeproductinfo"><div class="col-xs-7">'+variants[i].source_product_name+'</div><div class="col-xs-3">'+variants[i].available_price+'</div><div class="col-xs-2"><button class="btn btn-primary btn-xs viewproduct" type="button" data-id="'+variants[i].id+'">Buy</button></div></div>');
152
			// $('#storeproducts').parent().append(row);
14773 anikendra 153
			var row = $('<div class="clearfix varnts"></div><div class="col-xs-6 varnts text-small">'+variants[i].name+'</div><div class="col-xs-4 varnts"><span class="cashbackrupee"></span> <span>'+variants[i].available_price+'</span><span class="cashbackamount">'+getcashbackstring(variants[i].cash_back_type,variants[i].cash_back)+'</span></div><div class="col-xs-2 viewproduct varnts text-right" data-source="'+variants[i].source_id+'" data-price="'+variants[i].available_price+'" data-url="'+variants[i].url+'" data-id="'+variants[i].id+'"><button type="button" class="btn-xs btn btn-success">BUY</button></div>');
14687 anikendra 154
			$(this).parent().parent().append(row);			
13901 anikendra 155
		}
14713 anikendra 156
		var showless = $('<div class="showless col-xs-9 text-right">Less <i class="glyphicon glyphicon-chevron-up"></i></span>');
14687 anikendra 157
		$(this).parent().parent().append(showless);
158
		$(this).addClass('hidden');
14026 anikendra 159
		// console.log($(this).parent());
14685 anikendra 160
		// $('#storename').html($(this).parent().data('storename'));
161
		// $('#variantModal').modal();
13901 anikendra 162
	});
13695 anikendra 163
	$('.categorytab').on('click','.categorytabcontrol.active',function(){
164
		$(this).toggleClass('active');
165
		$('#preferences-'+$(this).data('id')).addClass('hidden');
166
		$(this).parent().parent().children().find('.savecategorypreferences',0).removeClass('savecategorypreferences').addClass('editcategorypreferences').html('Edit').removeClass('btn-success');
167
	});
13759 anikendra 168
	$('.categorytab').on('click','.categorytabcontrol:not(.active)',function(){
169
		$(this).parent().parent().find('.btn',0).click();
170
	});
13695 anikendra 171
	$('.categorytab').on('click','.editcategorypreferences',function(e){
172
		var that = $(this);
173
		$(that).removeClass('editcategorypreferences').addClass('savecategorypreferences').html('Done').addClass('btn-success');
174
		$('#togglepreferences-'+$(that).data('id')).toggleClass('active');
175
		$('#preferences-'+$(that).data('id')).removeClass('hidden');
176
		$('.brandselector').each(function(){
177
			if($(this).data('catid')==$(that).data('id')){
178
				$(this).addClass('active');
179
			}
180
		});
181
	});
182
	$('.categorytab').on('click','.savecategorypreferences',function(e){
183
		var that = $(this);
184
		$(that).removeClass('savecategorypreferences').addClass('editcategorypreferences').html('Edit').removeClass('btn-success');
185
		$('#togglepreferences-'+$(that).data('id')).toggleClass('active');
186
		$.ajax({
187
			url: $('#categorypreference-'+$(that).data('id')).attr('action'),
188
			data: $('#categorypreference-'+$(that).data('id')).serialize(),
189
			// Tell jQuery we're expecting JSONP
190
			dataType: "json",
191
			// Tell YQL what we want and that we want JSON			
192
			method: 'post',
193
			// Work with the response
194
			success: function( response ) {
195
				if(response.success){
196
					$('#preferences-'+$(that).data('id')).addClass('hidden');
197
				}
198
			}
199
		});
200
		ga('send', 'event', 'preferences', 'update', me);
201
	});
13901 anikendra 202
	// $('.row').on('click','.viewproduct',function(e){
203
	$(document).on('click','.viewproduct',function(e){
14386 anikendra 204
		if(typeof livePriceCalls != undefined) {
205
	    	for(var i in livePriceCalls){
14392 anikendra 206
	    		// console.log('killing ajax call '+i);
14386 anikendra 207
	    		livePriceCalls[i].abort();
208
	    	}
209
	    }
13686 anikendra 210
		$('#loadingModal').modal();
14392 anikendra 211
		// console.log($(this).data('id'));
14345 anikendra 212
		var url = apihost+"clicks/add/"+me+"/"+$(this).data('id')+'/'+$(this).data('source')+'/?url='+encodeURIComponent($(this).data('url'))+'&price='+$(this).data('price');
13579 anikendra 213
		$.ajax({
214
			url: url,
215
			// The name of the callback parameter, as specified by the YQL service
216
			jsonp: "callback",
217
			// Tell jQuery we're expecting JSONP
218
			dataType: "jsonp",
219
			// Tell YQL what we want and that we want JSON
220
			data: {
221
				format: "json"
222
			},
223
			// Work with the response
224
			success: function( response ) {
225
				if(response.success && response.type=='redirect'){
226
					document.location = response.url;
227
				}
228
			}
229
		});
14432 anikendra 230
		ga('send', 'event', 'product', 'click', $(this).data('url'));
13583 anikendra 231
	});
13719 anikendra 232
	$('.jscroll-inner').on('click','.likedeal',function(e){	
13583 anikendra 233
		var that = $(this);
13682 anikendra 234
		if($(that).hasClass('active')){
13583 anikendra 235
			//User has already liked it,so remove like
236
			var url = apihost+"/user_actions/rem/"+me+"/"+$(this).data('id')+"/like";	
237
		}else{
238
			var url = apihost+"/user_actions/update/"+me+"/"+$(this).data('id')+"/like";
239
		}
240
		$.ajax({
241
			url: url,
242
			// The name of the callback parameter, as specified by the YQL service
243
			jsonp: "callback",
244
			// Tell jQuery we're expecting JSONP
245
			dataType: "jsonp",
246
			// Tell YQL what we want and that we want JSON
247
			data: {
248
				format: "json"
249
			},
250
			// Work with the response
251
			success: function( response ) {
252
				if(response.success){
13672 anikendra 253
					$(that).toggleClass('active');
254
					$(that).parent().find('li.dislikedeal',0).removeClass('active');
13583 anikendra 255
				}
256
			}
257
		});
13686 anikendra 258
		ga('send', 'event', 'product', 'like', $(this).data('id'));
13583 anikendra 259
	});
13688 anikendra 260
	$('#myModal').on('click','#unlikebtn',function(e){
13682 anikendra 261
		e.preventDefault();
262
		var url = $('#unlikeproductform').attr('action')+'?'+$('#unlikeproductform').serialize()
14392 anikendra 263
		// console.log(url);
13682 anikendra 264
		$.ajax({
265
			url: url,
266
			// The name of the callback parameter, as specified by the YQL service
267
			jsonp: "callback",
268
			// Tell jQuery we're expecting JSONP
269
			dataType: "jsonp",
270
			// Tell YQL what we want and that we want JSON
271
			data: {
272
				format: "json"
273
			},
274
			// Work with the response
275
			success: function( response ) {
276
				// console.log(response);						
277
			}
278
		});
279
		$('#myModal').modal('hide');
13686 anikendra 280
		ga('send', 'event', 'brand', 'hide', $('#myModal').find('#productToHide',0).val());
13682 anikendra 281
	})
13719 anikendra 282
	$('.jscroll-inner').on('click','.dislikedeal',function(e){			
13583 anikendra 283
		var that = $(this);
13682 anikendra 284
		if($(that).hasClass('active')){
13583 anikendra 285
			//User has already liked it,so remove like
286
			var url = apihost+"/user_actions/rem/"+me+"/"+$(this).data('id')+"/dislike";	
287
		}else{
14392 anikendra 288
			// console.log('show modal');
13682 anikendra 289
			$('#myModal').find('#productToHide',0).val($(this).data('id'));
14139 anikendra 290
			//$('#myModal').modal();
13583 anikendra 291
			var url = apihost+"/user_actions/update/"+me+"/"+$(this).data('id')+"/dislike";
292
		}
293
		$.ajax({
294
			url: url,
295
			// The name of the callback parameter, as specified by the YQL service
296
			jsonp: "callback",
297
			// Tell jQuery we're expecting JSONP
298
			dataType: "jsonp",
299
			// Tell YQL what we want and that we want JSON
300
			data: {
301
				format: "json"
302
			},
303
			// Work with the response
304
			success: function( response ) {
305
				if(response.success){
13672 anikendra 306
					$(that).toggleClass('active');
307
					$(that).parent().find('li.likedeal',0).removeClass('active');
13583 anikendra 308
				}
309
			}
310
		});
13686 anikendra 311
		ga('send', 'event', 'product', 'dislike', $(this).data('id'));
13583 anikendra 312
	});
13731 anikendra 313
	$('.deletefav').on('click',function(){
314
		var that = $(this);
315
		$('#loadingModal').modal();
316
		var url = apihost+"/user_actions/deletefav/"+me+"/"+$(this).data('id');			
317
		$.ajax({
318
			url: url,
319
			// The name of the callback parameter, as specified by the YQL service
320
			jsonp: "callback",
321
			// Tell jQuery we're expecting JSONP
322
			dataType: "jsonp",
323
			// Tell YQL what we want and that we want JSON
324
			data: {
325
				format: "json"
326
			},
327
			// Work with the response
328
			success: function( response ) {
329
				$('#loadingModal').modal('hide');
330
				if(response.success){			
331
					$('#fav-'+$(that).data('id')).hide('slow');
332
				}
333
			}
334
		});
335
		ga('send', 'event', 'favourites', 'remove', $(this).data('id'));
336
	});
337
	$('.clearfavs').on('click',function(){
338
		var that = $(this);
339
		$('#loadingModal').modal();
14300 anikendra 340
		var url = apihost+"/user_actions/deleteallfavs/"+me+'/'+$(this).data('type');			
13731 anikendra 341
		$.ajax({
342
			url: url,
343
			// The name of the callback parameter, as specified by the YQL service
344
			jsonp: "callback",
345
			// Tell jQuery we're expecting JSONP
346
			dataType: "jsonp",
347
			// Tell YQL what we want and that we want JSON
348
			data: {
349
				format: "json"
350
			},
351
			// Work with the response
352
			success: function( response ) {
353
				$('#loadingModal').modal('hide');
354
				if(response.success){			
355
					$('.deletefav').each(function(){
356
						$('#fav-'+$(this).data('id')).hide('slow');
357
					})					
358
				}
359
				$(that).hide();
360
			}
361
		});
362
		ga('send', 'event', 'favourites', 'removeall', me);
363
	});
13759 anikendra 364
	$('.revealbrands').on('click',function(){
365
		$(this).parent().find('.notfeatured').toggleClass('hidden');
366
		if($(this).html()=='Others'){
367
			$(this).html('Hide');
368
		}else{
369
			$(this).html('Others');
370
		}
371
	});
14068 anikendra 372
	$(document).on('click','.creditedcashbacks',function(){
373
		if($(this).find('.glyphicon-plus',0).length>0){
374
			$(this).find('.glyphicon-plus',0).removeClass('glyphicon-plus').addClass('glyphicon-minus');
375
		}else{
376
			$(this).find('.glyphicon-minus',0).removeClass('glyphicon-minus').addClass('glyphicon-plus');
377
		}
378
		$(this).next().toggleClass('hidden');
379
	});
380
	$(document).on('click','.panel-heading',function(){
381
		if($(this).find('.glyphicon-plus',0).length>0){
382
			$(this).find('.glyphicon-plus',0).removeClass('glyphicon-plus').addClass('glyphicon-minus');
383
		}else{
384
			$(this).find('.glyphicon-minus',0).removeClass('glyphicon-minus').addClass('glyphicon-plus');
385
		}
386
	});
14135 anikendra 387
	$(document).on('click','.prefcatselect',function(){
388
		$('.prefcatselect').removeClass('active')
389
		$(this).addClass('active');
390
		$('.categorypreferences').addClass('hidden');
391
		$('#cat-'+$(this).data('id')).toggleClass('hidden');
14150 anikendra 392
	});
393
	$(document).on('click','.refresh',function(){
394
		document.location.reload();
395
	});
14215 anikendra 396
	$(document).on('click','.hasmoretext',function(){
397
		$('#fulltext').html($(this).data('fulltext'));
398
		$('#fullTextModal').modal();
399
	});
14300 anikendra 400
	$(document).on('click','.favswitch',function(){
401
		$('.favswitch').removeClass('active')
402
		$(this).addClass('active');
403
		$('.clearfavs').addClass('hidden');
404
		$('.card').addClass('hidden');
405
		$('.'+$(this).data('type')).removeClass('hidden');
406
	});
14386 anikendra 407
	$(document).on('click','.brandpreferences .brands',function(){
408
		$(this).parents('.row').siblings('.controls').find('.editcategorypreferences',0).click();
409
	});
410
	$(document).on('click','.tryagainforliveprice',function(){		
411
		fetchLivePrice($(this).parent());
412
		$(this).parent().html('Getting Live Prices');
413
	});
14579 anikendra 414
	$(document).on('click','.alert > a',function(){		
415
		ga('send', 'event', 'notifications', 'click', $(this).html());
416
	});
417
	$(document).on('click','.banner',function(){		
418
		ga('send', 'event', 'banners', 'click', $(this).data('name'));
419
	});
13992 anikendra 420
});