Subversion Repositories SmartDukaan

Rev

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