Subversion Repositories SmartDukaan

Rev

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

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