Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
3232 varun.gupt 1
$(function(){
3440 varun.gupt 2
	feedbacks = null;
3232 varun.gupt 3
 
3446 varun.gupt 4
	function getPriceAsInt(priceString)	{
5
		return parseInt(priceString.replace('Rs.', '').replace(',', ''));
6
	}
7
 
3551 varun.gupt 8
	function roundNumber(num, dec) {
9
		return Math.round(num * Math.pow(10, dec)) / Math.pow(10, dec);
10
	}
11
 
5291 varun.gupt 12
	function updateWithSecondaryCrawledData()	{
13
		$.ajax({
14
			url: '/feedback-url',
15
			type: 'GET',
5329 varun.gupt 16
			dataType: 'html',
17
 
18
			success: function(dataString){
19
				var data = eval('(' + dataString + ')');
20
				//console.log(dataString, data);
5291 varun.gupt 21
 
22
				$.each(data, function(entityId, sourceData){
5329 varun.gupt 23
					//console.log(entityId, sourceData);
5507 varun.gupt 24
					var tr = $('#' + entityId);
5291 varun.gupt 25
 
26
					$.each(sourceData, function(source, productData){
5329 varun.gupt 27
						//console.log(source, productData);
5291 varun.gupt 28
 
29
						var td = $(tr).children('td[source=' + source +']')[0];
30
						var anchor = $(td).children('a')[0];
5329 varun.gupt 31
						//console.log(anchor);
5291 varun.gupt 32
 
33
						$(anchor).attr('href', productData['product_url']);
34
						$(anchor).html(productData['price']);
35
 
36
						$(td).children('span.url-feedback-link').addClass('url-saved');
37
					});
5507 varun.gupt 38
					markBestPrice(tr);
5291 varun.gupt 39
				});
40
			}
41
		});
42
	}
43
 
3440 varun.gupt 44
	function updateWithFeedback()	{
45
		$.ajax({
46
			url: '/feedback',
47
			type: 'GET',
48
			success: function(data){
49
				feedbacks = eval('(' + data + ')');
50
 
51
				$.each(feedbacks, function(entityId, feedback){
52
					var tr = $('#' + entityId);
53
 
54
					$.each(feedback, function(source, feedback_instruction){
3462 varun.gupt 55
 
56
						var td = $(tr).children('td[source=' + source + ']')[0];
57
 
5738 amar.kumar 58
						//if($(td).hasClass('conflict'))	
59
						updateCellWithFeedback(td, feedback_instruction);
3440 varun.gupt 60
					});
3446 varun.gupt 61
					markBestPrice(tr);
3440 varun.gupt 62
				});
5291 varun.gupt 63
				updateWithSecondaryCrawledData();
3440 varun.gupt 64
			}
65
		})
66
	}
67
 
68
	function updateCellWithFeedback(td, feedback)	{
69
		var anchor = $(td).children('.link-conflict');
3462 varun.gupt 70
 
3440 varun.gupt 71
		var source = $(td).attr('source');
5291 varun.gupt 72
		var cellActionLinks = $(td).children('.url-feedback-link, .reject-link').clone();
3440 varun.gupt 73
		$(anchor).html('Filtered');
74
 
75
		if(feedback.type == 'reject')	{
76
			$(td).html('Not Found ' + $('<a>').append($(anchor).clone()).remove().html()).addClass('with-feedback');
5291 varun.gupt 77
			$(td).append(cellActionLinks);
78
			$(td).children('span.reject-link').addClass('rejected')
3440 varun.gupt 79
 
80
		} else	{
81
			var itemInfo = lookupInfoForItem(eval($(td).attr('data')), feedback.selected_item);
4198 varun.gupt 82
 
83
			if (itemInfo != null)	{
84
				$(td).html(
85
					'<a href="' + baseUrl[source] + itemInfo.url + '">' + itemInfo.price + '</a> ' + 
86
					$('<a>').append($(anchor).clone()).remove().html()
87
				).addClass('with-feedback');
88
			}
3440 varun.gupt 89
		}
90
	}
91
 
92
	function lookupInfoForItem(data, itemName)	{
93
		var info = null;
94
 
5738 amar.kumar 95
		try {
96
			$.each(data, function(index, item){
97
 
98
				if (item.name == itemName)	{
99
					info = {name: item.name, price: item.price, url: item.url};
100
				}
101
			});
102
		} catch(err) {
103
		}
104
			return info;
105
 
3440 varun.gupt 106
	}
107
 
108
	$('tbody tr').each(function(index, e)	{
109
 		var class_tr = index % 2 == 0 ? 'even' : 'odd';
110
 		$(e).addClass(class_tr);
111
 		markBestPrice(this);
4198 varun.gupt 112
 
3440 varun.gupt 113
 		$(this).find('td').each(function(){
3462 varun.gupt 114
 			if($(this).hasClass('conflict'))	$(this).children('a').addClass('link-conflict');
4198 varun.gupt 115
 
116
 			if(! $(this).hasClass('diff') && ! $(this).hasClass('name') && ! $(this).hasClass('saholic'))	{
5291 varun.gupt 117
 				$(this).append('<span class="reject-link" title="Click to reject the displayed figure">X</span>&nbsp;');
4198 varun.gupt 118
 				$(this).append('<span class="url-feedback-link" title="Click to map a product page URL">U</span>');
119
 			}
3440 varun.gupt 120
 		});
121
	});
122
 
123
	updateWithFeedback();
124
 
3313 varun.gupt 125
	function markBestPrice(trNode)	{
3446 varun.gupt 126
		$(trNode).children().removeClass('best');
5291 varun.gupt 127
		var tdBestPrice = $(trNode).children('.saholic')[0];
128
		var saholicPrice = parseInt($(tdBestPrice).html());
3313 varun.gupt 129
		var bestPrice = saholicPrice;
3551 varun.gupt 130
		var maxPrice = saholicPrice;
3446 varun.gupt 131
 
132
		$(trNode).find('a[href!="#"]').each(function(index, a){
133
			var price = getPriceAsInt(a.innerHTML);
5291 varun.gupt 134
 
3313 varun.gupt 135
			if (price < bestPrice)	{
136
				bestPrice = price;
137
				tdBestPrice = $(a).parent();
138
			}
3551 varun.gupt 139
 
140
			if(price > maxPrice)	maxPrice = price;
3313 varun.gupt 141
		});
3551 varun.gupt 142
		var maxMinDiff = roundNumber((maxPrice - bestPrice) * 100 / maxPrice, 2);
143
 
3313 varun.gupt 144
		$(tdBestPrice).addClass('best');
145
 
146
		if (bestPrice < saholicPrice)	$($(trNode).children()[0]).addClass('red');
3551 varun.gupt 147
		var tds = $(trNode).children()
148
 
5639 amar.kumar 149
		if (tds.length == 9)	{
150
			tds[8].innerHTML = maxMinDiff + '%'
4198 varun.gupt 151
		} else	{var diffCssClass = '';
152
 
153
		if(maxMinDiff >= 25.0)	{
154
			diffCssClass = 'dark-orange';
155
		} else if(maxMinDiff >= 10.0)	{
156
			diffCssClass = 'orange';
3551 varun.gupt 157
		}
4198 varun.gupt 158
		$(trNode).append('<td class="diff ' + diffCssClass + '" title="Max-Min price difference">' + maxMinDiff + '%</td>');
159
		}
3313 varun.gupt 160
	}
161
 
162
	var baseUrl = {
163
		'flipkart': 'http://www.flipkart.com',
164
		'homeshop18': '',
165
		'infibeam': 'http://www.infibeam.com',
5291 varun.gupt 166
		'letsbuy': '',
5639 amar.kumar 167
		'snapdeal': 'http://www.snapdeal.com/',
168
		'sulekha': '',
169
		'tradus': 'http://www.tradus.com'
3313 varun.gupt 170
	};
171
 
3440 varun.gupt 172
	$('.conflict a').live('click', function(){
173
		var td = $(this).parent();
3232 varun.gupt 174
 
3440 varun.gupt 175
		var entityId = $(td).parent().attr('id');
176
		var source = $(td).attr('source');
177
 
178
		var data = eval("{results: " + $(td).attr('data') + "}");
179
 
180
		var feedback = null;
181
		var selectedItem = null; 
182
 
183
		if(feedbacks && feedbacks[entityId] && feedbacks[entityId][source])	{
184
			feedback = feedbacks[entityId][source];
185
 
186
			if (feedback.type == 'select')	selectedItem = feedback.selected_item;
187
		}
188
 
189
		if (feedback && feedback.type == 'reject')	{
190
			var text = '<div class="msg">Currently following set is marked <i>rejected</i></div>';
191
 
192
		} else	{
193
			var text = '';
194
		}
195
		text += '<table id="' + entityId + '" source="' + source + '">';
196
 
3232 varun.gupt 197
		for (i in data)	{
3440 varun.gupt 198
			var checked = selectedItem && selectedItem == data[i]['name'] ? 'checked' : ''; 
3232 varun.gupt 199
			text += '<tr>';
200
			text += '<td>' + data[i]['name'] + '</td>';
3313 varun.gupt 201
			text += '<td><a target="_blank" href="' + baseUrl[data[i]['source']] + data[i]['url'] + '">' + data[i]['price'] + '</a></td>';
3440 varun.gupt 202
			text += '<td><input type="radio" name="chosen_one_' + entityId + '" value="' + data[i]['name'] + '" ' + checked + '></td>';
3232 varun.gupt 203
			text += '</tr>';
204
		}
3440 varun.gupt 205
		text += '<tr>';
206
		text += '<td colspan="2" align="center"><a id="feedback-reject" href="#">Reject All</a></td>';
207
		text += '<td><a id="feedback-select" href="#">Save</a></td>';
208
		text += '</tr>';
3232 varun.gupt 209
		text += '</table>';
3440 varun.gupt 210
 
3232 varun.gupt 211
		$.facebox(text);
212
	});
3440 varun.gupt 213
 
214
	$('#feedback-reject').live('click', function(){
215
		var table = $(this).parents('table');
216
		var feedback = {
217
				type: 'reject',
218
				entityId: $(table).attr('id'),
219
				source: $(table).attr('source')
220
		};
221
		postFeedback(feedback, table);
222
	});
223
 
224
	$('#feedback-select').live('click', function(){
225
		var table = $(this).parents('table');
226
		var entityId = $(table).attr('id');
227
 
228
		var selected_item = $("input:radio[name='chosen_one_" + entityId + "']:checked").val();
229
 
230
		if(! selected_item)	{
231
 
232
			showMsg(table, true, 'At least one option must be selected');
233
 
234
		} else	{
235
			var feedback = {
236
					type: 'select',
237
					entityId: entityId,
238
					source: $(table).attr('source'),
239
					selected: selected_item
240
			};
241
			postFeedback(feedback, table);
242
		}
243
	});
244
 
245
	function postFeedback(feedback, table)	{
246
		var feedbackType = feedback.type;
247
		var entityId = feedback.entityId;
248
 
249
		$.ajax({
250
			url: '/feedback',
251
			type: 'POST',
252
			data: feedback,
253
			success: function(msg)	{
254
 
255
				if (feedbackType == 'reject')	{
256
					$("input:radio[name='chosen_one_" + entityId + "']:checked").prop('checked', false);
257
				}
258
				var td = $('#' + feedback.entityId).children('td[source=' + feedback.source + ']')[0];
259
				updateCellWithFeedback(td, {type: feedback.type, selected_item: feedback.selected});
5291 varun.gupt 260
 
261
				if(table && table != 'undefined')	{
262
					showMsg(table, false, 'Your feedback has been saved successfully.');
263
				}
3446 varun.gupt 264
				markBestPrice($(td).parent());
3440 varun.gupt 265
			},
266
			error: function(msg)	{
267
				showMsg(table, true, 'Error! please try again.');
268
			}
269
		});
270
	}
271
 
272
	function showMsg(table, is_error, msg)	{
273
		msg_class = is_error ? 'error' : 'notice';
274
 
275
		if($(table).siblings('.msg').length > 0)	{
276
			var msgDiv = $(table).siblings('.msg')[0];
277
			$(msgDiv).html(msg);
278
 
279
		} else	{
280
			$(table).before('<div class="msg">' + msg + '</div>');
281
		}
282
	}
283
 
4198 varun.gupt 284
	$('.url-feedback-link').live('click', function(){
285
		var source = $(this).parent().attr('source');
286
		var entity = $(this).parent().parent().attr('id');
287
//		console.log(source, entity);
288
		var html = '<div class="form-url-feedback">\n';
289
		var html = 'Enter the URL you want to be crawled for this product<br>\n';
290
		html += '<input type="text" id="url-feedback" size="40"><br />\n';
291
		html += '<input type="hidden" id="entity-url-feedback" value="' + entity + '">\n';
292
		html += '<input type="hidden" id="source-url-feedback" value="' + source + '">\n';
293
		html += '<input type="button" id="submit-url-feedback" value="Save">\n';
294
		html += '</div>';
295
		$.facebox(html);
296
	});
297
 
298
	$('#submit-url-feedback').live('click', function(){
299
		$.ajax({
300
			url: '/feedback-url',
301
			type: 'POST',
302
			data: {
303
				entity: $('#entity-url-feedback').val(), 
304
				source: $('#source-url-feedback').val(), 
305
				url: $('#url-feedback').val()
306
			},
307
			beforeSend: function(){
308
				$('#url-feedback, #submit-url-feedback').attr('disabled', 'disabled');
309
				$('#submit-url-feedback').val('Saving...').after('<img src="/static/images/loading.gif" width="24" />');
310
			},
311
			success: function(data){
312
				var html = '<table>';
313
				html += '<tr><td class="msg" colspan="2" align="center">The URL is saved and will be crawled next time</td></tr>';
5291 varun.gupt 314
				html += '<tr><td colspan="2">&nbsp;</td></tr>';
4198 varun.gupt 315
				html += '<tr><td align="center">' + data.name + '</td><td align="center">Rs.' + data.price + '</td></tr>';
316
				html += '</table>';
317
				var form = $('#facebox').find('.content').html(html);
5291 varun.gupt 318
 
319
				var tr = $('#' + data.entityId);
320
				var td = $(tr).children('td[source=' + data.source +']')[0];
321
				var anchor = $(td).children('a')[0];
322
 
323
				$(anchor).attr('href', data.product_url);
324
				$(anchor).html(data.price);
325
 
326
				$(td).children('span').addClass('url-saved');
4198 varun.gupt 327
			}
328
		});
329
	});
330
 
5291 varun.gupt 331
	$('.reject-link').live('click', function(){
332
		var source = $(this).parent().attr('source');
333
		var entity = $(this).parent().parent().attr('id');
334
 
335
		if($(this).hasClass('rejected'))	{
336
			var td = $(this).parent();
337
 
338
			var url = $(td).attr('url');
339
			var price = $(td).attr('price');
340
 
341
			var cellActionLinks = $(td).children('.url-feedback-link, .reject-link');
342
			$(td).html('<a target="_blank" href="' + url + '">' + price + '</a>');
343
			$(td).append(cellActionLinks);
344
			console.log($(td).children('.reject-link'));
345
			$($(td).children('.reject-link')[0]).removeClass('rejected');
346
 
347
		} else	{
348
			var feedback = {
349
					type: 'reject',
350
					entityId: entity,
351
					source: source
352
			};
353
 
354
			//Setting rejected URL in the cell's DOM
355
			var td = $('#' + entity).children('td[source=' + source + ']')[0];
356
			var anchor = $(td).children('a')[0];
357
			$(td).attr('price', $.trim($(anchor).html()));
358
			$(td).attr('url', $(anchor).attr('href'));
359
 
360
			postFeedback(feedback);
361
		}
362
	});
363
 
4198 varun.gupt 364
	$('#howTo').click(function(){
365
		html = '<ul>\n\
366
			<li class="first">The price data is crawled directly from competitor websites at 8:00 AM everyday</li>\n\
367
			<li class="first">All price figures are linked to their respective product pages</li>\n\
368
			<li class="first">Cells showing "Not Found" are linked to search result page of relevant website.</li>\n\
369
			<li class="first">Cells showing "Conflict" are linked to set of products which are conflicting to match with\n\
370
			the product. You can either select one of them or reject all depending on the case. The selection \n\
371
			will be taken into account when showing the dashboard next time.</li>\n\
372
			<li class="first">Clicking on "U" (present in all price cells) will pop up a form where you can submit a URL \n\
373
			pointing to the product page of the product. After submission this URL will be crawled and indexed \n\
374
			in real-time and saved in the list of URLs for scheduled crawling in future.</li>\n\
375
			<li class="first">In each row, right most cell shows the percentage difference between the minimum and maximum \n\
376
			price of that product. Color coding:\n\
377
				<ul>\n\
378
					<li>Yellow: 10 - 24.99% difference</li>\n\
379
					<li>Orange: Above 24.99%</li>\n\
380
				</ul></li>\n\
381
			<li class="first">In case of further clarification, email your query at varun.gupta@shop2020.in</li>\n\
382
			</ul>';
383
		$.facebox(html);
384
	});
385
 
5291 varun.gupt 386
	$('#all').click(function(){
387
		$('tbody tr').show();
388
	});
389
 
390
	$('#watchlist').click(function(){
391
		$('tbody tr .watchlist-icon').each(function(){
392
			if(! $(this).hasClass('on-watchlist'))	$(this).parent().parent().hide();
393
		});
394
	});
395
 
5401 varun.gupt 396
	$('#downloadXL').click(function(){
397
		$('form[action="/download"]').submit();
398
	});
399
 
3440 varun.gupt 400
	$('#facebox .close').live('click', function(){});
5291 varun.gupt 401
 
402
	var watchList = {
403
			get: function()	{
404
				$.ajax({
405
					type: 'GET',
406
					url: '/watchlist',
407
 
5329 varun.gupt 408
 
5291 varun.gupt 409
					error: function(jqXHR, textStatus, errorThrown)	{
5329 varun.gupt 410
						console.log(jqXHR, textStatus, errorThrown);
5291 varun.gupt 411
						alert('Could not get WatchList. Try again later.');
412
					},
413
					success: function(data, textStatus, jqXHR)	{
5329 varun.gupt 414
						console.log(data, textStatus, jqXHR);
5291 varun.gupt 415
						entities = eval('(' + data + ')');
416
 
417
						$.each(entities, function(index, entity)	{
418
							var elementId = '#' + entity;
419
							var tr = $(elementId);
420
							$($('#' + entity).find('.watchlist-icon')[0]).addClass('on-watchlist');
421
						});
422
					}
423
				});
424
			},
425
			add: function(entity)	{
426
				$.ajax({
427
					type: 'POST',
428
					url: '/watchlist',
429
					data: {'type': 'save', 'entity': entity},
430
 
431
					error: function(jqXHR, textStatus, errorThrown)	{
432
						console.log(jqXHR, textStatus, errorThrown);
433
						alert('Could not add this item to WatchList. Try again later.');
434
					},
435
					success: function(data, textStatus, jqXHR)	{
436
						console.log(data, textStatus, jqXHR);
437
						$($('#' + entity).find('.watchlist-icon')[0]).addClass('on-watchlist');
438
					}
439
				});
440
			},
441
			remove: function(entity)	{
442
				$.ajax({
443
					type: 'POST',
444
					url: '/watchlist',
445
					data: {'type': 'delete', 'entity': entity},
446
 
447
					error: function(jqXHR, textStatus, errorThrown)	{
448
						console.log(jqXHR, textStatus, errorThrown);
449
						alert('Could not remove this item from WatchList. Try again later.');
450
					},
451
					success: function(data, textStatus, jqXHR)	{
452
						console.log(data, textStatus, jqXHR);
453
						$($('#' + entity).find('.watchlist-icon')[0]).removeClass('on-watchlist');
454
					}
455
				});
456
			}
457
	};
458
 
459
	watchList.get();
460
 
461
	$('.watchlist-icon').click(function(){
462
 
463
		var entity = $(this).parent().parent().attr('id');
464
 
465
		if($(this).hasClass('on-watchlist'))	{
466
			watchList.remove(entity);
467
		} else	{
468
			watchList.add(entity);
469
		}
470
	});
3232 varun.gupt 471
});