Subversion Repositories SmartDukaan

Rev

Rev 14797 | Rev 14802 | 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
								}
14799 anikendra 87
								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 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);							
14799 anikendra 94
							$(variantslink).data('variants',variants).data('minprice',minprice);
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');
14799 anikendra 150
		var minprice = $(this).data('minprice');
14685 anikendra 151
		// console.log(variants);
14026 anikendra 152
		$('#variantscount').html(variants.length);
13901 anikendra 153
		for(var i in variants){
14392 anikendra 154
			// console.log(variants[i]);
14685 anikendra 155
			// 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>');
156
			// $('#storeproducts').parent().append(row);
14789 anikendra 157
			// 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>');
14799 anikendra 158
			console.log(minprice);
159
			if(minprice == variants[i].available_price) {
14797 anikendra 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 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>');
161
			}else{
162
				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>');
163
			}			
14687 anikendra 164
			$(this).parent().parent().append(row);			
13901 anikendra 165
		}
14713 anikendra 166
		var showless = $('<div class="showless col-xs-9 text-right">Less <i class="glyphicon glyphicon-chevron-up"></i></span>');
14687 anikendra 167
		$(this).parent().parent().append(showless);
168
		$(this).addClass('hidden');
14026 anikendra 169
		// console.log($(this).parent());
14685 anikendra 170
		// $('#storename').html($(this).parent().data('storename'));
171
		// $('#variantModal').modal();
13901 anikendra 172
	});
13695 anikendra 173
	$('.categorytab').on('click','.categorytabcontrol.active',function(){
174
		$(this).toggleClass('active');
175
		$('#preferences-'+$(this).data('id')).addClass('hidden');
176
		$(this).parent().parent().children().find('.savecategorypreferences',0).removeClass('savecategorypreferences').addClass('editcategorypreferences').html('Edit').removeClass('btn-success');
177
	});
13759 anikendra 178
	$('.categorytab').on('click','.categorytabcontrol:not(.active)',function(){
179
		$(this).parent().parent().find('.btn',0).click();
180
	});
13695 anikendra 181
	$('.categorytab').on('click','.editcategorypreferences',function(e){
182
		var that = $(this);
183
		$(that).removeClass('editcategorypreferences').addClass('savecategorypreferences').html('Done').addClass('btn-success');
184
		$('#togglepreferences-'+$(that).data('id')).toggleClass('active');
185
		$('#preferences-'+$(that).data('id')).removeClass('hidden');
186
		$('.brandselector').each(function(){
187
			if($(this).data('catid')==$(that).data('id')){
188
				$(this).addClass('active');
189
			}
190
		});
191
	});
192
	$('.categorytab').on('click','.savecategorypreferences',function(e){
193
		var that = $(this);
194
		$(that).removeClass('savecategorypreferences').addClass('editcategorypreferences').html('Edit').removeClass('btn-success');
195
		$('#togglepreferences-'+$(that).data('id')).toggleClass('active');
196
		$.ajax({
197
			url: $('#categorypreference-'+$(that).data('id')).attr('action'),
198
			data: $('#categorypreference-'+$(that).data('id')).serialize(),
199
			// Tell jQuery we're expecting JSONP
200
			dataType: "json",
201
			// Tell YQL what we want and that we want JSON			
202
			method: 'post',
203
			// Work with the response
204
			success: function( response ) {
205
				if(response.success){
206
					$('#preferences-'+$(that).data('id')).addClass('hidden');
207
				}
208
			}
209
		});
210
		ga('send', 'event', 'preferences', 'update', me);
211
	});
13901 anikendra 212
	// $('.row').on('click','.viewproduct',function(e){
213
	$(document).on('click','.viewproduct',function(e){
14386 anikendra 214
		if(typeof livePriceCalls != undefined) {
215
	    	for(var i in livePriceCalls){
14392 anikendra 216
	    		// console.log('killing ajax call '+i);
14386 anikendra 217
	    		livePriceCalls[i].abort();
218
	    	}
219
	    }
13686 anikendra 220
		$('#loadingModal').modal();
14392 anikendra 221
		// console.log($(this).data('id'));
14345 anikendra 222
		var url = apihost+"clicks/add/"+me+"/"+$(this).data('id')+'/'+$(this).data('source')+'/?url='+encodeURIComponent($(this).data('url'))+'&price='+$(this).data('price');
13579 anikendra 223
		$.ajax({
224
			url: url,
225
			// The name of the callback parameter, as specified by the YQL service
226
			jsonp: "callback",
227
			// Tell jQuery we're expecting JSONP
228
			dataType: "jsonp",
229
			// Tell YQL what we want and that we want JSON
230
			data: {
231
				format: "json"
232
			},
233
			// Work with the response
234
			success: function( response ) {
235
				if(response.success && response.type=='redirect'){
236
					document.location = response.url;
237
				}
238
			}
239
		});
14432 anikendra 240
		ga('send', 'event', 'product', 'click', $(this).data('url'));
13583 anikendra 241
	});
