Subversion Repositories SmartDukaan

Rev

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