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
};
/**
* Populates notifications types with empty fields to be inserted
*
* @return void
*/
function _loadEmptyNotifications() {
var types = ['sms', 'email', 'phone', 'url', 'fax'];
for(var i in types) {
var notification = {
id_notification: '',
type: types[i],
value: '',
active: false,
}
_loadNotification(notification);
}
}
/**
* Sets the notifications in the restaurant form
*
* @param notifications
*/
function _loadNotification(notification) {
var $wrapper = 'div.check-content.' + notification.type;
// console.log($wrapper);
$wrapper = $($wrapper);
var active = parseInt(notification.active) ? 'checked="checked"' : '';
var id = parseInt(notification.id) ? notification.id : '';
html = '
' +
'' +
''+
'
';
$wrapper.append(html);
}
function _loadRestaurant() {
var restaurant = this;
var checkswap = {
'delivery_fee_check' : 'delivery_fee',
'delivery_min_check': 'delivery_min',
'fee_restaurant_check': 'fee_restaurant',
'fee_customer_check': 'fee_customer',
'id_community_check': 'id_community'
};
$('.admin-restaurant-form input, .admin-restaurant-form select, .admin-restaurant-form textarea').each(function() {
if ($(this).attr('type') == 'checkbox') {
if (restaurant[$(this).attr('name')] == 1 && $(this).attr('value') == '1') {
$(this).click();
}
if (restaurant[$(this).attr('name')] == 0 && $(this).attr('value') == '0') {
$(this).click();
}
} else {
$(this).val(restaurant[$(this).attr('name')]);
}
for (var x in checkswap) {
if ($(this).attr('name') == x) {
if (restaurant[checkswap[x]] && restaurant[checkswap[x]] != '0') {
//$('input[name="' + x + '"][value="0"]').prop('checked', false);
//$('input[name="' + x + '"][value="1"]').prop('checked', true);
$('input[name="' + x + '"][value="1"]').click();
} else {
//$('input[name="' + x + '"][value="0"]').prop('checked', true);
//$('input[name="' + x + '"][value="1"]').prop('checked', false);
$('input[name="' + x + '"][value="0"]').click();
}
}
}
});
App.restaurant = restaurant.id_restaurant; // Should be App.id_restaurant IMHO
App.restaurantObject = restaurant; // and this one should rellay be App.restaurant
$('.admin-restaurant-content').html('');
var notifications = restaurant.notifications();
for (var i in notifications) {
_loadNotification(notifications[i]);
}
_loadEmptyNotifications();
_newNotificationFields();
var categories = restaurant.categories();
var isDishes = false;
var $categoriesContainer = $('');
$('.admin-restaurant-dishes .admin-restaurant-content').append($categoriesContainer);
for (var i in categories) {
var dishes = categories[i].dishes();
var $categoryTab = $('
'+ categories[i].name+'
');
$categoriesContainer.append($categoryTab);
for (var x in dishes) {
App.showDish(dishes[x]);
isDishes = true;
}
}
$('.accordion').accordion({
collapsible: true,
active: false,
heightStyle: "content",
activate: function( event, ui ){
var speed = 100;
var accordionOptions = $('.accordion').accordion('option');
setTimeout(function() {
$('.accordion').accordion('destroy');
$('.accordion').accordion(accordionOptions);
}, 1.1 * speed);
}
});
// $('.accordion .ui-accordion-content').sortable().disableSelection();
if (!isDishes) {
$('input[name="dish_check"][value="0"]').prop('checked', true);
$('input[name="dish_check"][value="1"]').prop('checked', false);
$('.admin-restaurant-dishes').hide();
} else {
$('input[name="dish_check"][value="0"]').prop('checked', false);
$('input[name="dish_check"][value="1"]').prop('checked', true);
$('.admin-restaurant-dishes').show();
}
var days = {
'sun': 'Sunday',
'mon': 'Monday',
'tue': 'Tuesday',
'wed': 'Wednesday',
'thu': 'Thursday',
'fri': 'Friday',
'sat': 'Saturday'
};
for (var d in days) {
var day = $('
' + days[d] + '
');
var dayWrap = $('').appendTo(day);
dayWrap.after('');
if (!restaurant._hours) {
$('input[name="hours_check"][value="0"]').prop('checked', true);
$('input[name="hours_check"][value="1"]').prop('checked', false);
$('.admin-restaurant-hours').hide();
} else {
$('input[name="hours_check"][value="0"]').prop('checked', false);
$('input[name="hours_check"][value="1"]').prop('checked', true);
$('.admin-restaurant-hours').show();
var dayitem = restaurant._hours[d];
for (var x in dayitem) {
var row = $('');
row.append('' +
' TO ' +
' ');
dayWrap.append(row);
}
}
var row = $('');
row.append(' TO ');
dayWrap.append(row);
$('.admin-restaurant-hours').append(day);
}
}
/**
* Adds a new hours range if they are all filled up
*
* @return void
*/
function _newNotificationFields() {
var inputSelector = '.notification-wrap input[name="notification-value"]';
$(inputSelector).live('keyup', function() {
var $container = $(this).closest('.check-content');
var allfull = true;
$container.find(inputSelector).each(function() {
if ($(this).val() == '') {
allfull = false;
}
});
if (allfull) {
var notification = {
id_notification: '',
value: '',
active: false,
}
var types = ['sms', 'email', 'phone', 'url', 'fax'];
for (var i in types) {
if ($container.hasClass(types[i])) {
notification.type = types[i];
}
}
_loadNotification(notification);
}
});
}
/**
* Method to be called to save the Dish Categories
*
* @param function complete What to trigger after the categories are stored
*
* @return void
*
* @todo returned elements need to be reloaded
*/
function _saveCategories(complete) {
var selector = 'input.dataset-notification, select.dataset-notification, textarea.dataset-notification';
var elements = [];
$('.accordion h3').each(function() {
var id = $(this).attr('data-id_category');
var element = {
name: $(this).children().end().text()
};
if (id) {
element.id_category = id;
}
elements[elements.length] = element;
});
$.post('/api/restaurant/' + App.restaurant + '/categories', {elements: elements}, function() {
if (complete) {
complete();
}
});
}
/**
* Method to be called to save all dishes
*
* @param function compelte What to trigger after the dishes are stored
*
* @return void
*/
function saveDishes (complete) {
var selector = 'input.dataset-dish, select.dataset-dish, textarea.dataset-dish';
var dishes = [];
$('.admin-food-item-wrap').each(function() {
var id = $(this).attr('data-id_dish');
var values = getValues($(this).find(selector), {});
var dish = {
name: values['dish-name'],
description: values['dish-description'],
price: values['dish-price'],
id_category: values['dish-id_category'],
active: values['dish-active'],
};
if (id) {
dish.id_dish = id;
}
dish.optionGroups = [];
$(this).find('.admin-dish-options .admin-dish-options-wrapper').each(function() {
var id = $(this).attr('data-parent');
var name = $(this).find('.admin-dish-options-title').html();
name = name.substr(0,name.length-1);
var optionGroup = {
name: name,
'default': values['dish-options-default'],
type: $(this).attr('data-type'),
price: $(this).attr('data-modifies_price') == 'true' ? true : false,
options: []
};
if (id) {
optionGroup.id_option = id;
}
$(this).find('.dish-options').each(function() {
var id = $(this).attr('data-id_option');
var values = getValues($(this).find('input'), {});
if (values['dish-options-name']) {
var option = {
name: values['dish-options-name'],
price: values['dish-options-price'] || 0.00,
'default': $(this).find('input[type="checkbox"], input[type="radio"]').prop('checked'),
};
if (id) {
option.id_option = id;
}
optionGroup.options[optionGroup.options.length] = option;
}
});
dish.optionGroups[dish.optionGroups.length] = optionGroup;
});
dishes[dishes.length] = dish;
});
$.post('/api/restaurant/' + App.restaurant + '/dishes', {dishes: dishes}, function() {
if (complete) {
complete();
}
});
}
/**
* Method to be called to save notifications
*
* @param function compelte What to trigger after the dishes are stored
*
* @return void
*
* @todo wasn't able to take the function out becaues of the getValue() method which needs to be refactorized and moved out
* @todo returned elements need to be reloaded
*/
function _saveNotifications(complete) {
var selector = 'input.dataset-notification, select.dataset-notification, textarea.dataset-notification';
var elements = [];
$('.notification-wrap').each(function() {
var id = $(this).attr('data-id_notification');
var values = getValues($(this).find(selector), {});
var element = {
active: values['notification-active'],
value: values['notification-value']
};
var types = ['sms', 'email', 'phone', 'url', 'fax'];
for (var i in types) {
var $container = $(this).closest('.check-content');
if ($container.hasClass(types[i])) {
element.type = types[i];
}
}
if (id) {
element.id_notification = id;
}
elements[elements.length] = element;
});
$.post('/api/restaurant/' + App.restaurant + '/notifications', {elements: elements}, function() {
if (complete) {
complete();
}
});
}
function saveRestaurant (all) {
var selector = 'input.dataset-restaurant, select.dataset-restaurant, textarea.dataset-restaurant';
var id = App.restaurant;
if (id) {
App.cache('Restaurant', id, function() {
var restaurant = getValues(selector, this);
restaurant.save(function() {
if (all) {
saveHours(function() {
_saveCategories(function() {
saveDishes(function() {
_saveNotifications(function() {
location.href = '/admin/restaurants/' + App.restaurant;
});
});
});
});
}
});
});
} else {
var restaurant = getValues(selector, {});
restaurant = new Restaurant(restaurant);
restaurant.save(function(r) {
App.cache('Restaurant', r.id_restaurant, function() {
App.restaurant = this.id_restaurant;
if (all) {
saveHours(function() {
_saveCategories(function(){
saveDishes(function() {
_saveNotifications(function() {
location.href = '/admin/restaurants/' + App.restaurant;
});
});
});
});
}
});
});
}
};
function saveHours (complete) {
var selector = '.hours-date-hour input';
var id = App.restaurant;
if (id) {
App.cache('Restaurant', id, function() {
var h = getValues(selector, {});
var hours = {'sun': [],'mon': [],'tue': [],'wed': [],'thu': [],'fri': [],'sat': []};
var vals = getValues('input.dataset-restaurant', {});
if (vals.hours_check) {
for (var d in hours) {
for (var x in h[d + '-open']) {
if (!h[d + '-open'][x]) continue;
hours[d][hours[d].length] = [App.unFormatTime(h[d + '-open'][x]), App.unFormatTime(h[d + '-close'][x])];
}
}
}
$.post('/api/restaurant/' + id + '/hours', {hours: hours}, function() {
if (complete) {
complete();
}
});
});
}
};
function getValues(selector, restaurant) {
$(selector).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' || $(this).attr('type') == 'radio') {
value = $(this).prop('checked') ? true : false;
} else {
value = $(this).val();
}
if (group) {
restaurant[name][restaurant[name].length] = value;
} else {
restaurant[name] = value;
}
});
return restaurant;
}
App.loadRestaurant = function(id_restaurant) {
App.cache('Restaurant', id_restaurant , _loadRestaurant);
};
/**
* Generates HTML to show dish and it's items
*
* @todo Not sure if hide purges HTML or what.
*/
App.showDish = function(dishItem) {
dishItem = $.extend({
id_dish: '',
// id_category: '',
name: '',
description: '',
price: ''
}, dishItem);
var dish = $('');
dish.append('
');
dish.append(content);
content.append(padding);
var options = $('');
var basicOptions = $('');
var basicWrapper = $('
Basic options:
')
.append(basicOptions);
var optGroups = [];
if (dishItem.options) {
var opts = dishItem.options();
options.append(basicWrapper);
for (var x in opts) {
var option = opts[x];
if (option.id_option_parent) {
continue;
}
if (option.type == 'check') {
basicOptions.append(App.returnOption(option,option.type));
} else if (option.type == 'select') {
var optionAdder = $('');
var optionWrapper = $('