Subversion Repositories SmartDukaan

Rev

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