Subversion Repositories SmartDukaan

Rev

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