Subversion Repositories SmartDukaan

Rev

Rev 3551 | Rev 5291 | 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
 
3440 varun.gupt 12
	function updateWithFeedback()	{
13
		$.ajax({
14
			url: '/feedback',
15
			type: 'GET',
16
			success: function(data){
17
				feedbacks = eval('(' + data + ')');
18
 
19
				$.each(feedbacks, function(entityId, feedback){
20
					var tr = $('#' + entityId);
21
 
22
					$.each(feedback, function(source, feedback_instruction){
3462 varun.gupt 23
 
24
						var td = $(tr).children('td[source=' + source + ']')[0];
25
 
26
						if($(td).hasClass('conflict'))	updateCellWithFeedback(td, feedback_instruction);
3440 varun.gupt 27
					});
3446 varun.gupt 28
					markBestPrice(tr);
3440 varun.gupt 29
				});
30
			}
31
		})
32
	}
33
 
34
	function updateCellWithFeedback(td, feedback)	{
35
		var anchor = $(td).children('.link-conflict');
3462 varun.gupt 36
 
3440 varun.gupt 37
		var source = $(td).attr('source');
38
		$(anchor).html('Filtered');
39
 
40
		if(feedback.type == 'reject')	{
41
			$(td).html('Not Found ' + $('<a>').append($(anchor).clone()).remove().html()).addClass('with-feedback');
42
 
43
		} else	{
44
			var itemInfo = lookupInfoForItem(eval($(td).attr('data')), feedback.selected_item);
4198 varun.gupt 45
 
46
			if (itemInfo != null)	{
47
				$(td).html(
48
					'<a href="' + baseUrl[source] + itemInfo.url + '">' + itemInfo.price + '</a> ' + 
49
					$('<a>').append($(anchor).clone()).remove().html()
50
				).addClass('with-feedback');
51
			}
3440 varun.gupt 52
		}
53
	}
54
 
55
	function lookupInfoForItem(data, itemName)	{
56
		var info = null;
57
 
58
		$.each(data, function(index, item){
59
 
60
			if (item.name == itemName)	{
61
				info = {name: item.name, price: item.price, url: item.url};
62
			}
63
		});
64
		return info;
65
	}
66
 
67
	$('tbody tr').each(function(index, e)	{
68
 		var class_tr = index % 2 == 0 ? 'even' : 'odd';
69
 		$(e).addClass(class_tr);
70
 		markBestPrice(this);
4198 varun.gupt 71
 
3440 varun.gupt 72
 		$(this).find('td').each(function(){
3462 varun.gupt 73
 			if($(this).hasClass('conflict'))	$(this).children('a').addClass('link-conflict');
4198 varun.gupt 74
 
75
 			if(! $(this).hasClass('diff') && ! $(this).hasClass('name') && ! $(this).hasClass('saholic'))	{
76
 				$(this).append('<span class="url-feedback-link" title="Click to map a product page URL">U</span>');
77
 			}
3440 varun.gupt 78
 		});
79
	});
80
 
81
	updateWithFeedback();
82
 
3313 varun.gupt 83
	function markBestPrice(trNode)	{
3446 varun.gupt 84
		$(trNode).children().removeClass('best');
4198 varun.gupt 85
		var tdBestPrice = $(trNode).children('.saholic').html();
86
		var saholicPrice = parseInt(tdBestPrice);
3313 varun.gupt 87
		var bestPrice = saholicPrice;
3551 varun.gupt 88
		var maxPrice = saholicPrice;
3446 varun.gupt 89
 
90
		$(trNode).find('a[href!="#"]').each(function(index, a){
91
			var price = getPriceAsInt(a.innerHTML);
3313 varun.gupt 92
 
93
			if (price < bestPrice)	{
94
				bestPrice = price;
95
				tdBestPrice = $(a).parent();
96
			}
3551 varun.gupt 97
 
98
			if(price > maxPrice)	maxPrice = price;
3313 varun.gupt 99
		});
3551 varun.gupt 100
		var maxMinDiff = roundNumber((maxPrice - bestPrice) * 100 / maxPrice, 2);
101
 
3313 varun.gupt 102
		$(tdBestPrice).addClass('best');
103
 
104
		if (bestPrice < saholicPrice)	$($(trNode).children()[0]).addClass('red');
3551 varun.gupt 105
		var tds = $(trNode).children()
106
 
107
		if (tds.length == 7)	{
108
			tds[6].innerHTML = maxMinDiff + '%'
4198 varun.gupt 109
		} else	{var diffCssClass = '';
110
 
111
		if(maxMinDiff >= 25.0)	{
112
			diffCssClass = 'dark-orange';
113
		} else if(maxMinDiff >= 10.0)	{
114
			diffCssClass = 'orange';
3551 varun.gupt 115
		}
4198 varun.gupt 116
		$(trNode).append('<td class="diff ' + diffCssClass + '" title="Max-Min price difference">' + maxMinDiff + '%</td>');
117
		}
3313 varun.gupt 118
	}
