Subversion Repositories SmartDukaan

Rev

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