Subversion Repositories SmartDukaan

Rev

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