Subversion Repositories SmartDukaan

Rev

Rev 14345 | Rev 14386 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
13984 anikendra 1
$(function(){	
2
	if($('.scroll > .card').length>0) {
3
		setTimeout(function(){ document.location.reload(); }, 1000*60*35);
13820 anikendra 4
		$('.scroll').jscroll({
5
			loadingHtml: '<center><img src="/img/ajax-loader.gif" alt="Loading" /></center>',
6
		    autoTriggerUntil: 3,
7
		    padding: 20,
8
		});
9
	}
13901 anikendra 10
	if($('.storeminprice').length>0) {
11
		var globalminprice = 9999999;
14313 anikendra 12
		var globalminsku;		
14345 anikendra 13
		var calls = [];
13901 anikendra 14
		$('.storeminprice').each(function(){
14345 anikendra 15
			var temp = {'sort':$(this).data('searchorder'),'val':$(this)};
16
			calls.push(temp);
17
		});
18
		calls = sortByKey(calls,'sort');		
19
		for(var i in calls){
20
			console.log(calls[i]['val']);
21
			fetchLivePrice(calls[i]['val'])
22
		}
23
	};
24
	function sortByKey(array, key) {
25
	    return array.sort(function(a, b) {
26
	        var x = a[key]; var y = b[key];
27
	        return ((x < y) ? -1 : ((x > y) ? 1 : 0));
28
	    });
29
	};
30
	function fetchLivePrice(obj){
31
		console.log(obj);
32
		var inStock = false;
33
		var that = obj;			
34
		$.ajax({
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',
14357 anikendra 40
			// timeout: 180000,
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) {
57
								globalminprice = response.products[i].available_price;
58
								globalminsku = response.products[i]._id;
13901 anikendra 59
							}
14345 anikendra 60
							if(response.products[i].available_price <= minprice) {
61
								minprice = response.products[i].available_price;
62
								minpriceindex = i;
63
							} 
14357 anikendra 64
							// console.log(response);
14345 anikendra 65
							variants.push({'available_price' : 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 66
						}
14345 anikendra 67
						if(i>0){
68
							var variantslink = $('<span class="variants">+ '+i+' Options</span>');
69
							$(that).parent().append(variantslink);							
70
							$(variantslink).data('variants',variants)
13948 anikendra 71
						}
14345 anikendra 72
					}
73
					if(inStock){
74
						$(that).html(minprice);
75
						$('#sku-'+$(that).data('id')).data('id',response.products[minpriceindex]['_id']).removeClass('hidden');
14357 anikendra 76
						// console.log(globalminprice+' '+globalminsku);
14345 anikendra 77
						if(globalminprice != 9999999) {
78
							$('#bestprice').html(globalminprice);
79
							$('#beststorelink').data('id',globalminsku).removeClass('hidden');
80
							$('#bestpricecontainer').removeClass('hidden');
13901 anikendra 81
						}
82
					}else{
14345 anikendra 83
						$(that).parent().parent().remove();	
13901 anikendra 84
					}
14345 anikendra 85
					if(!response.products[0].available_price){
86
						$(that).parent().parent().remove();	
87
					}
88
				}else{
13901 anikendra 89
					$(that).parent().parent().remove();
90
				}
14345 anikendra 91
			},
92
			error : function() {
93
				$(that).parent().parent().remove();
94
			}
13901 anikendra 95
		});
14357 anikendra 96
	};
13901 anikendra 97
	$(document).on('click','.variants',function(){
98
		$('.storeproductinfo').empty();
14026 anikendra 99
		var variants = $(this).data('variants');	
100
		$('#variantscount').html(variants.length);
13901 anikendra 101
		for(var i in variants){
102
			console.log(variants[i]);
14026 anikendra 103
			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 104
			$('#storeproducts').parent().append(row);
105
		}
14026 anikendra 106
		// console.log($(this).parent());
107
		$('#storename').html($(this).parent().data('storename'));
13901 anikendra 108
		$('#variantModal').modal();
109
	});
13695 anikendra 110
	$('.categorytab').on('click','.categorytabcontrol.active',function(){
111
		$(this).toggleClass('active');
112
		$('#preferences-'+$(this).data('id')).addClass('hidden');
113
		$(this).parent().parent().children().find('.savecategorypreferences',0).removeClass('savecategorypreferences').addClass('editcategorypreferences').html('Edit').removeClass('btn-success');
114
	});
13759 anikendra 115
	$('.categorytab').on('click','.categorytabcontrol:not(.active)',function(){
116
		$(this).parent().parent().find('.btn',0).click();
117
	});
13695 anikendra 118
	$('.categorytab').on('click','.editcategorypreferences',function(e){
119
		var that = $(this);
120
		$(that).removeClass('editcategorypreferences').addClass('savecategorypreferences').html('Done').addClass('btn-success');
121
		$('#togglepreferences-'+$(that).data('id')).toggleClass('active');
122
		$('#preferences-'+$(that).data('id')).removeClass('hidden');
123
		$('.brandselector').each(function(){
124
			if($(this).data('catid')==$(that).data('id')){
125
				$(this).addClass('active');
126
			}
127
		});
128
	});
