var App = { cartHighlightEnabled: false, currentPage: null, slogans: ['order food in 5 seconds'], service: '/api/', cached: {}, cart: {}, community: null, page: {}, config: null, order: { cardChanged: false, pay_type: 'card', delivery_type: 'delivery', tip: '10' }, _init: false, _pageInit: false }; $(function() { $('.admin-restaurant-link').live('click',function() { if (!$(this).attr('data-id_restaurant')) { $('.admin-restaurant-form input, admin-restaurant-form select').val('').prop('checked',false); $('.admin-restaurant-panel').hide(); App.restaurant = null; $('.admin-restaurant-content').html(''); return; } App.cache('Restaurant', $(this).attr('data-id_restaurant'), function() { var restaurant = this; $('.admin-restaurant-form input, .admin-restaurant-form select').each(function() { if ($(this).attr('type') == 'checkbox') { $(this).prop('checked', restaurant[$(this).attr('name')] == 1 ? true : false); } else { $(this).val(restaurant[$(this).attr('name')]); } }); $('.admin-restaurant-panel').show(); App.restaurant = restaurant.id_restaurant; $('.admin-restaurant-content').html(''); var days = ['sun','mon','tue','wed','thu','fri','sat']; for (var d in days) { var day = $('
' + days[d] + '
'); var dayWrap = $('
').appendTo(day); if (!restaurant._hours) { continue; } var dayitem = restaurant._hours[days[d]]; for (var x in dayitem) { var row = $('
'); row.append(' - '); dayWrap.append(row); } var row = $('
'); row.append(' - '); dayWrap.append(row); $('.admin-restaurant-hours').append(day); } }); }); $('.hours-date-hour input').live('keyup', function() { var allfull = true; $(this).closest('.hours-date-hours').find('input').each(function() { if ($(this).val() == '') { allfull = false; } }); if (allfull) { var day = $(this).attr('name').replace(/-open|-close/,''); var row = $('
'); row.append(' - '); $(this).closest('.hours-date-hour').append(row); } }); var saveRestaurant = function(selector, restaurant) { $(selector + ' input, ' + selector + ' select').each(function() { var name, value, group = false; if ($(this).attr('name').match(/^.*\[\]$/)) { group = true; name = $(this).attr('name').replace(/^(.*)\[\]$/,'$1'); if (!restaurant[name]) { restaurant[name] = []; } } else { name = $(this).attr('name'); } if ($(this).attr('type') == 'checkbox') { value = $(this).prop('checked') ? true : false; } else { value = $(this).val(); } if (group) { restaurant[name][restaurant[name].length] = value; } else { restaurant[name] = value; } }); return restaurant; } $('.admin-restaurant-save').live('click',function() { var selector = '.admin-restaurant-form'; var id = App.restaurant; if (id) { App.cache('Restaurant', id, function() { var restaurant = saveRestaurant(selector, this); restaurant.save(); }); } else { var restaurant = saveRestaurant(selector, {}); restaurant = new Restaurant(restaurant) restaurant.save(); } }); $('.admin-restaurant-hours-save').live('click',function() { var selector = '.admin-restaurant-hours'; var id = App.restaurant; if (id) { App.cache('Restaurant', id, function() { var h = saveRestaurant(selector, {}); var hours = {'sun': [],'mon': [],'tue': [],'wed': [],'thu': [],'fri': [],'sat': []}; for (var d in hours) { $(selector).find('[name="' + d + '-open"]').each(function() { if (!$(this).val()) { return; } var close = $($(selector).find('[name="' + d + '-close"]').get(0)).val(); if (!close) { return; } var hour = [$(this).val(), close]; hours[d][hours[d].length] = hour; }); } console.log(hours); $.post('/api/restaurant/' + id + '/hours', {hours: hours}, function() { }); }); } }); $('.admin-restaurant-hours-save-all').live('click',function() { $('.admin-restaurant-hours-save-link').click(); }); });