Subversion Repositories SmartDukaan

Rev

Rev 13984 | Rev 14026 | 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;
12
		var globalminsku;
13
		$('.storeminprice').each(function(){
14
			var that = $(this);			
15
			$.ajax({
16
				url: '/store_products/getliveprice/'+$(this).data('bundle_id')+'/'+$(this).data('id'),
17
				// Tell jQuery we're expecting JSONP
18
				dataType: "json",
19
				// Tell YQL what we want and that we want JSON			
20
				method: 'get',
21
				// Work with the response
22
				success: function( response ) {
23
					if(response.success){
24
						var i = 0;	
25
						var minpriceindex = 0;										
26
						var minprice = response.products[0]['available_price'];
27
						var minpriceurl = response.products[0]['marketPlaceUrl'];
28
						if(response.products.length>0){
29
							//More than one products in store
30
							var variants = [];
31
							// var i = 0;
32
							for(var i in response.products){
33
								if(response.products[i].available_price && response.products[i].available_price < globalminprice) {
34
									globalminprice = response.products[i].available_price;
35
									globalminsku = response.products[i]._id;
36
								}
37
								if(response.products[i].available_price <= minprice) {
38
									minprice = response.products[i].available_price;
39
									minpriceindex = i;
40
								} 
41
								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});
42
							}
43
							if(i>0){
44
								var variantslink = $('<span class="variants">+ '+i+' Options</span>');
45
								$(that).parent().append(variantslink);							
46
								$(variantslink).data('variants',variants)
47
							}
48
						}
49
						$(that).html(minprice);
50
						$('#sku-'+$(that).data('id')).data('id',response.products[minpriceindex]['_id']).removeClass('hidden');
51
						console.log(globalminprice+' '+globalminsku);
13948 anikendra 52
						if(globalminprice != 9999999) {
53
							$('#bestprice').html(globalminprice);
13992 anikendra 54
							$('#beststorelink').data('id',globalminsku).removeClass('hidden');
55
							$('#bestpricecontainer').removeClass('hidden');
13948 anikendra 56
						}
13901 anikendra 57
						if(!response.products[0].available_price){
58
							$(that).parent().parent().remove();	
59
						}
60
					}else{
61
						$(that).parent().parent().remove();
62
					}
63
				},
64
				error : function() {
65
					$(that).parent().parent().remove();
66
				}
67
			});
68
		});
69
	}
70
	$(document).on('click','.variants',function(){
71
		$('.storeproductinfo').empty();
72
		var variants = $(this).data('variants');		
73
		for(var i in variants){
74
			console.log(variants[i]);
13992 anikendra 75
			var row = $('<div class="row storeproductinfo"><div class="col-xs-8">'+variants[i].source_product_name+'</div><div class="col-xs-3">'+variants[i].available_price+'</div><div class="col-xs-1"><button class="btn btn-primary btn-xs viewproduct" type="button" data-id="'+variants[i].id+'">Buy</button></div></div>');
13901 anikendra 76
			$('#storeproducts').parent().append(row);
77
		}
78
		$('#variantModal').modal();
79
	});
13695 anikendra 80
	$('.categorytab').on('click','.categorytabcontrol.active',function(){
81
		$(this).toggleClass('active');
82
		$('#preferences-'+$(this).data('id')).addClass('hidden');
83
		$(this).parent().parent().children().find('.savecategorypreferences',0).removeClass('savecategorypreferences').addClass('editcategorypreferences').html('Edit').removeClass('btn-success');
84
	});
13759 anikendra 85
	$('.categorytab').on('click','.categorytabcontrol:not(.active)',function(){
86
		$(this).parent().parent().find('.btn',0).click();
87
	});
13695 anikendra 88
	$('.categorytab').on('click','.editcategorypreferences',function(e){
89
		var that = $(this);
90
		$(that).removeClass('editcategorypreferences').addClass('savecategorypreferences').html('Done').addClass('btn-success');
91
		$('#togglepreferences-'+$(that).data('id')).toggleClass('active');
92
		$('#preferences-'+$(that).data('id')).removeClass('hidden');
93
		$('.brandselector').each(function(){
94
			if($(this).data('catid')==$(that).data('id')){
95
				$(this).addClass('active');
96
			}
97
		});
98
	});
99
	$('.categorytab').on('click','.savecategorypreferences',function(e){
100
		var that = $(this);
101
		$(that).removeClass('savecategorypreferences').addClass('editcategorypreferences').html('Edit').removeClass('btn-success');
102
		$('#togglepreferences-'+$(that).data('id')).toggleClass('active');
103
		$.ajax({
104
			url: $('#categorypreference-'+$(that).data('id')).attr('action'),
105
			data: $('#categorypreference-'+$(that).data('id')).serialize(),
106
			// Tell jQuery we're expecting JSONP
107
			dataType: "json",
108
			// Tell YQL what we want and that we want JSON			
109
			method: 'post',
110
			// Work with the response
111
			success: function( response ) {
112
				if(response.success){
113
					$('#preferences-'+$(that).data('id')).addClass('hidden');
114
				}
115
			}
116
		});
117
		ga('send', 'event', 'preferences', 'update', me);
118
	});
13901 anikendra 119
	// $('.row').on('click','.viewproduct',function(e){
120
	$(document).on('click','.viewproduct',function(e){
13686 anikendra 121
		$('#loadingModal').modal();
13901 anikendra 122
		console.log($(this).data('id'));
13579 anikendra 123
		var url = apihost+"clicks/add/"+me+"/"+$(this).data('id');
124
		$.ajax({
125
			url: url,
126
			// The name of the callback parameter, as specified by the YQL service
127
			jsonp: "callback",
128
			// Tell jQuery we're expecting JSONP
129
			dataType: "jsonp",
130
			// Tell YQL what we want and that we want JSON
131
			data: {
132
				format: "json"
133
			},
134
			// Work with the response
135
			success: function( response ) {
136
				if(response.success && response.type=='redirect'){
137
					document.location = response.url;
138
				}
139
			}
140
		});
13686 anikendra 141
		ga('send', 'event', 'product', 'click', $(this).data('id'));
13583 anikendra 142
	});