129
	$('.categorytab').on('click','.savecategorypreferences',function(e){
130
		var that = $(this);
131
		$(that).removeClass('savecategorypreferences').addClass('editcategorypreferences').html('Edit').removeClass('btn-success');
132
		$('#togglepreferences-'+$(that).data('id')).toggleClass('active');
133
		$.ajax({
134
			url: $('#categorypreference-'+$(that).data('id')).attr('action'),
135
			data: $('#categorypreference-'+$(that).data('id')).serialize(),
136
			// Tell jQuery we're expecting JSONP
137
			dataType: "json",
138
			// Tell YQL what we want and that we want JSON			
139
			method: 'post',
140
			// Work with the response
141
			success: function( response ) {
142
				if(response.success){
143
					$('#preferences-'+$(that).data('id')).addClass('hidden');
144
				}
145
			}
146
		});
147
		ga('send', 'event', 'preferences', 'update', me);
148
	});
13901 anikendra 149
	// $('.row').on('click','.viewproduct',function(e){
150
	$(document).on('click','.viewproduct',function(e){
13686 anikendra 151
		$('#loadingModal').modal();
13901 anikendra 152
		console.log($(this).data('id'));
14345 anikendra 153
		var url = apihost+"clicks/add/"+me+"/"+$(this).data('id')+'/'+$(this).data('source')+'/?url='+encodeURIComponent($(this).data('url'))+'&price='+$(this).data('price');
13579 anikendra 154
		$.ajax({
155
			url: url,
156
			// The name of the callback parameter, as specified by the YQL service
157
			jsonp: "callback",
158
			// Tell jQuery we're expecting JSONP
159
			dataType: "jsonp",
160
			// Tell YQL what we want and that we want JSON
161
			data: {
162
				format: "json"
163
			},
164
			// Work with the response
165
			success: function( response ) {
166
				if(response.success && response.type=='redirect'){
167
					document.location = response.url;
168
				}
169
			}
170
		});
13686 anikendra 171
		ga('send', 'event', 'product', 'click', $(this).data('id'));
13583 anikendra 172
	});
13719 anikendra 173
	$('.jscroll-inner').on('click','.likedeal',function(e){	
13583 anikendra 174
		var that = $(this);
13682 anikendra 175
		if($(that).hasClass('active')){
13583 anikendra 176
			//User has already liked it,so remove like
177
			var url = apihost+"/user_actions/rem/"+me+"/"+$(this).data('id')+"/like";	
178
		}else{
179
			var url = apihost+"/user_actions/update/"+me+"/"+$(this).data('id')+"/like";
180
		}
181
		$.ajax({
182
			url: url,
183
			// The name of the callback parameter, as specified by the YQL service
184
			jsonp: "callback",
185
			// Tell jQuery we're expecting JSONP
186
			dataType: "jsonp",
187
			// Tell YQL what we want and that we want JSON
188
			data: {
189
				format: "json"
190
			},
191
			// Work with the response
192
			success: function( response ) {
193
				if(response.success){
13672 anikendra 194
					$(that).toggleClass('active');
195
					$(that).parent().find('li.dislikedeal',0).removeClass('active');
13583 anikendra 196
				}
197
			}
198
		});
13686 anikendra 199
		ga('send', 'event', 'product', 'like', $(this).data('id'));
13583 anikendra 200
	});
13688 anikendra 201
	$('#myModal').on('click','#unlikebtn',function(e){
13682 anikendra 202
		e.preventDefault();
203
		var url = $('#unlikeproductform').attr('action')+'?'+$('#unlikeproductform').serialize()
204
		console.log(url);
205
		$.ajax({
206
			url: url,
207
			// The name of the callback parameter, as specified by the YQL service
208
			jsonp: "callback",
209
			// Tell jQuery we're expecting JSONP
210
			dataType: "jsonp",
211
			// Tell YQL what we want and that we want JSON
212
			data: {
213
				format: "json"
214
			},
215
			// Work with the response
216
			success: function( response ) {
217
				// console.log(response);						
218
			}
219
		});
220
		$('#myModal').modal('hide');
13686 anikendra 221
		ga('send', 'event', 'brand', 'hide', $('#myModal').find('#productToHide',0).val());
13682 anikendra 222
	})
