Subversion Repositories SmartDukaan

Rev

Rev 3462 | Rev 4198 | 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);
45
			$(td).html('<a href="' + baseUrl[source] + itemInfo.url + '">' + itemInfo.price + '</a> ' + $('<a>').append($(anchor).clone()).remove().html()).addClass('with-feedback');
46
		}
47
	}
48
 
49
	function lookupInfoForItem(data, itemName)	{
50
		var info = null;
51
 
52
		$.each(data, function(index, item){
53
 
54
			if (item.name == itemName)	{
55
				info = {name: item.name, price: item.price, url: item.url};
56
			}
57
		});
58
		return info;
59
	}
60
 
61
	$('tbody tr').each(function(index, e)	{
62
 		var class_tr = index % 2 == 0 ? 'even' : 'odd';
63
 		$(e).addClass(class_tr);
64
 		markBestPrice(this);
65
 		$(this).find('td').each(function(){
3462 varun.gupt 66
 			if($(this).hasClass('conflict'))	$(this).children('a').addClass('link-conflict');
3440 varun.gupt 67
 		});
68
	});
69
 
70
	updateWithFeedback();
71
 
3313 varun.gupt 72
	function markBestPrice(trNode)	{
3446 varun.gupt 73
		$(trNode).children().removeClass('best');
3313 varun.gupt 74
		var tdBestPrice = $(trNode).children('td')[1];
75
		var saholicPrice = parseInt(tdBestPrice.innerHTML);
76
		var bestPrice = saholicPrice;
3551 varun.gupt 77
		var maxPrice = saholicPrice;
3446 varun.gupt 78
 
79
		$(trNode).find('a[href!="#"]').each(function(index, a){
80
			var price = getPriceAsInt(a.innerHTML);
3313 varun.gupt 81
 
82
			if (price < bestPrice)	{
83
				bestPrice = price;
84
				tdBestPrice = $(a).parent();
85
			}
3551 varun.gupt 86
 
87
			if(price > maxPrice)	maxPrice = price;
3313 varun.gupt 88
		});
3551 varun.gupt 89
		var maxMinDiff = roundNumber((maxPrice - bestPrice) * 100 / maxPrice, 2);
90
 
3313 varun.gupt 91
		$(tdBestPrice).addClass('best');
92
 
93
		if (bestPrice < saholicPrice)	$($(trNode).children()[0]).addClass('red');
3551 varun.gupt 94
		var tds = $(trNode).children()
95
 
96
		if (tds.length == 7)	{
97
			tds[6].innerHTML = maxMinDiff + '%'
98
		} else	{
99
			$(trNode).append('<td class="diff" title="Max-Min price difference">' + maxMinDiff + '%</td>');
100
		}
3313 varun.gupt 101
	}
102
 
103
	var baseUrl = {
104
		'flipkart': 'http://www.flipkart.com',
105
		'homeshop18': '',
106
		'infibeam': 'http://www.infibeam.com',
107
		'letsbuy': ''
108
	};
109
 
3440 varun.gupt 110
	$('.conflict a').live('click', function(){
111
		var td = $(this).parent();
3232 varun.gupt 112
 
3440 varun.gupt 113
		var entityId = $(td).parent().attr('id');
114
		var source = $(td).attr('source');
115
 
116
		var data = eval("{results: " + $(td).attr('data') + "}");
117
 
118
		var feedback = null;
119
		var selectedItem = null; 
120
 
121
		if(feedbacks && feedbacks[entityId] && feedbacks[entityId][source])	{
122
			feedback = feedbacks[entityId][source];
123
 
124
			if (feedback.type == 'select')	selectedItem = feedback.selected_item;
125
		}
126
 
127
		if (feedback && feedback.type == 'reject')	{
128
			var text = '<div class="msg">Currently following set is marked <i>rejected</i></div>';
129
 
130
		} else	{
131
			var text = '';
132
		}
133
		text += '<table id="' + entityId + '" source="' + source + '">';
134
 
3232 varun.gupt 135
		for (i in data)	{
3440 varun.gupt 136
			var checked = selectedItem && selectedItem == data[i]['name'] ? 'checked' : ''; 
3232 varun.gupt 137
			text += '<tr>';
138
			text += '<td>' + data[i]['name'] + '</td>';
3313 varun.gupt 139
			text += '<td><a target="_blank" href="' + baseUrl[data[i]['source']] + data[i]['url'] + '">' + data[i]['price'] + '</a></td>';
3440 varun.gupt 140
			text += '<td><input type="radio" name="chosen_one_' + entityId + '" value="' + data[i]['name'] + '" ' + checked + '></td>';
3232 varun.gupt 141
			text += '</tr>';
142
		}
3440 varun.gupt 143
		text += '<tr>';
144
		text += '<td colspan="2" align="center"><a id="feedback-reject" href="#">Reject All</a></td>';
145
		text += '<td><a id="feedback-select" href="#">Save</a></td>';
146
		text += '</tr>';
3232 varun.gupt 147
		text += '</table>';
3440 varun.gupt 148
 
3232 varun.gupt 149
		$.facebox(text);
150
	});
3440 varun.gupt 151
 
152
	$('#feedback-reject').live('click', function(){
153
		var table = $(this).parents('table');
154
		var feedback = {
155
				type: 'reject',
156
				entityId: $(table).attr('id'),
157
				source: $(table).attr('source')
158
		};
159
		postFeedback(feedback, table);
160
	});
161
 
162
	$('#feedback-select').live('click', function(){
163
		var table = $(this).parents('table');
164
		var entityId = $(table).attr('id');
165
 
166
		var selected_item = $("input:radio[name='chosen_one_" + entityId + "']:checked").val();
167
 
168
		if(! selected_item)	{
169
 
170
			showMsg(table, true, 'At least one option must be selected');
171
 
172
		} else	{
173
			var feedback = {
174
					type: 'select',
175
					entityId: entityId,
176
					source: $(table).attr('source'),
177
					selected: selected_item
178
			};
179
			postFeedback(feedback, table);
180
		}
181
	});
182
 
183
	function postFeedback(feedback, table)	{
184
		var feedbackType = feedback.type;
185
		var entityId = feedback.entityId;
186
 
187
		$.ajax({
188
			url: '/feedback',
189
			type: 'POST',
190
			data: feedback,
191
			success: function(msg)	{
192
 
193
				if (feedbackType == 'reject')	{
194
					$("input:radio[name='chosen_one_" + entityId + "']:checked").prop('checked', false);
195
				} else	{
196
 
197
				}
198
				var td = $('#' + feedback.entityId).children('td[source=' + feedback.source + ']')[0];
199
				updateCellWithFeedback(td, {type: feedback.type, selected_item: feedback.selected});
200
				showMsg(table, false, 'Your feedback has been saved successfully.');
3446 varun.gupt 201
				markBestPrice($(td).parent());
3440 varun.gupt 202
			},
203
			error: function(msg)	{
204
				showMsg(table, true, 'Error! please try again.');
205
			}
206
		});
207
	}
208
 
209
	function showMsg(table, is_error, msg)	{
210
		msg_class = is_error ? 'error' : 'notice';
211
 
212
		if($(table).siblings('.msg').length > 0)	{
213
			var msgDiv = $(table).siblings('.msg')[0];
214
			$(msgDiv).html(msg);
215
 
216
		} else	{
217
			$(table).before('<div class="msg">' + msg + '</div>');
218
		}
219
	}
220
 
221
	$('#facebox .close').live('click', function(){});
3232 varun.gupt 222
});