13719 anikendra 242
	$('.jscroll-inner').on('click','.likedeal',function(e){	
13583 anikendra 243
		var that = $(this);
13682 anikendra 244
		if($(that).hasClass('active')){
13583 anikendra 245
			//User has already liked it,so remove like
246
			var url = apihost+"/user_actions/rem/"+me+"/"+$(this).data('id')+"/like";	
247
		}else{
248
			var url = apihost+"/user_actions/update/"+me+"/"+$(this).data('id')+"/like";
249
		}
250
		$.ajax({
251
			url: url,
252
			// The name of the callback parameter, as specified by the YQL service
253
			jsonp: "callback",
254
			// Tell jQuery we're expecting JSONP
255
			dataType: "jsonp",
256
			// Tell YQL what we want and that we want JSON
257
			data: {
258
				format: "json"
259
			},
260
			// Work with the response
261
			success: function( response ) {
262
				if(response.success){
13672 anikendra 263
					$(that).toggleClass('active');
264
					$(that).parent().find('li.dislikedeal',0).removeClass('active');
13583 anikendra 265
				}
266
			}
267
		});
13686 anikendra 268
		ga('send', 'event', 'product', 'like', $(this).data('id'));
13583 anikendra 269
	});
13688 anikendra 270
	$('#myModal').on('click','#unlikebtn',function(e){
13682 anikendra 271
		e.preventDefault();
272
		var url = $('#unlikeproductform').attr('action')+'?'+$('#unlikeproductform').serialize()
14392 anikendra 273
		// console.log(url);
13682 anikendra 274
		$.ajax({
275
			url: url,
276
			// The name of the callback parameter, as specified by the YQL service
277
			jsonp: "callback",
278
			// Tell jQuery we're expecting JSONP
279
			dataType: "jsonp",
280
			// Tell YQL what we want and that we want JSON
281
			data: {
282
				format: "json"
283
			},
284
			// Work with the response
285
			success: function( response ) {
286
				// console.log(response);						
287
			}
288
		});
289
		$('#myModal').modal('hide');
13686 anikendra 290
		ga('send', 'event', 'brand', 'hide', $('#myModal').find('#productToHide',0).val());
13682 anikendra 291
	})
13719 anikendra 292
	$('.jscroll-inner').on('click','.dislikedeal',function(e){			
13583 anikendra 293
		var that = $(this);
13682 anikendra 294
		if($(that).hasClass('active')){
13583 anikendra 295
			//User has already liked it,so remove like
296
			var url = apihost+"/user_actions/rem/"+me+"/"+$(this).data('id')+"/dislike";	
297
		}else{
14392 anikendra 298
			// console.log('show modal');
13682 anikendra 299
			$('#myModal').find('#productToHide',0).val($(this).data('id'));
14139 anikendra 300
			//$('#myModal').modal();
13583 anikendra 301
			var url = apihost+"/user_actions/update/"+me+"/"+$(this).data('id')+"/dislike";
302
		}
303
		$.ajax({
304
			url: url,
305
			// The name of the callback parameter, as specified by the YQL service
306
			jsonp: "callback",
307
			// Tell jQuery we're expecting JSONP
308
			dataType: "jsonp",
309
			// Tell YQL what we want and that we want JSON
310
			data: {
311
				format: "json"
312
			},
313
			// Work with the response
314
			success: function( response ) {
315
				if(response.success){
13672 anikendra 316
					$(that).toggleClass('active');
317
					$(that).parent().find('li.likedeal',0).removeClass('active');
13583 anikendra 318
				}
319
			}
320
		});
13686 anikendra 321
		ga('send', 'event', 'product', 'dislike', $(this).data('id'));
13583 anikendra 322
	});