13719 anikendra 223
	$('.jscroll-inner').on('click','.dislikedeal',function(e){			
13583 anikendra 224
		var that = $(this);
13682 anikendra 225
		if($(that).hasClass('active')){
13583 anikendra 226
			//User has already liked it,so remove like
227
			var url = apihost+"/user_actions/rem/"+me+"/"+$(this).data('id')+"/dislike";	
228
		}else{
13682 anikendra 229
			console.log('show modal');
230
			$('#myModal').find('#productToHide',0).val($(this).data('id'));
14139 anikendra 231
			//$('#myModal').modal();
13583 anikendra 232
			var url = apihost+"/user_actions/update/"+me+"/"+$(this).data('id')+"/dislike";
233
		}
234
		$.ajax({
235
			url: url,
236
			// The name of the callback parameter, as specified by the YQL service
237
			jsonp: "callback",
238
			// Tell jQuery we're expecting JSONP
239
			dataType: "jsonp",
240
			// Tell YQL what we want and that we want JSON
241
			data: {
242
				format: "json"
243
			},
244
			// Work with the response
245
			success: function( response ) {
246
				if(response.success){
13672 anikendra 247
					$(that).toggleClass('active');
248
					$(that).parent().find('li.likedeal',0).removeClass('active');
13583 anikendra 249
				}
250
			}
251
		});
13686 anikendra 252
		ga('send', 'event', 'product', 'dislike', $(this).data('id'));
13583 anikendra 253
	});
13731 anikendra 254
	$('.deletefav').on('click',function(){
255
		var that = $(this);
256
		$('#loadingModal').modal();
257
		var url = apihost+"/user_actions/deletefav/"+me+"/"+$(this).data('id');			
258
		$.ajax({
259
			url: url,
260
			// The name of the callback parameter, as specified by the YQL service
261
			jsonp: "callback",
262
			// Tell jQuery we're expecting JSONP
263
			dataType: "jsonp",
264
			// Tell YQL what we want and that we want JSON
265
			data: {
266
				format: "json"
267
			},
268
			// Work with the response
269
			success: function( response ) {
270
				$('#loadingModal').modal('hide');
271
				if(response.success){			
272
					$('#fav-'+$(that).data('id')).hide('slow');
273
				}
274
			}
275
		});
276
		ga('send', 'event', 'favourites', 'remove', $(this).data('id'));
277
	});
278
	$('.clearfavs').on('click',function(){
279
		var that = $(this);
280
		$('#loadingModal').modal();
14300 anikendra 281
		var url = apihost+"/user_actions/deleteallfavs/"+me+'/'+$(this).data('type');			
13731 anikendra 282
		$.ajax({
283
			url: url,
284
			// The name of the callback parameter, as specified by the YQL service
285
			jsonp: "callback",
286
			// Tell jQuery we're expecting JSONP
287
			dataType: "jsonp",
288
			// Tell YQL what we want and that we want JSON
289
			data: {
290
				format: "json"
291
			},
292
			// Work with the response
293
			success: function( response ) {
294
				$('#loadingModal').modal('hide');
295
				if(response.success){			
296
					$('.deletefav').each(function(){
297
						$('#fav-'+$(this).data('id')).hide('slow');
298
					})					
299
				}
300
				$(that).hide();
301
			}
302
		});
303
		ga('send', 'event', 'favourites', 'removeall', me);
304
	});
13759 anikendra 305
	$('.revealbrands').on('click',function(){
306
		$(this).parent().find('.notfeatured').toggleClass('hidden');
307
		if($(this).html()=='Others'){
308
			$(this).html('Hide');
309
		}else{
310
			$(this).html('Others');
311
		}
312
	});
14068 anikendra 313
	$(document).on('click','.creditedcashbacks',function(){
314
		if($(this).find('.glyphicon-plus',0).length>0){
315
			$(this).find('.glyphicon-plus',0).removeClass('glyphicon-plus').addClass('glyphicon-minus');
316
		}else{
317
			$(this).find('.glyphicon-minus',0).removeClass('glyphicon-minus').addClass('glyphicon-plus');
318
		}
319
		$(this).next().toggleClass('hidden');
320
	});
321
	$(document).on('click','.panel-heading',function(){
322
		if($(this).find('.glyphicon-plus',0).length>0){
323
			$(this).find('.glyphicon-plus',0).removeClass('glyphicon-plus').addClass('glyphicon-minus');
324
		}else{
325
			$(this).find('.glyphicon-minus',0).removeClass('glyphicon-minus').addClass('glyphicon-plus');
326
		}
327
	});
14135 anikendra 328
	$(document).on('click','.prefcatselect',function(){
329
		$('.prefcatselect').removeClass('active')
330
		$(this).addClass('active');
331
		$('.categorypreferences').addClass('hidden');
332
		$('#cat-'+$(this).data('id')).toggleClass('hidden');
14150 anikendra 333
	});
334
	$(document).on('click','.refresh',function(){
335
		document.location.reload();
336
	});
14215 anikendra 337
	$(document).on('click','.hasmoretext',function(){
338
		$('#fulltext').html($(this).data('fulltext'));
339
		$('#fullTextModal').modal();
340
	});
14300 anikendra 341
	$(document).on('click','.favswitch',function(){
342
		$('.favswitch').removeClass('active')
343
		$(this).addClass('active');
344
		$('.clearfavs').addClass('hidden');
345
		$('.card').addClass('hidden');
346
		$('.'+$(this).data('type')).removeClass('hidden');
347
	});
13992 anikendra 348
});