119
 
120
	var baseUrl = {
121
		'flipkart': 'http://www.flipkart.com',
122
		'homeshop18': '',
123
		'infibeam': 'http://www.infibeam.com',
124
		'letsbuy': ''
125
	};
126
 
3440 varun.gupt 127
	$('.conflict a').live('click', function(){
128
		var td = $(this).parent();
3232 varun.gupt 129
 
3440 varun.gupt 130
		var entityId = $(td).parent().attr('id');
131
		var source = $(td).attr('source');
132
 
133
		var data = eval("{results: " + $(td).attr('data') + "}");
134
 
135
		var feedback = null;
136
		var selectedItem = null; 
137
 
138
		if(feedbacks && feedbacks[entityId] && feedbacks[entityId][source])	{
139
			feedback = feedbacks[entityId][source];
140
 
141
			if (feedback.type == 'select')	selectedItem = feedback.selected_item;
142
		}
143
 
144
		if (feedback && feedback.type == 'reject')	{
145
			var text = '<div class="msg">Currently following set is marked <i>rejected</i></div>';
146
 
147
		} else	{
148
			var text = '';
149
		}
150
		text += '<table id="' + entityId + '" source="' + source + '">';
151
 
3232 varun.gupt 152
		for (i in data)	{
3440 varun.gupt 153
			var checked = selectedItem && selectedItem == data[i]['name'] ? 'checked' : ''; 
3232 varun.gupt 154
			text += '<tr>';
155
			text += '<td>' + data[i]['name'] + '</td>';
3313 varun.gupt 156
			text += '<td><a target="_blank" href="' + baseUrl[data[i]['source']] + data[i]['url'] + '">' + data[i]['price'] + '</a></td>';
3440 varun.gupt 157
			text += '<td><input type="radio" name="chosen_one_' + entityId + '" value="' + data[i]['name'] + '" ' + checked + '></td>';
3232 varun.gupt 158
			text += '</tr>';
159
		}
3440 varun.gupt 160
		text += '<tr>';
161
		text += '<td colspan="2" align="center"><a id="feedback-reject" href="#">Reject All</a></td>';
162
		text += '<td><a id="feedback-select" href="#">Save</a></td>';
163
		text += '</tr>';
3232 varun.gupt 164
		text += '</table>';
3440 varun.gupt 165
 
3232 varun.gupt 166
		$.facebox(text);
167
	});
3440 varun.gupt 168
 
169
	$('#feedback-reject').live('click', function(){
170
		var table = $(this).parents('table');
171
		var feedback = {
172
				type: 'reject',
173
				entityId: $(table).attr('id'),
174
				source: $(table).attr('source')
175
		};
176
		postFeedback(feedback, table);
177
	});
178
 
179
	$('#feedback-select').live('click', function(){
180
		var table = $(this).parents('table');
181
		var entityId = $(table).attr('id');
182
 
183
		var selected_item = $("input:radio[name='chosen_one_" + entityId + "']:checked").val();
184
 
185
		if(! selected_item)	{
186
 
187
			showMsg(table, true, 'At least one option must be selected');
188
 
189
		} else	{
190
			var feedback = {
191
					type: 'select',
192
					entityId: entityId,
193
					source: $(table).attr('source'),
194
					selected: selected_item
195
			};
196
			postFeedback(feedback, table);
197
		}
198
	});
199
 
