/
products.js
301 lines (265 loc) · 9.11 KB
/
products.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/**
* Ajax functions for loading things from the API and setting the appropriate templates
*/
// Categories array of {id, name} json objects
var categoriesLoaded = false,
categoriesById = {};
function loadCategoriesIfNeeded() {
if (categoriesLoaded) return false;
$('.add_product_categories, #categories_list').empty();
$.ajax({
url: "api/categories",
dataType: "json",
async: false,
success: function(data) {
categoriesLoaded = true;
$.each(data, function(i, item) {
categoriesById[item.id] = item;
});
// template on create product page
$( "#category_option_template" ).tmpl( data ).appendTo( ".add_product_categories" );
// template on list categories page
$( "#category_list_row_template" ).tmpl( data ).appendTo( "#categories_list" );
setTimeout(function() {
var myselect = $("select.add_product_categories");
for(var i = 0; i < myselect.length; i++)
myselect[i].selectedIndex = 0;
}, 0);
},
error: ajaxError
});
}
// Loads a given product with id = id
function loadProduct(id) {
$( "#view_product_content" ).empty();
$.ajax({
url: "api/product/" + id,
dataType: "json",
async: false,
success: function(data, textStatus, jqXHR) {
//Create The New Rows From Template
addProduct(data);
$( "#product_template" ).tmpl( data ).appendTo( "#view_product_content" );
$('#view_product_content').trigger('create');
},
error: ajaxError
});
}
// Loads all the products from a category
function loadProducts(catId) {
$( "#products_list" ).empty();
$.ajax({
url: "api/categories/" + catId,
dataType: "json",
async: false,
success: function(data) {
//Create The New Rows From Template
addProducts(data);
$( "#product_list_row_template" ).tmpl( data ).appendTo( "#products_list" );
$('#products_list').listview('refresh');
},
error: ajaxError
});
}
// Loads all the products for the user
function loadUserProducts() {
$( "#products_list" ).empty();
$.ajax({
url: "api/user_product",
dataType: "json",
async: false,
success: function(data) {
//Create The New Rows From Template
addProducts(data);
$( "#product_list_row_template" ).tmpl( data ).appendTo( "#products_list" );
$('#products_list').listview('refresh');
},
error: function() { showError('No Products','You have not posted any products up!'); }
});
}
//stores our loaded products so we can cache them and not call the db all the time
var productsById = {};
function addProducts(products) {
$.each(products, function(i, item) {
addProduct(item);
});
}
function addProduct(product) {
productsById[product.id] = product;
}
window.currentUser = null;
$(function() {
$.ajax({
url: "/user",
dataType: 'text',
success: function(data) {
currentUser = data;
var $view = $('#view_product_content');
if ($view.find('.contact[data-id]').attr('data-id') == data)
$view.find('.actions').removeClass('not-mine').addClass('mine');
},
error: function() { showError('Not logged in', 'You don\'t appear to be logged in'); }
});
// Load categories if we need
$('#list_categories_page').bind('pagebeforeshow',function(event, ui){
loadCategoriesIfNeeded();
$('#categories_list').listview('refresh');
});
//Load categories before viewing add product page for the first time
$('#add_product_page, #edit_product_page').bind('pagebeforeshow', function() {
loadCategoriesIfNeeded();
$(this).find('select.add_product_categories').selectmenu('refresh');
});
$('#view_product_page').bind('pagebeforeshow', function() {
//we can't always count on getting the id from the url since
//sometimes jQuery doesn't update it, so we mostly rely on the link clicked
//if, however, we navigate direct to the page, no link was clicked so we'll need
//to load from the url
var idFromUrl = location.hash.split('=').pop();
if (! $('#view_product_content').children().length && idFromUrl)
loadProduct(idFromUrl);
});
$('#list_products_page').bind('pagebeforeshow',function(event, ui){
// same nastiness as above
var idFromUrl = location.hash.split('=').pop();
if (! $('#products_list').children().length && idFromUrl)
loadProducts(idFromUrl);
});
// Links to category pages should load the products
// and populate the template
$('a.category').live('click', function() {
var id = $(this).attr('data-id');
loadProducts(id);
});
// Links to product pages should load the product info
// and populate the template
$('a.product').live('click', function() {
var pId = $(this).attr('data-id');
loadProduct(pId);
});
// Links to edit product page should populate the template
// We already have the product cached
$('a.edit').live('click', function() {
var pId = $(this).attr('data-id');
if (productsById[pId]) {
// Set the edit template with this product
$('#edit_product_content .product_form').empty();
$('#edit_product_template').tmpl( productsById[pId] ).appendTo('#edit_product_content .product_form');
// Select the right category
setTimeout(function() {
var myselect = $("#edit_product_content select");
var index = 0;
myselect.find('option').each(function(i, item) {
if ($(item).attr('value') == productsById[pId]['category_id'])
index = i;
});
myselect[0].selectedIndex = index;
myselect.selectmenu('refresh');
}, 0);
}
});
//Listener for the mail button. Used to send the owner of an item an email.
$('.contact').live('click', function() {
var button = this;
$.ajax({
url: "/proxy/iam-dev01.iam.gatech.edu/directory/people?attribute=uid&query=" + $(this).attr('data-id'),
dataType: "xml",
async: false,
success: function(xml) {
var email = $(xml).find('mail').text();
var product_title = $(button).parent().parent().parent().find('.title').text();
var mailString = "mailto:" + email + "?subject=TechTrader:" + product_title +"&body=I am interested in your product.";
setTimeout(function() {
location.href=mailString;
});
},
error: ajaxError
});
});
//Listener for the mail button. Used to send the owner of an item an email.
$('#my_products_link').live('click', function() {
loadUserProducts();
});
//Handles the add product form
$('#add_button').bind('click', function() {
var $form = $('#add_product_content'),
data = {
title: $form.find('#add_product_title').val(),
description: $form.find('#add_product_description').val(),
price: $form.find('#add_product_price').val(),
categoryId: $form.find('.add_product_categories').val()
};
if (data.title.trim() === '') {
showError('Product title required', 'Please insert a valid product name.');
return false;
}
$.ajax({
url: "api/product",
dataType: "json",
async: false,
data: data,
type: 'POST',
success: function(data) {
//reset form
$form
.find('#add_product_title').val('').end()
.find('#add_product_description').val('').end()
.find('#add_product_price').val('').end()
.find('.add_product_categories').val('');
//load the new product in the template since we redirect there
loadProduct(data.productId);
},
error: ajaxError
});
});
//Handles the edit product form
$('#edit_button').bind('click', function() {
var $form = $('#edit_product_content'),
id = $form.find('.title').attr('data-id'),
data = {
description: $form.find('#edit_product_description').val(),
price: $form.find('#edit_product_price').val(),
categoryId: $form.find('.add_product_categories').val()
};
$.ajax({
url: "api/product/" + id,
dataType: "json",
async: false,
data: data,
headers: {'X-HTTP-Method-Override': 'PUT'},
type: 'POST',
success: function(data) {
loadProduct(id);
},
error: ajaxError
});
});
//Delete button
$('#remove_button').bind('click', function() {
if (! confirm("Really delete (cannot be undone)?"))
return false;
var $form = $('#edit_product_content'),
id = $form.find('.title').attr('data-id');
$.ajax({
url: "api/product/"+id,
dataType: "json",
async: false,
type: 'DELETE',
error: ajaxError
});
});
});
/******************************************************************************/
function ajaxError(jqXHR, textStatus, errorThrown){
console.log('ajaxError '+textStatus+' '+errorThrown);
showError(textStatus, errorThrown);
}
function showError(name, description) {
$('#error_message').remove();
$("#error_message_template").tmpl( {errorName: name, errorDescription: description} ).appendTo( "#error_dialog_content" );
$.mobile.changePage($('#error_dialog'), {
transition: "pop",
reverse: false,
changeHash: false
});
}