13719 anikendra 143
	$('.jscroll-inner').on('click','.likedeal',function(e){	
13583 anikendra 144
		var that = $(this);
13682 anikendra 145
		if($(that).hasClass('active')){
13583 anikendra 146
			//User has already liked it,so remove like
147
			var url = apihost+"/user_actions/rem/"+me+"/"+$(this).data('id')+"/like";	
148
		}else{
149
			var url = apihost+"/user_actions/update/"+me+"/"+$(this).data('id')+"/like";
150
		}
151
		$.ajax({
152
			url: url,
153
			// The name of the callback parameter, as specified by the YQL service
154
			jsonp: "callback",
155
			// Tell jQuery we're expecting JSONP
156
			dataType: "jsonp",
157
			// Tell YQL what we want and that we want JSON
158
			data: {
159
				format: "json"
160
			},
161
			// Work with the response
162
			success: function( response ) {
163
				if(response.success){
13672 anikendra 164
					$(that).toggleClass('active');
165
					$(that).parent().find('li.dislikedeal',0).removeClass('active');
13583 anikendra 166
				}
167
			}
168
		});
13686 anikendra 169
		ga('send', 'event', 'product', 'like', $(this).data('id'));
13583 anikendra 170
	});
13688 anikendra 171
	$('#myModal').on('click','#unlikebtn',function(e){
13682 anikendra 172
		e.preventDefault();
173
		var url = $('#unlikeproductform').attr('action')+'?'+$('#unlikeproductform').serialize()
174
		console.log(url);
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
				// console.log(response);						
188
			}
189
		});
190
		$('#myModal').modal('hide');
13686 anikendra 191
		ga('send', 'event', 'brand', 'hide', $('#myModal').find('#productToHide',0).val());
13682 anikendra 192
	})
13719 anikendra 193
	$('.jscroll-inner').on('click','.dislikedeal',function(e){			
13583 anikendra 194
		var that = $(this);
13682 anikendra 195
		if($(that).hasClass('active')){
13583 anikendra 196
			//User has already liked it,so remove like
197
			var url = apihost+"/user_actions/rem/"+me+"/"+$(this).data('id')+"/dislike";	
198
		}else{
13682 anikendra 199
			console.log('show modal');
200
			$('#myModal').find('#productToHide',0).val($(this).data('id'));
201
			$('#myModal').modal();
13583 anikendra 202
			var url = apihost+"/user_actions/update/"+me+"/"+$(this).data('id')+"/dislike";
203
		}
204
		$.ajax({
205
			url: url,
206
			// The name of the callback parameter, as specified by the YQL service
207
			jsonp: "callback",
208
			// Tell jQuery we're expecting JSONP
209
			dataType: "jsonp",
210
			// Tell YQL what we want and that we want JSON
211
			data: {
212
				format: "json"
213
			},
214
			// Work with the response
215
			success: function( response ) {
216
				if(response.success){
13672 anikendra 217
					$(that).toggleClass('active');
218
					$(that).parent().find('li.likedeal',0).removeClass('active');
13583 anikendra 219
				}
220
			}
221
		});
13686 anikendra 222
		ga('send', 'event', 'product', 'dislike', $(this).data('id'));
13583 anikendra 223
	});
13731 anikendra 224
	$('.deletefav').on('click',function(){
225
		var that = $(this);
226
		$('#loadingModal').modal();
227
		var url = apihost+"/user_actions/deletefav/"+me+"/"+$(this).data('id');			
228
		$.ajax({
229
			url: url,
230
			// The name of the callback parameter, as specified by the YQL service
231
			jsonp: "callback",
232
			// Tell jQuery we're expecting JSONP
233
			dataType: "jsonp",
234
			// Tell YQL what we want and that we want JSON
235
			data: {
236
				format: "json"
237
			},
238
			// Work with the response
239
			success: function( response ) {
240
				$('#loadingModal').modal('hide');
241
				if(response.success){			
242
					$('#fav-'+$(that).data('id')).hide('slow');
243
				}
244
			}
245
		});
246
		ga('send', 'event', 'favourites', 'remove', $(this).data('id'));
247
	});
248
	$('.clearfavs').on('click',function(){
249
		var that = $(this);
250
		$('#loadingModal').modal();
251
		var url = apihost+"/user_actions/deleteallfavs/"+me;			
252
		$.ajax({
253
			url: url,
254
			// The name of the callback parameter, as specified by the YQL service
255
			jsonp: "callback",
256
			// Tell jQuery we're expecting JSONP
257
			dataType: "jsonp",
258
			// Tell YQL what we want and that we want JSON
259
			data: {
260
				format: "json"
261
			},
262
			// Work with the response
263
			success: function( response ) {
264
				$('#loadingModal').modal('hide');
265
				if(response.success){			
266
					$('.deletefav').each(function(){
267
						$('#fav-'+$(this).data('id')).hide('slow');
268
					})					
269
				}
270
				$(that).hide();
271
			}
272
		});
273
		ga('send', 'event', 'favourites', 'removeall', me);
274
	});
13759 anikendra 275
	$('.revealbrands').on('click',function(){
276
		$(this).parent().find('.notfeatured').toggleClass('hidden');
277
		if($(this).html()=='Others'){
278
			$(this).html('Hide');
279
		}else{
280
			$(this).html('Others');
281
		}
282
	});
13992 anikendra 283
});