13731 anikendra 323
	$('.deletefav').on('click',function(){
324
		var that = $(this);
325
		$('#loadingModal').modal();
326
		var url = apihost+"/user_actions/deletefav/"+me+"/"+$(this).data('id');			
327
		$.ajax({
328
			url: url,
329
			// The name of the callback parameter, as specified by the YQL service
330
			jsonp: "callback",
331
			// Tell jQuery we're expecting JSONP
332
			dataType: "jsonp",
333
			// Tell YQL what we want and that we want JSON
334
			data: {
335
				format: "json"
336
			},
337
			// Work with the response
338
			success: function( response ) {
339
				$('#loadingModal').modal('hide');
340
				if(response.success){			
341
					$('#fav-'+$(that).data('id')).hide('slow');
342
				}
343
			}
344
		});
345
		ga('send', 'event', 'favourites', 'remove', $(this).data('id'));
346
	});
347
	$('.clearfavs').on('click',function(){
348
		var that = $(this);
349
		$('#loadingModal').modal();
14300 anikendra 350
		var url = apihost+"/user_actions/deleteallfavs/"+me+'/'+$(this).data('type');			
13731 anikendra 351
		$.ajax({
352
			url: url,
353
			// The name of the callback parameter, as specified by the YQL service
354
			jsonp: "callback",
355
			// Tell jQuery we're expecting JSONP
356
			dataType: "jsonp",
357
			// Tell YQL what we want and that we want JSON
358
			data: {
359
				format: "json"
360
			},
361
			// Work with the response
362
			success: function( response ) {
363
				$('#loadingModal').modal('hide');
364
				if(response.success){			
365
					$('.deletefav').each(function(){
366
						$('#fav-'+$(this).data('id')).hide('slow');
367
					})					
368
				}
369
				$(that).hide();
370
			}
371
		});
372
		ga('send', 'event', 'favourites', 'removeall', me);
373
	});
13759 anikendra 374
	$('.revealbrands').on('click',function(){
375
		$(this).parent().find('.notfeatured').toggleClass('hidden');
376
		if($(this).html()=='Others'){
377
			$(this).html('Hide');
378
		}else{
379
			$(this).html('Others');
380
		}
381
	});
14068 anikendra 382
	$(document).on('click','.creditedcashbacks',function(){
383
		if($(this).find('.glyphicon-plus',0).length>0){
384
			$(this).find('.glyphicon-plus',0).removeClass('glyphicon-plus').addClass('glyphicon-minus');
385
		}else{
386
			$(this).find('.glyphicon-minus',0).removeClass('glyphicon-minus').addClass('glyphicon-plus');
387
		}
388
		$(this).next().toggleClass('hidden');
389
	});
390
	$(document).on('click','.panel-heading',function(){
391
		if($(this).find('.glyphicon-plus',0).length>0){
392
			$(this).find('.glyphicon-plus',0).removeClass('glyphicon-plus').addClass('glyphicon-minus');
393
		}else{
394
			$(this).find('.glyphicon-minus',0).removeClass('glyphicon-minus').addClass('glyphicon-plus');
395
		}
396
	});
14135 anikendra 397
	$(document).on('click','.prefcatselect',function(){
398
		$('.prefcatselect').removeClass('active')
399
		$(this).addClass('active');
400
		$('.categorypreferences').addClass('hidden');
401
		$('#cat-'+$(this).data('id')).toggleClass('hidden');
14150 anikendra 402
	});
403
	$(document).on('click','.refresh',function(){
404
		document.location.reload();
405
	});
14215 anikendra 406
	$(document).on('click','.hasmoretext',function(){
407
		$('#fulltext').html($(this).data('fulltext'));
408
		$('#fullTextModal').modal();
409
	});
14300 anikendra 410
	$(document).on('click','.favswitch',function(){
411
		$('.favswitch').removeClass('active')
412
		$(this).addClass('active');
413
		$('.clearfavs').addClass('hidden');
414
		$('.card').addClass('hidden');
415
		$('.'+$(this).data('type')).removeClass('hidden');
416
	});
14386 anikendra 417
	$(document).on('click','.brandpreferences .brands',function(){
418
		$(this).parents('.row').siblings('.controls').find('.editcategorypreferences',0).click();
419
	});
420
	$(document).on('click','.tryagainforliveprice',function(){		
421
		fetchLivePrice($(this).parent());
422
		$(this).parent().html('Getting Live Prices');
423
	});
14579 anikendra 424
	$(document).on('click','.alert > a',function(){		
425
		ga('send', 'event', 'notifications', 'click', $(this).html());
426
	});
427
	$(document).on('click','.banner',function(){		
428
		ga('send', 'event', 'banners', 'click', $(this).data('name'));
429
	});
13992 anikendra 430
});