Subversion Repositories SmartDukaan

Rev

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