Subversion Repositories SmartDukaan

Rev

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