Subversion Repositories SmartDukaan

Rev

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