200
	function postFeedback(feedback, table)	{
201
		var feedbackType = feedback.type;
202
		var entityId = feedback.entityId;
203
 
204
		$.ajax({
205
			url: '/feedback',
206
			type: 'POST',
207
			data: feedback,
208
			success: function(msg)	{
209
 
210
				if (feedbackType == 'reject')	{
211
					$("input:radio[name='chosen_one_" + entityId + "']:checked").prop('checked', false);
212
				} else	{
213
 
214
				}
215
				var td = $('#' + feedback.entityId).children('td[source=' + feedback.source + ']')[0];
216
				updateCellWithFeedback(td, {type: feedback.type, selected_item: feedback.selected});
217
				showMsg(table, false, 'Your feedback has been saved successfully.');
3446 varun.gupt 218
				markBestPrice($(td).parent());
3440 varun.gupt 219
			},
220
			error: function(msg)	{
221
				showMsg(table, true, 'Error! please try again.');
222
			}
223
		});
224
	}
225
 
226
	function showMsg(table, is_error, msg)	{
227
		msg_class = is_error ? 'error' : 'notice';
228
 
229
		if($(table).siblings('.msg').length > 0)	{
230
			var msgDiv = $(table).siblings('.msg')[0];
231
			$(msgDiv).html(msg);
232
 
233
		} else	{
234
			$(table).before('<div class="msg">' + msg + '</div>');
235
		}
236
	}
237
 
4198 varun.gupt 238
	$('.url-feedback-link').live('click', function(){
239
		var source = $(this).parent().attr('source');
240
		var entity = $(this).parent().parent().attr('id');
241
//		console.log(source, entity);
242
		var html = '<div class="form-url-feedback">\n';
243
		var html = 'Enter the URL you want to be crawled for this product<br>\n';
244
		html += '<input type="text" id="url-feedback" size="40"><br />\n';
245
		html += '<input type="hidden" id="entity-url-feedback" value="' + entity + '">\n';
246
		html += '<input type="hidden" id="source-url-feedback" value="' + source + '">\n';
247
		html += '<input type="button" id="submit-url-feedback" value="Save">\n';
248
		html += '</div>';
249
		$.facebox(html);
250
	});
251
 
252
	$('#submit-url-feedback').live('click', function(){
253
		$.ajax({
254
			url: '/feedback-url',
255
			type: 'POST',
256
			data: {
257
				entity: $('#entity-url-feedback').val(), 
258
				source: $('#source-url-feedback').val(), 
259
				url: $('#url-feedback').val()
260
			},
261
			beforeSend: function(){
262
				$('#url-feedback, #submit-url-feedback').attr('disabled', 'disabled');
263
				$('#submit-url-feedback').val('Saving...').after('<img src="/static/images/loading.gif" width="24" />');
264
			},
265
			success: function(data){
266
				console.log(data);
267
				var html = '<table>';
268
				html += '<tr><td class="msg" colspan="2" align="center">The URL is saved and will be crawled next time</td></tr>';
269
				html += '<tr><td align="center">' + data.name + '</td><td align="center">Rs.' + data.price + '</td></tr>';
270
				html += '<tr><td colspan="2" align="center">Please refresh the page to see the changes</td></tr>';
271
				html += '</table>';
272
				var form = $('#facebox').find('.content').html(html);
273
			}
274
		});
275
	});
276
 
277
	$('#howTo').click(function(){
278
		html = '<ul>\n\
279
			<li class="first">The price data is crawled directly from competitor websites at 8:00 AM everyday</li>\n\
280
			<li class="first">All price figures are linked to their respective product pages</li>\n\
281
			<li class="first">Cells showing "Not Found" are linked to search result page of relevant website.</li>\n\
282
			<li class="first">Cells showing "Conflict" are linked to set of products which are conflicting to match with\n\
283
			the product. You can either select one of them or reject all depending on the case. The selection \n\
284
			will be taken into account when showing the dashboard next time.</li>\n\
285
			<li class="first">Clicking on "U" (present in all price cells) will pop up a form where you can submit a URL \n\
286
			pointing to the product page of the product. After submission this URL will be crawled and indexed \n\
287
			in real-time and saved in the list of URLs for scheduled crawling in future.</li>\n\
288
			<li class="first">In each row, right most cell shows the percentage difference between the minimum and maximum \n\
289
			price of that product. Color coding:\n\
290
				<ul>\n\
291
					<li>Yellow: 10 - 24.99% difference</li>\n\
292
					<li>Orange: Above 24.99%</li>\n\
293
				</ul></li>\n\
294
			<li class="first">In case of further clarification, email your query at varun.gupta@shop2020.in</li>\n\
295
			</ul>';
296
		$.facebox(html);
297
	});
298
 
3440 varun.gupt 299
	$('#facebox .close').live('click', function(){});
3232 varun.gupt 300
});